Skip to content

Commit

Permalink
Avoid MockedSCM:init() call in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Montura committed May 7, 2024
1 parent 8ededd4 commit a7d5691
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,28 +66,34 @@ public final class MockedSCM {
private final StorageContainerManager scm;
private final TestableCluster cluster;
private final MockNodeManager mockNodeManager;
private MockedReplicationManager mockedReplicaManager;
private MoveManager moveManager;
private ContainerManager containerManager;

private final MockedReplicationManager mockedReplicaManager;
private final MoveManager moveManager;
private final ContainerManager containerManager;
private MockedPlacementPolicies mockedPlacementPolicies;

public MockedSCM(@Nonnull TestableCluster testableCluster) {
scm = mock(StorageContainerManager.class);
cluster = testableCluster;
mockNodeManager = new MockNodeManager(cluster.getDatanodeToContainersMap());
try {
moveManager = mockMoveManager();
containerManager = mockContainerManager(cluster);
mockedReplicaManager = MockedReplicationManager.doMock();
} catch (NodeNotFoundException | ContainerReplicaNotFoundException | ContainerNotFoundException | TimeoutException e) {
throw new RuntimeException("Can't create MockedSCM instance: ", e);
}
}

public void init(@Nonnull ContainerBalancerConfiguration balancerConfig) {
private void init(@Nonnull ContainerBalancerConfiguration balancerConfig) {
init(balancerConfig, new OzoneConfiguration());
}

public void init(@Nonnull ContainerBalancerConfiguration balancerConfig, @Nonnull OzoneConfiguration ozoneCfg) {
private void init(@Nonnull ContainerBalancerConfiguration balancerConfig, @Nonnull OzoneConfiguration ozoneCfg) {
ozoneCfg.setFromObject(balancerConfig);
try {
doMock(balancerConfig, ozoneCfg);
} catch (IOException | NodeNotFoundException | TimeoutException e) {
throw new RuntimeException("Can't initialize TestOzoneHDDS: ", e);
throw new RuntimeException("Can't create MockedSCM instance: ", e);
}
}

Expand All @@ -96,9 +102,6 @@ public void init(@Nonnull ContainerBalancerConfiguration balancerConfig, @Nonnul
*/
private void doMock(@Nonnull ContainerBalancerConfiguration cfg, @Nonnull OzoneConfiguration ozoneCfg)
throws IOException, NodeNotFoundException, TimeoutException {
containerManager = mockContainerManager(cluster);
mockedReplicaManager = MockedReplicationManager.doMock();
moveManager = mockMoveManager();
StatefulServiceStateManager stateManager = MockedServiceStateManager.doMock();
SCMServiceManager scmServiceManager = mockSCMServiceManger();

Expand Down Expand Up @@ -137,6 +140,7 @@ public String toString() {
}

public @Nonnull ContainerBalancerTask startBalancerTask(@Nonnull ContainerBalancerConfiguration config) {
init(config);
return startBalancerTask(new ContainerBalancer(scm), config);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ public void containerBalancerShouldObeyMaxDatanodesToInvolveLimit(@Nonnull Mocke
}
config.setIterations(1);
config.setMaxSizeToMovePerIteration(50 * STORAGE_UNIT);
mockedSCM.init(config);

ContainerBalancerTask task = mockedSCM.startBalancerTask(config);
ContainerBalancerMetrics metrics = task.getMetrics();
Expand Down Expand Up @@ -124,7 +123,6 @@ public void balancerShouldObeyMaxSizeEnteringTargetLimit(@Nonnull MockedSCM mock
config.setMaxSizeToMovePerIteration(50 * STORAGE_UNIT);
// No containers should be selected when the limit is just 2 MB.
config.setMaxSizeEnteringTarget(2 * OzoneConsts.MB);
mockedSCM.init(config);

ContainerBalancerTask task = mockedSCM.startBalancerTask(config);
// Container balancer still has unbalanced nodes due to MaxSizeEnteringTarget limit
Expand All @@ -140,7 +138,6 @@ public void balancerShouldObeyMaxSizeEnteringTargetLimit(@Nonnull MockedSCM mock
balancerConfig.setMaxDatanodesPercentageToInvolvePerIteration(100);
}
balancerConfig.setIterations(1);
mockedSCM.init(balancerConfig);

task = mockedSCM.startBalancerTask(balancerConfig);
// Balancer should have identified unbalanced nodes.
Expand All @@ -165,7 +162,6 @@ public void balancerShouldObeyMaxSizeLeavingSourceLimit(@Nonnull MockedSCM mocke
config.setMaxSizeToMovePerIteration(50 * STORAGE_UNIT);
// No source containers should be selected when the limit is just 2 MB.
config.setMaxSizeLeavingSource(2 * OzoneConsts.MB);
mockedSCM.init(config);

ContainerBalancerTask task = mockedSCM.startBalancerTask(config);
// Container balancer still has unbalanced nodes due to MaxSizeLeavingSource limit
Expand All @@ -181,7 +177,6 @@ public void balancerShouldObeyMaxSizeLeavingSourceLimit(@Nonnull MockedSCM mocke
newBalancerConfig.setMaxDatanodesPercentageToInvolvePerIteration(100);
}
newBalancerConfig.setIterations(1);
mockedSCM.init(newBalancerConfig);

task = mockedSCM.startBalancerTask(newBalancerConfig);
// Balancer should have identified unbalanced nodes.
Expand All @@ -208,7 +203,6 @@ public void initializeIterationShouldUpdateUnBalancedNodesWhenThresholdChanges(@
config.setIterations(1);
config.setMaxSizeToMovePerIteration(50 * STORAGE_UNIT);
config.setMaxSizeEnteringTarget(50 * STORAGE_UNIT);
mockedSCM.init(config);

// check for random threshold values
for (int i = 0; i < 50; i++) {
Expand Down Expand Up @@ -257,7 +251,6 @@ public void testBalancerWithMoveManager(@Nonnull MockedSCM mockedSCM)
config.setIterations(1);
config.setMaxSizeToMovePerIteration(50 * STORAGE_UNIT);
config.setMaxSizeEnteringTarget(50 * STORAGE_UNIT);
mockedSCM.init(config);

mockedSCM.disableLegacyReplicationManager();
mockedSCM.startBalancerTask(config);
Expand Down Expand Up @@ -285,7 +278,6 @@ public void unBalancedNodesListShouldBeEmptyWhenClusterIsBalanced(@Nonnull Mocke
config.setMaxSizeToMovePerIteration(50 * STORAGE_UNIT);
config.setMaxSizeEnteringTarget(50 * STORAGE_UNIT);
config.setThreshold(99.99);
mockedSCM.init(config);

ContainerBalancerTask task = mockedSCM.startBalancerTask(config);
ContainerBalancerMetrics metrics = task.getMetrics();
Expand All @@ -309,13 +301,10 @@ public void testMetrics(@Nonnull MockedSCM mockedSCM) throws IOException, NodeNo
config.setMaxSizeEnteringTarget(6 * STORAGE_UNIT);
// deliberately set max size per iteration to a low value, 6 GB
config.setMaxSizeToMovePerIteration(6 * STORAGE_UNIT);
mockedSCM.init(config);

when(mockedSCM.getMoveManager().move(any(), any(), any()))
.thenReturn(CompletableFuture.completedFuture(
MoveManager.MoveResult.REPLICATION_FAIL_NODE_UNHEALTHY))
.thenReturn(CompletableFuture.completedFuture(
MoveManager.MoveResult.COMPLETED));
.thenReturn(CompletableFuture.completedFuture(MoveManager.MoveResult.REPLICATION_FAIL_NODE_UNHEALTHY))
.thenReturn(CompletableFuture.completedFuture(MoveManager.MoveResult.COMPLETED));
ContainerBalancerTask task = mockedSCM.startBalancerTask(config);

ContainerBalancerMetrics metrics = task.getMetrics();
Expand Down

0 comments on commit a7d5691

Please sign in to comment.