Java Code Examples for io.etcd.jetcd.watch.WatchEvent

The following examples show how to use io.etcd.jetcd.watch.WatchEvent. These examples are extracted from open source projects. 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 Project: jetcd   Source File: Main.java    License: Apache License 2.0 7 votes vote down vote up
public static void main(String[] args) {
    Args cmd = new Args();

    JCommander.newBuilder().addObject(cmd).build().parse(args);

    CountDownLatch latch = new CountDownLatch(cmd.maxEvents);
    ByteSequence key = ByteSequence.from(cmd.key, StandardCharsets.UTF_8);
    Collection<URI> endpoints = Util.toURIs(cmd.endpoints);

    Watch.Listener listener = Watch.listener(response -> {
        LOGGER.info("Watching for key={}", cmd.key);

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

        latch.countDown();
    });

    try (Client client = Client.builder().endpoints(endpoints).build();
        Watch watch = client.getWatchClient();
        Watch.Watcher watcher = watch.watch(key, listener)) {

        latch.await();
    } catch (Exception e) {
        LOGGER.error("Watching Error {}", e);
        System.exit(1);
    }
}
 
Example 2
Source Project: joyrpc   Source File: EtcdRegistry.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 更新
 *
 * @param events     事件
 * @param version    版本
 * @param updateType 更新类型
 */
public void onUpdate(final List<WatchEvent> events, final long version, final UpdateType updateType) {
    List<ShardEvent> shardEvents = new ArrayList<>();
    events.forEach(e -> {
        try {
            String value = e.getKeyValue().getValue().toString(UTF_8);
            switch (e.getEventType()) {
                case PUT:
                    shardEvents.add(new ShardEvent(new DefaultShard(URL.valueOf(value)), ShardEventType.ADD));
                    break;
                case DELETE:
                    //通过删除的key,转换成URL(集群删除节点,只根据shardName删除,所有能够获得ip:port即可)
                    int pos = value.lastIndexOf('/');
                    if (pos >= 0) {
                        value = value.substring(pos + 1);
                    }
                    String[] parts = value.split("_");
                    shardEvents.add(new ShardEvent(new DefaultShard(new URL(parts[0], parts[1], Integer.parseInt(parts[2]))), ShardEventType.DELETE));
                    break;
            }
        } catch (Exception ignored) {
        }
    });
    handle(new ClusterEvent(this, null, updateType, version, shardEvents));
}
 
Example 3
Source Project: Sentinel   Source File: EtcdDataSource.java    License: Apache License 2.0 6 votes vote down vote up
private void initWatcher() {
    watcher = client.getWatchClient().watch(ByteSequence.from(key, charset), (watchResponse) -> {
        for (WatchEvent watchEvent : watchResponse.getEvents()) {
            WatchEvent.EventType eventType = watchEvent.getEventType();
            if (eventType == WatchEvent.EventType.PUT) {
                try {
                    T newValue = loadConfig();
                    getProperty().updateValue(newValue);
                } catch (Exception e) {
                    RecordLog.warn("[EtcdDataSource] Failed to update config", e);
                }
            } else if (eventType == WatchEvent.EventType.DELETE) {
                RecordLog.info("[EtcdDataSource] Cleaning config for key <{}>", key);
                getProperty().updateValue(null);
            }
        }
    });
}
 
Example 4
Source Project: jetcd   Source File: WatchUnitTest.java    License: 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 5
Source Project: jetcd   Source File: WatchTest.java    License: 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 6
Source Project: joyrpc   Source File: EtcdRegistry.java    License: 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 7
Source Project: joyrpc   Source File: EtcdRegistry.java    License: 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 8
Source Project: joyrpc   Source File: EtcdRegistry.java    License: 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 9
Source Project: joyrpc   Source File: EtcdRegistry.java    License: 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 10
Source Project: joyrpc   Source File: EtcdRegistry.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 更新
 *
 * @param events  事件
 * @param version 版本
 */
public void onUpdate(final List<WatchEvent> events, final long version) {
    if (events != null && !events.isEmpty()) {
        events.forEach(event -> {
            Map<String, String> datum = null;
            switch (event.getEventType()) {
                case PUT:
                    String text = event.getKeyValue().getValue().toString(UTF_8);
                    try {
                        datum = JSON.get().parseObject(text, Map.class);
                    } catch (SerializerException e) {
                        logger.error("Error occurs while parsing config.\n" + text, e);
                    }
                    break;
                case DELETE:
                    datum = new HashMap<>();
                    break;
            }
            if (datum != null) {
                String className = url.getPath();
                Map<String, String> oldAttrs = GlobalContext.getInterfaceConfig(className);
                final Map<String, String> data = datum;
                //全局配置动态配置变更
                CONFIG_EVENT_HANDLER.extensions().forEach(v -> v.handle(className, oldAttrs == null ? new HashMap<>() : oldAttrs, data));
                //修改全局配置
                GlobalContext.put(className, datum);
                //TODO 是否需要实例配置
                handle(new ConfigEvent(this, null, version, datum));
            }
        });
    } else {
        handle(new ConfigEvent(this, null, version, new HashMap<>()));
    }
}
 
