Search in sources :

Example 1 with WatchIterator

use of com.ibm.etcd.client.kv.KvClient.WatchIterator in project etcd-java by IBM.

the class WatchTest method testWatch.

@Test
public void testWatch() throws Exception {
    proxy.start();
    try (KvStoreClient directClient = EtcdClient.forEndpoint("localhost", 2379).withPlainText().build();
        KvStoreClient client = EtcdClient.forEndpoint("localhost", 2390).withPlainText().build()) {
        KvClient kvc = client.getKvClient();
        long start = System.currentTimeMillis();
        final BlockingQueue<Object> watchEvents = new LinkedBlockingQueue<>();
        final Object COMPLETED = new Object();
        final StreamObserver<WatchUpdate> observer = new StreamObserver<WatchUpdate>() {

            @Override
            public void onNext(WatchUpdate value) {
                System.out.println(t(start) + "watch event: " + value);
                watchEvents.add(value);
            }

            @Override
            public void onError(Throwable t) {
                System.out.println(t(start) + "watch error: " + t);
                watchEvents.add(t);
            }

            @Override
            public void onCompleted() {
                System.out.println(t(start) + "watch completed");
                watchEvents.add(COMPLETED);
            }
        };
        Watch watch = kvc.watch(bs("/watchtest")).asPrefix().start(observer);
        // assertFalse(watch.isDone());
        // test blocking watch at the same time
        WatchIterator watchIterator = kvc.watch(bs("/watchtest")).asPrefix().start();
        assertTrue(watch.get(1, TimeUnit.SECONDS));
        kvc.put(bs("/watchtest/a"), bs("a value")).sync();
        directClient.getKvClient().put(bs("/watchtest/b"), bs("b value")).sync();
        WatchUpdate wu = getNextSkipEmpty(watchEvents);
        assertNotNull(wu);
        assertEquals("event: " + wu, 1, wu.getEvents().size());
        assertEquals(EventType.PUT, wu.getEvents().get(0).getType());
        assertEquals(bs("a value"), wu.getEvents().get(0).getKv().getValue());
        assertEquals(bs("/watchtest/a"), wu.getEvents().get(0).getKv().getKey());
        WatchUpdate wu2 = getNextSkipEmpty(watchIterator);
        assertEquals(EventType.PUT, wu2.getEvents().get(0).getType());
        assertEquals(bs("a value"), wu2.getEvents().get(0).getKv().getValue());
        assertEquals(bs("/watchtest/a"), wu2.getEvents().get(0).getKv().getKey());
        watchEvents.poll(500, TimeUnit.MILLISECONDS);
        watch.close();
        assertEquals(COMPLETED, watchEvents.poll(500, TimeUnit.MILLISECONDS));
        kvc.put(bs("/watchtest/c"), bs("c value")).sync();
        assertNull(watchEvents.poll(500, TimeUnit.MILLISECONDS));
        assertTrue(watchIterator.hasNext());
        assertTrue(watchIterator.hasNext());
        assertEquals(bs("b value"), watchIterator.next().getEvents().get(0).getKv().getValue());
        assertEquals(EventType.PUT, watchIterator.next().getEvents().get(0).getType());
        // fresh new watch
        watch = kvc.watch(ByteString.copyFromUtf8("/watchtest")).asPrefix().start(observer);
        assertTrue(watch.get(1, TimeUnit.SECONDS));
        kvc.batch().put(kvc.put(bs("/watchtest/d"), bs("d value")).asRequest()).delete(kvc.delete(bs("/watchtest/a")).asRequest()).sync();
        wu = getNextSkipEmpty(watchEvents);
        assertEquals(2, wu.getEvents().size());
        assertTrue(wu.getEvents().stream().anyMatch(e -> e.getType() == EventType.DELETE));
        assertTrue(wu.getEvents().stream().anyMatch(e -> e.getType() == EventType.PUT));
        // kill path to server
        proxy.kill();
        Thread.sleep(1000L);
        // while disconnected, put a new key
        directClient.getKvClient().put(bs("/watchtest/e"), bs("e value")).sync();
        Thread.sleep(1000L);
        // reinstate path to server
        proxy.start();
        // watch should be unaffected - next event seen should be the missed one
        wu = (WatchUpdate) watchEvents.poll(3000L, TimeUnit.MILLISECONDS);
        assertEquals(bs("/watchtest/e"), wu.getEvents().get(0).getKv().getKey());
        watch.close();
        // (earlier batch update)
        assertEquals(2, watchIterator.next().getEvents().size());
        assertEquals(bs("/watchtest/e"), watchIterator.next().getEvents().get(0).getKv().getKey());
        watchIterator.close();
        assertNull(watchIterator.next().getHeader());
        assertFalse(watchIterator.hasNext());
        try {
            watchIterator.next();
            fail("should throw NSEE here");
        } catch (NoSuchElementException nsee) {
        }
    } finally {
        proxy.kill();
    }
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) AfterClass(org.junit.AfterClass) WatchUpdate(com.ibm.etcd.client.kv.WatchUpdate) BeforeClass(org.junit.BeforeClass) Watch(com.ibm.etcd.client.kv.KvClient.Watch) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) KvClient(com.ibm.etcd.client.kv.KvClient) BlockingQueue(java.util.concurrent.BlockingQueue) Test(org.junit.Test) WatchIterator(com.ibm.etcd.client.kv.KvClient.WatchIterator) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) AtomicReference(java.util.concurrent.atomic.AtomicReference) WatchCreateException(com.ibm.etcd.client.watch.WatchCreateException) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) ByteString(com.google.protobuf.ByteString) StreamObserver(io.grpc.stub.StreamObserver) Map(java.util.Map) Phaser(java.util.concurrent.Phaser) EventType(com.ibm.etcd.api.Event.EventType) KvTest.bs(com.ibm.etcd.client.KvTest.bs) Assert(org.junit.Assert) NoSuchElementException(java.util.NoSuchElementException) PutResponse(com.ibm.etcd.api.PutResponse) KvTest.t(com.ibm.etcd.client.KvTest.t) WatchUpdate(com.ibm.etcd.client.kv.WatchUpdate) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Watch(com.ibm.etcd.client.kv.KvClient.Watch) KvClient(com.ibm.etcd.client.kv.KvClient) NoSuchElementException(java.util.NoSuchElementException) WatchIterator(com.ibm.etcd.client.kv.KvClient.WatchIterator) Test(org.junit.Test)

