/** * Subscribe for the specified service link and provide a notification handler to be callback * when there is an update or delete of the specified services. * The Consumer<String> callback will contain either the subscription ID or null in case * something went wrong. */ public void start(Consumer<SubscriptionNotification<T>> notificationHandler, Consumer<String> callback) { start(notificationHandler, false, callback); }
@Override public void close() { this.subscriptionManager.close(); }
new SubscriptionManager<>( getHost(), UUID.randomUUID().toString(), containerHostRemovalTaskState, ContainerHostRemovalTaskState.class); subscriptionManager.start(notification -> { ContainerHostRemovalTaskState eats = notification.getResult(); EnumSet<TaskStage> terminalStages = of(TaskStage.FINISHED, TaskStage.FAILED, TaskStage.CANCELLED); if (terminalStages.contains(eats.taskInfo.stage)) { subscriptionManager.close(); ClusterUtils.deletePlacementZoneAndPlacement(pZILink, resourcePoolLink, delete, getHost());
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; }
.getDeclaredField("SUBSCRIPTION_POLLING_STRATEGY"), true); subscriptionManager.close(); subscriptionManager = new SubscriptionManager<>( host, host.getId(), state.documentSelfLink, ContainerState.class, true); assertTrue(notification.isDelete()); assertFalse(subscriptionManager.isSubscribed());
public DockerDriverBase(ServiceHost serviceHost, DriverRegistry driverRegistry, ClosureDockerClientFactory dockerClientFactory) { this.serviceHost = serviceHost; this.driverRegistry = driverRegistry; this.dockerClientFactory = dockerClientFactory; this.trustCertificates = new AtomicReference<>(); this.subscriptionManager = new SubscriptionManager<>(serviceHost, serviceHost.getId() + getDockerImage(), SSL_TRUST_CONFIG_SUBSCRIBE_FOR_LINK, ConfigurationService.ConfigurationState.class, true); this.sslTrustQuery = new ServiceDocumentQuery<SslTrustCertificateState>(serviceHost, SslTrustCertificateState.class); queryHandler = new ClosureSslTrustQueryCompletionHandler(); subscribeForSslTrustCertNotifications(); loadSslTrustCertServices(); }
private String subscribe() throws Throwable { TestContext ctx = testCreate(2); subscriptionManager.setCompletionHandler((e) -> { if (e != null) { ctx.failIteration(e); return; } ctx.completeIteration(); }); final String[] subscriptionId = new String[1]; subscriptionManager.start(handler(), (id) -> { subscriptionId[0] = id; ctx.completeIteration(); }); testWait(ctx); // reset completion handler subscriptionManager.setCompletionHandler(null); return subscriptionId[0]; }
private void unsubscribe() { stopPolling = true; if (!isSubscribed()) { Utils.logWarning("No subscription link to unsubscribe for service: %s", this.subscribeForServiceLink); if (completionHandler != null) { completionHandler.accept(null); } return; } Operation unSubscribe = Operation.createDelete(buildSubscribeForUri()) .setReferer(host.getUri()) .setCompletion((o, e) -> { if (e != null) { Utils.logWarning("Error unsubscribing from: %s. Error: %s", o.getUri(), Utils.toString(e)); } if (completionHandler != null) { // the completion handler should be called in both cases: error or success: completionHandler.accept(e); } }); host.stopSubscriptionService(unSubscribe, UriUtils.buildUri(host, subscriptionLink)); }
if (!subscribeForNotifications) { documentUpdateTimeMicros = Utils.getNowMicrosUtc(); schedulePolling(notificationHandler); if (completionHandler != null) { completionHandler.accept(null); ServiceSubscriber sr = ServiceSubscriber.create(replayState).setUsePublicUri(usePublicUri); StatelessService notificationTarget = ReliableSubscriptionService.create(subscribe, sr, (op) -> handleNotification(op, notificationHandler));
@Test public void testNotificationSubscriptionUpdates() throws Throwable { subscribe(); state.documentSelfLink = state.documentSelfLink; doOperation(Action.PUT, state); notification = getNotification(); assertNotNull(notification); assertNotNull(notification.getResult()); assertTrue(notification.isUpdate()); updatedState = notification.getResult(); assertEquals(updatedTestId, updatedState.id); assertEquals(updatedTestValue, updatedState.image); state.id = updatedTestValue + updatedTestId; doOperation(Action.PATCH, state); notification = getNotification(); assertNotNull(notification.getResult()); assertTrue(notification.isUpdate()); updatedState = notification.getResult(); assertEquals(updatedTestValue + updatedTestId, updatedState.id); assertEquals(updatedTestValue, updatedState.image); doOperation(Action.DELETE, new ServiceDocument()); notification = getNotification(); assertNotNull(notification.getResult()); assertTrue(notification.isDelete()); assertFalse(subscriptionManager.isSubscribed()); }
new SubscriptionManager<>( getHost(), UUID.randomUUID().toString(), containerHostRemovalTaskState, ContainerHostRemovalTaskState.class); sm.start(notification -> { ContainerHostRemovalTaskState eats = notification.getResult(); EnumSet<TaskStage> terminalStages = of(TaskStage.FINISHED, TaskStage.FAILED, TaskStage.CANCELLED); if (terminalStages.contains(eats.taskInfo.stage)) { sm.close(); delete.complete();
this.subscriptionManager = new SubscriptionManager<>(host, host.getId(), SSL_TRUST_CONFIG_SUBSCRIBE_FOR_LINK, ConfigurationState.class, true);
private void subscribeForSslTrustCertNotifications() { this.subscriptionManager.start((n) -> { loadSslTrustCertServices(); }, null); }
@Before public void setUp() throws Throwable { waitForServiceAvailability(ContainerDescriptionService.FACTORY_LINK); waitForServiceAvailability(CompositeDescriptionFactoryService.SELF_LINK); waitForServiceAvailability(ContainerFactoryService.SELF_LINK); state = new ContainerState(); state.id = updatedTestId; state.image = updatedTestValue; state = doPost(state, ContainerFactoryService.SELF_LINK); results = Collections.synchronizedList(new ArrayList<>()); subscriptionManager = new SubscriptionManager<>( host, host.getId(), state.documentSelfLink, ContainerState.class); waitForServiceAvailability(ServiceUriPaths.CORE_QUERY_TASKS); waitForServiceAvailability(state.documentSelfLink); }
private void subscribeForSslTrustCertNotifications() { this.subscriptionManager.start((n) -> { loadSslTrustCertServices(); }, null); }