Search in sources :

Example 26 with ByteSequence

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;
    }
}
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 27 with ByteSequence

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);
    }
}
Also used : Op(io.etcd.jetcd.op.Op) CompletableFuture(java.util.concurrent.CompletableFuture) Cmp(io.etcd.jetcd.op.Cmp) Txn(io.etcd.jetcd.Txn) TxnResponse(io.etcd.jetcd.kv.TxnResponse) ByteSequence(io.etcd.jetcd.ByteSequence) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 28 with ByteSequence

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));
    }
}
Also used : List(java.util.List) ByteSequence(io.etcd.jetcd.ByteSequence) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 29 with ByteSequence

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();
    }
}
Also used : KV(io.etcd.jetcd.KV) Client(io.etcd.jetcd.Client) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) EtcdClusterExtension(io.etcd.jetcd.test.EtcdClusterExtension) FileInputStream(java.io.FileInputStream) TestUtil.bytesOf(io.etcd.jetcd.impl.TestUtil.bytesOf) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) ByteSequence(io.etcd.jetcd.ByteSequence) URI(java.net.URI) Timeout(org.junit.jupiter.api.Timeout) InputStream(java.io.InputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) KV(io.etcd.jetcd.KV) Client(io.etcd.jetcd.Client) URI(java.net.URI) ByteSequence(io.etcd.jetcd.ByteSequence) FileInputStream(java.io.FileInputStream) Test(org.junit.jupiter.api.Test)

Example 30 with ByteSequence

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

Aggregations

ByteSequence (io.etcd.jetcd.ByteSequence)40 Test (org.junit.jupiter.api.Test)24 TestUtil.randomByteSequence (io.etcd.jetcd.impl.TestUtil.randomByteSequence)17 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)14 Watcher (io.etcd.jetcd.Watch.Watcher)12 Client (io.etcd.jetcd.Client)11 WatchResponse (io.etcd.jetcd.watch.WatchResponse)8 MethodSource (org.junit.jupiter.params.provider.MethodSource)8 GetResponse (io.etcd.jetcd.kv.GetResponse)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 Watch (io.etcd.jetcd.Watch)6 TxnResponse (io.etcd.jetcd.kv.TxnResponse)6 Cmp (io.etcd.jetcd.op.Cmp)6 Txn (io.etcd.jetcd.Txn)5 CampaignResponse (io.etcd.jetcd.election.CampaignResponse)5 LeaderResponse (io.etcd.jetcd.election.LeaderResponse)4 GetOption (io.etcd.jetcd.options.GetOption)4 WatchOption (io.etcd.jetcd.options.WatchOption)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Election (io.etcd.jetcd.Election)3