io.etcd.jetcd.options.WatchOption Java Examples

The following examples show how to use io.etcd.jetcd.options.WatchOption. 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: WatchTest.java    From jetcd with Apache License 2.0 6 votes vote down vote up
@ParameterizedTest
@MethodSource("parameters")
public void testWatchCompacted(final Client client) throws Exception {
    final ByteSequence key = randomByteSequence();
    final ByteSequence value = randomByteSequence();

    // Insert key twice to ensure we have at least two revisions
    client.getKVClient().put(key, value).get();
    final PutResponse putResponse = client.getKVClient().put(key, value).get();
    // Compact until latest revision
    client.getKVClient().compact(putResponse.getHeader().getRevision()).get();

    final AtomicReference<Throwable> ref = new AtomicReference<>();
    // Try to listen from previous revision on
    final WatchOption options = WatchOption.newBuilder().withRevision(putResponse.getHeader().getRevision() - 1).build();
    final Watch wc = client.getWatchClient();

    try (Watcher watcher = wc.watch(key, options, Watch.listener(TestUtil::noOpWatchResponseConsumer, ref::set))) {
        await().atMost(15, TimeUnit.SECONDS).untilAsserted(() -> assertThat(ref.get()).isNotNull());
        assertThat(ref.get().getClass()).isEqualTo(CompactedException.class);
    }
}
 
Example #2
Source File: WatchUnitTest.java    From jetcd with Apache License 2.0 6 votes vote down vote up
@Test
public void testWatchOnUnrecoverableConnectionIssue() throws InterruptedException {
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<Throwable> ref = new AtomicReference<>();
    Watch.Listener listener = Watch.listener(r -> {
    }, t -> {
        ref.set(t);
        latch.countDown();
    });

    try (Watch.Watcher watcher = watchClient.watch(KEY, WatchOption.DEFAULT, listener)) {
        WatchResponse createdResponse = createWatchResponse(0);

        responseObserverRef.get().onNext(createdResponse);
        responseObserverRef.get().onError(Status.ABORTED.withDescription("connection error").asRuntimeException());

        latch.await(4, TimeUnit.SECONDS);

        assertThat(ref.get()).isNotNull();
        assertThat(ref.get()).isInstanceOf(EtcdException.class).hasMessageContaining("connection error");
    }
}
 
Example #3
Source File: WatchUnitTest.java    From jetcd with Apache License 2.0 6 votes vote down vote up
@Test
public void testWatcherListenOnResponse() throws InterruptedException {
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<io.etcd.jetcd.watch.WatchResponse> ref = new AtomicReference<>();
    Watch.Listener listener = Watch.listener(response -> {
        ref.set(response);
        latch.countDown();
    });

    try (Watch.Watcher watcher = watchClient.watch(KEY, WatchOption.DEFAULT, listener)) {
        WatchResponse createdResponse = createWatchResponse(0);
        responseObserverRef.get().onNext(createdResponse);

        io.etcd.jetcd.api.WatchResponse putResponse = io.etcd.jetcd.api.WatchResponse.newBuilder().setWatchId(0)
            .addEvents(Event.newBuilder().setType(EventType.PUT).build()).build();
        responseObserverRef.get().onNext(putResponse);

        latch.await(4, TimeUnit.SECONDS);

        assertThat(ref.get()).isNotNull();
        assertThat(ref.get().getEvents().size()).isEqualTo(1);
        assertThat(ref.get().getEvents().get(0).getEventType()).isEqualTo(WatchEvent.EventType.PUT);
    }
}
 
Example #4
Source File: WatchImpl.java    From jetcd with Apache License 2.0 6 votes vote down vote up
@Override
public Watcher watch(ByteSequence key, WatchOption option, Listener listener) {
    if (closed.get()) {
        throw newClosedWatchClientException();
    }

    WatcherImpl impl;

    synchronized (this.lock) {
        impl = new WatcherImpl(key, option, listener);
        impl.resume();

        watchers.add(impl);
    }

    return impl;
}
 
