Skip to content

Commit

Permalink
HDDS-10225. Speed up TestSCMHAManagerImpl. (#6109)
Browse files Browse the repository at this point in the history
  • Loading branch information
adoroszlai authored Jan 30, 2024
1 parent 7579bd0 commit 1e6e6d1
Showing 1 changed file with 53 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,21 @@
import org.apache.hadoop.security.authentication.client.AuthenticationException;
import org.apache.ozone.test.GenericTestUtils;
import org.apache.ratis.server.DivisionInfo;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.io.TempDir;

import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeoutException;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assumptions.assumeThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
Expand All @@ -64,27 +69,34 @@
/**
* Test cases to verify {@link org.apache.hadoop.hdds.scm.ha.SCMHAManagerImpl}.
*/
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class TestSCMHAManagerImpl {

@TempDir
private static final String FOLLOWER_SCM_ID = "follower";

private Path storageBaseDir;
private String clusterID;
private SCMHAManager primarySCMHAManager;
private SCMRatisServer follower;

@BeforeEach
void setup() throws IOException, InterruptedException,
@BeforeAll
void setup(@TempDir Path tempDir) throws IOException, InterruptedException,
TimeoutException {
storageBaseDir = tempDir;
clusterID = UUID.randomUUID().toString();
OzoneConfiguration conf = getConfig("scm1", 9894);
final StorageContainerManager scm = getMockStorageContainerManager(conf);
SCMRatisServerImpl.initialize(clusterID, scm.getScmId(),
scm.getScmNodeDetails(), conf);
scm.getScmHAManager().start();
primarySCMHAManager = scm.getScmHAManager();
primarySCMHAManager.start();
final DivisionInfo ratisDivision = primarySCMHAManager.getRatisServer()
.getDivision().getInfo();
// Wait for Ratis Server to be ready
waitForSCMToBeReady(ratisDivision);
follower = getMockStorageContainerManager(getConfig(FOLLOWER_SCM_ID, 9898))
.getScmHAManager().getRatisServer();
}

private OzoneConfiguration getConfig(String scmId, int ratisPort) {
Expand All @@ -97,42 +109,55 @@ private OzoneConfiguration getConfig(String scmId, int ratisPort) {
return conf;
}

public void waitForSCMToBeReady(DivisionInfo ratisDivision)
private void waitForSCMToBeReady(DivisionInfo ratisDivision)
throws TimeoutException,
InterruptedException {
GenericTestUtils.waitFor(ratisDivision::isLeaderReady,
1000, 10000);
}

@AfterEach
public void cleanup() throws IOException {
@AfterAll
void cleanup() throws IOException {
follower.stop();
primarySCMHAManager.stop();
}

@Test
public void testAddSCM() throws IOException, InterruptedException {
assertEquals(1, primarySCMHAManager.getRatisServer()
.getDivision().getGroup().getPeers().size());
@Order(1)
void testAddSCM() throws IOException {
assertEquals(1, getPeerCount());

follower.start();
final AddSCMRequest request = new AddSCMRequest(
clusterID, FOLLOWER_SCM_ID, getFollowerAddress());
primarySCMHAManager.addSCM(request);
assertEquals(2, getPeerCount());
}

final StorageContainerManager scm2 = getMockStorageContainerManager(
getConfig("scm2", 9898));
try {
scm2.getScmHAManager().getRatisServer().start();
final AddSCMRequest request = new AddSCMRequest(
clusterID, scm2.getScmId(),
"localhost:" + scm2.getScmHAManager().getRatisServer()
.getDivision().getRaftServer().getServerRpc()
.getInetSocketAddress().getPort());
primarySCMHAManager.addSCM(request);
assertEquals(2, primarySCMHAManager.getRatisServer()
.getDivision().getGroup().getPeers().size());
} finally {
scm2.getScmHAManager().getRatisServer().stop();
}
@Test
@Order(2) // requires testAddSCM
void testRemoveSCM() throws IOException {
assumeThat(getPeerCount()).isEqualTo(2);

final RemoveSCMRequest removeSCMRequest = new RemoveSCMRequest(
clusterID, FOLLOWER_SCM_ID, getFollowerAddress());
primarySCMHAManager.removeSCM(removeSCMRequest);
assertEquals(1, getPeerCount());
}

private int getPeerCount() {
return primarySCMHAManager.getRatisServer()
.getDivision().getGroup().getPeers().size();
}

private String getFollowerAddress() {
return "localhost:" +
follower.getDivision()
.getRaftServer().getServerRpc().getInetSocketAddress().getPort();
}

@Test
public void testHARingRemovalErrors() throws IOException,
void testHARingRemovalErrors() throws IOException,
AuthenticationException {
OzoneConfiguration config = new OzoneConfiguration();
config.set(ScmConfigKeys.OZONE_SCM_PRIMORDIAL_NODE_ID_KEY, "scm1");
Expand Down Expand Up @@ -160,35 +185,6 @@ public void testHARingRemovalErrors() throws IOException,
scm2.getScmHAManager().getRatisServer().stop();
}
}
@Test
public void testRemoveSCM() throws IOException, InterruptedException {
assertEquals(1, primarySCMHAManager.getRatisServer()
.getDivision().getGroup().getPeers().size());

final StorageContainerManager scm2 = getMockStorageContainerManager(
getConfig("scm2", 9898));
try {
scm2.getScmHAManager().getRatisServer().start();
final AddSCMRequest addSCMRequest = new AddSCMRequest(
clusterID, scm2.getScmId(),
"localhost:" + scm2.getScmHAManager().getRatisServer()
.getDivision().getRaftServer().getServerRpc()
.getInetSocketAddress().getPort());
primarySCMHAManager.addSCM(addSCMRequest);
assertEquals(2, primarySCMHAManager.getRatisServer()
.getDivision().getGroup().getPeers().size());

final RemoveSCMRequest removeSCMRequest = new RemoveSCMRequest(
clusterID, scm2.getScmId(), "localhost:" +
scm2.getScmHAManager().getRatisServer().getDivision()
.getRaftServer().getServerRpc().getInetSocketAddress().getPort());
primarySCMHAManager.removeSCM(removeSCMRequest);
assertEquals(1, primarySCMHAManager.getRatisServer()
.getDivision().getGroup().getPeers().size());
} finally {
scm2.getScmHAManager().getRatisServer().stop();
}
}

private StorageContainerManager getMockStorageContainerManager(
OzoneConfiguration conf) throws IOException {
Expand Down

0 comments on commit 1e6e6d1

Please sign in to comment.