use of org.apache.hadoop.ozone.container.common.statemachine.SCMConnectionManager in project ozone by apache.
the class TestRunningDatanodeState method testAwait.
@Test
public void testAwait() throws InterruptedException {
SCMConnectionManager connectionManager = Mockito.mock(SCMConnectionManager.class);
List<EndpointStateMachine> stateMachines = new ArrayList<>();
when(connectionManager.getValues()).thenReturn(stateMachines);
RunningDatanodeState state = new RunningDatanodeState(null, connectionManager, null);
int threadPoolSize = 2;
ExecutorService executorService = Executors.newFixedThreadPool(threadPoolSize);
ExecutorCompletionService ecs = new ExecutorCompletionService<>(executorService);
state.setExecutorCompletionService(ecs);
for (int i = 0; i < threadPoolSize; i++) {
stateMachines.add(new EndpointStateMachine(null, null, null));
}
CompletableFuture<EndpointStateMachine.EndPointStates> futureOne = new CompletableFuture<>();
for (int i = 0; i < threadPoolSize; i++) {
ecs.submit(() -> futureOne.get());
}
long startTime = Time.monotonicNow();
state.await(500, TimeUnit.MILLISECONDS);
long endTime = Time.monotonicNow();
Assert.assertTrue((endTime - startTime) >= 500);
futureOne.complete(SHUTDOWN);
CompletableFuture<EndpointStateMachine.EndPointStates> futureTwo = new CompletableFuture<>();
for (int i = 0; i < threadPoolSize; i++) {
ecs.submit(() -> futureTwo.get());
}
futureTwo.complete(SHUTDOWN);
startTime = Time.monotonicNow();
state.await(500, TimeUnit.MILLISECONDS);
endTime = Time.monotonicNow();
Assert.assertTrue((endTime - startTime) < 500);
executorService.shutdown();
}
use of org.apache.hadoop.ozone.container.common.statemachine.SCMConnectionManager in project ozone by apache.
the class CreatePipelineCommandHandler method handle.
/**
* Handles a given SCM command.
*
* @param command - SCM Command
* @param ozoneContainer - Ozone Container.
* @param context - Current Context.
* @param connectionManager - The SCMs that we are talking to.
*/
@Override
public void handle(SCMCommand command, OzoneContainer ozoneContainer, StateContext context, SCMConnectionManager connectionManager) {
invocationCount.incrementAndGet();
final long startTime = Time.monotonicNow();
final DatanodeDetails dn = context.getParent().getDatanodeDetails();
final CreatePipelineCommand createCommand = (CreatePipelineCommand) command;
final PipelineID pipelineID = createCommand.getPipelineID();
final HddsProtos.PipelineID pipelineIdProto = pipelineID.getProtobuf();
final List<DatanodeDetails> peers = createCommand.getNodeList();
final List<Integer> priorityList = createCommand.getPriorityList();
try {
XceiverServerSpi server = ozoneContainer.getWriteChannel();
if (!server.isExist(pipelineIdProto)) {
final RaftGroupId groupId = RaftGroupId.valueOf(pipelineID.getId());
final RaftGroup group = RatisHelper.newRaftGroup(groupId, peers, priorityList);
server.addGroup(pipelineIdProto, peers, priorityList);
peers.stream().filter(d -> !d.getUuid().equals(dn.getUuid())).forEach(d -> {
final RaftPeer peer = RatisHelper.toRaftPeer(d);
try (RaftClient client = RatisHelper.newRaftClient(peer, conf, ozoneContainer.getTlsClientConfig())) {
client.getGroupManagementApi(peer.getId()).add(group);
} catch (AlreadyExistsException ae) {
// do not log
} catch (IOException ioe) {
LOG.warn("Add group failed for {}", d, ioe);
}
});
LOG.info("Created Pipeline {} {} {}.", createCommand.getReplicationType(), createCommand.getFactor(), pipelineID);
}
} catch (IOException e) {
// from another peer, so we may got an AlreadyExistsException.
if (!(e.getCause() instanceof AlreadyExistsException)) {
LOG.error("Can't create pipeline {} {} {}", createCommand.getReplicationType(), createCommand.getFactor(), pipelineID, e);
}
} finally {
long endTime = Time.monotonicNow();
totalTime += endTime - startTime;
}
}
use of org.apache.hadoop.ozone.container.common.statemachine.SCMConnectionManager in project ozone by apache.
the class TestDatanodeStateMachine method testStartStopDatanodeStateMachine.
/**
* Assert that starting statemachine executes the Init State.
*/
@Test
public void testStartStopDatanodeStateMachine() throws IOException, InterruptedException, TimeoutException {
try (DatanodeStateMachine stateMachine = new DatanodeStateMachine(getNewDatanodeDetails(), conf, null, null, null)) {
stateMachine.startDaemon();
SCMConnectionManager connectionManager = stateMachine.getConnectionManager();
GenericTestUtils.waitFor(() -> {
int size = connectionManager.getValues().size();
LOG.info("connectionManager.getValues().size() is {}", size);
return size == 1;
}, 1000, 30000);
stateMachine.stopDaemon();
assertTrue(stateMachine.isDaemonStopped());
}
}
Aggregations