Example 11
Source Project: shardingsphere   Source File: EtcdCenterRepository.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void watch(final String key, final DataChangedEventListener dataChangedEventListener) {
    Watch.Listener listener = Watch.listener(response -> {
        for (WatchEvent each : response.getEvents()) {
            DataChangedEvent.ChangedType changedType = getEventChangedType(each);
            if (DataChangedEvent.ChangedType.IGNORED != changedType) {
                dataChangedEventListener.onChange(new DataChangedEvent(each.getKeyValue().getKey().toString(Charsets.UTF_8), each.getKeyValue().getValue().toString(Charsets.UTF_8), changedType));
            }
        }
    });
    client.getWatchClient().watch(ByteSequence.from(key, Charsets.UTF_8), listener);
}
 
Example 12
Source Project: shardingsphere   Source File: EtcdCenterRepository.java    License: Apache License 2.0 5 votes vote down vote up
private DataChangedEvent.ChangedType getEventChangedType(final WatchEvent event) {
    switch (event.getEventType()) {
        case PUT:
            return DataChangedEvent.ChangedType.UPDATED;
        case DELETE:
            return DataChangedEvent.ChangedType.DELETED;
        default:
            return DataChangedEvent.ChangedType.IGNORED;
    }
}
 
Example 13
Source Project: shardingsphere   Source File: EtcdCenterRepositoryTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void assertWatchUpdate() {
    doAnswer(invocationOnMock -> {
        Watch.Listener listener = (Watch.Listener) invocationOnMock.getArguments()[1];
        listener.onNext(buildWatchResponse(WatchEvent.EventType.PUT));
        return mock(Watch.Watcher.class);
    }).when(watch).watch(any(ByteSequence.class), any(Watch.Listener.class));
    centerRepository.watch("key1", dataChangedEvent -> {
    });
    verify(watch).watch(any(ByteSequence.class), any(Watch.Listener.class));
}
 
Example 14
Source Project: shardingsphere   Source File: EtcdCenterRepositoryTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void assertWatchDelete() {
    doAnswer(invocationOnMock -> {
        Watch.Listener listener = (Watch.Listener) invocationOnMock.getArguments()[1];
        listener.onNext(buildWatchResponse(WatchEvent.EventType.DELETE));
        return mock(Watch.Watcher.class);
    }).when(watch).watch(any(ByteSequence.class), any(Watch.Listener.class));
    centerRepository.watch("key1", dataChangedEvent -> {
    });
    verify(watch).watch(any(ByteSequence.class), any(Watch.Listener.class));
}
 
Example 15
Source Project: shardingsphere   Source File: EtcdCenterRepositoryTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void assertWatchIgnored() {
    doAnswer(invocationOnMock -> {
        Watch.Listener listener = (Watch.Listener) invocationOnMock.getArguments()[1];
        listener.onNext(buildWatchResponse(WatchEvent.EventType.UNRECOGNIZED));
        return mock(Watch.Watcher.class);
    }).when(watch).watch(any(ByteSequence.class), any(Watch.Listener.class));
    centerRepository.watch("key1", dataChangedEvent -> {
    });
    verify(watch).watch(any(ByteSequence.class), any(Watch.Listener.class));
}
 
Example 16
Source Project: shardingsphere   Source File: EtcdCenterRepositoryTest.java    License: 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 17
Source Project: jetcd   Source File: WatchUnitTest.java    License: Apache License 2.0 5 votes vote down vote up
private static void assertEqualOnWatchResponses(io.etcd.jetcd.watch.WatchResponse expected,
    io.etcd.jetcd.watch.WatchResponse actual) {
    assertThat(actual.getEvents().size()).isEqualTo(expected.getEvents().size());

    for (int idx = 0; idx < expected.getEvents().size(); idx++) {
        WatchEvent act = actual.getEvents().get(idx);
        WatchEvent exp = actual.getEvents().get(idx);
        assertThat(act.getEventType()).isEqualTo(exp.getEventType());
        assertEqualOnKeyValues(act.getKeyValue(), exp.getKeyValue());
        assertEqualOnKeyValues(act.getPrevKV(), exp.getPrevKV());
    }
}
 
Example 18
Source Project: jetcd   Source File: CommandWatch.java    License: 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 19
Source Project: kkbinlog   Source File: BinLogClientFactory.java    License: 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 20
Source Project: kkbinlog   Source File: OpLogClientFactory.java    License: 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 21
Source Project: kkbinlog   Source File: BinaryLogConfigContainer.java    License: 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();
                    }
                }
        );
    }