io.etcd.jetcd.ByteSequence Java Examples

The following examples show how to use io.etcd.jetcd.ByteSequence. 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: Main.java    From jetcd with 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 File: EtcdCenterRepositoryTest.java    From shardingsphere with Apache License 2.0 6 votes vote down vote up
@Test
public void assertGetChildrenKeys() {
    io.etcd.jetcd.api.KeyValue keyValue1 = io.etcd.jetcd.api.KeyValue.newBuilder()
        .setKey(ByteString.copyFromUtf8("/key/key1/key1-1"))
        .setValue(ByteString.copyFromUtf8("value1")).build();
    io.etcd.jetcd.api.KeyValue keyValue2 = io.etcd.jetcd.api.KeyValue.newBuilder()
        .setKey(ByteString.copyFromUtf8("/key/key2"))
        .setValue(ByteString.copyFromUtf8("value3")).build();
    List<KeyValue> keyValues = Arrays.asList(new KeyValue(keyValue1, ByteSequence.EMPTY), new KeyValue(keyValue2, ByteSequence.EMPTY), 
            new KeyValue(keyValue1, ByteSequence.EMPTY));
    when(getResponse.getKvs()).thenReturn(keyValues);
    List<String> actual = centerRepository.getChildrenKeys("/key");
    assertThat(actual.size(), is(2));
    Iterator<String> iterator = actual.iterator();
    assertThat(iterator.next(), is("key1"));
    assertThat(iterator.next(), is("key2"));
}
 
Example #3
Source File: EtcdCenterRepositoryTest.java    From shardingsphere with Apache License 2.0 6 votes vote down vote up
@SneakyThrows({InterruptedException.class, ExecutionException.class})
@SuppressWarnings("unchecked")
private Client mockClient() {
    when(client.getKVClient()).thenReturn(kv);
    when(kv.get(any(ByteSequence.class))).thenReturn(getFuture);
    when(kv.get(any(ByteSequence.class), any(GetOption.class))).thenReturn(getFuture);
    when(kv.put(any(ByteSequence.class), any(ByteSequence.class))).thenReturn(putFuture);
    when(kv.put(any(ByteSequence.class), any(ByteSequence.class), any(PutOption.class))).thenReturn(putFuture);
    when(getFuture.get()).thenReturn(getResponse);
    when(client.getLeaseClient()).thenReturn(lease);
    when(lease.grant(anyLong())).thenReturn(leaseFuture);
    when(leaseFuture.get()).thenReturn(leaseGrantResponse);
    when(leaseGrantResponse.getID()).thenReturn(123L);
    when(client.getWatchClient()).thenReturn(watch);
    return client;
}
 
Example #4
Source File: GetOption.java    From jetcd with Apache License 2.0 6 votes vote down vote up
private GetOption(Optional<ByteSequence> endKey, long limit, long revision, SortOrder sortOrder, SortTarget sortTarget,
    boolean serializable, boolean keysOnly, boolean countOnly, long minCreateRevision, long maxCreateRevision,
    long minModRevision, long maxModRevision) {
    this.endKey = endKey;
    this.limit = limit;
    this.revision = revision;
    this.sortOrder = sortOrder;
    this.sortTarget = sortTarget;
    this.serializable = serializable;
    this.keysOnly = keysOnly;
    this.countOnly = countOnly;
    this.minCreateRevision = minCreateRevision;
    this.maxCreateRevision = maxCreateRevision;
    this.minModRevision = minModRevision;
    this.maxModRevision = maxModRevision;
}
 
Example #5
Source File: Op.java    From jetcd with Apache License 2.0 6 votes vote down vote up
RequestOp toRequestOp(ByteSequence namespace) {
    TxnRequest.Builder txn = TxnRequest.newBuilder();

    if (cmps != null) {
        for (Cmp cmp : cmps) {
            txn.addCompare(cmp.toCompare(namespace));
        }
    }

    if (thenOps != null) {
        for (Op thenOp : thenOps) {
            txn.addSuccess(thenOp.toRequestOp(namespace));
        }
    }

    if (elseOps != null) {
        for (Op elseOp : elseOps) {
            txn.addFailure(elseOp.toRequestOp(namespace));
        }
    }

    return RequestOp.newBuilder().setRequestTxn(txn).build();
}
 
Example #6
Source File: WatchResponse.java    From jetcd with Apache License 2.0 6 votes vote down vote up
/**
 * convert API watch event to client event.
 */
