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();
}
}
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;
}
}
}
}
}
Aggregations