Search in sources :

Example 1 with LeaderResponse

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

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

use of io.etcd.jetcd.election.LeaderResponse in project jetcd by coreos.

the class ElectionImpl method leader.

@Override
public CompletableFuture<LeaderResponse> leader(ByteSequence electionName) {
    checkNotNull(electionName, "election name should not be null");
    LeaderRequest request = LeaderRequest.newBuilder().setName(Util.prefixNamespace(electionName, namespace)).build();
    return completable(stub.leader(request), r -> new LeaderResponse(r, namespace), this::convertException);
}
Also used : LeaderRequest(io.etcd.jetcd.api.LeaderRequest) LeaderResponse(io.etcd.jetcd.election.LeaderResponse)

Example 4 with LeaderResponse

use of io.etcd.jetcd.election.LeaderResponse in project jetcd by coreos.

the class ElectionTest method testObserveElections.

@Test
public void testObserveElections() throws Exception {
    int electionCount = 3;
    final AtomicInteger electionsSeen = new AtomicInteger(0);
    ByteSequence electionName = ByteSequence.from(randomString(), StandardCharsets.UTF_8);
    electionClient.observe(electionName, new Election.Listener() {

        @Override
        public void onNext(LeaderResponse response) {
            electionsSeen.incrementAndGet();
        }

        @Override
        public void onError(Throwable error) {
        }

        @Override
        public void onCompleted() {
        }
    });
    long leaseId = leaseClient.grant(10).get().getID();
    for (int i = 0; i < electionCount; ++i) {
        ByteSequence proposal = ByteSequence.from(randomString(), StandardCharsets.UTF_8);
        CampaignResponse campaignResponse = electionClient.campaign(electionName, leaseId, proposal).get(OPERATION_TIMEOUT, TimeUnit.SECONDS);
        Thread.sleep(100);
        electionClient.resign(campaignResponse.getLeader()).get(OPERATION_TIMEOUT, TimeUnit.SECONDS);
    }
    TestUtil.waitForCondition(() -> electionsSeen.get() == electionCount, OPERATION_TIMEOUT * 1000, "Observer did not receive expected notifications, got: " + electionsSeen.get());
    leaseClient.revoke(leaseId).get();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CampaignResponse(io.etcd.jetcd.election.CampaignResponse) LeaderResponse(io.etcd.jetcd.election.LeaderResponse) Election(io.etcd.jetcd.Election) ByteSequence(io.etcd.jetcd.ByteSequence) TestUtil.randomByteSequence(io.etcd.jetcd.impl.TestUtil.randomByteSequence) Test(org.junit.jupiter.api.Test)

Example 5 with LeaderResponse

use of io.etcd.jetcd.election.LeaderResponse 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)

Aggregations

LeaderResponse (io.etcd.jetcd.election.LeaderResponse)5 ByteSequence (io.etcd.jetcd.ByteSequence)4 CampaignResponse (io.etcd.jetcd.election.CampaignResponse)4 TestUtil.randomByteSequence (io.etcd.jetcd.impl.TestUtil.randomByteSequence)3 Test (org.junit.jupiter.api.Test)3 Election (io.etcd.jetcd.Election)2 KeyValue (io.etcd.jetcd.KeyValue)2 LeaderRequest (io.etcd.jetcd.api.LeaderRequest)2 GetOption (io.etcd.jetcd.options.GetOption)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 ByteString (com.google.protobuf.ByteString)1 CampaignRequest (io.etcd.jetcd.api.CampaignRequest)1 ProclaimRequest (io.etcd.jetcd.api.ProclaimRequest)1 ResignRequest (io.etcd.jetcd.api.ResignRequest)1 VertxElectionGrpc (io.etcd.jetcd.api.VertxElectionGrpc)1 EtcdExceptionFactory (io.etcd.jetcd.common.exception.EtcdExceptionFactory)1 EtcdExceptionFactory.toEtcdException (io.etcd.jetcd.common.exception.EtcdExceptionFactory.toEtcdException)1 LeaderKey (io.etcd.jetcd.election.LeaderKey)1 NoLeaderException (io.etcd.jetcd.election.NoLeaderException)1 NotLeaderException (io.etcd.jetcd.election.NotLeaderException)1