private static WatchEvent toEvent(Event event, ByteSequence namespace) {
    WatchEvent.EventType eventType;
    switch (event.getType()) {
        case DELETE:
            eventType = WatchEvent.EventType.DELETE;
            break;
        case PUT:
            eventType = WatchEvent.EventType.PUT;
            break;
        default:
            eventType = WatchEvent.EventType.UNRECOGNIZED;
    }

    return new WatchEvent(new KeyValue(event.getKv(), namespace), new KeyValue(event.getPrevKv(), namespace), eventType);
}
 
Example #7
Source File: AuthRoleGetResponse.java    From jetcd with Apache License 2.0 6 votes vote down vote up
private static Permission toPermission(io.etcd.jetcd.api.Permission perm) {
    ByteSequence key = ByteSequence.from(perm.getKey());
    ByteSequence rangeEnd = ByteSequence.from(perm.getRangeEnd());

    Permission.Type type;
    switch (perm.getPermType()) {
        case READ:
            type = Type.READ;
            break;
        case WRITE:
            type = Type.WRITE;
            break;
        case READWRITE:
            type = Type.READWRITE;
            break;
        default:
            type = Type.UNRECOGNIZED;
    }

    return new Permission(type, key, rangeEnd);
}
 
Example #8
Source File: EtcdDataSource.java    From Sentinel with 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 #9
Source File: EtcdDataSource.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
/**
 * Create an etcd data-source. The connection configuration will be retrieved from {@link EtcdConfig}.
 *
 * @param key    config key
 * @param parser data parser
 */
public EtcdDataSource(String key, Converter<String, T> parser) {
    super(parser);
    if (!EtcdConfig.isAuthEnable()) {
        this.client = Client.builder()
            .endpoints(EtcdConfig.getEndPoints().split(",")).build();
    } else {
        this.client = Client.builder()
            .endpoints(EtcdConfig.getEndPoints().split(","))
            .user(ByteSequence.from(EtcdConfig.getUser(), charset))
            .password(ByteSequence.from(EtcdConfig.getPassword(), charset))
            .authority(EtcdConfig.getAuthority())
            .build();
    }
    this.key = key;
    loadInitialConfig();
    initWatcher();
}
 
Example #10
Source File: EtcdClusterUsingTest.java    From jetcd with Apache License 2.0 6 votes vote down vote up
@Test
public void testUseEtcd() throws Exception {
    try (EtcdCluster etcd = EtcdClusterFactory.buildCluster(getClass().getSimpleName(), 3, false)) {
        etcd.start();
        try (Client client = Client.builder().endpoints(etcd.getClientEndpoints()).build()) {
            try (KV kvClient = client.getKVClient()) {
                ByteSequence key = bytesOf("test_key");
                ByteSequence value = bytesOf("test_value");
                kvClient.put(key, value).get();

                CompletableFuture<GetResponse> getFuture = kvClient.get(key);
                GetResponse response = getFuture.get();
                List<KeyValue> values = response.getKvs();
                assertThat(values.size()).isEqualTo(1);
                KeyValue value1 = values.get(0);
                assertThat(value1.getValue()).isEqualTo(value);
                assertThat(value1.getKey()).isEqualTo(key);
            }
        }
    }
}
 
Example #11
Source File: EtcdDataSourceTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadSource() throws Exception {
    EtcdDataSource dataSource = new EtcdDataSource("foo", value -> value);
    KV kvClient = Client.builder()
            .endpoints(endPoints)
            .build().getKVClient();

    kvClient.put(ByteSequence.from("foo".getBytes()), ByteSequence.from("test".getBytes()));
    Assert.assertNotNull(dataSource.readSource().equals("test"));

    kvClient.put(ByteSequence.from("foo".getBytes()), ByteSequence.from("test2".getBytes()));
    Assert.assertNotNull(dataSource.getProperty().equals("test2"));
}
 
Example #12
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 #13
Source File: EtcdCenterRepository.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
@SneakyThrows({InterruptedException.class, ExecutionException.class})
@Override
public List<String> getChildrenKeys(final String key) {
    String prefix = key + "/";
    ByteSequence prefixByteSequence = ByteSequence.from(prefix, Charsets.UTF_8);
    GetOption getOption = GetOption.newBuilder().withPrefix(prefixByteSequence).withSortField(GetOption.SortTarget.KEY).withSortOrder(GetOption.SortOrder.ASCEND).build();
    List<KeyValue> keyValues = client.getKVClient().get(prefixByteSequence, getOption).get().getKvs();
    return keyValues.stream().map(e -> getSubNodeKeyName(prefix, e.getKey().toString(Charsets.UTF_8))).distinct().collect(Collectors.toList());
}
 
