Java Code Examples for org.apache.ratis.protocol.RaftGroupId#valueOf()

The following examples show how to use org.apache.ratis.protocol.RaftGroupId#valueOf() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XceiverServerRatis.java    From hadoop-ozone with Apache License 2.0 6 votes vote down vote up
@Override
public void addGroup(HddsProtos.PipelineID pipelineId,
    Collection<DatanodeDetails> peers) throws IOException {
  final PipelineID pipelineID = PipelineID.getFromProtobuf(pipelineId);
  final RaftGroupId groupId = RaftGroupId.valueOf(pipelineID.getId());
  final RaftGroup group = RatisHelper.newRaftGroup(groupId, peers);
  GroupManagementRequest request = GroupManagementRequest.newAdd(
      clientId, server.getId(), nextCallId(), group);

  RaftClientReply reply;
  try {
    reply = server.groupManagement(request);
  } catch (Exception e) {
    throw new IOException(e.getMessage(), e);
  }
  processReply(reply);
}
 
Example 2
Source File: TestRatisPipelineLeader.java    From hadoop-ozone with Apache License 2.0 6 votes vote down vote up
private boolean verifyLeaderInfo(Pipeline ratisPipeline) throws Exception {
  Optional<HddsDatanodeService> hddsDatanodeService =
      cluster.getHddsDatanodes().stream().filter(s ->
          s.getDatanodeStateMachine().getDatanodeDetails().getUuid()
              .equals(ratisPipeline.getLeaderId())).findFirst();
  Assert.assertTrue(hddsDatanodeService.isPresent());

  XceiverServerRatis serverRatis =
      (XceiverServerRatis) hddsDatanodeService.get()
          .getDatanodeStateMachine().getContainer().getWriteChannel();

  GroupInfoRequest groupInfoRequest = new GroupInfoRequest(
      ClientId.randomId(), serverRatis.getServer().getId(),
      RaftGroupId.valueOf(ratisPipeline.getId().getId()), 100);
  GroupInfoReply reply =
      serverRatis.getServer().getGroupInfo(groupInfoRequest);
  return reply.getRoleInfoProto().hasLeaderInfo() &&
      ratisPipeline.getLeaderId().toString().equals(
          reply.getRoleInfoProto().getSelf().getId().toStringUtf8());
}
 
Example 3
Source File: XceiverServerRatis.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
private RaftClientRequest createRaftClientRequest(
    ContainerCommandRequestProto request, HddsProtos.PipelineID pipelineID,
    RaftClientRequest.Type type) {
  return new RaftClientRequest(clientId, server.getId(),
      RaftGroupId.valueOf(PipelineID.getFromProtobuf(pipelineID).getId()),
      nextCallId(), ContainerCommandRequestMessage.toMessage(request, null),
      type, null);
}
 
Example 4
Source File: FollowerAppendLogEntryGenerator.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
/**
 * Configure raft group before using raft service.
 */
private void configureGroup() throws IOException {

  ClientId clientId = ClientId.randomId();

  RaftGroupId groupId = RaftGroupId
      .valueOf(UUID.fromString(pipelineId));

  RaftPeerId peerId =
      RaftPeerId.getRaftPeerId(serverId);

  RaftGroup group = RaftGroup.valueOf(groupId,
      new RaftPeer(RaftPeerId.valueOf(serverId), serverAddress),
      new RaftPeer(RaftPeerId.valueOf(FAKE_LEADER_ID),
          FAKE_LEADER_ADDDRESS));
  RaftClient client = RaftClient.newBuilder()
      .setClientId(clientId)
      .setProperties(new RaftProperties(true))
      .setRaftGroup(group)
      .build();

  RaftClientReply raftClientReply = client.groupAdd(group, peerId);

  LOG.info(
      "Group is configured in the RAFT server (one follower, one fake "
          + "leader): {}", raftClientReply);
}
 
Example 5
Source File: FollowerAppendLogEntryGenerator.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
private RaftRpcRequestProto createServerRequest(long callId) {
  RaftGroupId raftGroupId = RaftGroupId
      .valueOf(UUID.fromString(pipelineId));

  return RaftRpcRequestProto.newBuilder()
      .setRaftGroupId(
          RaftGroupIdProto.newBuilder().setId(raftGroupId.toByteString())
              .build())
      .setRequestorId(requestor.getId())
      .setCallId(callId)
      .build();
}
 
