Search in sources :

Example 1 with ByteSequence

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

the class ElectionTest method testRetryCampaignWithDifferentValue.

@Test
public void testRetryCampaignWithDifferentValue() throws Exception {
    ByteSequence electionName = ByteSequence.from(randomString(), StandardCharsets.UTF_8);
    long leaseId = leaseClient.grant(10).get().getID();
    ByteSequence firstProposal = ByteSequence.from("proposal1", StandardCharsets.UTF_8);
    CampaignResponse campaignResponse1 = electionClient.campaign(electionName, leaseId, firstProposal).get(OPERATION_TIMEOUT, TimeUnit.SECONDS);
    ByteSequence secondProposal = ByteSequence.from("proposal2", StandardCharsets.UTF_8);
    CampaignResponse campaignResponse2 = electionClient.campaign(electionName, leaseId, secondProposal).get(OPERATION_TIMEOUT, TimeUnit.SECONDS);
    // check that for sure we are the leader
    LeaderResponse leaderResponse = electionClient.leader(electionName).get(OPERATION_TIMEOUT, TimeUnit.SECONDS);
    assertThat(leaderResponse.getKv().getKey()).isEqualTo(campaignResponse1.getLeader().getKey());
    assertThat(campaignResponse1.getLeader().getKey()).isEqualTo(campaignResponse2.getLeader().getKey());
    assertThat(campaignResponse1.getLeader().getRevision()).isEqualTo(campaignResponse2.getLeader().getRevision());
    // latest proposal should be persisted
    GetOption getOption = GetOption.newBuilder().isPrefix(true).build();
    List<KeyValue> keys = kvClient.get(electionName, getOption).get().getKvs();
    assertThat(keys.size()).isEqualTo(1);
    assertThat(keys.get(0).getValue()).isEqualTo(secondProposal);
    electionClient.resign(campaignResponse1.getLeader()).get(OPERATION_TIMEOUT, TimeUnit.SECONDS);
    leaseClient.revoke(leaseId).get();
}
Also used : KeyValue(io.etcd.jetcd.KeyValue) CampaignResponse(io.etcd.jetcd.election.CampaignResponse) GetOption(io.etcd.jetcd.options.GetOption) LeaderResponse(io.etcd.jetcd.election.LeaderResponse) ByteSequence(io.etcd.jetcd.ByteSequence) TestUtil.randomByteSequence(io.etcd.jetcd.impl.TestUtil.randomByteSequence) Test(org.junit.jupiter.api.Test)

Example 2 with ByteSequence

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

the class ElectionTest method testIsolatedElection.

@Test
public void testIsolatedElection() throws Exception {
    ByteSequence electionName = ByteSequence.from(randomString(), StandardCharsets.UTF_8);
    // register lease
    long leaseId = leaseClient.grant(10).get().getID();
    // start new campaign
    ByteSequence firstProposal = ByteSequence.from("proposal1", StandardCharsets.UTF_8);
    CampaignResponse campaignResponse = electionClient.campaign(electionName, leaseId, firstProposal).get(OPERATION_TIMEOUT, TimeUnit.SECONDS);
    assertThat(campaignResponse.getLeader()).isNotNull();
    assertThat(campaignResponse.getLeader().getLease()).isEqualTo(leaseId);
    assertThat(campaignResponse.getLeader().getName()).isEqualTo(electionName);
    // election is backed by standard key in etcd. let us examine it
    GetOption getOption = GetOption.newBuilder().isPrefix(true).build();
    List<KeyValue> keys = kvClient.get(electionName, getOption).get().getKvs();
    assertThat(keys.size()).isEqualTo(1);
    assertThat(keys.get(0).getKey().toString()).isEqualTo(campaignResponse.getLeader().getKey().toString());
    assertThat(keys.get(0).getValue()).isEqualTo(firstProposal);
    // check that we really are the leader (just to test API)
    LeaderResponse leaderResponse = electionClient.leader(electionName).get(OPERATION_TIMEOUT, TimeUnit.SECONDS);
    assertThat(leaderResponse.getKv().getKey()).isEqualTo(campaignResponse.getLeader().getKey());
    assertThat(leaderResponse.getKv().getValue()).isEqualTo(firstProposal);
    assertThat(leaderResponse.getKv().getLease()).isEqualTo(leaseId);
    assertThat(leaderResponse.getKv().getCreateRevision()).isEqualTo(campaignResponse.getLeader().getRevision());
    // as a leader change your proposal
    ByteSequence secondProposal = ByteSequence.from("proposal2", StandardCharsets.UTF_8);
    electionClient.proclaim(campaignResponse.getLeader(), secondProposal).get(OPERATION_TIMEOUT, TimeUnit.SECONDS);
    keys = kvClient.get(electionName, getOption).get().getKvs();
    assertThat(keys.size()).isEqualTo(1);
    assertThat(keys.get(0).getValue()).isEqualTo(secondProposal);
    // finally resign
    electionClient.resign(campaignResponse.getLeader()).get(OPERATION_TIMEOUT, TimeUnit.SECONDS);
    keys = kvClient.get(electionName, getOption).get().getKvs();
    assertThat(keys).isEmpty();
    leaseClient.revoke(leaseId).get();
}
Also used : KeyValue(io.etcd.jetcd.KeyValue) CampaignResponse(io.etcd.jetcd.election.CampaignResponse) GetOption(io.etcd.jetcd.options.GetOption) LeaderResponse(io.etcd.jetcd.election.LeaderResponse) ByteSequence(io.etcd.jetcd.ByteSequence) TestUtil.randomByteSequence(io.etcd.jetcd.impl.TestUtil.randomByteSequence) Test(org.junit.jupiter.api.Test)

