Search in sources :

Example 6 with Watcher

use of io.etcd.jetcd.Watch.Watcher in project jetcd by coreos.

the class CommandWatch method accept.

@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(Charsets.UTF_8)).orElse(""), Optional.ofNullable(event.getKeyValue().getValue()).map(bs -> bs.toString(Charsets.UTF_8)).orElse(""));
            }
            latch.countDown();
        });
        latch.await();
    } catch (Exception e) {
        if (watcher != null) {
            watcher.close();
        }
        throw e;
    }
}
Also used : Charsets(com.google.common.base.Charsets) Parameters(com.beust.jcommander.Parameters) Logger(org.slf4j.Logger) Client(io.etcd.jetcd.Client) WatchEvent(io.etcd.jetcd.watch.WatchEvent) Parameter(com.beust.jcommander.Parameter) LoggerFactory(org.slf4j.LoggerFactory) CountDownLatch(java.util.concurrent.CountDownLatch) ByteSequence(io.etcd.jetcd.ByteSequence) Optional(java.util.Optional) CheckedConsumer(org.jooq.lambda.fi.util.function.CheckedConsumer) WatchOption(io.etcd.jetcd.options.WatchOption) Watcher(io.etcd.jetcd.Watch.Watcher) Watcher(io.etcd.jetcd.Watch.Watcher) WatchEvent(io.etcd.jetcd.watch.WatchEvent) CountDownLatch(java.util.concurrent.CountDownLatch) ByteSequence(io.etcd.jetcd.ByteSequence) WatchOption(io.etcd.jetcd.options.WatchOption)

Example 7 with Watcher

use of io.etcd.jetcd.Watch.Watcher in project jetcd by coreos.

the class WatchTest method testCancelledWatchGetsClosed.

@ParameterizedTest
@MethodSource("parameters")
public void testCancelledWatchGetsClosed(final Client client) throws Exception {
    final ByteSequence key = randomByteSequence();
    final Watch wc = client.getWatchClient();
    long revision = getCompactedRevision(client, key);
    final WatchOption options = WatchOption.newBuilder().withRevision(revision).build();
    final AtomicReference<Throwable> ref = new AtomicReference<>();
    final AtomicReference<Boolean> completed = new AtomicReference<>();
    Watch.Listener listener = Watch.listener(TestUtil::noOpWatchResponseConsumer, ref::set, () -> {
        completed.set(Boolean.TRUE);
    });
    try (Watcher watcher = wc.watch(key, options, listener)) {
        await().atMost(TIME_OUT_SECONDS, TimeUnit.SECONDS).untilAsserted(() -> assertThat(ref.get()).isNotNull());
        assertThat(ref.get().getClass()).isEqualTo(CompactedException.class);
        assertThat(completed.get()).isNotNull();
        assertThat(completed.get()).isEqualTo(Boolean.TRUE);
    }
}
Also used : Watcher(io.etcd.jetcd.Watch.Watcher) AtomicReference(java.util.concurrent.atomic.AtomicReference) Watch(io.etcd.jetcd.Watch) ByteSequence(io.etcd.jetcd.ByteSequence) TestUtil.randomByteSequence(io.etcd.jetcd.impl.TestUtil.randomByteSequence) WatchOption(io.etcd.jetcd.options.WatchOption) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 8 with Watcher

use of io.etcd.jetcd.Watch.Watcher in project jetcd by coreos.

the class WatchTest method testMultipleWatch.

@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(TIME_OUT_SECONDS, 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);
    }
}
Also used : Watcher(io.etcd.jetcd.Watch.Watcher) CountDownLatch(java.util.concurrent.CountDownLatch) WatchResponse(io.etcd.jetcd.watch.WatchResponse) ByteSequence(io.etcd.jetcd.ByteSequence) TestUtil.randomByteSequence(io.etcd.jetcd.impl.TestUtil.randomByteSequence) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 9 with Watcher

use of io.etcd.jetcd.Watch.Watcher in project jetcd by coreos.

the class WatchTest method testWatchClose.

@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(TIME_OUT_SECONDS, TimeUnit.SECONDS).untilAsserted(() -> assertThat(events).isNotEmpty());
    }
    client.getKVClient().put(key, randomByteSequence()).get();
    await().atMost(TIME_OUT_SECONDS, 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);
}
Also used : Watcher(io.etcd.jetcd.Watch.Watcher) WatchResponse(io.etcd.jetcd.watch.WatchResponse) ByteSequence(io.etcd.jetcd.ByteSequence) TestUtil.randomByteSequence(io.etcd.jetcd.impl.TestUtil.randomByteSequence) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 10 with Watcher

