org.apache.ratis.util.ExitUtils Java Examples

The following examples show how to use org.apache.ratis.util.ExitUtils. 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: MiniRaftCluster.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
public void shutdown() {
  LOG.info("************************************************************** ");
  LOG.info("*** ");
  LOG.info("***     Stopping " + getClass().getSimpleName());
  LOG.info("*** ");
  LOG.info("************************************************************** ");
  LOG.info(printServers());

  // TODO: classes like RaftLog may throw uncaught exception during shutdown (e.g. write after close)
  ExitUtils.setTerminateOnUncaughtException(false);

  final ExecutorService executor = Executors.newFixedThreadPool(servers.size(), Daemon::new);
  getServers().forEach(proxy -> executor.submit(proxy::close));
  try {
    executor.shutdown();
    // just wait for a few seconds
    executor.awaitTermination(5, TimeUnit.SECONDS);
  } catch(InterruptedException e) {
    LOG.warn("shutdown interrupted", e);
  }

  Optional.ofNullable(timer.get()).ifPresent(Timer::cancel);
  ExitUtils.assertNotTerminated();
  LOG.info(getClass().getSimpleName() + " shutdown completed");
}
 
Example #2
Source File: RaftBasicTests.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
static CompletableFuture<Void> killAndRestartServer(
    RaftPeerId id, long killSleepMs, long restartSleepMs, MiniRaftCluster cluster, Logger LOG) {
  final CompletableFuture<Void> future = new CompletableFuture<>();
  new Thread(() -> {
    try {
      Thread.sleep(killSleepMs);
      cluster.killServer(id);
      Thread.sleep(restartSleepMs);
      LOG.info("restart server: " + id);
      cluster.restartServer(id, false);
      future.complete(null);
    } catch (Exception e) {
      ExitUtils.terminate(-1, "Failed to kill/restart server: " + id, e, LOG);
    }
  }).start();
  return future;
}
 
Example #3
Source File: MiniRaftCluster.java    From ratis with Apache License 2.0 6 votes vote down vote up
public void shutdown() {
  LOG.info("************************************************************** ");
  LOG.info("*** ");
  LOG.info("***     Stopping " + getClass().getSimpleName());
  LOG.info("*** ");
  LOG.info("************************************************************** ");
  LOG.info(printServers());

  // TODO: classes like RaftLog may throw uncaught exception during shutdown (e.g. write after close)
  ExitUtils.setTerminateOnUncaughtException(false);

  final ExecutorService executor = Executors.newFixedThreadPool(servers.size(), Daemon::new);
  getServers().forEach(proxy -> executor.submit(proxy::close));
  try {
    executor.shutdown();
    // just wait for a few seconds
    executor.awaitTermination(5, TimeUnit.SECONDS);
  } catch(InterruptedException e) {
    LOG.warn("shutdown interrupted", e);
  }

  Optional.ofNullable(timer.get()).ifPresent(Timer::cancel);
  ExitUtils.assertNotTerminated();
  LOG.info(getClass().getSimpleName() + " shutdown completed");
}
 
Example #4
Source File: RaftBasicTests.java    From ratis with Apache License 2.0 6 votes vote down vote up
static CompletableFuture<Void> killAndRestartServer(
    RaftPeerId id, long killSleepMs, long restartSleepMs, MiniRaftCluster cluster, Logger LOG) {
  final CompletableFuture<Void> future = new CompletableFuture<>();
  new Thread(() -> {
    try {
      Thread.sleep(killSleepMs);
      cluster.killServer(id);
      Thread.sleep(restartSleepMs);
      LOG.info("restart server: " + id);
      cluster.restartServer(id, false);
      future.complete(null);
    } catch (Exception e) {
      ExitUtils.terminate(-1, "Failed to kill/restart server: " + id, e, LOG);
    }
  }).start();
  return future;
}
 
Example #5
Source File: OzoneManagerStateMachine.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
/**
 * Terminate OM.
 * @param omResponse
 * @param resultCode
 */