Example 6
Source File: LeaderAppendLogEntryGenerator.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
private void configureGroup() throws IOException {
  ClientId clientId = ClientId.randomId();

  RaftGroupId groupId = RaftGroupId
      .valueOf(UUID.fromString(pipelineId));
  RaftPeerId peerId =
      RaftPeerId.getRaftPeerId(serverId);

  RaftGroup group = RaftGroup.valueOf(groupId,
      new RaftPeer(RaftPeerId.valueOf(serverId), serverAddress),
      new RaftPeer(RaftPeerId.valueOf(FAKE_FOLLOWER_ID1),
          FAKE_LEADER_ADDDRESS1),
      new RaftPeer(RaftPeerId.valueOf(FAKE_FOLLOWER_ID1),
          FAKE_LEADER_ADDDRESS2));
  RaftClient client = RaftClient.newBuilder()
      .setClientId(clientId)
      .setProperties(new RaftProperties(true))
      .setRaftGroup(group)
      .build();

  RaftClientReply raftClientReply = client.groupAdd(group, peerId);

  LOG.info(
      "Group is configured in the RAFT server (with two fake leader leader)"
          + ": {}",
      raftClientReply);
}
 
Example 7
Source File: CreatePipelineCommandHandler.java    From hadoop-ozone with Apache License 2.0 4 votes vote down vote up
/**
 * 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 CreatePipelineCommandProto createCommand =
      ((CreatePipelineCommand)command).getProto();
  final HddsProtos.PipelineID pipelineID = createCommand.getPipelineID();
  final Collection<DatanodeDetails> peers =
      createCommand.getDatanodeList().stream()
          .map(DatanodeDetails::getFromProtoBuf)
          .collect(Collectors.toList());

  try {
    XceiverServerSpi server = ozoneContainer.getWriteChannel();
    if (!server.isExist(pipelineID)) {
      final RaftGroupId groupId = RaftGroupId.valueOf(
          PipelineID.getFromProtobuf(pipelineID).getId());
      final RaftGroup group = RatisHelper.newRaftGroup(groupId, peers);
      server.addGroup(pipelineID, peers);
      peers.stream().filter(
          d -> !d.getUuid().equals(dn.getUuid()))
          .forEach(d -> {
            final RaftPeer peer = RatisHelper.toRaftPeer(d);
            try (RaftClient client = RatisHelper.newRaftClient(peer, conf)) {
              client.groupAdd(group, peer.getId());
            } catch (AlreadyExistsException ae) {
              // do not log
            } catch (IOException ioe) {
              LOG.warn("Add group failed for {}", d, ioe);
            }
          });
      LOG.info("Created Pipeline {} {} #{}.",
          createCommand.getType(), createCommand.getFactor(), pipelineID);
    }
  } catch (IOException e) {
    LOG.error("Can't create pipeline {} {} #{}", createCommand.getType(),
        createCommand.getFactor(), pipelineID, e);
  } finally {
    long endTime = Time.monotonicNow();
    totalTime += endTime - startTime;
  }
}
 
Example 8
Source File: XceiverServerRatis.java    From hadoop-ozone with Apache License 2.0 4 votes vote down vote up
private GroupInfoRequest createGroupInfoRequest(
    HddsProtos.PipelineID pipelineID) {
  return new GroupInfoRequest(clientId, server.getId(),
      RaftGroupId.valueOf(PipelineID.getFromProtobuf(pipelineID).getId()),
      nextCallId());
}
 
Example 9
Source File: TestPipelineClose.java    From hadoop-ozone with Apache License 2.0 4 votes vote down vote up
@Test
public void testPipelineCloseWithLogFailure() throws IOException {

  EventQueue eventQ = (EventQueue) scm.getEventQueue();
  PipelineActionHandler pipelineActionTest =
      Mockito.mock(PipelineActionHandler.class);
  eventQ.addHandler(SCMEvents.PIPELINE_ACTIONS, pipelineActionTest);
  ArgumentCaptor<PipelineActionsFromDatanode> actionCaptor =
      ArgumentCaptor.forClass(PipelineActionsFromDatanode.class);

  ContainerInfo containerInfo = containerManager
      .allocateContainer(RATIS, THREE, "testOwner");
  ContainerWithPipeline containerWithPipeline =
      new ContainerWithPipeline(containerInfo,
          pipelineManager.getPipeline(containerInfo.getPipelineID()));
  Pipeline openPipeline = containerWithPipeline.getPipeline();
  RaftGroupId groupId = RaftGroupId.valueOf(openPipeline.getId().getId());

  try {
    pipelineManager.getPipeline(openPipeline.getId());
  } catch (PipelineNotFoundException e) {
    Assert.assertTrue("pipeline should exist", false);
  }

  DatanodeDetails datanodeDetails = openPipeline.getNodes().get(0);
  int index = cluster.getHddsDatanodeIndex(datanodeDetails);

  XceiverServerRatis xceiverRatis =
      (XceiverServerRatis) cluster.getHddsDatanodes().get(index)
      .getDatanodeStateMachine().getContainer().getWriteChannel();

  /**
   * Notify Datanode Ratis Server endpoint of a Ratis log failure.
   * This is expected to trigger an immediate pipeline actions report to SCM
   */
  xceiverRatis.handleNodeLogFailure(groupId, null);

  // verify SCM receives a pipeline action report "immediately"
  Mockito.verify(pipelineActionTest, Mockito.timeout(100))
      .onMessage(
          actionCaptor.capture(),
          Mockito.any(EventPublisher.class));

  PipelineActionsFromDatanode actionsFromDatanode =
      actionCaptor.getValue();

  // match the pipeline id
  verifyCloseForPipeline(openPipeline, actionsFromDatanode);
}
 
