Search in sources :

Example 1 with PutResponse

use of com.ibm.etcd.api.PutResponse in project etcd-java by IBM.

the class WatchTest method testResiliency.

@Test
public void testResiliency() throws Exception {
    try (EtcdClient directClient = EtcdClient.forEndpoint("localhost", 2379).withPlainText().build();
        EtcdClient client = EtcdClient.forEndpoint("localhost", 2396).withPlainText().build()) {
        directClient.getKvClient().delete(bs("watch-tr-test/")).asPrefix().sync();
        try (final LocalNettyProxy prox = new LocalNettyProxy(2396)) {
            Thread proxyThread = new Thread() {

                {
                    setDaemon(true);
                }

                @Override
                public void run() {
                    try {
                        int N = 4;
                        for (int i = 1; i <= N; i++) {
                            prox.start();
                            Thread.sleep(1000L + (long) (Math.random() * 5000));
                            System.out.println("killing proxy " + i);
                            // finish in running state
                            if (i < N)
                                prox.kill();
                            Thread.sleep((long) (Math.random() * 4000));
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };
            proxyThread.start();
            KvClient directKv = directClient.getKvClient();
            Phaser p = new Phaser(1);
            Map<ByteString, Object> watchedKeys = new ConcurrentHashMap<>();
            int i = 0;
            // the proxy is stopped/started
            while (proxyThread.isAlive()) {
                // put a key
                ByteString key = bs("watch-tr-test/" + Math.random());
                ByteString value = bs("value " + (i++));
                PutResponse pr = directKv.put(key, value).sync();
                watchedKeys.put(key, "pending");
                p.register();
                final int ii = i;
                AtomicReference<Watch> w = new AtomicReference<>();
                w.set(client.getKvClient().watch(key).startRevision(pr.getHeader().getRevision()).start((ListenerObserver<WatchUpdate>) (complete, wu, err) -> {
                    if (complete) {
                        if (err != null) {
                            watchedKeys.replace(key, err);
                            err.printStackTrace();
                        } else
                            watchedKeys.remove(key);
                        if (w.get() == null)
                            p.arrive();
                    } else if (!wu.getEvents().isEmpty()) {
                        if (value.equals(wu.getEvents().get(0).getKv().getValue())) {
                            if (ii % 2 == 0) {
                                // cancel every other watch
                                w.get().close();
                                w.set(null);
                            } else {
                                watchedKeys.remove(key);
                                p.arrive();
                            }
                        } else {
                            watchedKeys.replace(key, "unexpected watch event value");
                            p.arrive();
                        }
                    }
                }));
                Thread.sleep((long) (Math.random() * 500));
            }
            p.arrive();
            p.awaitAdvanceInterruptibly(0, 8, TimeUnit.SECONDS);
            System.out.println("created " + i + " watches; left incomplete: " + watchedKeys.size());
            watchedKeys.entrySet().forEach(e -> System.out.println("** " + e.getKey().toStringUtf8() + "=" + e.getValue()));
            assertTrue(watchedKeys.isEmpty());
        }
    }
}
Also used : ByteString(com.google.protobuf.ByteString) AtomicReference(java.util.concurrent.atomic.AtomicReference) PutResponse(com.ibm.etcd.api.PutResponse) WatchCreateException(com.ibm.etcd.client.watch.WatchCreateException) ExecutionException(java.util.concurrent.ExecutionException) NoSuchElementException(java.util.NoSuchElementException) Watch(com.ibm.etcd.client.kv.KvClient.Watch) KvClient(com.ibm.etcd.client.kv.KvClient) Phaser(java.util.concurrent.Phaser) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test)

Aggregations

ByteString (com.google.protobuf.ByteString)1 PutResponse (com.ibm.etcd.api.PutResponse)1 KvClient (com.ibm.etcd.client.kv.KvClient)1 Watch (com.ibm.etcd.client.kv.KvClient.Watch)1 WatchCreateException (com.ibm.etcd.client.watch.WatchCreateException)1 NoSuchElementException (java.util.NoSuchElementException)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ExecutionException (java.util.concurrent.ExecutionException)1 Phaser (java.util.concurrent.Phaser)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Test (org.junit.Test)1