use of io.etcd.jetcd.Watch.Watcher in project jetcd by coreos.

the class WatchTest method testProgressRequest.

@ParameterizedTest
@MethodSource("parameters")
public void testProgressRequest(final Client client) throws Exception {
    final ByteSequence key = randomByteSequence();
    final ByteSequence value = randomByteSequence();
    final Watch watchClient = client.getWatchClient();
    final AtomicReference<WatchResponse> emptyWatcherEventRef = new AtomicReference<>();
    final AtomicReference<WatchResponse> activeWatcherEventRef = new AtomicReference<>();
    try (Watcher activeWatcher = watchClient.watch(key, activeWatcherEventRef::set);
        Watcher emptyWatcher = watchClient.watch(key.concat(randomByteSequence()), emptyWatcherEventRef::set)) {
        // Check that a requestProgress returns identical revisions initially
        watchClient.requestProgress();
        await().atMost(TIME_OUT_SECONDS, TimeUnit.SECONDS).untilAsserted(() -> {
            assertThat(activeWatcherEventRef.get()).isNotNull();
            assertThat(emptyWatcherEventRef.get()).isNotNull();
        });
        WatchResponse activeEvent = activeWatcherEventRef.get();
        WatchResponse emptyEvent = emptyWatcherEventRef.get();
        assertThat(activeEvent).satisfies(WatchResponse::isProgressNotify);
        assertThat(emptyEvent).satisfies(WatchResponse::isProgressNotify);
        assertThat(activeEvent.getHeader().getRevision()).isEqualTo(emptyEvent.getHeader().getRevision());
        // Put a value being watched by only the active watcher
        activeWatcherEventRef.set(null);
        emptyWatcherEventRef.set(null);
        client.getKVClient().put(key, value).get();
        await().atMost(TIME_OUT_SECONDS, TimeUnit.SECONDS).untilAsserted(() -> {
            assertThat(activeWatcherEventRef.get()).isNotNull();
        });
        activeEvent = activeWatcherEventRef.get();
        emptyEvent = emptyWatcherEventRef.get();
        assertThat(emptyEvent).isNull();
        assertThat(activeEvent).isNotNull();
        long latestRevision = activeEvent.getHeader().getRevision();
        // verify the next progress notify brings both watchers to the latest revision
        activeWatcherEventRef.set(null);
        emptyWatcherEventRef.set(null);
        watchClient.requestProgress();
        await().atMost(TIME_OUT_SECONDS, TimeUnit.SECONDS).untilAsserted(() -> {
            assertThat(activeWatcherEventRef.get()).isNotNull();
            assertThat(emptyWatcherEventRef.get()).isNotNull();
        });
        activeEvent = activeWatcherEventRef.get();
        emptyEvent = emptyWatcherEventRef.get();
        assertThat(activeEvent).satisfies(WatchResponse::isProgressNotify);
        assertThat(emptyEvent).satisfies(WatchResponse::isProgressNotify);
        assertThat(activeEvent.getHeader().getRevision()).isEqualTo(emptyEvent.getHeader().getRevision()).isEqualTo(latestRevision);
    }
}
Also used : Watch(io.etcd.jetcd.Watch) Watcher(io.etcd.jetcd.Watch.Watcher) AtomicReference(java.util.concurrent.atomic.AtomicReference) WatchResponse(io.etcd.jetcd.watch.WatchResponse) ByteSequence(io.etcd.jetcd.ByteSequence) TestUtil.randomByteSequence(io.etcd.jetcd.impl.TestUtil.randomByteSequence) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

ByteSequence (io.etcd.jetcd.ByteSequence)12 Watcher (io.etcd.jetcd.Watch.Watcher)12 TestUtil.randomByteSequence (io.etcd.jetcd.impl.TestUtil.randomByteSequence)10 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)10 WatchResponse (io.etcd.jetcd.watch.WatchResponse)8 MethodSource (org.junit.jupiter.params.provider.MethodSource)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 Client (io.etcd.jetcd.Client)4 Watch (io.etcd.jetcd.Watch)4 WatchOption (io.etcd.jetcd.options.WatchOption)4 WatchEvent (io.etcd.jetcd.watch.WatchEvent)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Test (org.junit.jupiter.api.Test)2 Parameter (com.beust.jcommander.Parameter)1 Parameters (com.beust.jcommander.Parameters)1 Charsets (com.google.common.base.Charsets)1 KV (io.etcd.jetcd.KV)1 EtcdException (io.etcd.jetcd.common.exception.EtcdException)1 PutResponse (io.etcd.jetcd.kv.PutResponse)1 Optional (java.util.Optional)1