private void terminate(OMResponse omResponse,
    OMException.ResultCodes resultCode) {
  OMException exception = new OMException(omResponse.getMessage(),
      resultCode);
  String errorMessage = "OM Ratis Server has received unrecoverable " +
      "error, to avoid further DB corruption, terminating OM. Error " +
      "Response received is:" + omResponse;
  ExitUtils.terminate(1, errorMessage, exception, LOG);
}
 
Example #6
Source File: OzoneManagerStateMachine.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
/**
 * Submits write request to OM and returns the response Message.
 * @param request OMRequest
 * @return response from OM
 * @throws ServiceException
 */
private OMResponse runCommand(OMRequest request, long trxLogIndex) {
  try {
    return handler.handleWriteRequest(request,
        trxLogIndex).getOMResponse();
  } catch (Throwable e) {
    // For any Runtime exceptions, terminate OM.
    String errorMessage = "Request " + request + "failed with exception";
    ExitUtils.terminate(1, errorMessage, e, LOG);
  }
  return null;
}
 
Example #7
Source File: BaseTest.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@After
public void assertNoFailures() {
  final Throwable e = firstException.get();
  if (e != null) {
    throw new IllegalStateException("Failed: first exception was set", e);
  }

  ExitUtils.assertNotTerminated();
}
 
Example #8
Source File: MiniRaftCluster.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
protected MiniRaftCluster(String[] ids, RaftProperties properties, Parameters parameters) {
  this.group = initRaftGroup(Arrays.asList(ids));
  LOG.info("new {} with {}", getClass().getSimpleName(), group);
  this.properties = new RaftProperties(properties);
  this.parameters = parameters;

  ExitUtils.disableSystemExit();
}
 
Example #9
Source File: LeaderElectionTests.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@Test
public void testChangeLeader() throws Exception {
  SegmentedRaftLogTestUtils.setRaftLogWorkerLogLevel(Level.TRACE);
  LOG.info("Running testChangeLeader");
  final MiniRaftCluster cluster = newCluster(3);
  cluster.start();

  RaftPeerId leader = RaftTestUtil.waitForLeader(cluster).getId();
  for(int i = 0; i < 10; i++) {
    leader = RaftTestUtil.changeLeader(cluster, leader, IllegalStateException::new);
    ExitUtils.assertNotTerminated();
  }
  SegmentedRaftLogTestUtils.setRaftLogWorkerLogLevel(Level.INFO);
  cluster.shutdown();
}
 
Example #10
Source File: MiniRaftCluster.java    From ratis with Apache License 2.0 5 votes vote down vote up
protected MiniRaftCluster(String[] ids, RaftProperties properties, Parameters parameters) {
  this.group = initRaftGroup(Arrays.asList(ids));
  LOG.info("new {} with {}", getClass().getSimpleName(), group);
  this.properties = new RaftProperties(properties);
  this.parameters = parameters;

  ExitUtils.disableSystemExit();
}
 
Example #11
Source File: LeaderElectionTests.java    From ratis with Apache License 2.0 5 votes vote down vote up
@Test
public void testChangeLeader() throws Exception {
  RaftStorageTestUtils.setRaftLogWorkerLogLevel(Level.TRACE);
  LOG.info("Running testChangeLeader");
  final MiniRaftCluster cluster = newCluster(3);
  cluster.start();

  RaftPeerId leader = RaftTestUtil.waitForLeader(cluster).getId();
  for(int i = 0; i < 10; i++) {
    leader = RaftTestUtil.changeLeader(cluster, leader);
    ExitUtils.assertNotTerminated();
  }
  RaftStorageTestUtils.setRaftLogWorkerLogLevel(Level.INFO);
  cluster.shutdown();
}
 
Example #12
Source File: OzoneManagerDoubleBuffer.java    From hadoop-ozone with Apache License 2.0 4 votes vote down vote up
private void terminate(IOException ex) {
  String message = "During flush to DB encountered error in " +
      "OMDoubleBuffer flush thread " + Thread.currentThread().getName();
  ExitUtils.terminate(1, message, ex, LOG);
}
 
Example #13
Source File: BaseTest.java    From ratis with Apache License 2.0 4 votes vote down vote up
@After
public void assertNotTerminated() {
  ExitUtils.assertNotTerminated();
}