org.apache.ratis.util.LifeCycle Java Examples

The following examples show how to use org.apache.ratis.util.LifeCycle. 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: TestOzoneManagerConfiguration.java    From hadoop-ozone with Apache License 2.0 6 votes vote down vote up
/**
 * Test a single node OM service (default setting for MiniOzoneCluster).
 * @throws Exception
 */
@Test
public void testSingleNodeOMservice() throws Exception {
  // Default settings of MiniOzoneCluster start a sinle node OM service.
  startCluster();
  om = cluster.getOzoneManager();
  omRatisServer = om.getOmRatisServer();

  Assert.assertEquals(LifeCycle.State.RUNNING, om.getOmRatisServerState());
  // OM's Ratis server should have only 1 peer (itself) in its RaftGroup
  Collection<RaftPeer> peers = omRatisServer.getRaftGroup().getPeers();
  Assert.assertEquals(1, peers.size());

  // The RaftPeer id should match the configured omId
  RaftPeer raftPeer = peers.toArray(new RaftPeer[1])[0];
  Assert.assertEquals(omId, raftPeer.getId().toString());
}
 
Example #2
Source File: Server.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
@Override
public void run() throws Exception {
  RaftPeerId peerId = RaftPeerId.valueOf(id);
  RaftProperties properties = new RaftProperties();

  final int port = NetUtils.createSocketAddr(getPeer(peerId).getAddress()).getPort();
  GrpcConfigKeys.Server.setPort(properties, port);
  properties.setInt(GrpcConfigKeys.OutputStream.RETRY_TIMES_KEY, Integer.MAX_VALUE);
  RaftServerConfigKeys.setStorageDir(properties, Collections.singletonList(storageDir));
  StateMachine stateMachine = new ArithmeticStateMachine();

  final RaftGroup raftGroup = RaftGroup.valueOf(RaftGroupId.valueOf(ByteString.copyFromUtf8(getRaftGroupId())),
          getPeers());
  RaftServer raftServer = RaftServer.newBuilder()
      .setServerId(RaftPeerId.valueOf(id))
      .setStateMachine(stateMachine).setProperties(properties)
      .setGroup(raftGroup)
      .build();
  raftServer.start();

  for(; raftServer.getLifeCycleState() != LifeCycle.State.CLOSED;) {
    TimeUnit.SECONDS.sleep(1);
  }
}
 
Example #3
Source File: Server.java    From ratis with Apache License 2.0 6 votes vote down vote up
@Override
public void run() throws Exception {
  RaftPeerId peerId = RaftPeerId.valueOf(id);
  RaftProperties properties = new RaftProperties();

  RaftPeer[] peers = getPeers();
  final int port = NetUtils.createSocketAddr(getPeer(peerId).getAddress()).getPort();
  GrpcConfigKeys.Server.setPort(properties, port);
  properties.setInt(GrpcConfigKeys.OutputStream.RETRY_TIMES_KEY, Integer.MAX_VALUE);
  RaftServerConfigKeys.setStorageDirs(properties, Collections.singletonList(storageDir));
  ConfUtils.setFile(properties::setFile, FileStoreCommon.STATEMACHINE_DIR_KEY,
      storageDir);
  StateMachine stateMachine = new FileStoreStateMachine(properties);

  final RaftGroup raftGroup = RaftGroup.valueOf(RaftGroupId.valueOf(ByteString.copyFromUtf8(raftGroupId)), peers);
  RaftServer raftServer = RaftServer.newBuilder()
      .setServerId(RaftPeerId.valueOf(id))
      .setStateMachine(stateMachine).setProperties(properties)
      .setGroup(raftGroup)
      .build();
  raftServer.start();

  for(; raftServer.getLifeCycleState() != LifeCycle.State.CLOSED;) {
    TimeUnit.SECONDS.sleep(1);
  }
}
 
