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;
}
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();
}
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);
}
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();
}
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);
}
Aggregations