Example #5
Source File: EtcdRegistry.java    From joyrpc with Apache License 2.0 5 votes vote down vote up
@Override
protected CompletableFuture<Void> doSubscribe(final ConfigBooking booking) {
    CompletableFuture<Void> future = new CompletableFuture<>();
    EtcdConfigBooking etcdBooking = (EtcdConfigBooking) booking;
    //先查询
    ByteSequence key = ByteSequence.from(etcdBooking.getPath(), UTF_8);
    //先查询,无异常后添加watcher,若结果不为空,通知FULL事件
    client.getKVClient().get(key).whenComplete((res, err) -> {
        if (!isOpen()) {
            future.completeExceptionally(new IllegalStateException("controller is closed."));
        } else if (err != null) {
            logger.error(String.format("Error occurs while subscribe of %s, caused by %s. retry....", etcdBooking.getInterface(), err.getMessage()), err);
            future.completeExceptionally(err);
        } else {
            List<WatchEvent> events = new ArrayList<>();
            res.getKvs().forEach(kv -> events.add(new WatchEvent(kv, null, PUT)));
            etcdBooking.onUpdate(events, res.getHeader().getRevision());
            //添加watch
            try {
                WatchOption watchOption = WatchOption.newBuilder().withPrefix(key).build();
                Watch.Watcher watcher = client.getWatchClient().watch(key, watchOption, etcdBooking);
                etcdBooking.setWatcher(watcher);
                future.complete(null);
            } catch (Exception e) {
                logger.error(String.format("Error occurs while subscribe of %s, caused by %s. retry....", etcdBooking.getInterface(), e.getMessage()), e);
                future.completeExceptionally(e);
            }
        }
    });
    return future;
}
 
Example #6
Source File: CommandWatch.java    From jetcd with Apache License 2.0 5 votes vote down vote up
@Override
public void accept(Client client) throws Exception {
    CountDownLatch latch = new CountDownLatch(maxEvents);
    Watcher watcher = null;

    try {
        ByteSequence watchKey = ByteSequence.from(key, Charsets.UTF_8);
        WatchOption watchOpts = WatchOption.newBuilder().withRevision(rev).build();

        watcher = client.getWatchClient().watch(watchKey, watchOpts, response -> {
            for (WatchEvent event : response.getEvents()) {
                LOGGER.info("type={}, key={}, value={}", event.getEventType().toString(),
                    Optional.ofNullable(event.getKeyValue().getKey()).map(bs -> bs.toString(UTF_8)).orElse(""),
                    Optional.ofNullable(event.getKeyValue().getValue()).map(bs -> bs.toString(UTF_8)).orElse(""));
            }

            latch.countDown();
        });

        latch.await();
    } catch (Exception e) {
        if (watcher != null) {
            watcher.close();
        }

        throw e;
    }
}
 
Example #7
Source File: WatchUnitTest.java    From jetcd with Apache License 2.0 5 votes vote down vote up
@Test
public void testWatchOnSendingWatchCreateRequest() {
    try (Watch.Watcher watcher = watchClient.watch(
        KEY,
        WatchOption.DEFAULT,
        Watch.listener(TestUtil::noOpWatchResponseConsumer))) {
        // expects a WatchCreateRequest is created.
        verify(this.requestStreamObserverMock, timeout(100).times(1)).onNext(argThat(hasCreateKey(KEY)));
    }
}
 
Example #8
Source File: WatchImpl.java    From jetcd with Apache License 2.0 5 votes vote down vote up
WatcherImpl(ByteSequence key, WatchOption option, Listener listener) {
    this.key = key;
    this.option = option;
    this.listener = listener;
    this.closed = new AtomicBoolean();

    this.stream = null;
    this.id = -1;
    this.revision = this.option.getRevision();
}
 