Example #4
Source File: Server.java    From ratis with Apache License 2.0 6 votes vote down vote up
@Override
public void run() throws Exception {
  RaftPeerId peerId = RaftPeerId.valueOf(id);
  RaftProperties properties = new RaftProperties();

  RaftPeer[] peers = getPeers();
  final int port = NetUtils.createSocketAddr(getPeer(peerId).getAddress()).getPort();
  GrpcConfigKeys.Server.setPort(properties, port);
  properties.setInt(GrpcConfigKeys.OutputStream.RETRY_TIMES_KEY, Integer.MAX_VALUE);
  RaftServerConfigKeys.setStorageDirs(properties, Collections.singletonList(storageDir));
  StateMachine stateMachine = new ArithmeticStateMachine();

  final RaftGroup raftGroup = RaftGroup.valueOf(RaftGroupId.valueOf(ByteString.copyFromUtf8(raftGroupId)), peers);
  RaftServer raftServer = RaftServer.newBuilder()
      .setServerId(RaftPeerId.valueOf(id))
      .setStateMachine(stateMachine).setProperties(properties)
      .setGroup(raftGroup)
      .build();
  raftServer.start();

  for(; raftServer.getLifeCycleState() != LifeCycle.State.CLOSED;) {
    TimeUnit.SECONDS.sleep(1);
  }
}
 
Example #5
Source File: LeaderElection.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
private void startIfNew(Runnable starter) {
  if (lifeCycle.compareAndTransition(NEW, STARTING)) {
    starter.run();
  } else {
    final LifeCycle.State state = lifeCycle.getCurrentState();
    LOG.info("{}: skip starting since this is already {}", this, state);
  }
}
 
Example #6
Source File: LeaderElectionTests.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@Test
public void testImmediatelyRevertedToFollower() {
  RaftServerImpl server = createMockServer(true);
  LeaderElection subject = new LeaderElection(server);

  try {
    subject.startInForeground();
    assertEquals(LifeCycle.State.CLOSED, subject.getCurrentState());
  } catch (Exception e) {
    LOG.info("Error starting LeaderElection", e);
    fail(e.getMessage());
  }
}
 
Example #7
Source File: LeaderElectionTests.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@Test
public void testShutdownBeforeStart() {
  RaftServerImpl server = createMockServer(false);
  LeaderElection subject = new LeaderElection(server);

  try {
    subject.shutdown();
    subject.startInForeground();
    assertEquals(LifeCycle.State.CLOSED, subject.getCurrentState());
  } catch (Exception e) {
    LOG.info("Error starting LeaderElection", e);
    fail(e.getMessage());
  }
}
 
Example #8
Source File: RaftServerProxy.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
RaftServerProxy(RaftPeerId id, StateMachine.Registry stateMachineRegistry,
    RaftProperties properties, Parameters parameters) {
  this.properties = properties;
  this.stateMachineRegistry = stateMachineRegistry;

  final RpcType rpcType = RaftConfigKeys.Rpc.type(properties, LOG::info);
  this.factory = ServerFactory.cast(rpcType.newFactory(parameters));

  this.serverRpc = factory.newRaftServerRpc(this);
  this.id = id != null? id: RaftPeerId.valueOf(getIdStringFrom(serverRpc));
  this.lifeCycle = new LifeCycle(this.id + "-" + getClass().getSimpleName());

  this.implExecutor = Executors.newSingleThreadExecutor();
}
 
Example #9
Source File: OzoneManager.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
public LifeCycle.State getOmRatisServerState() {
  if (omRatisServer == null) {
    return null;
  } else {
    return omRatisServer.getServerState();
  }
}
 
Example #10
Source File: RaftServerProxy.java    From ratis with Apache License 2.0 5 votes vote down vote up
RaftServerProxy(RaftPeerId id, StateMachine.Registry stateMachineRegistry,
    RaftProperties properties, Parameters parameters) {
  this.properties = properties;
  this.stateMachineRegistry = stateMachineRegistry;

  final RpcType rpcType = RaftConfigKeys.Rpc.type(properties, LOG::info);
  this.factory = ServerFactory.cast(rpcType.newFactory(parameters));

  this.serverRpc = factory.newRaftServerRpc(this);
  this.id = id != null? id: RaftPeerId.valueOf(getIdStringFrom(serverRpc));
  this.lifeCycle = new LifeCycle(this.id + "-" + getClass().getSimpleName());
}
 
