Search in sources :

Example 1 with SCMConnectionManager

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();
}
Also used : EndpointStateMachine(org.apache.hadoop.ozone.container.common.statemachine.EndpointStateMachine) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) ExecutorService(java.util.concurrent.ExecutorService) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) SCMConnectionManager(org.apache.hadoop.ozone.container.common.statemachine.SCMConnectionManager) Test(org.junit.Test)

Example 2 with SCMConnectionManager

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;
    }
}
Also used : XceiverServerSpi(org.apache.hadoop.ozone.container.common.transport.server.XceiverServerSpi) HddsProtos(org.apache.hadoop.hdds.protocol.proto.HddsProtos) Logger(org.slf4j.Logger) StateContext(org.apache.hadoop.ozone.container.common.statemachine.StateContext) RaftGroup(org.apache.ratis.protocol.RaftGroup) RaftPeer(org.apache.ratis.protocol.RaftPeer) LoggerFactory(org.slf4j.LoggerFactory) DatanodeDetails(org.apache.hadoop.hdds.protocol.DatanodeDetails) IOException(java.io.IOException) CreatePipelineCommand(org.apache.hadoop.ozone.protocol.commands.CreatePipelineCommand) AlreadyExistsException(org.apache.ratis.protocol.exceptions.AlreadyExistsException) RaftGroupId(org.apache.ratis.protocol.RaftGroupId) AtomicLong(java.util.concurrent.atomic.AtomicLong) SCMCommandProto(org.apache.hadoop.hdds.protocol.proto.StorageContainerDatanodeProtocolProtos.SCMCommandProto) List(java.util.List) RatisHelper(org.apache.hadoop.hdds.ratis.RatisHelper) ConfigurationSource(org.apache.hadoop.hdds.conf.ConfigurationSource) PipelineID(org.apache.hadoop.hdds.scm.pipeline.PipelineID) OzoneContainer(org.apache.hadoop.ozone.container.ozoneimpl.OzoneContainer) Time(org.apache.hadoop.util.Time) SCMConnectionManager(org.apache.hadoop.ozone.container.common.statemachine.SCMConnectionManager) RaftClient(org.apache.ratis.client.RaftClient) SCMCommand(org.apache.hadoop.ozone.protocol.commands.SCMCommand) AlreadyExistsException(org.apache.ratis.protocol.exceptions.AlreadyExistsException) CreatePipelineCommand(org.apache.hadoop.ozone.protocol.commands.CreatePipelineCommand) RaftGroupId(org.apache.ratis.protocol.RaftGroupId) RaftGroup(org.apache.ratis.protocol.RaftGroup) IOException(java.io.IOException) RaftPeer(org.apache.ratis.protocol.RaftPeer) XceiverServerSpi(org.apache.hadoop.ozone.container.common.transport.server.XceiverServerSpi) HddsProtos(org.apache.hadoop.hdds.protocol.proto.HddsProtos) DatanodeDetails(org.apache.hadoop.hdds.protocol.DatanodeDetails) PipelineID(org.apache.hadoop.hdds.scm.pipeline.PipelineID) RaftClient(org.apache.ratis.client.RaftClient)

Example 3 with SCMConnectionManager

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());
    }
}
Also used : DatanodeStateMachine(org.apache.hadoop.ozone.container.common.statemachine.DatanodeStateMachine) SCMConnectionManager(org.apache.hadoop.ozone.container.common.statemachine.SCMConnectionManager) Test(org.junit.Test)

Aggregations

SCMConnectionManager (org.apache.hadoop.ozone.container.common.statemachine.SCMConnectionManager)3 Test (org.junit.Test)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutorCompletionService (java.util.concurrent.ExecutorCompletionService)1 ExecutorService (java.util.concurrent.ExecutorService)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 ConfigurationSource (org.apache.hadoop.hdds.conf.ConfigurationSource)1 DatanodeDetails (org.apache.hadoop.hdds.protocol.DatanodeDetails)1 HddsProtos (org.apache.hadoop.hdds.protocol.proto.HddsProtos)1 SCMCommandProto (org.apache.hadoop.hdds.protocol.proto.StorageContainerDatanodeProtocolProtos.SCMCommandProto)1 RatisHelper (org.apache.hadoop.hdds.ratis.RatisHelper)1 PipelineID (org.apache.hadoop.hdds.scm.pipeline.PipelineID)1 DatanodeStateMachine (org.apache.hadoop.ozone.container.common.statemachine.DatanodeStateMachine)1 EndpointStateMachine (org.apache.hadoop.ozone.container.common.statemachine.EndpointStateMachine)1 StateContext (org.apache.hadoop.ozone.container.common.statemachine.StateContext)1 XceiverServerSpi (org.apache.hadoop.ozone.container.common.transport.server.XceiverServerSpi)1 OzoneContainer (org.apache.hadoop.ozone.container.ozoneimpl.OzoneContainer)1