Example #9
Source File: EtcdRegistry.java    From joyrpc with Apache License 2.0 5 votes vote down vote up
@Override
protected CompletableFuture<Void> doSubscribe(final ClusterBooking booking) {
    CompletableFuture<Void> future = new CompletableFuture<>();
    EtcdClusterBooking etcdBooking = (EtcdClusterBooking) booking;
    //先查询
    ByteSequence key = ByteSequence.from(etcdBooking.getPath(), UTF_8);
    //先查询,无异常后添加watcher,若结果不为空,通知FULL事件
    GetOption getOption = GetOption.newBuilder().withPrefix(key).build();
    client.getKVClient().get(key, getOption).whenComplete((res, err) -> {
        if (!isOpen()) {
            future.completeExceptionally(new IllegalStateException("controller is closed."));
        } else if (err != null) {
            logger.error(String.format("Error occurs while subscribe of %s, caused by %s. retry....", etcdBooking.getService(), err.getMessage()), err);
            future.completeExceptionally(err);
        } else {
            List<WatchEvent> events = new ArrayList<>();
            res.getKvs().forEach(kv -> events.add(new WatchEvent(kv, null, PUT)));
            etcdBooking.onUpdate(events, res.getHeader().getRevision(), FULL);
            //添加watch
            try {
                WatchOption watchOption = WatchOption.newBuilder().withPrefix(key).build();
                Watch.Watcher watcher = client.getWatchClient().watch(key, watchOption, etcdBooking);
                etcdBooking.setWatcher(watcher);
                future.complete(null);
            } catch (Exception e) {
                logger.error(String.format("Error occurs while subscribe of %s, caused by %s. retry....", etcdBooking.getService(), e.getMessage()), e);
                future.completeExceptionally(e);
            }
        }
    });
    return future;
}
 
