Search in sources :

Example 1 with LeaderElectionService

use of com.palantir.leader.LeaderElectionService in project atlasdb by palantir.

the class PaxosLeaderElectionServiceSerializationTest method canSerializeNoSuspectedLeader.

@Test
public void canSerializeNoSuspectedLeader() throws JsonProcessingException {
    LeaderElectionService leaderElectionService = mock(LeaderElectionService.class);
    when(leaderElectionService.getSuspectedLeaderInMemory()).thenReturn(Optional.empty());
    // Be very careful about changing the following! Doing so would be a wire break.
    assertThat(MAPPER.writeValueAsString(leaderElectionService.getSuspectedLeaderInMemory())).isEqualTo("null");
}
Also used : LeaderElectionService(com.palantir.leader.LeaderElectionService) Test(org.junit.Test)

Example 2 with LeaderElectionService

use of com.palantir.leader.LeaderElectionService in project atlasdb by palantir.

the class PaxosLeaderElectionServiceSerializationTest method canSerializeSuspectedLeader.

@Test
public void canSerializeSuspectedLeader() throws JsonProcessingException {
    Optional<HostAndPort> suspectedLeader = Optional.of(HostAndPort.fromParts("foo", 123));
    LeaderElectionService leaderElectionService = mock(LeaderElectionService.class);
    when(leaderElectionService.getSuspectedLeaderInMemory()).thenReturn(suspectedLeader);
    // Be very careful about changing the following! Doing so would be a wire break.
    assertThat(MAPPER.writeValueAsString(leaderElectionService.getSuspectedLeaderInMemory())).isEqualTo("\"foo:123\"");
}
Also used : HostAndPort(com.google.common.net.HostAndPort) LeaderElectionService(com.palantir.leader.LeaderElectionService) Test(org.junit.Test)

Example 3 with LeaderElectionService

use of com.palantir.leader.LeaderElectionService in project atlasdb by palantir.

the class AwaitingLeadershipProxyTest method shouldAllowObjectMethodsWhenLeading.

@Test
@SuppressWarnings("SelfEquals")
public // We're asserting that calling .equals on a proxy does not redirect the .equals call to the instance its being proxied.
void shouldAllowObjectMethodsWhenLeading() throws Exception {
    Runnable mockRunnable = mock(Runnable.class);
    Supplier<Runnable> delegateSupplier = Suppliers.ofInstance(mockRunnable);
    LeaderElectionService mockLeader = mock(LeaderElectionService.class);
    when(mockLeader.getSuspectedLeaderInMemory()).thenReturn(Optional.empty());
    when(mockLeader.getCurrentTokenIfLeading()).thenReturn(Optional.empty());
    when(mockLeader.isStillLeading(any(LeaderElectionService.LeadershipToken.class))).thenReturn(LeaderElectionService.StillLeadingStatus.LEADING);
    Runnable proxy = AwaitingLeadershipProxy.newProxyInstance(Runnable.class, delegateSupplier, mockLeader);
    assertThat(proxy.hashCode()).isNotNull();
    assertThat(proxy.equals(proxy)).isTrue();
    assertThat(proxy.equals(null)).isFalse();
    assertThat(proxy.toString()).startsWith("com.palantir.leader.proxy.AwaitingLeadershipProxy@");
}
Also used : PaxosLeadershipToken(com.palantir.leader.PaxosLeadershipToken) LeaderElectionService(com.palantir.leader.LeaderElectionService) Test(org.junit.Test)

Example 4 with LeaderElectionService

use of com.palantir.leader.LeaderElectionService in project atlasdb by palantir.

the class PaxosConsensusTestUtils method setup.

