Search in sources :

Example 1 with PaxosLearner

use of com.palantir.paxos.PaxosLearner in project atlasdb by palantir.

the class PaxosLeaderElectionService method updateLearnedStateFromPeers.

/**
 * Queries all other learners for unknown learned values
 *
 * @returns true if new state was learned, otherwise false
 */
public boolean updateLearnedStateFromPeers(Optional<PaxosValue> greatestLearned) {
    final long nextToLearnSeq = greatestLearned.map(value -> value.getRound()).orElse(PaxosAcceptor.NO_LOG_ENTRY) + 1;
    List<PaxosUpdate> updates = PaxosQuorumChecker.<PaxosLearner, PaxosUpdate>collectQuorumResponses(learners, new Function<PaxosLearner, PaxosUpdate>() {

        @Override
        @Nullable
        public PaxosUpdate apply(@Nullable PaxosLearner learner) {
            return new PaxosUpdate(copyOf(learner.getLearnedValuesSince(nextToLearnSeq)));
        }
    }, proposer.getQuorumSize(), executor, PaxosQuorumChecker.DEFAULT_REMOTE_REQUESTS_TIMEOUT_IN_SECONDS);
    // learn the state accumulated from peers
    boolean learned = false;
    for (PaxosUpdate update : updates) {
        ImmutableCollection<PaxosValue> values = update.getValues();
        for (PaxosValue value : values) {
            PaxosValue currentLearnedValue = knowledge.getLearnedValue(value.getRound());
            if (currentLearnedValue == null) {
                knowledge.learn(value.getRound(), value);
                learned = true;
            }
        }
    }
    return learned;
}
Also used : PaxosUpdate(com.palantir.paxos.PaxosUpdate) PaxosLearner(com.palantir.paxos.PaxosLearner) Nullable(javax.annotation.Nullable) PaxosValue(com.palantir.paxos.PaxosValue)

Example 2 with PaxosLearner

use of com.palantir.paxos.PaxosLearner in project atlasdb by palantir.

the class Leaders method createInstrumentedLocalServices.

