sodonnel commented on code in PR #5746: URL: https://github.com/apache/ozone/pull/5746#discussion_r1431784238
########## hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/container/replication/TestReplicationManagerScenarios.java: ########## @@ -0,0 +1,783 @@ +/** + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.hadoop.hdds.scm.container.replication; + +import com.fasterxml.jackson.databind.MappingIterator; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectReader; +import org.apache.commons.lang3.tuple.Pair; +import org.apache.hadoop.hdds.client.ECReplicationConfig; +import org.apache.hadoop.hdds.client.RatisReplicationConfig; +import org.apache.hadoop.hdds.client.ReplicationConfig; +import org.apache.hadoop.hdds.conf.OzoneConfiguration; +import org.apache.hadoop.hdds.protocol.DatanodeDetails; +import org.apache.hadoop.hdds.protocol.MockDatanodeDetails; +import org.apache.hadoop.hdds.protocol.proto.HddsProtos; +import org.apache.hadoop.hdds.protocol.proto.StorageContainerDatanodeProtocolProtos.ContainerReplicaProto; +import org.apache.hadoop.hdds.protocol.proto.StorageContainerDatanodeProtocolProtos.SCMCommandProto; +import org.apache.hadoop.hdds.scm.PlacementPolicy; +import org.apache.hadoop.hdds.scm.container.ContainerID; +import org.apache.hadoop.hdds.scm.container.ContainerInfo; +import org.apache.hadoop.hdds.scm.container.ContainerManager; +import org.apache.hadoop.hdds.scm.container.ContainerReplica; +import org.apache.hadoop.hdds.scm.container.ReplicationManagerReport; +import org.apache.hadoop.hdds.scm.ha.SCMContext; +import org.apache.hadoop.hdds.scm.node.NodeManager; +import org.apache.hadoop.hdds.scm.node.NodeStatus; +import org.apache.hadoop.hdds.scm.node.states.NodeNotFoundException; +import org.apache.hadoop.hdds.server.events.EventPublisher; +import org.apache.hadoop.ozone.protocol.commands.ReplicateContainerCommand; +import org.apache.hadoop.ozone.protocol.commands.SCMCommand; +import org.apache.ozone.test.TestClock; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; +import org.mockito.Mockito; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; +import java.net.URISyntaxException; +import java.time.Instant; +import java.time.ZoneId; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.UUID; +import java.util.stream.Stream; + +import static org.apache.hadoop.hdds.HddsConfigKeys.HDDS_SCM_WAIT_TIME_AFTER_SAFE_MODE_EXIT; +import static org.mockito.ArgumentMatchers.any; + +/** + * This class tests the replication manager using a set of scenarios defined in + * JSON files. The scenarios define a container and a set of replicas, and the + * expected results from the replication manager. The scenarios are defined in + * JSON files in the test resources directory. The test files are loaded in + * {@link #init()} and the tests are run in {@link #testAllScenarios(Scenario)}. + * + * There are several inner class defined within this class, and they are used to + * deserialize the JSON files into Java objects. In general any field which is a + * setter on the inner class can be set in the JSON file. + * + * TODO - The framework does not allow for testing mis-replicated containers. + */ + +public class TestReplicationManagerScenarios { + private static final Map<String, UUID> ORIGINS = new HashMap<>(); + private static final Map<String, DatanodeDetails> DATANODE_ALIASES + = new HashMap<>(); + private static final Map<DatanodeDetails, NodeStatus> NODE_STATUS_MAP + = new HashMap<>(); + private static final String TEST_RESOURCE_PATH = "/replicationManagerTests"; + private static final List<Scenario> TEST_SCENARIOS = new ArrayList<>(); + + private Map<ContainerID, Set<ContainerReplica>> containerReplicaMap; + private Set<ContainerInfo> containerInfoSet; + private ContainerReplicaPendingOps containerReplicaPendingOps; + private Set<Pair<UUID, SCMCommand<?>>> commandsSent; + + private OzoneConfiguration configuration; + private ReplicationManager replicationManager; + private LegacyReplicationManager legacyReplicationManager; + private ContainerManager containerManager; + private PlacementPolicy ratisPlacementPolicy; + private PlacementPolicy ecPlacementPolicy; + private EventPublisher eventPublisher; + private SCMContext scmContext; + private NodeManager nodeManager; + private TestClock clock; + private ReplicationManagerReport repReport; + private ReplicationQueue repQueue; + + private static List<URI> getTestFiles() throws URISyntaxException { + File[] fileList = (new File(TestReplicationManagerScenarios.class + .getResource(TEST_RESOURCE_PATH) + .toURI())).listFiles(); + if (fileList == null) { + Assertions.fail("No test file resources found"); + // Make findbugs happy. + return Collections.emptyList(); + } + List<URI> uris = new ArrayList<>(); + for (File file : fileList) { + uris.add(file.toURI()); + } + return uris; + } + + private static List<Scenario> loadTestsInFile(URI testFile) + throws IOException { + ObjectReader reader = new ObjectMapper().readerFor(Scenario.class); + try (InputStream stream = testFile.toURL().openStream()) { + try (MappingIterator<Scenario> iterator = reader.readValues(stream)) { + return iterator.readAll(); + } + } catch (Exception e) { + System.out.println("Failed to load test file: " + testFile); + throw e; + } + } + + /** + * Load all the JSON files in the test resources directory and add them to the + * list of tests to run. If there is a parsing failure in any of the json + * files, the entire test will fail. + */ + @BeforeAll + public static void init() throws IOException, URISyntaxException { + List<URI> testFiles = getTestFiles(); + for (URI file : testFiles) { + List<Scenario> scenarios = loadTestsInFile(file); + Set<String> names = new HashSet<>(); + for (Scenario scenario : scenarios) { + if (!names.add(scenario.getDescription())) { + Assertions.fail("Duplicate test name: " + scenario.getDescription() + " in file: " + file); + } + scenario.setResourceName(file.toString()); + } + TEST_SCENARIOS.addAll(scenarios); + } + } + + @BeforeEach + public void setup() throws IOException, NodeNotFoundException { + configuration = new OzoneConfiguration(); + configuration.set(HDDS_SCM_WAIT_TIME_AFTER_SAFE_MODE_EXIT, "0s"); + containerManager = Mockito.mock(ContainerManager.class); + + scmContext = Mockito.mock(SCMContext.class); + nodeManager = Mockito.mock(NodeManager.class); + + ratisPlacementPolicy = ReplicationTestUtil.getSimpleTestPlacementPolicy(nodeManager, configuration); + ecPlacementPolicy = ReplicationTestUtil.getSimpleTestPlacementPolicy(nodeManager, configuration); + + commandsSent = new HashSet<>(); + eventPublisher = Mockito.mock(EventPublisher.class); + Mockito.doAnswer(invocation -> { + commandsSent.add(Pair.of(invocation.getArgument(0), + invocation.getArgument(1))); + return null; + }).when(nodeManager).addDatanodeCommand(any(), any()); + + legacyReplicationManager = Mockito.mock(LegacyReplicationManager.class); + clock = new TestClock(Instant.now(), ZoneId.systemDefault()); + containerReplicaPendingOps = new ContainerReplicaPendingOps(clock); + + Mockito.when(containerManager.getContainerReplicas(Mockito.any(ContainerID.class))).thenAnswer( + invocation -> { + ContainerID cid = invocation.getArgument(0); + return containerReplicaMap.get(cid); + }); + + Mockito.when(containerManager.getContainers()).thenAnswer( + invocation -> new ArrayList<>(containerInfoSet)); + + Mockito.when(nodeManager.getNodeStatus(any(DatanodeDetails.class))) + .thenAnswer(invocation -> { + DatanodeDetails dn = invocation.getArgument(0); + return NODE_STATUS_MAP.getOrDefault(dn, NodeStatus.inServiceHealthy()); + }); + + final HashMap<SCMCommandProto.Type, Integer> countMap = new HashMap<>(); + for (SCMCommandProto.Type type : SCMCommandProto.Type.values()) { + countMap.put(type, 0); + } + Mockito.when( + nodeManager.getTotalDatanodeCommandCounts(any(DatanodeDetails.class), + any(SCMCommandProto.Type.class), any(SCMCommandProto.Type.class))) + .thenReturn(countMap); + + // Ensure that RM will run when asked. + Mockito.when(scmContext.isLeaderReady()).thenReturn(true); + Mockito.when(scmContext.isInSafeMode()).thenReturn(false); + containerReplicaMap = new HashMap<>(); + containerInfoSet = new HashSet<>(); + repReport = new ReplicationManagerReport(); + repQueue = new ReplicationQueue(); + ORIGINS.clear(); + DATANODE_ALIASES.clear(); + NODE_STATUS_MAP.clear(); + } + + private ReplicationManager createReplicationManager() throws IOException { + return new ReplicationManager( + configuration, + containerManager, + ratisPlacementPolicy, + ecPlacementPolicy, + eventPublisher, + scmContext, + nodeManager, + clock, + legacyReplicationManager, + containerReplicaPendingOps) { + @Override + protected void startSubServices() { + // do not start any threads for processing + } + }; + } + + protected static UUID getOrCreateOrigin(String origin) { + return ORIGINS.computeIfAbsent(origin, (k) -> UUID.randomUUID()); + } + + private static Stream<Scenario> getTestScenarios() { + return TEST_SCENARIOS.stream(); + } + + private void loadPendingOps(ContainerInfo container, Scenario scenario) { + for (PendingReplica r : scenario.getPendingReplicas()) { + if (r.getType() == ContainerReplicaOp.PendingOpType.ADD) { + containerReplicaPendingOps.scheduleAddReplica(container.containerID(), r.getDatanodeDetails(), + r.getReplicaIndex(), Long.MAX_VALUE); + } else if (r.getType() == ContainerReplicaOp.PendingOpType.DELETE) { + containerReplicaPendingOps.scheduleDeleteReplica(container.containerID(), r.getDatanodeDetails(), + r.getReplicaIndex(), Long.MAX_VALUE); + } + } + } + + @ParameterizedTest + @MethodSource("getTestScenarios") + public void testAllScenarios(Scenario scenario) throws IOException { + ReplicationManager.ReplicationManagerConfiguration conf = + new ReplicationManager.ReplicationManagerConfiguration(); + conf.setMaintenanceRemainingRedundancy(scenario.getEcMaintenanceRedundancy()); + conf.setMaintenanceReplicaMinimum(scenario.getRatisMaintenanceMinimum()); + configuration.setFromObject(conf); + replicationManager = createReplicationManager(); + + ContainerInfo containerInfo = scenario.buildContainerInfo(); + loadPendingOps(containerInfo, scenario); + + Set<ContainerReplica> replicas = new HashSet<>(); + for (TestReplica replica : scenario.getReplicas()) { + replicas.add(replica.buildContainerReplica()); Review Comment: The inner class here represents a replica, so having `buildContainerReplica` on it makes sense to me. Having it on the scenario might be somewhat strange too. I guess Scenario could have getContainerReplicas, where it iterates the replicas internally, but all that does is move this for loop from here to there. Being that these methods are only used inside this one test method and the classes are localized to this class for testing, I am not sure its worth over thinking the access methods. It seems reasonably clear to me as it stands. -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