Example #14
Source File: EtcdCenterRepository.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
@SneakyThrows({InterruptedException.class, ExecutionException.class})
@Override
public void persistEphemeral(final String key, final String value) {
    long leaseId = client.getLeaseClient().grant(this.etcdProperties.getValue(EtcdPropertyKey.TIME_TO_LIVE_SECONDS)).get().getID();
    client.getLeaseClient().keepAlive(leaseId, Observers.observer(response -> { }));
    client.getKVClient().put(ByteSequence.from(key, Charsets.UTF_8), ByteSequence.from(value, Charsets.UTF_8), PutOption.newBuilder().withLeaseId(leaseId).build()).get();
}
 
Example #15
Source File: EtcdCenterRepository.java    From shardingsphere with 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 #16
Source File: EtcdConfigSender.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws InterruptedException {


        String rule_key = "sentinel_demo_rule_key";

        Client client = Client.builder()
                .endpoints("http://127.0.0.1:2379")
                .user(ByteSequence.from("root".getBytes()))
                .password(ByteSequence.from("12345".getBytes()))
                .build();
        final String rule = "[\n"
                + "  {\n"
                + "    \"resource\": \"TestResource\",\n"
                + "    \"controlBehavior\": 0,\n"
                + "    \"count\": 5.0,\n"
                + "    \"grade\": 1,\n"
                + "    \"limitApp\": \"default\",\n"
                + "    \"strategy\": 0\n"
                + "  }\n"
                + "]";
        client.getKVClient()
                .put(ByteSequence.from(rule_key.getBytes()), ByteSequence.from(rule.getBytes()));

        System.out.println("setting rule success");
        Thread.sleep(10000);

    }
 
Example #17
Source File: EtcdRegistry.java    From joyrpc with Apache License 2.0 5 votes vote down vote up
@Override
protected CompletableFuture<Void> doDeregister(final Registion registion) {
    CompletableFuture<Void> future = new CompletableFuture<>();
    ByteSequence key = ByteSequence.from(registion.getPath(), UTF_8);
    client.getKVClient().delete(key).whenComplete((r, t) -> {
        if (t != null) {
            future.completeExceptionally(t);
        } else {
            future.complete(null);
        }
    });
    return future;
}
 
Example #18
Source File: WatchOption.java    From jetcd with Apache License 2.0 5 votes vote down vote up
/**
 * Enables watch all the keys with matching prefix.
 *
 * @param  prefix the common prefix of all the keys that you want to watch
 * @return        builder
 */
public Builder withPrefix(ByteSequence prefix) {
    checkNotNull(prefix, "prefix should not be null");
    ByteSequence prefixEnd = OptionsUtil.prefixEndOf(prefix);
    this.withRange(prefixEnd);
    return this;
}
 
Example #19
Source File: EtcdCenterRepositoryTest.java    From shardingsphere with 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 #20
Source File: EtcdCenterRepositoryTest.java    From shardingsphere with 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 #21
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 #22
Source File: OptionsUtil.java    From jetcd with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the range end of the given prefix.
 *
 * <p>
 * The range end is the key plus one (e.g., "aa"+1 == "ab", "a\xff"+1 == "b").
 *
 * @param  prefix the given prefix
 * @return        the range end of the given prefix
 */
static ByteSequence prefixEndOf(ByteSequence prefix) {
    byte[] endKey = prefix.getBytes().clone();
    for (int i = endKey.length - 1; i >= 0; i--) {
        if (endKey[i] != (byte) 0xff) {
            endKey[i] = (byte) (endKey[i] + 1);
            return ByteSequence.from(Arrays.copyOf(endKey, i + 1));
        }
    }

    return ByteSequence.from(NO_PREFIX_END);
}
 
