dlmarion commented on code in PR #30: URL: https://github.com/apache/accumulo-classloaders/pull/30#discussion_r2582140473
########## modules/local-caching-classloader/src/test/java/org/apache/accumulo/classloader/lcc/MiniAccumuloClusterClassLoaderFactoryTest.java: ########## @@ -0,0 +1,284 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.accumulo.classloader.lcc; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static java.nio.file.attribute.PosixFilePermission.OWNER_EXECUTE; +import static java.nio.file.attribute.PosixFilePermission.OWNER_READ; +import static java.nio.file.attribute.PosixFilePermission.OWNER_WRITE; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertIterableEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.File; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; +import java.nio.file.StandardOpenOption; +import java.nio.file.attribute.FileAttribute; +import java.nio.file.attribute.PosixFilePermission; +import java.nio.file.attribute.PosixFilePermissions; +import java.util.Collections; +import java.util.EnumSet; +import java.util.List; +import java.util.Map.Entry; +import java.util.Set; +import java.util.TreeSet; +import java.util.stream.Collectors; + +import org.apache.accumulo.classloader.lcc.definition.ContextDefinition; +import org.apache.accumulo.core.client.Accumulo; +import org.apache.accumulo.core.client.AccumuloClient; +import org.apache.accumulo.core.client.IteratorSetting; +import org.apache.accumulo.core.client.Scanner; +import org.apache.accumulo.core.clientImpl.AccumuloServerException; +import org.apache.accumulo.core.clientImpl.ClientContext; +import org.apache.accumulo.core.conf.Property; +import org.apache.accumulo.core.data.Key; +import org.apache.accumulo.core.data.TableId; +import org.apache.accumulo.core.data.Value; +import org.apache.accumulo.core.iterators.IteratorUtil.IteratorScope; +import org.apache.accumulo.core.metadata.schema.Ample; +import org.apache.accumulo.core.metadata.schema.TabletMetadata; +import org.apache.accumulo.core.metadata.schema.TabletsMetadata; +import org.apache.accumulo.harness.MiniClusterConfigurationCallback; +import org.apache.accumulo.harness.SharedMiniClusterBase; +import org.apache.accumulo.miniclusterImpl.MiniAccumuloConfigImpl; +import org.apache.accumulo.test.TestIngest; +import org.apache.accumulo.test.TestIngest.IngestParams; +import org.apache.accumulo.test.VerifyIngest; +import org.apache.accumulo.test.VerifyIngest.VerifyParams; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +public class MiniAccumuloClusterClassLoaderFactoryTest extends SharedMiniClusterBase { + + private static class TestMACConfiguration implements MiniClusterConfigurationCallback { + + @Override + public void configureMiniCluster(MiniAccumuloConfigImpl cfg, + org.apache.hadoop.conf.Configuration coreSite) { + cfg.setNumTservers(3); + cfg.setProperty(Property.TSERV_NATIVEMAP_ENABLED.getKey(), "false"); + cfg.setProperty(Property.GENERAL_CONTEXT_CLASSLOADER_FACTORY.getKey(), + LocalCachingContextClassLoaderFactory.class.getName()); + cfg.setProperty(Constants.CACHE_DIR_PROPERTY, tempDir.resolve("base").toUri().toString()); + cfg.setProperty(Constants.UPDATE_FAILURE_GRACE_PERIOD_MINS, "1"); + } + } + + @TempDir + private static java.nio.file.Path tempDir; + + private static final Set<PosixFilePermission> CACHE_DIR_PERMS = + EnumSet.of(OWNER_READ, OWNER_WRITE, OWNER_EXECUTE); + private static final FileAttribute<Set<PosixFilePermission>> PERMISSIONS = + PosixFilePermissions.asFileAttribute(CACHE_DIR_PERMS); + private static final String ITER_CLASS_NAME = + "org.apache.accumulo.classloader.vfs.examples.ExampleIterator"; + private static final int MONITOR_INTERVAL_SECS = + LocalCachingContextClassLoaderFactoryTest.MONITOR_INTERVAL_SECS; + + private static URL jarAOrigLocation; + private static URL jarBOrigLocation; + + @BeforeAll + public static void beforeAll() throws Exception { + + // Find the Test jar files + jarAOrigLocation = MiniAccumuloClusterClassLoaderFactoryTest.class + .getResource("/ExampleIteratorsA/example-iterators-a.jar"); + assertNotNull(jarAOrigLocation); + jarBOrigLocation = MiniAccumuloClusterClassLoaderFactoryTest.class + .getResource("/ExampleIteratorsB/example-iterators-b.jar"); + assertNotNull(jarBOrigLocation); + + startMiniClusterWithConfig(new TestMACConfiguration()); + } + + @AfterAll + public static void afterAll() throws Exception { + stopMiniCluster(); + } + + @Test + public void testClassLoader() throws Exception { + + Path baseDirPath = tempDir.resolve("base"); + Path jsonDirPath = baseDirPath.resolve("contextFiles"); + Files.createDirectory(jsonDirPath, PERMISSIONS); + + // Create a context definition that only references jar A + final ContextDefinition testContextDef = + ContextDefinition.create("test", MONITOR_INTERVAL_SECS, jarAOrigLocation); + final String testContextDefJson = testContextDef.toJson(); + final File testContextDefFile = jsonDirPath.resolve("testContextDefinition.json").toFile(); + Files.writeString(testContextDefFile.toPath(), testContextDefJson, StandardOpenOption.CREATE); + assertTrue(Files.exists(testContextDefFile.toPath())); + + final String[] names = this.getUniqueNames(1); + try (AccumuloClient client = + Accumulo.newClient().from(getCluster().getClientProperties()).build()) { + + List<String> tservers = client.instanceOperations().getTabletServers(); + Collections.sort(tservers); + assertEquals(3, tservers.size()); + + final String tableName = names[0]; + + final IngestParams params = new IngestParams(client.properties(), tableName, 100); + params.cols = 10; + params.dataSize = 10; + params.startRow = 0; + params.columnFamily = "test"; + params.createTable = true; + params.numsplits = 3; + params.flushAfterRows = 0; + + TestIngest.createTable(client, params); + + // Confirm 4 tablets, spread across 3 tablet servers + client.instanceOperations().waitForBalance(); + + final List<TabletMetadata> tm = getLocations(((ClientContext) client).getAmple(), + client.tableOperations().tableIdMap().get(tableName)); + assertEquals(4, tm.size()); // 3 tablets + + final Set<String> tabletLocations = new TreeSet<>(); + tm.forEach(t -> tabletLocations.add(t.getLocation().getHostPort())); + assertEquals(3, tabletLocations.size()); // 3 locations + + // both collections are sorted + assertIterableEquals(tservers, tabletLocations); + + TestIngest.ingest(client, params); + + final VerifyParams vp = new VerifyParams(client.properties(), tableName, params.rows); + vp.cols = params.cols; + vp.rows = params.rows; + vp.dataSize = params.dataSize; + vp.startRow = params.startRow; + vp.columnFamily = params.columnFamily; + vp.cols = params.cols; + VerifyIngest.verifyIngest(client, vp); + + // Set the table classloader context. Context name is the URL to the context + // definition file + final String contextURL = testContextDefFile.toURI().toURL().toString(); + client.tableOperations().setProperty(tableName, Property.TABLE_CLASSLOADER_CONTEXT.getKey(), + contextURL); + + // Attach a scan iterator to the table + IteratorSetting is = new IteratorSetting(101, "example", ITER_CLASS_NAME); + client.tableOperations().attachIterator(tableName, is, EnumSet.of(IteratorScope.scan)); + + // confirm that all values get transformed to "foo" + // by the iterator + final byte[] jarAValueBytes = "foo".getBytes(UTF_8); + Scanner scanner = client.createScanner(tableName); Review Comment: Does the VerifyIngest call before this satisfy this, or does the verify not check the Value? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