Example 10
Source File: OzoneManagerRatisServer.java    From hadoop-ozone with Apache License 2.0 4 votes vote down vote up
/**
 * Returns an OM Ratis server.
 * @param conf configuration
 * @param om the OM instance starting the ratis server
 * @param raftGroupIdStr raft group id string
 * @param localRaftPeerId raft peer id of this Ratis server
 * @param addr address of the ratis server
 * @param raftPeers peer nodes in the raft ring
 * @throws IOException
 */
private OzoneManagerRatisServer(ConfigurationSource conf,
    OzoneManager om,
    String raftGroupIdStr, RaftPeerId localRaftPeerId,
    InetSocketAddress addr, List<RaftPeer> raftPeers)
    throws IOException {
  this.ozoneManager = om;
  this.omRatisAddress = addr;
  this.port = addr.getPort();
  RaftProperties serverProperties = newRaftProperties(conf);

  this.raftPeerId = localRaftPeerId;
  this.raftGroupId = RaftGroupId.valueOf(
      getRaftGroupIdFromOmServiceId(raftGroupIdStr));
  this.raftGroup = RaftGroup.valueOf(raftGroupId, raftPeers);

  StringBuilder raftPeersStr = new StringBuilder();
  for (RaftPeer peer : raftPeers) {
    raftPeersStr.append(", ").append(peer.getAddress());
  }
  LOG.info("Instantiating OM Ratis server with GroupID: {} and " +
      "Raft Peers: {}", raftGroupIdStr, raftPeersStr.toString().substring(2));

  this.omStateMachine = getStateMachine(conf);

  this.server = RaftServer.newBuilder()
      .setServerId(this.raftPeerId)
      .setGroup(this.raftGroup)
      .setProperties(serverProperties)
      .setStateMachine(omStateMachine)
      .build();

  // Run a scheduler to check and update the server role on the leader
  // periodically
  this.scheduledRoleChecker = Executors.newSingleThreadScheduledExecutor();
  this.scheduledRoleChecker.scheduleWithFixedDelay(new Runnable() {
    @Override
    public void run() {
      // Run this check only on the leader OM
      if (cachedPeerRole.isPresent() &&
          cachedPeerRole.get() == RaftPeerRole.LEADER) {
        updateServerRole();
      }
    }
  }, roleCheckInitialDelayMs, roleCheckIntervalMs, TimeUnit.MILLISECONDS);
}
 
Example 11
Source File: ProtoUtils.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
static RaftGroupId toRaftGroupId(RaftGroupIdProto proto) {
  return RaftGroupId.valueOf(proto.getId());
}
 