Example #11
Source File: Server.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@Override
public void run() throws Exception {
  JVMMetrics.initJvmMetrics(TimeDuration.valueOf(10, TimeUnit.SECONDS));

  RaftPeerId peerId = RaftPeerId.valueOf(id);
  RaftProperties properties = new RaftProperties();

  final int port = NetUtils.createSocketAddr(getPeer(peerId).getAddress()).getPort();
  GrpcConfigKeys.Server.setPort(properties, port);
  properties.setInt(GrpcConfigKeys.OutputStream.RETRY_TIMES_KEY, Integer.MAX_VALUE);
  RaftServerConfigKeys.setStorageDir(properties, Collections.singletonList(storageDir));
  ConfUtils.setFile(properties::setFile, FileStoreCommon.STATEMACHINE_DIR_KEY,
      storageDir);
  StateMachine stateMachine = new FileStoreStateMachine(properties);

  final RaftGroup raftGroup = RaftGroup.valueOf(RaftGroupId.valueOf(ByteString.copyFromUtf8(getRaftGroupId())),
          getPeers());
  RaftServer raftServer = RaftServer.newBuilder()
      .setServerId(RaftPeerId.valueOf(id))
      .setStateMachine(stateMachine).setProperties(properties)
      .setGroup(raftGroup)
      .build();

  raftServer.start();

  for (; raftServer.getLifeCycleState() != LifeCycle.State.CLOSED; ) {
    TimeUnit.SECONDS.sleep(1);
  }
}
 
Example #12
Source File: MetadataServer.java    From ratis with Apache License 2.0 5 votes vote down vote up
public void start() throws IOException  {
    final ServerOpts opts = getServerOpts();
    if (opts.getHost() == null) {
        opts.setHost(LogServiceUtils.getHostName());
    }
    this.lifeCycle = new LifeCycle(this.id);
    RaftProperties properties = new RaftProperties();
    if(opts.getWorkingDir() != null) {
        RaftServerConfigKeys.setStorageDirs(properties, Collections.singletonList(new File(opts.getWorkingDir())));
    }
    GrpcConfigKeys.Server.setPort(properties, opts.getPort());
    NettyConfigKeys.Server.setPort(properties, opts.getPort());
    Set<RaftPeer> peers = getPeersFromQuorum(opts.getMetaQuorum());
    RaftGroupId raftMetaGroupId = RaftGroupId.valueOf(opts.getMetaGroupId());
    RaftGroup metaGroup = RaftGroup.valueOf(raftMetaGroupId, peers);
    metaStateMachine = new MetaStateMachine(raftMetaGroupId, RaftGroupId.valueOf(opts.getLogServerGroupId()));
    server = RaftServer.newBuilder()
            .setGroup(metaGroup)
            .setServerId(RaftPeerId.valueOf(id))
            .setStateMachineRegistry(raftGroupId -> {
                if(raftGroupId.equals(META_GROUP_ID)) {
                    return metaStateMachine;
                }
                return null;
            })
            .setProperties(properties).build();
    lifeCycle.startAndTransition(() -> {
        server.start();
    }, IOException.class);
}
 
Example #13
Source File: BaseStateMachine.java    From ratis with Apache License 2.0 4 votes vote down vote up
@Override
public LifeCycle.State getLifeCycleState() {
  return lifeCycle.getCurrentState();
}
 
Example #14
Source File: SimpleStateMachine4Testing.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
@Override
public synchronized void pause() {
  getLifeCycle().transition(LifeCycle.State.PAUSING);
  getLifeCycle().transition(LifeCycle.State.PAUSED);
}
 
Example #15
Source File: MetadataServer.java    From ratis with Apache License 2.0 4 votes vote down vote up
public MetadataServer(ServerOpts opts) {
  super(opts);
  LOG.debug("Metadata Server options: {}", opts);
  this.id = opts.getHost() + "_" + opts.getPort();
  this.lifeCycle = new LifeCycle(this.id);
}
 
Example #16
Source File: NettyClient.java    From ratis with Apache License 2.0 4 votes vote down vote up
public ChannelFuture writeAndFlush(Object msg) {
  lifeCycle.assertCurrentState(LifeCycle.State.RUNNING);
  return channel.writeAndFlush(msg);
}
 
Example #17
Source File: LeaderElection.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
LeaderElection(RaftServerImpl server) {
  this.name = server.getMemberId() + "-" + getClass().getSimpleName() + COUNT.incrementAndGet();
  this.lifeCycle = new LifeCycle(this);
  this.daemon = new Daemon(this);
  this.server = server;
}
 
Example #18
Source File: RaftServerProxy.java    From ratis with Apache License 2.0 4 votes vote down vote up
@Override
public LifeCycle.State getLifeCycleState() {
  return lifeCycle.getCurrentState();
}
 
