io.etcd.jetcd.watch.WatchResponse Java Examples

The following examples show how to use io.etcd.jetcd.watch.WatchResponse. 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 testWatchClose(final Client client) throws Exception {
    final ByteSequence key = randomByteSequence();
    final ByteSequence value = randomByteSequence();
    final List<WatchResponse> events = Collections.synchronizedList(new ArrayList<>());

    try (Watcher watcher = client.getWatchClient().watch(key, events::add)) {
        client.getKVClient().put(key, value).get();
        await().atMost(15, TimeUnit.SECONDS).untilAsserted(() -> assertThat(events).isNotEmpty());
    }

    client.getKVClient().put(key, randomByteSequence()).get();

    await().atMost(15, TimeUnit.SECONDS).untilAsserted(() -> assertThat(events).hasSize(1));
    assertThat(events.get(0).getEvents()).hasSize(1);
    assertThat(events.get(0).getEvents().get(0).getEventType()).isEqualTo(EventType.PUT);
    assertThat(events.get(0).getEvents().get(0).getKeyValue().getKey()).isEqualTo(key);
    assertThat(events.get(0).getEvents().get(0).getKeyValue().getValue()).isEqualTo(value);
}
 
Example #2
Source File: WatchTest.java    From jetcd with Apache License 2.0 6 votes vote down vote up
@ParameterizedTest
@MethodSource("parameters")
public void testWatchOnDelete(final Client client) throws Exception {
    final ByteSequence key = randomByteSequence();
    final ByteSequence value = randomByteSequence();
    final AtomicReference<WatchResponse> ref = new AtomicReference<>();

    client.getKVClient().put(key, value).get();

    try (Watcher watcher = client.getWatchClient().watch(key, ref::set)) {
        client.getKVClient().delete(key).get();

        await().atMost(15, TimeUnit.SECONDS).untilAsserted(() -> assertThat(ref.get()).isNotNull());

        assertThat(ref.get().getEvents().size()).isEqualTo(1);

        WatchEvent event = ref.get().getEvents().get(0);
        assertThat(event.getEventType()).isEqualTo(EventType.DELETE);
        assertThat(Arrays.equals(event.getKeyValue().getKey().getBytes(), key.getBytes())).isTrue();
    }
}
 
Example #3
Source File: WatchTest.java    From jetcd with Apache License 2.0 6 votes vote down vote up
@ParameterizedTest
@MethodSource("parameters")
public void testMultipleWatch(final Client client) throws Exception {
    final ByteSequence key = randomByteSequence();
    final CountDownLatch latch = new CountDownLatch(2);
    final ByteSequence value = randomByteSequence();
    final List<WatchResponse> res = Collections.synchronizedList(new ArrayList<>(2));

    try (Watcher w1 = client.getWatchClient().watch(key, res::add);
        Watcher w2 = client.getWatchClient().watch(key, res::add)) {

        client.getKVClient().put(key, value).get();
        latch.await(4, TimeUnit.SECONDS);

        await().atMost(15, TimeUnit.SECONDS).untilAsserted(() -> assertThat(res).hasSize(2));
        assertThat(res.get(0)).usingRecursiveComparison().isEqualTo(res.get(1));
        assertThat(res.get(0).getEvents().size()).isEqualTo(1);
        assertThat(res.get(0).getEvents().get(0).getEventType()).isEqualTo(EventType.PUT);
        assertThat(res.get(0).getEvents().get(0).getKeyValue().getKey()).isEqualTo(key);
    }
}
 
Example #4
Source File: WatchTest.java    From jetcd with Apache License 2.0 6 votes vote down vote up
@ParameterizedTest
@MethodSource("parameters")
public void testWatchOnPut(final Client client) throws Exception {
    final ByteSequence key = randomByteSequence();
    final ByteSequence value = randomByteSequence();
    final AtomicReference<WatchResponse> ref = new AtomicReference<>();

    try (Watcher watcher = client.getWatchClient().watch(key, ref::set)) {

        client.getKVClient().put(key, value).get();

        await().atMost(15, TimeUnit.SECONDS).untilAsserted(() -> assertThat(ref.get()).isNotNull());

        assertThat(ref.get()).isNotNull();
        assertThat(ref.get().getEvents().size()).isEqualTo(1);
        assertThat(ref.get().getEvents().get(0).getEventType()).isEqualTo(EventType.PUT);
        assertThat(ref.get().getEvents().get(0).getKeyValue().getKey()).isEqualTo(key);
    }
}
 
Example #5
Source File: WatchTest.java    From jetcd with Apache License 2.0 6 votes vote down vote up
@Test
public void testNamespacedAndNotNamespacedClient() throws Exception {
    final ByteSequence key = randomByteSequence();
    final ByteSequence nsKey = ByteSequence.from(namespace.getByteString().concat(key.getByteString()));
    final Client client = Client.builder().endpoints(cluster.getClientEndpoints()).build();
    final Client nsClient = Client.builder().endpoints(cluster.getClientEndpoints()).namespace(namespace).build();

    final ByteSequence value = randomByteSequence();
    final AtomicReference<WatchResponse> ref = new AtomicReference<>();

    // From client with namespace watch for key. Since client is namespaced it should watch for namespaced key.
    try (Watcher watcher = nsClient.getWatchClient().watch(key, ref::set)) {
        // Using non-namespaced client put namespaced key.
        client.getKVClient().put(nsKey, value).get();
        await().atMost(15, TimeUnit.SECONDS).untilAsserted(() -> assertThat(ref.get()).isNotNull());

        assertThat(ref.get()).isNotNull();
        assertThat(ref.get().getEvents().size()).isEqualTo(1);
        assertThat(ref.get().getEvents().get(0).getEventType()).isEqualTo(EventType.PUT);
        assertThat(ref.get().getEvents().get(0).getKeyValue().getKey()).isEqualTo(key);
    }
}
 