Example 12
Source File: LogServer.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
public void start() throws IOException {
    final ServerOpts opts = getServerOpts();
    Set<RaftPeer> peers = LogServiceUtils.getPeersFromQuorum(opts.getMetaQuorum());
    RaftProperties properties = new RaftProperties();

    // Set properties for the log server state machine
    setRaftProperties(properties);

    InetSocketAddress addr = new InetSocketAddress(opts.getHost(), opts.getPort());
    if(opts.getWorkingDir() != null) {
        RaftServerConfigKeys.setStorageDir(properties, Collections.singletonList(new File(opts.getWorkingDir())));
    }
    String id = opts.getHost() +"_" +  opts.getPort();
    RaftPeer peer = new RaftPeer(RaftPeerId.valueOf(id), addr);
    final RaftGroupId logServerGroupId = RaftGroupId.valueOf(opts.getLogServerGroupId());
    RaftGroup all = RaftGroup.valueOf(logServerGroupId, peer);
    RaftGroup meta = RaftGroup.valueOf(RaftGroupId.valueOf(opts.getMetaGroupId()), peers);

    // Make sure that we aren't setting any invalid/harmful properties
    validateRaftProperties(properties);

    raftServer = RaftServer.newBuilder()
            .setStateMachineRegistry(new StateMachine.Registry() {
                @Override
                public StateMachine apply(RaftGroupId raftGroupId) {
                    // TODO this looks wrong. Why isn't this metaGroupId?
                    if(raftGroupId.equals(logServerGroupId)) {
                        return new ManagementStateMachine();
                    }
                    return new LogStateMachine(properties);
                }
            })
            .setProperties(properties)
            .setServerId(RaftPeerId.valueOf(id))
            .setGroup(all)
            .build();
    raftServer.start();

    metaClient = RaftClient.newBuilder()
            .setRaftGroup(meta)
            .setClientId(ClientId.randomId())
            .setProperties(properties)
            .build();
    metaClient.send(() -> MetaServiceProtoUtil.toPingRequestProto(peer).toByteString());
    daemon = new Daemon(new HeartbeatSender(new RaftPeer(raftServer.getId())),
            "heartbeat-Sender"+raftServer.getId());
    daemon.start();
}
 
Example 13
Source File: ProtoUtils.java    From ratis with Apache License 2.0 4 votes vote down vote up
static RaftGroupId toRaftGroupId(RaftGroupIdProto proto) {
  return RaftGroupId.valueOf(proto.getId());
}
 
Example 14
Source File: LogServer.java    From ratis with Apache License 2.0 4 votes vote down vote up
public void start() throws IOException {
    final ServerOpts opts = getServerOpts();
    Set<RaftPeer> peers = LogServiceUtils.getPeersFromQuorum(opts.getMetaQuorum());
    RaftProperties properties = new RaftProperties();
    properties.set("raft.client.rpc.request.timeout", "100000");
    GrpcConfigKeys.Server.setPort(properties, opts.getPort());
    NettyConfigKeys.Server.setPort(properties, opts.getPort());
    InetSocketAddress addr = new InetSocketAddress(opts.getHost(), opts.getPort());
    if(opts.getWorkingDir() != null) {
        RaftServerConfigKeys.setStorageDirs(properties, Collections.singletonList(new File(opts.getWorkingDir())));
    }
    String id = opts.getHost() +"_" +  opts.getPort();
    RaftPeer peer = new RaftPeer(RaftPeerId.valueOf(id), addr);
    final RaftGroupId logServerGroupId = RaftGroupId.valueOf(opts.getLogServerGroupId());
    RaftGroup all = RaftGroup.valueOf(logServerGroupId, peer);
    RaftGroup meta = RaftGroup.valueOf(RaftGroupId.valueOf(opts.getMetaGroupId()), peers);
    raftServer = RaftServer.newBuilder()
            .setStateMachineRegistry(new StateMachine.Registry() {
                private final StateMachine managementMachine = new ManagementStateMachine();
                private final StateMachine logMachine  = new LogStateMachine();
                @Override
                public StateMachine apply(RaftGroupId raftGroupId) {
                    // TODO this looks wrong. Why isn't this metaGroupId?
                    if(raftGroupId.equals(logServerGroupId)) {
                        return managementMachine;
                    }
                    return logMachine;
                }
            })
            .setProperties(properties)
            .setServerId(RaftPeerId.valueOf(id))
            .setGroup(all)
            .build();
    raftServer.start();

    metaClient = RaftClient.newBuilder()
            .setRaftGroup(meta)
            .setClientId(ClientId.randomId())
            .setProperties(properties)
            .build();
    metaClient.send(() -> MetaServiceProtoUtil.toPingRequestProto(peer).toByteString());
}