Example #19
Source File: RaftServerRpcWithProxy.java    From ratis with Apache License 2.0 4 votes vote down vote up
public RaftServerRpcWithProxy(Supplier<RaftPeerId> idSupplier, Function<RaftPeerId, PROXIES> proxyCreater) {
  this.idSupplier = idSupplier;
  this.lifeCycleSupplier = JavaUtils.memoize(() -> new LifeCycle(getId() + "-" + getClass().getSimpleName()));
  this.proxiesSupplier = JavaUtils.memoize(() -> proxyCreater.apply(getId()));
}
 
Example #20
Source File: RaftServerRpcWithProxy.java    From ratis with Apache License 2.0 4 votes vote down vote up
public LifeCycle getLifeCycle() {
  return lifeCycleSupplier.get();
}
 
Example #21
Source File: SimpleStateMachine4Testing.java    From ratis with Apache License 2.0 4 votes vote down vote up
@Override
public synchronized void pause() {
  lifeCycle.transition(LifeCycle.State.PAUSING);
  lifeCycle.transition(LifeCycle.State.PAUSED);
}
 
Example #22
Source File: SimulatedServerRpc.java    From ratis with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isAlive() {
  return !server.getLifeCycleState().isOneOf(LifeCycle.State.CLOSING, LifeCycle.State.CLOSED);
}
 
Example #23
Source File: SimulatedServerRpc.java    From ratis with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isAlive() {
  return !server.getLifeCycleState().isOneOf(LifeCycle.State.CLOSING, LifeCycle.State.CLOSED);
}
 
Example #24
Source File: NettyClient.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
public ChannelFuture writeAndFlush(Object msg) {
  lifeCycle.assertCurrentState(LifeCycle.States.RUNNING);
  return channel.writeAndFlush(msg);
}
 
Example #25
Source File: TestOzoneManagerConfiguration.java    From hadoop-ozone with Apache License 2.0 4 votes vote down vote up
/**
 * Test multiple OM service configuration.
 */
@Test
public void testMultipleOMServiceIds() throws Exception {
  // Set up OZONE_OM_SERVICES_KEY with 2 service Ids.
  String om1ServiceId = "om-service-test1";
  String om2ServiceId = "om-service-test2";
  String omServices = om1ServiceId + "," + om2ServiceId;
  conf.set(OMConfigKeys.OZONE_OM_SERVICE_IDS_KEY, omServices);

  String omNode1Id = "omNode1";
  String omNode2Id = "omNode2";
  String omNode3Id = "omNode3";
  String omNodesKeyValue = omNode1Id + "," + omNode2Id + "," + omNode3Id;

  // Set the node Ids for the 2 services. The nodeIds need to be
  // distinch within one service. The ids can overlap between
  // different services.
  String om1NodesKey = OmUtils.addKeySuffixes(
      OMConfigKeys.OZONE_OM_NODES_KEY, om1ServiceId);
  String om2NodesKey = OmUtils.addKeySuffixes(
      OMConfigKeys.OZONE_OM_NODES_KEY, om2ServiceId);
  conf.set(om1NodesKey, omNodesKeyValue);
  conf.set(om2NodesKey, omNodesKeyValue);

  // Set the RPC addresses for all 6 OMs (3 for each service). Only one
  // node out of these must have the localhost address.
  conf.set(getOMAddrKeyWithSuffix(om1ServiceId, omNode1Id),
      "122.0.0.123:9862");
  conf.set(getOMAddrKeyWithSuffix(om1ServiceId, omNode2Id),
      "123.0.0.124:9862");
  conf.set(getOMAddrKeyWithSuffix(om1ServiceId, omNode3Id),
      "124.0.0.125:9862");
  conf.set(getOMAddrKeyWithSuffix(om2ServiceId, omNode1Id),
      "125.0.0.126:9862");
  conf.set(getOMAddrKeyWithSuffix(om2ServiceId, omNode2Id),
      "0.0.0.0:9862");
  conf.set(getOMAddrKeyWithSuffix(om2ServiceId, omNode3Id),
      "126.0.0.127:9862");

  startCluster();
  om = cluster.getOzoneManager();
  omRatisServer = om.getOmRatisServer();

  Assert.assertEquals(LifeCycle.State.RUNNING, om.getOmRatisServerState());

  // OM's Ratis server should have 3 peers in its RaftGroup
  Collection<RaftPeer> peers = omRatisServer.getRaftGroup().getPeers();
  Assert.assertEquals(3, peers.size());

  // Verify that the serviceId and nodeId match the node with the localhost
  // address - om-service-test2 and omNode2
  Assert.assertEquals(om2ServiceId, om.getOMServiceId());
  Assert.assertEquals(omNode2Id, omRatisServer.getRaftPeerId().toString());
}
 