Example #10
Source File: BinaryLogConfigContainer.java    From kkbinlog with Apache License 2.0 4 votes vote down vote up
public void registerConfigCommandWatcher() {

        Watch watchClient = etcdClient.getWatchClient();
        watchClient.watch(
                ByteSequence.from(etcdKeyPrefixUtil.withPrefix(Constants.DEFAULT_BINLOG_CONFIG_COMMAND_KEY), StandardCharsets.UTF_8),
                WatchOption.newBuilder().withPrevKV(true).withNoDelete(true).build(),
                new Watch.Listener() {

                    @Override
                    public void onNext(WatchResponse response) {

                        List<WatchEvent> eventList = response.getEvents();
                        for(WatchEvent event: eventList) {

                            if (WatchEvent.EventType.PUT.equals(event.getEventType())) {
                                BinLogCommand command = JSON.parseObject(event.getKeyValue().getValue().toString(StandardCharsets.UTF_8), BinLogCommand.class);

                                // 根据不同的命令类型(START/STOP)执行不同的逻辑
                                if(BinLogCommandType.START_DATASOURCE.equals(command.getType())) {
                                    handleStartDatasource(command.getNamespace(), command.getDelegatedIp());
                                } else if (BinLogCommandType.STOP_DATASOURCE.equals(command.getType())) {
                                    handleStopDatasource(command.getNamespace());
                                }
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable throwable) {
                        logger.error("Watch binlog config command error.", throwable);
                        new Thread(() -> registerConfigCommandWatcher()).start();
                    }

                    @Override
                    public void onCompleted() {
                        logger.info("Watch binlog config command completed.");
                        new Thread(() -> registerConfigCommandWatcher()).start();
                    }
                }
        );
    }
 
Example #11
Source File: OpLogClientFactory.java    From kkbinlog with Apache License 2.0 4 votes vote down vote up
/**
 * 注册Client列表更新监听
 *
 * @param binaryLogConfig
 * @param opLogEventHandlerFactory
 */
private void registerMetaDataWatcher(BinaryLogConfig binaryLogConfig, OpLogEventHandlerFactory opLogEventHandlerFactory) {

    String namespace = binaryLogConfig.getNamespace();
    String binLogClientSet = binaryLogConfig.getBinLogClientSet();

    Watch watchClient = etcdClient.getWatchClient();
    watchClient.watch(
            ByteSequence.from(etcdKeyPrefixUtil.withPrefix(namespace).concat(Constants.PATH_SEPARATOR).concat(binLogClientSet), StandardCharsets.UTF_8),
            WatchOption.newBuilder().withNoDelete(true).build(),
            new Watch.Listener() {
                @Override
                public void onNext(WatchResponse response) {
                    List<WatchEvent> eventList = response.getEvents();
                    for (WatchEvent event : eventList) {
                        if (WatchEvent.EventType.PUT.equals(event.getEventType())) {

                            KeyValue currentKV = event.getKeyValue();
                            Set<ClientInfo> currentClientInfoSet = getClientInfos(currentKV);

                            currentClientInfoSet
                                    .stream()
                                    .collect(Collectors.groupingBy(ClientInfo::getDatabaseEvent))
                                    .forEach(opLogEventHandlerFactory::updateClientBatch);
                        }
                    }
                }

                @Override
                public void onError(Throwable throwable) {
                    log.error("Watch clientInfo list change error.", throwable);
                }

                @Override
                public void onCompleted() {
                    log.info("Watch clientInfo list change completed.");
                }
            }
    );

}
 
Example #12
Source File: BinLogClientFactory.java    From kkbinlog with Apache License 2.0 4 votes vote down vote up
/**
 * 注册Client列表更新监听
 * @param binaryLogConfig
 * @param binLogEventHandlerFactory
 */
private void registerMetaDataWatcher(BinaryLogConfig binaryLogConfig, BinLogEventHandlerFactory binLogEventHandlerFactory) {

    String namespace = binaryLogConfig.getNamespace();
    String binLogClientSet = binaryLogConfig.getBinLogClientSet();

    Watch watchClient = etcdClient.getWatchClient();
    watchClient.watch(
            ByteSequence.from(etcdKeyPrefixUtil.withPrefix(namespace).concat(Constants.PATH_SEPARATOR).concat(binLogClientSet), StandardCharsets.UTF_8),
            WatchOption.newBuilder().withNoDelete(true).build(),
            new Watch.Listener() {
                @Override
                public void onNext(WatchResponse response) {
                    List<WatchEvent> eventList = response.getEvents();
                    for(WatchEvent event: eventList) {
                        if(WatchEvent.EventType.PUT.equals(event.getEventType())) {

                            KeyValue currentKV = event.getKeyValue();
                            Set<ClientInfo> currentClientInfoSet = getClientInfos(currentKV);

                            currentClientInfoSet
                                    .stream()
                                    .collect(Collectors.groupingBy(ClientInfo::getDatabaseEvent))
                                    .forEach(binLogEventHandlerFactory::updateClientBatch);
                        }
                    }
                }

                @Override
                public void onError(Throwable throwable) {
                    log.error("Watch clientInfo list change error.", throwable);
                }

                @Override
                public void onCompleted() {
                    log.info("Watch clientInfo list change completed.");
                }
            }
    );

}
 
Example #13
Source File: Watch.java    From jetcd with Apache License 2.0 2 votes vote down vote up
/**
 * watch on a key with option.
 *
 * @param  key                   key to be watched on.
 * @param  option                see {@link io.etcd.jetcd.options.WatchOption}.
 * @param  listener              the event consumer
 * @return                       this watcher
 * @throws ClosedClientException if watch client has been closed.
 */
Watcher watch(ByteSequence key, WatchOption option, Listener listener);
 
Example #14
Source File: Watch.java    From jetcd with Apache License 2.0 2 votes vote down vote up
/**
 * watch on a key.
 *
 * @param  key                   key to be watched on.
 * @param  listener              the event consumer
 * @return                       this watcher
 * @throws ClosedClientException if watch client has been closed.
 **/
default Watcher watch(ByteSequence key, Listener listener) {
    return watch(key, WatchOption.DEFAULT, listener);
}
 
Example #15
Source File: Watch.java    From jetcd with Apache License 2.0 2 votes vote down vote up
/**
 * @param  key    key to be watched on.
 * @param  onNext the on next consumer
 * @return        this watcher
 */
default Watcher watch(ByteSequence key, Consumer<WatchResponse> onNext) {
    return watch(key, WatchOption.DEFAULT, listener(onNext));
}
 
Example #16
Source File: Watch.java    From jetcd with Apache License 2.0 2 votes vote down vote up
/**
 * @param  key     key to be watched on.
 * @param  onNext  the on next consumer
 * @param  onError the on error consumer
 * @return         this watcher
 */
default Watcher watch(ByteSequence key, Consumer<WatchResponse> onNext, Consumer<Throwable> onError) {
    return watch(key, WatchOption.DEFAULT, listener(onNext, onError));
}
 
Example #17
Source File: Watch.java    From jetcd with Apache License 2.0 2 votes vote down vote up
/**
 * @param  key         key to be watched on.
 * @param  onNext      the on next consumer
 * @param  onError     the on error consumer
 * @param  onCompleted the on completion consumer
 * @return             this watcher
 */
default Watcher watch(ByteSequence key, Consumer<WatchResponse> onNext, Consumer<Throwable> onError, Runnable onCompleted) {
    return watch(key, WatchOption.DEFAULT, listener(onNext, onError, onCompleted));
}
 
Example #18
Source File: Watch.java    From jetcd with Apache License 2.0 2 votes vote down vote up
/**
 * @param  key         key to be watched on.
 * @param  onNext      the on next consumer
 * @param  onCompleted the on completion consumer
 * @return             this watcher
 */
default Watcher watch(ByteSequence key, Consumer<WatchResponse> onNext, Runnable onCompleted) {
    return watch(key, WatchOption.DEFAULT, listener(onNext, t -> {
    }, onCompleted));
}
 
Example #19
Source File: Watch.java    From jetcd with Apache License 2.0 2 votes vote down vote up
/**
 *
 * @param  key    key to be watched on.
 * @param  option the options
 * @param  onNext the on next consumer
 * @return        this watcher
 */
default Watcher watch(ByteSequence key, WatchOption option, Consumer<WatchResponse> onNext) {
    return watch(key, option, listener(onNext));
}
 
Example #20
Source File: Watch.java    From jetcd with Apache License 2.0 2 votes vote down vote up
/**
 * @param  key     key to be watched on.
 * @param  option  the options
 * @param  onNext  the on next consumer
 * @param  onError the on error consumer
 * @return         this watcher
 */
default Watcher watch(ByteSequence key, WatchOption option, Consumer<WatchResponse> onNext, Consumer<Throwable> onError) {
    return watch(key, option, listener(onNext, onError));
}
 
Example #21
Source File: Watch.java    From jetcd with Apache License 2.0 2 votes vote down vote up
/**
 * @param  key         key to be watched on.
 * @param  option      the options
 * @param  onNext      the on next consumer
 * @param  onCompleted the on completion consumer
 * @return             this watcher
 */
default Watcher watch(ByteSequence key, WatchOption option, Consumer<WatchResponse> onNext, Runnable onCompleted) {
    return watch(key, option, listener(onNext, t -> {
    }, onCompleted));
}
 
Example #22
Source File: Watch.java    From jetcd with Apache License 2.0 2 votes vote down vote up
/**
 * @param  key         key to be watched on.
 * @param  option      the options
 * @param  onNext      the on next consumer
 * @param  onError     the on error consumer
 * @param  onCompleted the on completion consumer
 * @return             this watcher
 */
default Watcher watch(ByteSequence key, WatchOption option, Consumer<WatchResponse> onNext, Consumer<Throwable> onError,
    Runnable onCompleted) {
    return watch(key, option, listener(onNext, onError, onCompleted));
}