Example 3 with ByteSequence

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

the class KVTest method putKeysWithPrefix.

private static void putKeysWithPrefix(String prefix, int numPrefixes) throws ExecutionException, InterruptedException {
    for (int i = 0; i < numPrefixes; i++) {
        ByteSequence key = bytesOf(prefix + i);
        ByteSequence value = bytesOf("" + i);
        kvClient.put(key, value).get();
    }
}
Also used : ByteSequence(io.etcd.jetcd.ByteSequence)

Example 4 with ByteSequence

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

the class KVTest method testKVClientCanRetryPutOnEtcdRestart.

@Test
@SuppressWarnings("FutureReturnValueIgnored")
public void testKVClientCanRetryPutOnEtcdRestart() throws InterruptedException {
    try (Client customClient = TestUtil.client(cluster).retryMaxDuration(Duration.ofMinutes(5)).retryDelay(10).retryMaxDelay(30).retryChronoUnit(ChronoUnit.SECONDS).build()) {
        ByteSequence key = ByteSequence.from("retry_dummy_key", StandardCharsets.UTF_8);
        int putCount = 1000;
        ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
        // start putting values in a sequence, at least on of them has to be retried
        executor.submit(() -> {
            for (int i = 0; i < putCount; ++i) {
                ByteSequence value = ByteSequence.from(Integer.toString(i), StandardCharsets.UTF_8);
                customClient.getKVClient().put(key, value).join();
            }
        });
        // restart the cluster while uploading
        executor.schedule(() -> cluster.restart(), 100, TimeUnit.MILLISECONDS);
        executor.shutdown();
        assertThat(executor.awaitTermination(30, TimeUnit.SECONDS)).isTrue();
        GetResponse getResponse = kvClient.get(key).join();
        assertThat(getResponse.getKvs().size()).as("There should be exactly one KeyValue for the test key").isEqualTo(1);
        ByteSequence lastPutValue = ByteSequence.from(Integer.toString(putCount - 1), StandardCharsets.UTF_8);
        assertThat(getResponse.getKvs().get(0).getValue()).as("The sequence of put operations should finish successfully. " + "Last seen value should match the expected value.").isEqualTo(lastPutValue);
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Client(io.etcd.jetcd.Client) GetResponse(io.etcd.jetcd.kv.GetResponse) ByteSequence(io.etcd.jetcd.ByteSequence) Test(org.junit.jupiter.api.Test)

Example 5 with ByteSequence

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

the class KVTest method testDelete.

@Test
public void testDelete() throws Exception {
    // Put content so that we actually have something to delete
    testPut();
    ByteSequence keyToDelete = SAMPLE_KEY;
    // count keys about to delete
    CompletableFuture<GetResponse> getFeature = kvClient.get(keyToDelete);
    GetResponse resp = getFeature.get();
    // delete the keys
    CompletableFuture<DeleteResponse> deleteFuture = kvClient.delete(keyToDelete);
    DeleteResponse delResp = deleteFuture.get();
    assertThat(delResp.getDeleted()).isEqualTo(resp.getKvs().size());
}
Also used : DeleteResponse(io.etcd.jetcd.kv.DeleteResponse) GetResponse(io.etcd.jetcd.kv.GetResponse) ByteSequence(io.etcd.jetcd.ByteSequence) 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