Example #26
Source File: TestOzoneFsHAURLs.java    From hadoop-ozone with Apache License 2.0 4 votes vote down vote up
@Before
public void init() throws Exception {
  conf = new OzoneConfiguration();
  omId = UUID.randomUUID().toString();
  omServiceId = "om-service-test1";
  numOfOMs = 3;
  clusterId = UUID.randomUUID().toString();
  scmId = UUID.randomUUID().toString();
  final String path = GenericTestUtils.getTempPath(omId);
  java.nio.file.Path metaDirPath = java.nio.file.Paths.get(path, "om-meta");
  conf.set(HddsConfigKeys.OZONE_METADATA_DIRS, metaDirPath.toString());
  conf.set(ScmConfigKeys.OZONE_SCM_CLIENT_ADDRESS_KEY, "127.0.0.1:0");
  conf.setInt(ScmConfigKeys.OZONE_DATANODE_PIPELINE_LIMIT, 3);

  OMStorage omStore = new OMStorage(conf);
  omStore.setClusterId(clusterId);
  omStore.setScmId(scmId);
  // writes the version file properties
  omStore.initialize();

  // Start the cluster
  cluster = MiniOzoneCluster.newHABuilder(conf)
      .setNumDatanodes(7)
      .setTotalPipelineNumLimit(10)
      .setClusterId(clusterId)
      .setScmId(scmId)
      .setOMServiceId(omServiceId)
      .setNumOfOzoneManagers(numOfOMs)
      .build();
  cluster.waitForClusterToBeReady();

  om = cluster.getOzoneManager();
  Assert.assertEquals(LifeCycle.State.RUNNING, om.getOmRatisServerState());

  volumeName = "volume" + RandomStringUtils.randomNumeric(5);
  ObjectStore objectStore =
      OzoneClientFactory.getRpcClient(omServiceId, conf).getObjectStore();
  objectStore.createVolume(volumeName);

  OzoneVolume retVolumeinfo = objectStore.getVolume(volumeName);
  bucketName = "bucket" + RandomStringUtils.randomNumeric(5);
  retVolumeinfo.createBucket(bucketName);

  rootPath = String.format("%s://%s.%s.%s/", OzoneConsts.OZONE_URI_SCHEME,
      bucketName, volumeName, omServiceId);
  // Set fs.defaultFS
  conf.set(CommonConfigurationKeysPublic.FS_DEFAULT_NAME_KEY, rootPath);
  FileSystem fs = FileSystem.get(conf);
  // Create some dirs
  Path root = new Path("/");
  Path dir1 = new Path(root, "dir1");
  Path dir12 = new Path(dir1, "dir12");
  Path dir2 = new Path(root, "dir2");
  fs.mkdirs(dir12);
  fs.mkdirs(dir2);
}
 
Example #27
Source File: OzoneManagerStateMachine.java    From hadoop-ozone with Apache License 2.0 4 votes vote down vote up
@Override
public void pause() {
  getLifeCycle().transition(LifeCycle.State.PAUSING);
  getLifeCycle().transition(LifeCycle.State.PAUSED);
  ozoneManagerDoubleBuffer.stop();
}
 
Example #28
Source File: OzoneManagerRatisServer.java    From hadoop-ozone with Apache License 2.0 4 votes vote down vote up
@VisibleForTesting
public LifeCycle.State getServerState() {
  return server.getLifeCycleState();
}
 
Example #29
Source File: TestOzoneManagerRatisServer.java    From hadoop-ozone with Apache License 2.0 4 votes vote down vote up
/**
 * Start a OM Ratis Server and checks its state.
 */
@Test
public void testStartOMRatisServer() throws Exception {
  Assert.assertEquals("Ratis Server should be in running state",
      LifeCycle.State.RUNNING, omRatisServer.getServerState());
}
 
Example #30
Source File: MetadataServer.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
public MetadataServer(ServerOpts opts) {
  super(opts);
  LOG.debug("Metadata Server options: {}", opts);
  this.id = opts.getHost() + "_" + opts.getPort();
  this.lifeCycle = new LifeCycle(this.id);
}