public void registerForServiceAvailability(CompletionHandler completion, boolean checkReplica, String... servicePaths) { registerForServiceAvailability(completion, ServiceUriPaths.DEFAULT_NODE_SELECTOR, checkReplica, servicePaths); }
public void registerForServiceAvailability(Operation opTemplate, String... servicePaths) { registerForServiceAvailability(opTemplate, false, ServiceUriPaths.DEFAULT_NODE_SELECTOR, servicePaths); }
/** * Registers a completion that is invoked every time one of the supplied services reaches the * available stage. If service start fails for any one, the completion will be called with a * failure argument. * * When {@code checkReplica} flag is on(see other overloading methods), this method checks not * only the local node, but also checks the service availability in node group for factory links * that produce replicated services. * * Note that supplying multiple self links will result in multiple completion invocations. The * handler provided must track how many times it has been called * * @see #checkReplicatedServiceAvailable(CompletionHandler, String) * @see NodeGroupUtils#registerForReplicatedServiceAvailability(ServiceHost, Operation, String, String) * @see NodeGroupUtils#checkServiceAvailability(CompletionHandler, Service) */ public void registerForServiceAvailability(CompletionHandler completion, String... servicePaths) { registerForServiceAvailability(completion, ServiceUriPaths.DEFAULT_NODE_SELECTOR, false, servicePaths); }
public static void waitForServiceAvailability(@NotNull ServiceHost host, String... paths) throws InterruptedException { CountDownLatch latch = new CountDownLatch(1); host.registerForServiceAvailability(((completedOp, failure) -> latch.countDown()), paths); latch.await(5, TimeUnit.SECONDS); }
private static void waitForUser(ServiceHost host, User user, Runnable successfulCallback, Consumer<Throwable> failureCallback) { final AtomicInteger servicesCounter = new AtomicInteger(1); final AtomicBoolean hasError = new AtomicBoolean(false); host.registerForServiceAvailability((o, e) -> { if (e != null) { hasError.set(true); failureCallback.accept(e); } else { if (servicesCounter.decrementAndGet() == 0 && !hasError.get()) { successfulCallback.run(); } } }, true, buildUriPath(LocalPrincipalFactoryService.SELF_LINK, encode(user.email))); }
private void startMonitoringState(T body) { if (body.kubernetesSelfLink != null) { getHost().registerForServiceAvailability((o, ex) -> { if (ex != null) { logWarning("Skipping maintenance because service failed to start: %s", ex.getMessage()); } else { handlePeriodicMaintenance(o); } }, getSelfLink()); } }
@Override public void registerForServiceAvailability(Operation.CompletionHandler completion, String nodeSelectorPath, boolean checkReplica, String... servicePaths) { if (this.enablePostgres && checkReplica) { PostgresHostUtils.registerForReplicatedServiceAvailability(this, completion, isStarted() ? getSystemAuthorizationContext() : null, nodeSelectorPath, servicePaths); return; } super.registerForServiceAvailability(completion, nodeSelectorPath, checkReplica, servicePaths); }
public static void main(String[] args) throws Throwable { ServiceHost host = ServiceHost.create("--sandbox=/tmp/xenon-query/" + UUID.randomUUID().toString()); host.start(); host.startDefaultCoreServicesSynchronously(); host.startFactory(ExampleService.class, ExampleService::createFactory); host.registerForServiceAvailability((o, e) -> handleFactoryAvailability(host, e), ExampleService.FACTORY_LINK); }
@Override public void handleStart(Operation post) { // we depend on another service, start, when it starts getHost().registerForServiceAvailability((o, e) -> { if (e != null) { post.fail(e); } else { post.complete(); } }, RegistryConfigCertificateDistributionService.SELF_LINK); }
static ServiceDocument buildDefaultStateInstance(ServiceHost host) { RegistryState state = new RegistryState(); state.documentSelfLink = DEFAULT_INSTANCE_LINK; state.address = DEFAULT_REGISTRY_ADDRESS; state.name = DEFAULT_REGISTRY_NAME; state.endpointType = RegistryState.DOCKER_REGISTRY_ENDPOINT_TYPE; state.disabled = Boolean.FALSE; state.customProperties = new HashMap<>(); state.customProperties.put(API_VERSION_PROP_NAME, ApiVersion.V1.toString()); boolean disableDefaultRegistry = Boolean.valueOf( ConfigurationUtil.getProperty(RegistryService.DISABLE_DEFAULT_REGISTRY_PROP_NAME)); // create or delete default registry if (disableDefaultRegistry) { // ensure default registry does not exist host.registerForServiceAvailability((o, e) -> { host.log(Level.INFO, "registerForServiceAvailability: %s", RegistryFactoryService.SELF_LINK); deleteDefaultRegistry(host); }, true, RegistryFactoryService.SELF_LINK); return null; } return state; }
public void registerForServiceAvailability(CompletionHandler completion, String nodeSelectorPath, boolean checkReplica, String... servicePaths) { if (servicePaths == null || servicePaths.length == 0) { throw new IllegalArgumentException("selfLinks are required"); } Operation op = Operation.createPost(null) .setCompletion(completion) .setExpiration(Utils.fromNowMicrosUtc(getOperationTimeoutMicros())); registerForServiceAvailability(op, checkReplica, nodeSelectorPath, servicePaths); }
@Override public void handleStart(Operation post) { super.handleStart(post); // perform maintenance on startup to refresh the container attributes getHost().registerForServiceAvailability((o, ex) -> { if (ex != null) { logWarning("Skipping maintenance because service failed to start: %s", ex.getMessage()); } else { handlePeriodicMaintenance(Operation.createGet(null)); } }, getSelfLink()); }
private void initInstance(ServiceDocument state, boolean checkIfExists, Consumer<Throwable> callback) { String factoryPath = UriUtils.getParentPath(state.documentSelfLink); try { getHost().registerForServiceAvailability((o, e) -> { if (e != null) { logSevere("Error waiting for service: %s. Error: %s", factoryPath, Utils.toString(e)); callback.accept(e); return; } if (checkIfExists) { ensureInstanceExists(state, callback); } else { createDefaultInstance(state, callback, RETRIES_COUNT); } }, true, factoryPath); } catch (Throwable t) { logSevere("Error registering for service availability: %s. Error: %s", state.documentSelfLink, (t instanceof CancellationException) ? t.getClass().getName() : Utils.toString(t)); callback.accept(t); return; } }
protected void waitForServiceAvailability(ServiceHost h, String... serviceLinks) throws Throwable { if (serviceLinks == null || serviceLinks.length == 0) { throw new IllegalArgumentException("null or empty serviceLinks"); } TestContext ctx = testCreate(serviceLinks.length); h.registerForServiceAvailability(ctx.getCompletion(), serviceLinks); ctx.await(); }
public static void main(String[] args) throws Throwable { ServiceHost host = ServiceHost.create(); host.start(); host.startDefaultCoreServicesSynchronously(); // Start the root namespace factory: this will respond to the root URI (/) and list all // the factory services. host.startService(new RootNamespaceService()); host.startFactory(MySubscriptionService.class, MySubscriptionService::createFactory); // subscription callback Consumer<Operation> target = (notifyOp) -> { System.out.println("notification: " + notifyOp); notifyOp.complete(); }; Operation createSub = Operation.createPost( UriUtils.buildUri(host, MySubscriptionService.FACTORY_LINK)) .setReferer(host.getUri()); // start subscription when factory became available // For reliable subscription, use "startReliableSubscriptionService()" instead of "startSubscriptionService()" host.registerForServiceAvailability((o, e) -> { host.startSubscriptionService(createSub, target); }, MySubscriptionService.FACTORY_LINK); }
public static void main(String[] args) throws Throwable { // create adhoc ServiceHost. // In real application, you should create own ServiceHost implementation. ServiceHost host = ServiceHost.create(); host.start(); host.startDefaultCoreServicesSynchronously(); host.startService(new RootNamespaceService()); host.startFactory(new BookService()); host.startService(new Top10BookRankingService()); // Subscription // Let's create a subscription that will be called when BookService is created. // Create a consumer of subscription callbacks // >> IMPLEMENT HERE << // start subscription when factory became available // For reliable subscription, use "startReliableSubscriptionService()" instead of "startSubscriptionService()" host.registerForServiceAvailability((o, e) -> { // Register subscription to the factory of BookService // >> IMPLEMENT HERE << }, BookService.FACTORY_LINK); }
public static void clearCacheOnNodeGroupChange(ServiceHost host) { host.registerForServiceAvailability((op, ex) -> { if (ex != null) { host.log(Level.SEVERE, "Failed register for default node group availability: %s", Utils.toString(ex)); return; } // subscribe to node group and reset persisted serviced state cache on every change Operation subscribeOp = Operation.createPost(host, ServiceUriPaths.DEFAULT_NODE_GROUP) .setReferer(host.getUri()) .setCompletion((o, e) -> { if (e != null) { host.log(Level.SEVERE, "Failed subscription to default node group: %s", Utils.toString(e)); } }); host.startSubscriptionService(subscribeOp, notifyOp -> { // clear cache to prevent serving stale data for owner selected services due to owner change host.log(Level.INFO, "Clear persisted service state cache on node group change"); host.clearPersistedServiceStateCache(); notifyOp.complete(); }); }, ServiceUriPaths.DEFAULT_NODE_GROUP); }
private SubscriptionManager<ExampleServiceState> subscribe(ServiceHost h, ExampleServiceState serviceState, Consumer<SubscriptionNotification<ExampleServiceState>> notificationConsumer) { SubscriptionManager<ExampleServiceState> subscriptionManager = new SubscriptionManager<>( h, h.getId(), serviceState.documentSelfLink, ExampleServiceState.class); TestContext ctx = new TestContext(1, Duration.ofSeconds(SUBSCRIPTION_START_TIMEOUT_SECONDS)); String subscriptionServiceLink; // This single element array is used to extract the result from lambda. final String[] subscriptionServiceLinkResult = new String[1]; subscriptionManager.start(notificationConsumer, (subscriptionLink) -> { subscriptionServiceLinkResult[0] = subscriptionLink; ctx.completeIteration(); }); ctx.await(); subscriptionServiceLink = subscriptionServiceLinkResult[0]; cluster.log(Level.INFO, "Waiting for subscription for %s on host %s", serviceState.documentSelfLink, cluster.getId()); TestContext testCtx = new TestContext(1, Duration.ofSeconds(SUBSCRIPTION_START_TIMEOUT_SECONDS)); h.registerForServiceAvailability((o, e) -> { if (e != null) { testCtx.fail(e); } else { testCtx.complete(); } }, subscriptionServiceLink); testCtx.await(); return subscriptionManager; }
@Override public void handleStart(Operation post) { // do not complete post for start, until we see a instance of the parent // being available. If there is an issue with factory start, this will // deadlock ExampleServiceState st = getBody(post); String id = Service.getId(st.documentSelfLink); String parentPath = UriUtils.buildUriPath(ParentService.FACTORY_LINK, id); post.nestCompletion((o, e) -> { if (e != null) { post.fail(e); return; } logInfo("Parent service started!"); post.complete(); }); getHost().registerForServiceAvailability(post, parentPath); } }
@Override public void handleStart(Operation post) { // do not complete post for start, until we see a instance of the parent // being available. If there is an issue with factory start, this will // deadlock ExampleServiceState st = getBody(post); String id = Service.getId(st.documentSelfLink); String parentPath = UriUtils.buildUriPath(ParentService.FACTORY_LINK, id); post.nestCompletion((o, e) -> { if (e != null) { post.fail(e); return; } logInfo("Parent service started!"); post.complete(); }); getHost().registerForServiceAvailability(post, parentPath); } }