Example 2 with WatchIterator

use of com.ibm.etcd.client.kv.KvClient.WatchIterator in project etcd-java by IBM.

the class PersistentLeaseKeyTest method testLeaseKey.

@Test
public void testLeaseKey() throws Exception {
    try (EtcdClient client = EtcdClient.forEndpoint("localhost", 2392).withPlainText().build();
        EtcdClient directClient = EtcdClient.forEndpoint("localhost", 2379).withPlainText().build()) {
        KvClient directKvClient = directClient.getKvClient();
        ByteString mykey = bs("mykeyy");
        PersistentLeaseKey plk = new PersistentLeaseKey(client, mykey, bs("defaultdata"), null);
        ListenableFuture<ByteString> startFuture = plk.startWithFuture();
        Thread.sleep(300L);
        // network conn to server not established yet
        assertFalse(startFuture.isDone());
        // key won't exist yet
        assertEquals(0, directKvClient.get(mykey).countOnly().sync().getCount());
        // reestablish network
        proxy.start();
        assertEquals(mykey, startFuture.get(3000, MILLISECONDS));
        // check key is present via other client
        assertEquals(bs("defaultdata"), directKvClient.get(mykey).sync().getKvs(0).getValue());
        plk.closeWithFuture().get(500, MILLISECONDS);
        // key should now be gone
        assertEquals(0, directKvClient.get(bs("mykeyy")).countOnly().sync().getCount());
        // -----------------
        PersistentLease pl = client.getLeaseClient().maintain().minTtl(2).start();
        System.out.println("PL state: " + pl.getState());
        plk = new PersistentLeaseKey(client, pl, mykey, bs("somedata"), null);
        plk.start();
        assertFalse(pl.isDone());
        Long leaseId = pl.get(1, TimeUnit.SECONDS);
        assertNotNull(leaseId);
        System.out.println("PL state: " + pl.getState());
        // will take a small amount of time after lease is created for PLK to be
        // created
        assertFalse(plk.isDone());
        plk.get(1, TimeUnit.SECONDS);
        KeyValue kv = directKvClient.get(mykey).sync().getKvs(0);
        assertEquals(bs("somedata"), kv.getValue());
        assertEquals((long) leaseId, kv.getLease());
        plk.setDefaultValue(bs("updateddata"));
        Thread.sleep(200L);
        // data doesn't change until key has to be recreated
        assertEquals(bs("somedata"), directKvClient.get(mykey).sync().getKvs(0).getValue());
        proxy.kill();
        long ttl = pl.getCurrentTtlSecs();
        System.out.println("TTL after kill is " + ttl);
        Thread.sleep(1000L);
        // key should still be there (lease not yet expired)
        kv = directKvClient.get(mykey).sync().getKvs(0);
        assertEquals(bs("somedata"), kv.getValue());
        assertEquals((long) leaseId, kv.getLease());
        Thread.sleep((pl.getCurrentTtlSecs() + 2) * 1000L);
        // lease should have now expired and key should be gone
        assertEquals(0, directKvClient.get(bs("mykeyy")).sync().getCount());
        proxy.start();
        long before = System.nanoTime();
        try (WatchIterator it = directKvClient.watch(bs("mykeyy")).start()) {
            for (int i = 0; i < 5; i++) {
                List<Event> events = it.next().getEvents();
                if (!events.isEmpty()) {
                    // key should be updated with new value once
                    // connection is reestablished
                    assertEquals(bs("updateddata"), events.get(0).getKv().getValue());
                    System.out.println("took " + (System.nanoTime() - before) / 1000_000L + "ms for key to re-appear");
                    break;
                }
            }
        }
    }
}
Also used : KeyValue(com.ibm.etcd.api.KeyValue) ByteString(com.google.protobuf.ByteString) EtcdClient(com.ibm.etcd.client.EtcdClient) KvClient(com.ibm.etcd.client.kv.KvClient) PersistentLease(com.ibm.etcd.client.lease.PersistentLease) Event(com.ibm.etcd.api.Event) WatchIterator(com.ibm.etcd.client.kv.KvClient.WatchIterator) Test(org.junit.Test)

Aggregations

ByteString (com.google.protobuf.ByteString)2 KvClient (com.ibm.etcd.client.kv.KvClient)2 WatchIterator (com.ibm.etcd.client.kv.KvClient.WatchIterator)2 Test (org.junit.Test)2 Event (com.ibm.etcd.api.Event)1 EventType (com.ibm.etcd.api.Event.EventType)1 KeyValue (com.ibm.etcd.api.KeyValue)1 PutResponse (com.ibm.etcd.api.PutResponse)1 EtcdClient (com.ibm.etcd.client.EtcdClient)1 KvTest.bs (com.ibm.etcd.client.KvTest.bs)1 KvTest.t (com.ibm.etcd.client.KvTest.t)1 Watch (com.ibm.etcd.client.kv.KvClient.Watch)1 WatchUpdate (com.ibm.etcd.client.kv.WatchUpdate)1 PersistentLease (com.ibm.etcd.client.lease.PersistentLease)1 WatchCreateException (com.ibm.etcd.client.watch.WatchCreateException)1 StreamObserver (io.grpc.stub.StreamObserver)1 Map (java.util.Map)1 NoSuchElementException (java.util.NoSuchElementException)1 BlockingQueue (java.util.concurrent.BlockingQueue)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1