Example #6
Source File: WatchResumeTest.java    From jetcd with Apache License 2.0 6 votes vote down vote up
@Test
public void testWatchOnPut() throws Exception {
    final ByteSequence key = TestUtil.randomByteSequence();
    final ByteSequence value = TestUtil.randomByteSequence();
    final AtomicReference<WatchResponse> ref = new AtomicReference<>();

    try (Watcher watcher = watchClient.watch(key, ref::set)) {
        cluster.restart();
        kvClient.put(key, value).get(1, TimeUnit.SECONDS);

        await().atMost(15, TimeUnit.SECONDS).untilAsserted(() -> assertThat(ref.get()).isNotNull());

        assertThat(ref.get().getEvents().size()).isEqualTo(1);
        assertThat(ref.get().getEvents().get(0).getEventType()).isEqualTo(EventType.PUT);
        assertThat(ref.get().getEvents().get(0).getKeyValue().getKey()).isEqualTo(key);
    }
}
 
Example #7
Source File: Watch.java    From jetcd with Apache License 2.0 6 votes vote down vote up
static Listener listener(Consumer<WatchResponse> onNext, Consumer<Throwable> onError, Runnable onCompleted) {
    return new Listener() {
        @Override
        public void onNext(WatchResponse response) {
            onNext.accept(response);
        }

        @Override
        public void onError(Throwable throwable) {
            onError.accept(throwable);
        }

        @Override
        public void onCompleted() {
            onCompleted.run();
        }
    };
}
 
Example #8
Source File: EtcdRegistry.java    From joyrpc with Apache License 2.0 5 votes vote down vote up
@Override
public void onNext(final WatchResponse response) {
    List<WatchEvent> events = response.getEvents();
    if (events != null && !events.isEmpty()) {
        onUpdate(events, response.getHeader().getRevision());
    }
}
 
Example #9
Source File: EtcdRegistry.java    From joyrpc with Apache License 2.0 5 votes vote down vote up
@Override
public void onNext(WatchResponse response) {
    List<WatchEvent> events = response.getEvents();
    if (events != null && !events.isEmpty()) {
        onUpdate(events, response.getHeader().getRevision(), UPDATE);
    }
}
 
Example #10
Source File: EtcdCenterRepositoryTest.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
@SneakyThrows({NoSuchFieldException.class, SecurityException.class})
private WatchResponse buildWatchResponse(final WatchEvent.EventType eventType) {
    WatchResponse watchResponse = new WatchResponse(mock(io.etcd.jetcd.api.WatchResponse.class), ByteSequence.EMPTY);
    List<WatchEvent> events = new ArrayList<>();
    io.etcd.jetcd.api.KeyValue keyValue1 = io.etcd.jetcd.api.KeyValue.newBuilder()
            .setKey(ByteString.copyFromUtf8("key1"))
            .setValue(ByteString.copyFromUtf8("value1")).build();
    KeyValue keyValue = new KeyValue(keyValue1, ByteSequence.EMPTY);
    events.add(new WatchEvent(keyValue, mock(KeyValue.class), eventType));
    FieldSetter.setField(watchResponse, watchResponse.getClass().getDeclaredField("events"), events);
    return watchResponse;
}
 
Example #11
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 #12
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 #13
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 #14
Source File: Watch.java    From jetcd with Apache License 2.0 4 votes vote down vote up
static Listener listener(Consumer<WatchResponse> onNext) {
    return listener(onNext, t -> {
    }, () -> {
    });
}
 
Example #15
Source File: Watch.java    From jetcd with Apache License 2.0 4 votes vote down vote up
static Listener listener(Consumer<WatchResponse> onNext, Consumer<Throwable> onError) {
    return listener(onNext, onError, () -> {
    });
}
 
Example #16
Source File: Watch.java    From jetcd with Apache License 2.0 4 votes vote down vote up
static Listener listener(Consumer<WatchResponse> onNext, Runnable onCompleted) {
    return listener(onNext, t -> {
    }, onCompleted);
}
 
Example #17
Source File: TestUtil.java    From jetcd with Apache License 2.0 4 votes vote down vote up
public static void noOpWatchResponseConsumer(WatchResponse response) {
    // no-op
}
 
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  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 #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  onNext the on next consumer
 * @return        this watcher
 */
default Watcher watch(ByteSequence key, Consumer<WatchResponse> onNext) {
    return watch(key, WatchOption.DEFAULT, listener(onNext));
}
 
Example #20
Source File: Watch.java    From jetcd with Apache License 2.0 2 votes vote down vote up
/**
 * Invoked on new events.
 *
 * @param response the response.
 */
void onNext(WatchResponse response);
 
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  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 #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));
}
 
Example #23
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 #24
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 #25
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 #26
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));
}