public static LocalPaxosServices createInstrumentedLocalServices(LeaderConfig config, Supplier<LeaderRuntimeConfig> runtime, RemotePaxosServerSpec remotePaxosServerSpec, String userAgent) {
    UUID leaderUuid = UUID.randomUUID();
    PaxosLeadershipEventRecorder leadershipEventRecorder = PaxosLeadershipEventRecorder.create(AtlasDbMetrics.getMetricRegistry(), leaderUuid.toString());
    PaxosAcceptor ourAcceptor = AtlasDbMetrics.instrument(PaxosAcceptor.class, PaxosAcceptorImpl.newAcceptor(config.acceptorLogDir().getPath()));
    PaxosLearner ourLearner = AtlasDbMetrics.instrument(PaxosLearner.class, PaxosLearnerImpl.newLearner(config.learnerLogDir().getPath(), leadershipEventRecorder));
    Optional<SSLSocketFactory> sslSocketFactory = ServiceCreator.createSslSocketFactory(config.sslConfiguration());
    List<PaxosLearner> learners = createProxyAndLocalList(ourLearner, remotePaxosServerSpec.remoteLearnerUris(), sslSocketFactory, PaxosLearner.class, userAgent);
    List<PaxosAcceptor> acceptors = createProxyAndLocalList(ourAcceptor, remotePaxosServerSpec.remoteAcceptorUris(), sslSocketFactory, PaxosAcceptor.class, userAgent);
    Map<PingableLeader, HostAndPort> otherLeaders = generatePingables(remotePaxosServerSpec.remoteLeaderUris(), sslSocketFactory, userAgent);
    InstrumentedExecutorService proposerExecutorService = new InstrumentedExecutorService(PTExecutors.newCachedThreadPool(new ThreadFactoryBuilder().setNameFormat("atlas-proposer-%d").setDaemon(true).build()), AtlasDbMetrics.getMetricRegistry(), MetricRegistry.name(PaxosProposer.class, "executor"));
    PaxosProposer proposer = AtlasDbMetrics.instrument(PaxosProposer.class, PaxosProposerImpl.newProposer(ourLearner, acceptors, learners, config.quorumSize(), leaderUuid, proposerExecutorService));
    InstrumentedExecutorService leaderElectionExecutor = new InstrumentedExecutorService(PTExecutors.newCachedThreadPool(new ThreadFactoryBuilder().setNameFormat("atlas-leaders-election-%d").setDaemon(true).build()), AtlasDbMetrics.getMetricRegistry(), MetricRegistry.name(PaxosLeaderElectionService.class, "executor"));
    PaxosLeaderElectionService paxosLeaderElectionService = new PaxosLeaderElectionServiceBuilder().proposer(proposer).knowledge(ourLearner).potentialLeadersToHosts(otherLeaders).acceptors(acceptors).learners(learners).executor(leaderElectionExecutor).pingRateMs(config.pingRateMs()).randomWaitBeforeProposingLeadershipMs(config.randomWaitBeforeProposingLeadershipMs()).leaderPingResponseWaitMs(config.leaderPingResponseWaitMs()).eventRecorder(leadershipEventRecorder).onlyLogOnQuorumFailure(JavaSuppliers.compose(LeaderRuntimeConfig::onlyLogOnQuorumFailure, runtime)).build();
    LeaderElectionService leaderElectionService = AtlasDbMetrics.instrument(LeaderElectionService.class, paxosLeaderElectionService);
    PingableLeader pingableLeader = AtlasDbMetrics.instrument(PingableLeader.class, paxosLeaderElectionService);
    return ImmutableLocalPaxosServices.builder().ourAcceptor(ourAcceptor).ourLearner(ourLearner).leaderElectionService(leaderElectionService).pingableLeader(pingableLeader).build();
}
Also used : PaxosAcceptor(com.palantir.paxos.PaxosAcceptor) InstrumentedExecutorService(com.codahale.metrics.InstrumentedExecutorService) PaxosProposer(com.palantir.paxos.PaxosProposer) PaxosLearner(com.palantir.paxos.PaxosLearner) HostAndPort(com.google.common.net.HostAndPort) PingableLeader(com.palantir.leader.PingableLeader) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) PaxosLeaderElectionService(com.palantir.leader.PaxosLeaderElectionService) PaxosLeaderElectionService(com.palantir.leader.PaxosLeaderElectionService) LeaderElectionService(com.palantir.leader.LeaderElectionService) PaxosLeadershipEventRecorder(com.palantir.leader.PaxosLeadershipEventRecorder) UUID(java.util.UUID) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) PaxosLeaderElectionServiceBuilder(com.palantir.leader.PaxosLeaderElectionServiceBuilder) LeaderRuntimeConfig(com.palantir.atlasdb.config.LeaderRuntimeConfig)

Example 3 with PaxosLearner

use of com.palantir.paxos.PaxosLearner in project atlasdb by palantir.

the class PaxosResourceTest method newClientCanBeCreated.

@Test
public void newClientCanBeCreated() {
    PaxosLearner learner = paxosResource.getPaxosLearner(CLIENT_1);
    learner.learn(PAXOS_ROUND_ONE, PAXOS_VALUE);
    assertThat(learner.getGreatestLearnedValue()).isNotNull();
    assertThat(learner.getGreatestLearnedValue().getLeaderUUID()).isEqualTo(PAXOS_UUID);
    assertThat(learner.getGreatestLearnedValue().getData()).isEqualTo(PAXOS_DATA);
    PaxosAcceptor acceptor = paxosResource.getPaxosAcceptor(CLIENT_1);
    acceptor.accept(PAXOS_ROUND_TWO, PAXOS_PROPOSAL);
    assertThat(acceptor.getLatestSequencePreparedOrAccepted()).isEqualTo(PAXOS_ROUND_TWO);
}
Also used : PaxosAcceptor(com.palantir.paxos.PaxosAcceptor) PaxosLearner(com.palantir.paxos.PaxosLearner) Test(org.junit.Test)