public static PaxosTestState setup(int numLeaders, int quorumSize) {
    List<LeaderElectionService> leaders = Lists.newArrayList();
    List<PaxosAcceptor> acceptors = Lists.newArrayList();
    List<PaxosLearner> learners = Lists.newArrayList();
    List<AtomicBoolean> failureToggles = Lists.newArrayList();
    ExecutorService executor = PTExecutors.newCachedThreadPool();
    RuntimeException e = new RuntimeException("mock server failure");
    for (int i = 0; i < numLeaders; i++) {
        failureToggles.add(new AtomicBoolean(false));
        PaxosLearner learner = PaxosLearnerImpl.newLearner(getLearnerLogDir(i));
        learners.add(ToggleableExceptionProxy.newProxyInstance(PaxosLearner.class, learner, failureToggles.get(i), e));
        PaxosAcceptor acceptor = PaxosAcceptorImpl.newAcceptor(getAcceptorLogDir(i));
        acceptors.add(ToggleableExceptionProxy.newProxyInstance(PaxosAcceptor.class, acceptor, failureToggles.get(i), e));
    }
    for (int i = 0; i < numLeaders; i++) {
        PaxosProposer proposer = PaxosProposerImpl.newProposer(learners.get(i), ImmutableList.<PaxosAcceptor>copyOf(acceptors), ImmutableList.<PaxosLearner>copyOf(learners), quorumSize, UUID.randomUUID(), executor);
        PaxosLeaderElectionService leader = new PaxosLeaderElectionServiceBuilder().proposer(proposer).knowledge(learners.get(i)).potentialLeadersToHosts(ImmutableMap.<PingableLeader, HostAndPort>of()).acceptors(acceptors).learners(learners).executor(executor).pingRateMs(0L).randomWaitBeforeProposingLeadershipMs(0L).leaderPingResponseWaitMs(0L).onlyLogOnQuorumFailure(() -> true).build();
        leaders.add(SimulatingFailingServerProxy.newProxyInstance(LeaderElectionService.class, leader, SERVER_DELAY_TIME_MS, failureToggles.get(i)));
    }
    return new PaxosTestState(leaders, acceptors, learners, failureToggles, executor);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ExecutorService(java.util.concurrent.ExecutorService) LeaderElectionService(com.palantir.leader.LeaderElectionService) PaxosLeaderElectionService(com.palantir.leader.PaxosLeaderElectionService) PaxosLeaderElectionService(com.palantir.leader.PaxosLeaderElectionService) PaxosLeaderElectionServiceBuilder(com.palantir.leader.PaxosLeaderElectionServiceBuilder)

Example 5 with LeaderElectionService

use of com.palantir.leader.LeaderElectionService 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)

Aggregations

LeaderElectionService (com.palantir.leader.LeaderElectionService)8 Test (org.junit.Test)4 HostAndPort (com.google.common.net.HostAndPort)2 LeaderRuntimeConfig (com.palantir.atlasdb.config.LeaderRuntimeConfig)2 PaxosLeaderElectionService (com.palantir.leader.PaxosLeaderElectionService)2 PaxosLeaderElectionServiceBuilder (com.palantir.leader.PaxosLeaderElectionServiceBuilder)2 PaxosLeadershipToken (com.palantir.leader.PaxosLeadershipToken)2 PingableLeader (com.palantir.leader.PingableLeader)2 InstrumentedExecutorService (com.codahale.metrics.InstrumentedExecutorService)1 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 ImmutableLeaderRuntimeConfig (com.palantir.atlasdb.config.ImmutableLeaderRuntimeConfig)1 ImmutableServerListConfig (com.palantir.atlasdb.config.ImmutableServerListConfig)1 LocalPaxosServices (com.palantir.atlasdb.factory.Leaders.LocalPaxosServices)1 KvsBackedPersistentLockService (com.palantir.atlasdb.persistentlock.KvsBackedPersistentLockService)1 NoOpPersistentLockService (com.palantir.atlasdb.persistentlock.NoOpPersistentLockService)1 PersistentLockService (com.palantir.atlasdb.persistentlock.PersistentLockService)1 PaxosLeadershipEventRecorder (com.palantir.leader.PaxosLeadershipEventRecorder)1 LockService (com.palantir.lock.LockService)1 LockRefreshingLockService (com.palantir.lock.client.LockRefreshingLockService)1 LegacyTimelockService (com.palantir.lock.impl.LegacyTimelockService)1