use of io.etcd.jetcd.ByteSequence 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;
}
}
use of io.etcd.jetcd.ByteSequence in project jetcd by coreos.
the class KVNamespaceTest method testNestedTxn.
@Test
public void testNestedTxn() throws Exception {
// kvClient without namespace used as the judge for the final result
kvClient = TestUtil.client(cluster).build().getKVClient();
// kvClient with one namespace used to test operations with namespace
ByteSequence namespace = ByteSequence.from(TestUtil.randomByteSequence().concat(ByteSequence.NAMESPACE_DELIMITER).getBytes());
kvClientWithNamespace = TestUtil.client(cluster).namespace(namespace).build().getKVClient();
ByteSequence cmpKey1 = getNonexistentKey();
putKVWithAssertion(kvClient, cmpKey1, TestUtil.randomByteSequence(), null);
ByteSequence cmpKey2 = getNonexistentKey();
putKVWithAssertion(kvClientWithNamespace, cmpKey2, TestUtil.randomByteSequence(), null);
ByteSequence key1 = getNonexistentKey();
ByteSequence value1 = TestUtil.randomByteSequence();
putKVWithAssertion(kvClientWithNamespace, key1, value1, null);
ByteSequence key2 = getNonexistentKey();
ByteSequence value2 = TestUtil.randomByteSequence();
putKVWithAssertion(kvClientWithNamespace, key2, value2, null);
{
Txn txn = kvClientWithNamespace.txn();
ByteSequence nextValue1 = TestUtil.randomByteSequence();
CompletableFuture<TxnResponse> txnFuture = txn.If(new Cmp(cmpKey1, Cmp.Op.EQUAL, CmpTarget.version(0))).Then(Op.txn(new Cmp[] { new Cmp(cmpKey2, Cmp.Op.GREATER, CmpTarget.version(0)) }, new Op[] { Op.put(key1, nextValue1, PutOption.newBuilder().withPrevKV().build()) }, new Op[] { Op.put(key2, TestUtil.randomByteSequence(), PutOption.newBuilder().withPrevKV().build()) })).Else(Op.txn(new Cmp[] { new Cmp(cmpKey2, Cmp.Op.GREATER, CmpTarget.version(0)) }, new Op[] { Op.put(key2, TestUtil.randomByteSequence(), PutOption.newBuilder().withPrevKV().build()) }, new Op[] { Op.put(key1, TestUtil.randomByteSequence(), PutOption.newBuilder().withPrevKV().build()) })).commit();
TxnResponse response = txnFuture.get();
assertThat(response.getTxnResponses().size()).isEqualTo(1);
assertThat(response.getTxnResponses().get(0).getPutResponses().size()).isEqualTo(1);
assertThat(response.getTxnResponses().get(0).getPutResponses().get(0).hasPrevKv()).isTrue();
assertThat(response.getTxnResponses().get(0).getPutResponses().get(0).getPrevKv().getKey()).isEqualTo(key1);
assertThat(response.getTxnResponses().get(0).getPutResponses().get(0).getPrevKv().getValue()).isEqualTo(value1);
value1 = nextValue1;
assertExistentKey(kvClient, ByteSequence.from(namespace.concat(key1).getBytes()), value1);
}
}
use of io.etcd.jetcd.ByteSequence in project jetcd by coreos.
the class KVNamespaceTest method testKV.
@Test
public void testKV() throws Exception {
// kvClient without namespace used as the judge for the final result
kvClient = TestUtil.client(cluster).build().getKVClient();
// kvClient with one namespace used to test operations with namespace
ByteSequence namespace = ByteSequence.from(TestUtil.randomByteSequence().concat(ByteSequence.NAMESPACE_DELIMITER).getBytes());
kvClientWithNamespace = TestUtil.client(cluster).namespace(namespace).build().getKVClient();
// kvClient with another namespace used to test keyed segregation based on namespace
ByteSequence namespace2 = ByteSequence.from(TestUtil.randomByteSequence().concat(ByteSequence.NAMESPACE_DELIMITER).getBytes());
kvClientWithNamespace2 = TestUtil.client(cluster).namespace(namespace2).build().getKVClient();
// test single key
{
ByteSequence key = getNonexistentKey();
ByteSequence nsKey = ByteSequence.from(namespace.concat(key).getBytes());
ByteSequence value;
assertNonexistentKey(kvClient, nsKey);
assertNonexistentKey(kvClientWithNamespace, key);
assertNonexistentKey(kvClientWithNamespace2, key);
// 1. kvClient with namespace should not see keys without such prefix
value = TestUtil.randomByteSequence();
assertThat(putKVWithAssertion(kvClient, key, value, null)).isFalse();
assertExistentKey(kvClient, key, value);
assertNonexistentKey(kvClientWithNamespace, key);
assertNonexistentKey(kvClientWithNamespace2, key);
deleteKVWithAssertion(kvClient, key, value);
// 2. kvClient with namespace should see keys with such prefix
value = TestUtil.randomByteSequence();
assertThat(putKVWithAssertion(kvClient, nsKey, value, null)).isFalse();
assertExistentKey(kvClient, nsKey, value);
assertExistentKey(kvClientWithNamespace, key, value);
assertNonexistentKey(kvClientWithNamespace2, key);
// 3. put the same key using the client with namespace
ByteSequence prevValue = value;
value = TestUtil.randomByteSequence();
assertThat(putKVWithAssertion(kvClientWithNamespace, key, value, prevValue)).isTrue();
assertExistentKey(kvClient, nsKey, value);
assertExistentKey(kvClientWithNamespace, key, value);
assertNonexistentKey(kvClientWithNamespace2, key);
// 4. delete the key using client with namespace
deleteKVWithAssertion(kvClientWithNamespace, key, value);
}
// test range
{
// prepare KVs in root, "namespace" and "namespace2"
List<TestKeyValue> kvsOfNoNamespace = Arrays.asList(new TestKeyValue(getNonexistentKey(), TestUtil.randomByteSequence()), new TestKeyValue(getNonexistentKey(), TestUtil.randomByteSequence()), new TestKeyValue(getNonexistentKey(), TestUtil.randomByteSequence()));
putKVsWithAssertion(kvClient, kvsOfNoNamespace);
for (TestKeyValue keyValue : kvsOfNoNamespace) {
assertExistentKey(kvClient, keyValue.key, keyValue.value);
}
List<TestKeyValue> kvsOfNamespace = Arrays.asList(new TestKeyValue(getNonexistentKey(), TestUtil.randomByteSequence()), new TestKeyValue(getNonexistentKey(), TestUtil.randomByteSequence()), new TestKeyValue(getNonexistentKey(), TestUtil.randomByteSequence()), new TestKeyValue(getNonexistentKey(), TestUtil.randomByteSequence()));
putKVsWithAssertion(kvClientWithNamespace, kvsOfNamespace);
for (TestKeyValue keyValue : kvsOfNamespace) {
assertExistentKey(kvClientWithNamespace, keyValue.key, keyValue.value);
}
List<TestKeyValue> kvsOfNamespace2 = Arrays.asList(new TestKeyValue(getNonexistentKey(), TestUtil.randomByteSequence()), new TestKeyValue(getNonexistentKey(), TestUtil.randomByteSequence()), new TestKeyValue(getNonexistentKey(), TestUtil.randomByteSequence()), new TestKeyValue(getNonexistentKey(), TestUtil.randomByteSequence()), new TestKeyValue(getNonexistentKey(), TestUtil.randomByteSequence()));
putKVsWithAssertion(kvClientWithNamespace2, kvsOfNamespace2);
for (TestKeyValue keyValue : kvsOfNamespace2) {
assertExistentKey(kvClientWithNamespace2, keyValue.key, keyValue.value);
}
// get operations with namespace have been tested in previous cases, so here we only test
// get range operations.
assertExistentKVs(kvClient, kvsOfNoNamespace.get(0).key, END_KEY, kvsOfNoNamespace);
assertExistentKVs(kvClientWithNamespace, kvsOfNamespace.get(0).key, END_KEY, kvsOfNamespace);
assertExistentKVs(kvClientWithNamespace2, kvsOfNamespace2.get(0).key, END_KEY, kvsOfNamespace2);
assertExistentKVs(kvClient, kvsOfNoNamespace.get(0).key, kvsOfNoNamespace.get(2).key, kvsOfNoNamespace.subList(0, 2));
assertExistentKVs(kvClientWithNamespace, kvsOfNamespace.get(1).key, kvsOfNamespace.get(3).key, kvsOfNamespace.subList(1, 3));
assertExistentKVs(kvClientWithNamespace2, kvsOfNamespace2.get(1).key, kvsOfNamespace2.get(3).key, kvsOfNamespace2.subList(1, 3));
// test deletion with key range
// delete part of keys in each namespace
deleteKVsWithAssertion(kvClient, kvsOfNoNamespace.get(0).key, kvsOfNoNamespace.get(2).key, kvsOfNoNamespace.subList(0, 2));
deleteKVsWithAssertion(kvClientWithNamespace, kvsOfNamespace.get(1).key, kvsOfNamespace.get(3).key, kvsOfNamespace.subList(1, 3));
deleteKVsWithAssertion(kvClientWithNamespace2, kvsOfNamespace2.get(1).key, kvsOfNamespace2.get(3).key, kvsOfNamespace2.subList(1, 3));
// delete the rest of keys in each namespace
deleteKVsWithAssertion(kvClient, kvsOfNoNamespace.get(2).key, END_KEY, kvsOfNoNamespace.subList(2, 3));
deleteKVsWithAssertion(kvClientWithNamespace, kvsOfNamespace.get(3).key, END_KEY, kvsOfNamespace.subList(3, 4));
deleteKVsWithAssertion(kvClientWithNamespace2, kvsOfNamespace2.get(3).key, END_KEY, kvsOfNamespace2.subList(3, 5));
}
}
use of io.etcd.jetcd.ByteSequence in project jetcd by coreos.
the class SslTest method testSimpleSllSetup.
@Test
public void testSimpleSllSetup() throws Exception {
final ByteSequence key = bytesOf(TestUtil.randomString());
final ByteSequence val = bytesOf(TestUtil.randomString());
final String capath = System.getProperty("ssl.cert.capath");
final String authority = System.getProperty("ssl.cert.authority", DEFAULT_SSL_AUTHORITY);
final URI endpoint = new URI(System.getProperty("ssl.cert.endpoints", cluster.clientEndpoints().get(0).toString()));
try (InputStream is = Objects.nonNull(capath) ? new FileInputStream(capath) : getClass().getResourceAsStream(DEFAULT_SSL_CA_PATH)) {
Client client = Client.builder().endpoints(endpoint).authority(authority).sslContext(b -> b.trustManager(is)).build();
KV kv = client.getKVClient();
kv.put(key, val).join();
assertThat(kv.get(key).join().getCount()).isEqualTo(1);
assertThat(kv.get(key).join().getKvs().get(0).getValue()).isEqualTo(val);
kv.close();
client.close();
}
}
use of io.etcd.jetcd.ByteSequence 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);
}
}
Aggregations