@Override public void stop() { super.stop(); if (this.enablePostgres) { PostgresHostUtils.closeDataSource(this.ds); } }
public void stopHostAndPreserveState(ServiceHost host) { log("Stopping host %s", host.getUri()); // Do not delete the temporary directory with the lucene index. Notice that // we do not call host.tearDown(), which will delete disk state, we simply // stop the host and remove it from the peer node tracking tables host.stop(); this.peerHostIdToNodeState.remove(host.getId()); this.peerNodeGroups.remove(host.getUri()); this.localPeerHosts.remove(host.getUri()); }
public void stopHostAndPreserveState(ServiceHost host) { log("Stopping host %s", host.getUri()); // Do not delete the temporary directory with the lucene index. Notice that // we do not call host.tearDown(), which will delete disk state, we simply // stop the host and remove it from the peer node tracking tables host.stop(); this.peerHostIdToNodeState.remove(host.getId()); this.peerNodeGroups.remove(host.getUri()); this.localPeerHosts.remove(host.getUri()); }
failure[0] = e; log(Level.SEVERE, "Exception creating service %s:", e.toString()); stop(); latch.countDown(); return;
private static void startFactoryServices(ServiceHost host, List<FactoryService> factoryServices) { for (Service factoryService : factoryServices) { host.startService( Operation.createPost(UriUtils.buildFactoryUri(host, factoryService.getClass())) .setCompletion((o, ex) -> { if (ex != null) { // shutdown the server when encountering an error host.log(Level.SEVERE, "Stopping the host because service %s failed to start: %s", o.getUri(), Utils.toString(ex)); host.stop(); } }), factoryService); } } }
public static void startService(ServiceHost host, Class<? extends Service> serviceClass, Service serviceInstance) { long startTime = System.currentTimeMillis(); host.startService( Operation.createPost(UriUtils.buildUri(host, serviceClass)) .setCompletion((o, ex) -> { long endTime = System.currentTimeMillis(); // temporary log to trace down service startup time host.log(Level.INFO, "Start service %s took %d millis to complete", o.getUri(), endTime - startTime); if (ex != null) { // shutdown the server when encountering an error host.log(Level.SEVERE, "Stopping the host because service %s failed to start: %s", o.getUri(), Utils.toString(ex)); host.stop(); return; } }), serviceInstance); }
public static boolean restartStatefulHost(ServiceHost host, boolean failOnIndexDeletion) throws Throwable { long exp = Utils.fromNowMicrosUtc(host.getOperationTimeoutMicros()); do { Thread.sleep(2000); try { if (host.isAuthorizationEnabled()) { host.setAuthenticationService(new AuthorizationContextService()); } host.start(); return true; } catch (Throwable e) { Logger.getAnonymousLogger().warning(String .format("exception on host restart: %s", e.getMessage())); try { host.stop(); } catch (Throwable e1) { return false; } if (e instanceof LockObtainFailedException && !failOnIndexDeletion) { Logger.getAnonymousLogger() .warning("Lock held exception on host restart, retrying"); continue; } return false; } } while (Utils.getSystemNowMicrosUtc() < exp); return false; }
public static boolean restartStatefulHost(ServiceHost host, boolean failOnIndexDeletion) throws Throwable { long exp = Utils.fromNowMicrosUtc(host.getOperationTimeoutMicros()); do { Thread.sleep(2000); try { if (host.isAuthorizationEnabled()) { host.setAuthenticationService(new AuthorizationContextService()); } host.start(); return true; } catch (Throwable e) { Logger.getAnonymousLogger().warning(String .format("exception on host restart: %s", e.getMessage())); try { host.stop(); } catch (Throwable e1) { return false; } if (e instanceof LockObtainFailedException && !failOnIndexDeletion) { Logger.getAnonymousLogger() .warning("Lock held exception on host restart, retrying"); continue; } return false; } } while (Utils.getSystemNowMicrosUtc() < exp); return false; }
/** * Shutdown the host. * * When host is the process owner, {@code System.exit(0);} is called at the end. */ @RouteDocumentation(description = "Shuts down this host. If the host is the process owner then the process" + " is terminated with exit code 0.") @Override public void handleDelete(Operation delete) { logInfo("Received shutdown request from %s", delete.getReferer()); boolean isProcessOwner = getHost().isProcessOwner(); // DELETE to this service causes a graceful host shutdown. // Because shut down can take several seconds on an active system // we complete the operation right away, and the remote client relies // on polling to determine when we really went down. That is the safest // option anyway, for clients that really do care delete.setStatusCode(Operation.STATUS_CODE_ACCEPTED); delete.complete(); getHost().stop(); if (isProcessOwner) { System.exit(0); } else { logInfo("This host is NOT the process owner. Skipping System.exit()"); } }
@Test public void create() throws Throwable { ServiceHost h = ServiceHost.create("--port=0"); try { h.start(); h.startDefaultCoreServicesSynchronously(); // Start the example service factory h.startFactory(ExampleService.class, ExampleService::createFactory); boolean[] isReady = new boolean[1]; h.registerForServiceAvailability((o, e) -> { isReady[0] = true; }, ExampleService.FACTORY_LINK); Duration timeout = Duration.of(ServiceHost.ServiceHostState.DEFAULT_MAINTENANCE_INTERVAL_MICROS * 5, ChronoUnit.MICROS); TestContext.waitFor(timeout, () -> { return isReady[0]; }, "ExampleService did not start"); // verify ExampleService exists TestRequestSender sender = new TestRequestSender(h); Operation get = Operation.createGet(h, ExampleService.FACTORY_LINK); sender.sendAndWait(get); } finally { if (h != null) { h.unregisterRuntimeShutdownHook(); h.stop(); } } }
@Test public void create() throws Throwable { ServiceHost h = ServiceHost.create("--port=0"); try { h.start(); h.startDefaultCoreServicesSynchronously(); // Start the example service factory h.startFactory(ExampleService.class, ExampleService::createFactory); boolean[] isReady = new boolean[1]; h.registerForServiceAvailability((o, e) -> { isReady[0] = true; }, ExampleService.FACTORY_LINK); Duration timeout = Duration.of(ServiceHost.ServiceHostState.DEFAULT_MAINTENANCE_INTERVAL_MICROS * 5, ChronoUnit.MICROS); TestContext.waitFor(timeout, () -> { return isReady[0]; }, "ExampleService did not start"); // verify ExampleService exists TestRequestSender sender = new TestRequestSender(h); Operation get = Operation.createGet(h, ExampleService.FACTORY_LINK); sender.sendAndWait(get); } finally { if (h != null) { h.unregisterRuntimeShutdownHook(); h.stop(); } } }