Search in sources :

Example 1 with KeyValue

use of io.etcd.jetcd.KeyValue 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 KeyValue

use of io.etcd.jetcd.KeyValue 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 KeyValue

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

the class ElectionTest method testProposeValueNotBeingLeader.

@Test
public void testProposeValueNotBeingLeader() throws Exception {
    ByteSequence electionName = ByteSequence.from(randomString(), StandardCharsets.UTF_8);
    LeaderKey leaderKey = new LeaderKey(electionName, randomByteSequence(), 1, 1);
    ByteSequence proposal = ByteSequence.from("proposal", StandardCharsets.UTF_8);
    try {
        electionClient.proclaim(leaderKey, proposal).get(OPERATION_TIMEOUT, TimeUnit.SECONDS);
        fail("Cannot proclaim proposal not being a leader");
    } catch (ExecutionException e) {
        assertThat(e.getCause()).isInstanceOf(NotLeaderException.class);
    }
    GetOption getOption = GetOption.newBuilder().isPrefix(true).build();
    List<KeyValue> keys = kvClient.get(electionName, getOption).get().getKvs();
    assertThat(keys).isEmpty();
}
Also used : NotLeaderException(io.etcd.jetcd.election.NotLeaderException) KeyValue(io.etcd.jetcd.KeyValue) GetOption(io.etcd.jetcd.options.GetOption) LeaderKey(io.etcd.jetcd.election.LeaderKey) ExecutionException(java.util.concurrent.ExecutionException) ByteSequence(io.etcd.jetcd.ByteSequence) TestUtil.randomByteSequence(io.etcd.jetcd.impl.TestUtil.randomByteSequence) Test(org.junit.jupiter.api.Test)

Example 4 with KeyValue

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

the class KVNamespaceTest method deleteKVsWithAssertion.

private static void deleteKVsWithAssertion(KV kvClient, ByteSequence key, ByteSequence end, List<TestKeyValue> previousKVs) throws Exception {
    CompletableFuture<DeleteResponse> deleteFuture = kvClient.delete(key, DeleteOption.newBuilder().withRange(end).withPrevKV(true).build());
    DeleteResponse deleteResponse = deleteFuture.get();
    assertThat(deleteResponse.getDeleted()).isEqualTo(previousKVs.size());
    assertThat(deleteResponse.getPrevKvs().size()).isEqualTo(previousKVs.size());
    for (KeyValue keyValue : deleteResponse.getPrevKvs()) {
        boolean exist = false;
        for (TestKeyValue previousKV : previousKVs) {
            if (previousKV.key.equals(keyValue.getKey())) {
                exist = true;
                assertThat(keyValue.getValue()).isEqualTo(previousKV.value);
                break;
            }
        }
        assertThat(exist).isTrue();
    }
}
Also used : DeleteResponse(io.etcd.jetcd.kv.DeleteResponse) KeyValue(io.etcd.jetcd.KeyValue)

Example 5 with KeyValue

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

the class KVNamespaceTest method assertExistentKVs.

private static void assertExistentKVs(KV kvClient, ByteSequence key, ByteSequence end, List<TestKeyValue> expectedKVs) throws Exception {
    CompletableFuture<GetResponse> getFuture = kvClient.get(key, GetOption.newBuilder().withRange(end).build());
    GetResponse getResponse = getFuture.get();
    assertThat(getResponse.getKvs().size()).isEqualTo(expectedKVs.size());
    for (KeyValue keyValue : getResponse.getKvs()) {
        boolean exist = false;
        for (TestKeyValue expectedKV : expectedKVs) {
            if (expectedKV.key.equals(keyValue.getKey())) {
                exist = true;
                assertThat(keyValue.getValue()).isEqualTo(expectedKV.value);
                break;
            }
        }
        assertThat(exist).isTrue();
    }
}
Also used : KeyValue(io.etcd.jetcd.KeyValue) GetResponse(io.etcd.jetcd.kv.GetResponse)

Aggregations

KeyValue (io.etcd.jetcd.KeyValue)5 ByteSequence (io.etcd.jetcd.ByteSequence)3 TestUtil.randomByteSequence (io.etcd.jetcd.impl.TestUtil.randomByteSequence)3 GetOption (io.etcd.jetcd.options.GetOption)3 Test (org.junit.jupiter.api.Test)3 CampaignResponse (io.etcd.jetcd.election.CampaignResponse)2 LeaderResponse (io.etcd.jetcd.election.LeaderResponse)2 LeaderKey (io.etcd.jetcd.election.LeaderKey)1 NotLeaderException (io.etcd.jetcd.election.NotLeaderException)1 DeleteResponse (io.etcd.jetcd.kv.DeleteResponse)1 GetResponse (io.etcd.jetcd.kv.GetResponse)1 ExecutionException (java.util.concurrent.ExecutionException)1