Example 4 with PaxosLearner

use of com.palantir.paxos.PaxosLearner in project atlasdb by palantir.

the class PaxosResource method createInstrumentedComponents.

public PaxosComponents createInstrumentedComponents(String client) {
    String learnerLogDir = Paths.get(logDirectory, client, PaxosTimeLockConstants.LEARNER_SUBDIRECTORY_PATH).toString();
    PaxosLearner learner = instrument(PaxosLearner.class, PaxosLearnerImpl.newLearner(learnerLogDir), client);
    String acceptorLogDir = Paths.get(logDirectory, client, PaxosTimeLockConstants.ACCEPTOR_SUBDIRECTORY_PATH).toString();
    PaxosAcceptor acceptor = instrument(PaxosAcceptor.class, PaxosAcceptorImpl.newAcceptor(acceptorLogDir), client);
    return ImmutablePaxosComponents.builder().acceptor(acceptor).learner(learner).build();
}
Also used : PaxosAcceptor(com.palantir.paxos.PaxosAcceptor) PaxosLearner(com.palantir.paxos.PaxosLearner)

Example 5 with PaxosLearner

use of com.palantir.paxos.PaxosLearner in project atlasdb by palantir.

the class LeadersTest method canCreateProxyAndLocalListOfPaxosLearners.

@Test
public void canCreateProxyAndLocalListOfPaxosLearners() {
    PaxosLearner localLearner = mock(PaxosLearner.class);
    PaxosValue value = mock(PaxosValue.class);
    when(localLearner.getGreatestLearnedValue()).thenReturn(value);
    List<PaxosLearner> paxosLearners = Leaders.createProxyAndLocalList(localLearner, REMOTE_SERVICE_ADDRESSES, Optional.empty(), PaxosLearner.class);
    MatcherAssert.assertThat(paxosLearners.size(), is(REMOTE_SERVICE_ADDRESSES.size() + 1));
    paxosLearners.forEach(object -> MatcherAssert.assertThat(object, not(nullValue())));
    MatcherAssert.assertThat(Iterables.getLast(paxosLearners).getGreatestLearnedValue(), is(value));
    verify(localLearner).getGreatestLearnedValue();
    verifyNoMoreInteractions(localLearner);
}
Also used : PaxosLearner(com.palantir.paxos.PaxosLearner) PaxosValue(com.palantir.paxos.PaxosValue) Test(org.junit.Test)

Aggregations

PaxosLearner (com.palantir.paxos.PaxosLearner)10 PaxosAcceptor (com.palantir.paxos.PaxosAcceptor)5 Test (org.junit.Test)4 PaxosValue (com.palantir.paxos.PaxosValue)3 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)2 PaxosProposer (com.palantir.paxos.PaxosProposer)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Before (org.junit.Before)2 InstrumentedExecutorService (com.codahale.metrics.InstrumentedExecutorService)1 HostAndPort (com.google.common.net.HostAndPort)1 LeaderRuntimeConfig (com.palantir.atlasdb.config.LeaderRuntimeConfig)1 LeaderElectionService (com.palantir.leader.LeaderElectionService)1 PaxosLeaderElectionService (com.palantir.leader.PaxosLeaderElectionService)1 PaxosLeaderElectionServiceBuilder (com.palantir.leader.PaxosLeaderElectionServiceBuilder)1 PaxosLeadershipEventRecorder (com.palantir.leader.PaxosLeadershipEventRecorder)1 PingableLeader (com.palantir.leader.PingableLeader)1 PaxosUpdate (com.palantir.paxos.PaxosUpdate)1 UUID (java.util.UUID)1 ExecutorService (java.util.concurrent.ExecutorService)1 Nullable (javax.annotation.Nullable)1