List of usage examples for com.google.common.util.concurrent Service state
State state();
From source file:org.xtreemfs.foundation.flease.sim.FleaseSim.java
/** * @param args the command line arguments *//*from ww w .java 2 s . co m*/ public static void main(String[] args) { try { final int dmax = 500; final int leaseTimeout = 5000; final int numHosts = 10; final FleaseStage[] stages = new FleaseStage[numHosts]; final boolean useME = true; TimeSync.initializeLocal(50); final Communicator com = new Communicator(10, 100, 30000, 5, true, 0.2, 0.05); if (com.startAndWait() != Service.State.RUNNING) { LOG.error("Unable to start communicator", com.failureCause()); System.exit(2); } List<InetSocketAddress> allPorts = new ArrayList(numHosts); List<InetSocketAddress>[] acceptors = new List[numHosts]; MasterEpochHandlerInterface[] meHandlers = new MasterEpochHandlerInterface[numHosts]; final AtomicReference<Flease>[] leaseStates = new AtomicReference[numHosts]; for (int i = 0; i < numHosts; i++) { final int portNo = 1024 + i; final int myI = i; FleaseConfig cfg = new FleaseConfig(leaseTimeout, dmax, 500, new InetSocketAddress(portNo), "localhost:" + (1024 + i), 5, true, 0); leaseStates[i] = new AtomicReference<Flease>(Flease.EMPTY_LEASE); if (useME) { meHandlers[i] = new MasterEpochHandlerInterface() { long me = 0; public long getMasterEpoch() { return me; } @Override public void sendMasterEpoch(FleaseMessage response, Continuation callback) { response.setMasterEpochNumber(me); callback.processingFinished(); } @Override public void storeMasterEpoch(FleaseMessage request, Continuation callback) { me = request.getMasterEpochNumber(); callback.processingFinished(); } }; } stages[i] = new FleaseStage(cfg, "/tmp/xtreemfs-test", new FleaseMessageSenderInterface() { public void sendMessage(FleaseMessage message, InetSocketAddress recipient) { assert (message != null); LOG.debug("received message for delivery to port {}: {}", recipient.getPort(), message); message.setSender(new InetSocketAddress("localhost", portNo)); com.send(recipient.getPort(), message); } }, true, new FleaseViewChangeListenerInterface() { public void viewIdChangeEvent(ASCIIString cellId, int viewId) { } }, new FleaseStatusListener() { public void statusChanged(ASCIIString cellId, Flease lease) { synchronized (leaseStates) { leaseStates[myI].set(lease); } System.out.println("state change for " + portNo + ": " + lease.getLeaseHolder() + "/" + lease.getLeaseTimeout_ms()); } public void leaseFailed(ASCIIString cellId, FleaseException error) { System.out.println("lease failed: " + error); synchronized (leaseStates) { leaseStates[myI].set(Flease.EMPTY_LEASE); } //restart my cell } }, meHandlers[i]); stages[i].addListener(new Service.Listener() { @Override public void starting() { } @Override public void running() { } @Override public void stopping(Service.State from) { } @Override public void terminated(Service.State from) { } @Override public void failed(Service.State from, Throwable failure) { LOG.error("Failed service, from state: {} with exception {}", from, failure); System.exit(100); } }, MoreExecutors.sameThreadExecutor()); stages[i].start(); allPorts.add(new InetSocketAddress("localhost", 1024 + i)); com.openPort(1024 + i, stages[i]); } for (int i = 0; i < numHosts; i++) { final int portNo = 1024 + i; acceptors[i] = new ArrayList(numHosts - 1); for (InetSocketAddress ia : allPorts) { if (ia.getPort() != portNo) acceptors[i].add(ia); } stages[i].openCell(new ASCIIString("testcell"), acceptors[i], false); } //do something do { final AtomicBoolean sync = new AtomicBoolean(); final AtomicReference<ASCIIString> ref = new AtomicReference(); Thread.sleep(100); System.out.print("checking local states: "); ASCIIString leaseHolder = null; int leaseInstanceId = 0; synchronized (leaseStates) { for (int i = 0; i < numHosts; i++) { if (!leaseStates[i].get().isEmptyLease()) { if (leaseHolder == null) { leaseHolder = leaseStates[i].get().getLeaseHolder(); } else { if (!leaseHolder.equals(leaseStates[i].get().getLeaseHolder())) { com.stop(); System.out.println("INVARIANT VIOLATED!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); System.out.println("\n\n"); System.out.println("got lease for: " + leaseHolder); System.out.println( "but host " + i + " learned " + leaseStates[i].get().getLeaseHolder()); for (int j = 0; j < numHosts; j++) { System.out.println( stages[j]._dump_acceptor_state(new ASCIIString("testcell"))); System.out.println("signalled result: " + leaseStates[j].get()); System.out.println(" valid: " + leaseStates[j].get().isValid()); } System.exit(2); } else { System.out.print("+"); } } } else { System.out.print("o"); } } } System.out.println(""); final int host = (int) (Math.random() * numHosts); stages[host].closeCell(new ASCIIString("testcell"), false); //make sure that cells also time out some times (requires a wait > 2*lease_timeout) final int waitTime = (int) (Math.random() * (leaseTimeout * 2 + 1000)); System.out.println("waiting for " + waitTime + "ms"); Thread.sleep(waitTime); stages[host].openCell(new ASCIIString("testcell"), acceptors[host], false); } while (true); /* for (int i = 0; i < numHosts; i++) { stages[i].shutdown(); } com.shutdown(); */ } catch (Exception ex) { ex.printStackTrace(); System.exit(1); } }
From source file:co.runrightfast.core.utils.ServiceUtils.java
static void start(@NonNull final Service service) { switch (service.state()) { case NEW:// ww w.j a v a 2 s.c o m service.startAsync(); awaitRunning(service); return; case STARTING: service.awaitRunning(); awaitRunning(service); return; case RUNNING: return; default: throw new IllegalStateException( "Service cannot be started because the service state is :" + service.state()); } }
From source file:co.runrightfast.commons.utils.ServiceUtils.java
public static void start(@NonNull final Service service) { switch (service.state()) { case NEW://ww w . j a v a2s . c om service.startAsync(); awaitRunning(service); return; case STARTING: service.awaitRunning(); awaitRunning(service); return; case RUNNING: return; default: throw new IllegalServiceStateTransitionException(service.state(), RUNNING); } }
From source file:com.griddynamics.jagger.engine.e1.process.Services.java
public static void awaitTermination(Service service, long timeout) { long begin = System.currentTimeMillis(); while (true) { State state = service.state(); if (state == State.TERMINATED) { break; }//from w w w . ja v a 2s.c o m if (state == State.FAILED) { throw new IllegalStateException("Service '" + service + "' execution unexpectedly failed"); } TimeUtils.sleepMillis(500); long now = System.currentTimeMillis(); long diff = now - begin; if (diff > timeout) { throw new RuntimeException( String.format("Waiting for service %s is failed. Timeout %d", service, diff)); } } }
From source file:co.runrightfast.core.utils.ServiceUtils.java
static void stopAsync(final Service service) { if (service != null) { switch (service.state()) { case STARTING: case RUNNING: service.stopAsync();//from ww w.j a v a 2 s. co m return; case STOPPING: return; case NEW: case FAILED: case TERMINATED: LOG.logp(FINE, ServiceUtils.class.getName(), "stop", () -> String .format("Service (%s) is not running: %s", service.getClass().getName(), service.state())); } } }
From source file:co.runrightfast.commons.utils.ServiceUtils.java
public static void stopAsync(final Service service) { if (service != null) { switch (service.state()) { case STARTING: case RUNNING: service.stopAsync();// w w w.j av a 2s . c o m return; case STOPPING: return; case NEW: case FAILED: case TERMINATED: log.debug("Service ({}) is not running: {}", service.getClass().getName(), service.state()); } } }
From source file:co.runrightfast.core.utils.ServiceUtils.java
/** * * @param service if null, then do nothing */// ww w. ja v a 2 s . com static void stop(final Service service) { if (service != null) { switch (service.state()) { case STARTING: case RUNNING: service.stopAsync(); awaitTerminated(service); return; case STOPPING: awaitTerminated(service); return; case NEW: case FAILED: case TERMINATED: LOG.logp(FINE, ServiceUtils.class.getName(), "stop", () -> String .format("Service (%s) is not running: %s", service.getClass().getName(), service.state())); } } }
From source file:co.runrightfast.commons.utils.ServiceUtils.java
/** * * @param service if null, then do nothing *///from w ww. j ava 2 s .c o m public static void stop(final Service service) { if (service != null) { switch (service.state()) { case STARTING: case RUNNING: service.stopAsync(); awaitTerminated(service); return; case STOPPING: awaitTerminated(service); return; case NEW: case FAILED: case TERMINATED: log.debug("Service ({}) is not running: {}", service.getClass().getName(), service.state()); } } }
From source file:io.pravega.segmentstore.server.ServiceListeners.java
/** * Awaits for the given Service to shut down, whether normally or exceptionally. * * @param service The store to monitor. * @param throwIfFailed Throw the resulting exception if the store ended up in a FAILED state. *//*from ww w. ja v a 2 s . com*/ public static void awaitShutdown(Service service, boolean throwIfFailed) { try { service.awaitTerminated(); } catch (IllegalStateException ex) { if (throwIfFailed || service.state() != Service.State.FAILED) { throw ex; } } }
From source file:io.pravega.segmentstore.server.ServiceListeners.java
/** * Awaits for the given Service to shut down, whether normally or exceptionally. * * @param service The store to monitor. * @param timeout The maximum amount of time to wait for the shutdown. * @param throwIfFailed Throw the resulting exception if the store ended up in a FAILED state. * @throws TimeoutException If the store did not shut down within the specified amount of time. This exception is * thrown regardless of the value passed in as throwIfFailed. *//* www . j ava2 s . c o m*/ public static void awaitShutdown(Service service, Duration timeout, boolean throwIfFailed) throws TimeoutException { try { service.awaitTerminated(timeout.toMillis(), TimeUnit.MILLISECONDS); } catch (IllegalStateException ex) { if (throwIfFailed || service.state() != Service.State.FAILED) { throw ex; } } }