Example #23
Source File: EtcdRegistry.java    From joyrpc with Apache License 2.0 5 votes vote down vote up
@Override
protected CompletableFuture<Void> doRegister(final Registion registion) {
    CompletableFuture<Void> future = new CompletableFuture<>();
    if (leaseId <= 0) {
        //没有租约
        future.completeExceptionally(new IllegalStateException(
                String.format("Error occurs while register provider of %s, caused by no leaseId. retry....", registion.getService())));
    } else {
        //有租约
        PutOption putOption = PutOption.newBuilder().withLeaseId(leaseId).build();
        ByteSequence key = ByteSequence.from(registion.getPath(), UTF_8);
        ByteSequence value = ByteSequence.from(registion.getUrl().toString(), UTF_8);
        client.getKVClient().put(key, value, putOption).whenComplete((r, t) -> {
            if (!isOpen()) {
                //已经关闭,或者创建了新的客户端
                future.completeExceptionally(new IllegalStateException("controller is closed."));
            } else if (t != null) {
                //TODO 判断租期失效异常,触发重连逻辑
                logger.error(String.format("Error occurs while register provider of %s, caused by %s. retry....",
                        registion.getPath(), t.getMessage()), t);
                future.completeExceptionally(t);
            } else {
                future.complete(null);
            }
        });
    }
    return future;
}
 
Example #24
Source File: WatchOption.java    From jetcd with Apache License 2.0 5 votes vote down vote up
private WatchOption(Optional<ByteSequence> endKey, long revision, boolean prevKV, boolean progressNotify, boolean noPut,
    boolean noDelete) {
    this.endKey = endKey;
    this.revision = revision;
    this.prevKV = prevKV;
    this.progressNotify = progressNotify;
    this.noPut = noPut;
    this.noDelete = noDelete;
}
 
Example #25
Source File: Op.java    From jetcd with Apache License 2.0 5 votes vote down vote up
RequestOp toRequestOp(ByteSequence namespace) {
    RangeRequest.Builder range = RangeRequest.newBuilder().setKey(Util.prefixNamespace(this.key, namespace))
        .setCountOnly(this.option.isCountOnly()).setLimit(this.option.getLimit())
        .setRevision(this.option.getRevision()).setKeysOnly(this.option.isKeysOnly())
        .setSerializable(this.option.isSerializable())
        .setSortOrder(toRangeRequestSortOrder(this.option.getSortOrder()))
        .setSortTarget(toRangeRequestSortTarget(this.option.getSortField()));

    this.option.getEndKey()
        .map(endKey -> Util.prefixNamespaceToRangeEnd(ByteString.copyFrom(endKey.getBytes()), namespace))
        .ifPresent(range::setRangeEnd);

    return RequestOp.newBuilder().setRequestRange(range).build();
}
 
Example #26
Source File: Op.java    From jetcd with Apache License 2.0 5 votes vote down vote up
RequestOp toRequestOp(ByteSequence namespace) {
    DeleteRangeRequest.Builder delete = DeleteRangeRequest.newBuilder()
        .setKey(Util.prefixNamespace(this.key, namespace)).setPrevKv(this.option.isPrevKV());

    this.option.getEndKey()
        .map(endKey -> Util.prefixNamespaceToRangeEnd(ByteString.copyFrom(endKey.getBytes()), namespace))
        .ifPresent(delete::setRangeEnd);

    return RequestOp.newBuilder().setRequestDeleteRange(delete).build();
}
 
Example #27
Source File: LeaseTimeToLiveResponse.java    From jetcd with Apache License 2.0 5 votes vote down vote up
/**
 * @return the list of keys attached to this lease.
 */
public synchronized List<ByteSequence> getKeys() {
    if (keys == null) {
        keys = getResponse().getKeysList().stream().map(ByteSequence::from).collect(Collectors.toList());
    }

    return keys;
}
 
Example #28
Source File: CommandPut.java    From jetcd with Apache License 2.0 5 votes vote down vote up
@Override
public void accept(Client client) throws Exception {
    client.getKVClient()
        .put(ByteSequence.from(keyValue.get(0), Charsets.UTF_8), ByteSequence.from(keyValue.get(1), Charsets.UTF_8)).get();

    LOGGER.info("OK");
}
 
Example #29
Source File: CommandGet.java    From jetcd with Apache License 2.0 5 votes vote down vote up
@Override
public void accept(Client client) throws Exception {
    GetResponse getResponse = client.getKVClient()
        .get(ByteSequence.from(key, UTF_8), GetOption.newBuilder().withRevision(rev).build()).get();

    if (getResponse.getKvs().isEmpty()) {
        // key does not exist
        return;
    }

    LOGGER.info(key);
    LOGGER.info(getResponse.getKvs().get(0).getValue().toString(UTF_8));
}
 
Example #30
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;
    }
}