Search in sources :

Example 36 with ByteSequence

use of io.etcd.jetcd.ByteSequence in project jetcd by coreos.

the class ElectionImpl method observe.

@Override
public void observe(ByteSequence electionName, Listener listener) {
    checkNotNull(electionName, "election name should not be null");
    checkNotNull(listener, "listener should not be null");
    LeaderRequest request = LeaderRequest.newBuilder().setName(ByteString.copyFrom(electionName.getBytes())).build();
    stub.observe(request).handler(value -> listener.onNext(new LeaderResponse(value, namespace))).endHandler(ignored -> listener.onCompleted()).exceptionHandler(error -> listener.onError(EtcdExceptionFactory.toEtcdException(error)));
}
Also used : ProclaimRequest(io.etcd.jetcd.api.ProclaimRequest) EtcdExceptionFactory.toEtcdException(io.etcd.jetcd.common.exception.EtcdExceptionFactory.toEtcdException) CampaignResponse(io.etcd.jetcd.election.CampaignResponse) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) VertxElectionGrpc(io.etcd.jetcd.api.VertxElectionGrpc) LeaderResponse(io.etcd.jetcd.election.LeaderResponse) CompletableFuture(java.util.concurrent.CompletableFuture) ProclaimResponse(io.etcd.jetcd.election.ProclaimResponse) StatusRuntimeException(io.grpc.StatusRuntimeException) ByteString(com.google.protobuf.ByteString) ResignRequest(io.etcd.jetcd.api.ResignRequest) CampaignRequest(io.etcd.jetcd.api.CampaignRequest) LeaderKey(io.etcd.jetcd.election.LeaderKey) NoLeaderException(io.etcd.jetcd.election.NoLeaderException) Util(io.etcd.jetcd.support.Util) ByteSequence(io.etcd.jetcd.ByteSequence) Election(io.etcd.jetcd.Election) ResignResponse(io.etcd.jetcd.election.ResignResponse) NotLeaderException(io.etcd.jetcd.election.NotLeaderException) LeaderRequest(io.etcd.jetcd.api.LeaderRequest) EtcdExceptionFactory(io.etcd.jetcd.common.exception.EtcdExceptionFactory) LeaderRequest(io.etcd.jetcd.api.LeaderRequest) LeaderResponse(io.etcd.jetcd.election.LeaderResponse)

Example 37 with ByteSequence

use of io.etcd.jetcd.ByteSequence in project jetcd by coreos.

the class AuthRoleGetResponse method toPermission.

private static Permission toPermission(io.etcd.jetcd.api.Permission perm) {
    ByteSequence key = ByteSequence.from(perm.getKey());
    ByteSequence rangeEnd = ByteSequence.from(perm.getRangeEnd());
    Permission.Type type;
    switch(perm.getPermType()) {
        case READ:
            type = Permission.Type.READ;
            break;
        case WRITE:
            type = Permission.Type.WRITE;
            break;
        case READWRITE:
            type = Permission.Type.READWRITE;
            break;
        default:
            type = Permission.Type.UNRECOGNIZED;
    }
    return new Permission(type, key, rangeEnd);
}
Also used : ByteSequence(io.etcd.jetcd.ByteSequence)

Example 38 with ByteSequence

use of io.etcd.jetcd.ByteSequence in project jetcd by coreos.

the class Requests method defineRangeRequestEnd.

private static void defineRangeRequestEnd(ByteSequence key, Optional<ByteSequence> endKeyOptional, boolean hasPrefix, ByteSequence namespace, Consumer<ByteString> setRangeEndConsumer) {
    if (endKeyOptional.isPresent()) {
        setRangeEndConsumer.accept(Util.prefixNamespaceToRangeEnd(ByteString.copyFrom(endKeyOptional.get().getBytes()), namespace));
    } else {
        if (hasPrefix) {
            ByteSequence endKey = OptionsUtil.prefixEndOf(key);
            setRangeEndConsumer.accept(Util.prefixNamespaceToRangeEnd(ByteString.copyFrom(endKey.getBytes()), namespace));
        }
    }
}
Also used : ByteSequence(io.etcd.jetcd.ByteSequence)

Example 39 with ByteSequence

use of io.etcd.jetcd.ByteSequence in project jetcd by coreos.

the class WatchTest method testWatchCompacted.

@ParameterizedTest
@MethodSource("parameters")
public void testWatchCompacted(final Client client) throws Exception {
    final ByteSequence key = randomByteSequence();
    final AtomicReference<Throwable> ref = new AtomicReference<>();
    // Try to listen from previous revision on
    final WatchOption options = WatchOption.newBuilder().withRevision(getCompactedRevision(client, key)).build();
    final Watch wc = client.getWatchClient();
    try (Watcher watcher = wc.watch(key, options, Watch.listener(TestUtil::noOpWatchResponseConsumer, ref::set))) {
        await().atMost(TIME_OUT_SECONDS, TimeUnit.SECONDS).untilAsserted(() -> assertThat(ref.get()).isNotNull());
        assertThat(ref.get().getClass()).isEqualTo(CompactedException.class);
    }
}
Also used : Watch(io.etcd.jetcd.Watch) Watcher(io.etcd.jetcd.Watch.Watcher) AtomicReference(java.util.concurrent.atomic.AtomicReference) 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)

Example 40 with ByteSequence

use of io.etcd.jetcd.ByteSequence in project dubbo by alibaba.

the class JEtcdClientTest method test_watch_when_modify.

@Test
public void test_watch_when_modify() {
    String path = "/dubbo/config/jetcd-client-unit-test/configurators";
    String endpoint = "http://127.0.0.1:2379";
    CountDownLatch latch = new CountDownLatch(1);
    ByteSequence key = ByteSequence.from(path, UTF_8);
    Watch.Listener listener = Watch.listener(response -> {
        for (WatchEvent event : response.getEvents()) {
            Assertions.assertEquals("PUT", event.getEventType().toString());
            Assertions.assertEquals(path, event.getKeyValue().getKey().toString(UTF_8));
            Assertions.assertEquals("Hello", event.getKeyValue().getValue().toString(UTF_8));
            latch.countDown();
        }
    });
    try (Client client = Client.builder().endpoints(endpoint).build();
        Watch watch = client.getWatchClient();
        Watch.Watcher watcher = watch.watch(key, listener)) {
        // try to modify the key
        client.getKVClient().put(ByteSequence.from(path, UTF_8), ByteSequence.from("Hello", UTF_8));
        latch.await();
    } catch (Exception e) {
        Assertions.fail(e.getMessage());
    }
}
Also used : Watch(io.etcd.jetcd.Watch) ByteString(com.google.protobuf.ByteString) WatchEvent(io.etcd.jetcd.watch.WatchEvent) CountDownLatch(java.util.concurrent.CountDownLatch) Client(io.etcd.jetcd.Client) ByteSequence(io.etcd.jetcd.ByteSequence) ClosedClientException(io.etcd.jetcd.common.exception.ClosedClientException) Test(org.junit.jupiter.api.Test)

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