Search in sources :

Example 1 with TimeDuration

use of org.apache.ratis.util.TimeDuration in project incubator-ratis by apache.

the class RaftAsyncTests method setup.

@Before
public void setup() {
    properties = new RaftProperties();
    properties.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY, SimpleStateMachine4Testing.class, StateMachine.class);
    TimeDuration retryCacheExpiryDuration = TimeDuration.valueOf(5, TimeUnit.SECONDS);
    RaftServerConfigKeys.RetryCache.setExpiryTime(properties, retryCacheExpiryDuration);
}
Also used : RaftProperties(org.apache.ratis.conf.RaftProperties) TimeDuration(org.apache.ratis.util.TimeDuration)

Example 2 with TimeDuration

use of org.apache.ratis.util.TimeDuration in project incubator-ratis by apache.

the class RaftBasicTests method testRequestTimeout.

public static void testRequestTimeout(boolean async, MiniRaftCluster cluster, Logger LOG, RaftProperties properties) throws InterruptedException, IOException, ExecutionException {
    LOG.info("Running testRequestTimeout");
    waitForLeader(cluster);
    long time = System.currentTimeMillis();
    try (final RaftClient client = cluster.createClient()) {
        // Get the next callId to be used by the client
        long callId = RaftClientTestUtil.getCallId(client);
        // Create an entry corresponding to the callId and clientId
        // in each server's retry cache.
        cluster.getServerAliveStream().forEach(raftServer -> RetryCacheTestUtil.getOrCreateEntry(raftServer.getRetryCache(), client.getId(), callId));
        // The retry is successful when the retry cache entry for the corresponding callId and clientId expires.
        if (async) {
            CompletableFuture<RaftClientReply> replyFuture = client.sendAsync(new SimpleMessage("abc"));
            replyFuture.get();
        } else {
            client.send(new SimpleMessage("abc"));
        }
        // Eventually the request would be accepted by the server
        // when the retry cache entry is invalidated.
        // The duration for which the client waits should be more than the retryCacheExpiryDuration.
        TimeDuration duration = TimeDuration.valueOf(System.currentTimeMillis() - time, TimeUnit.MILLISECONDS);
        TimeDuration retryCacheExpiryDuration = RaftServerConfigKeys.RetryCache.expiryTime(properties);
        Assert.assertTrue(duration.compareTo(retryCacheExpiryDuration) >= 0);
    }
}
Also used : RaftClientReply(org.apache.ratis.protocol.RaftClientReply) TimeDuration(org.apache.ratis.util.TimeDuration) RaftClient(org.apache.ratis.client.RaftClient)

Example 3 with TimeDuration

use of org.apache.ratis.util.TimeDuration in project alluxio by Alluxio.

the class RaftJournalSystem method initServer.

private synchronized void initServer() throws IOException {
    LOG.debug("Creating journal with max segment size {}", mConf.getMaxLogSize());
    if (mStateMachine != null) {
        mStateMachine.close();
    }
    mStateMachine = new JournalStateMachine(mJournals, this, mConf.getMaxConcurrencyPoolSize());
    RaftProperties properties = new RaftProperties();
    Parameters parameters = new Parameters();
    // TODO(feng): implement a custom RpcType to integrate with Alluxio authentication service
    RaftConfigKeys.Rpc.setType(properties, SupportedRpcType.GRPC);
    // RPC port
    GrpcConfigKeys.Server.setPort(properties, mConf.getLocalAddress().getPort());
    // storage path
    maybeMigrateOldJournal();
    RaftServerConfigKeys.setStorageDir(properties, Collections.singletonList(RaftJournalUtils.getRaftJournalDir(mConf.getPath())));
    // segment size
    RaftServerConfigKeys.Log.setSegmentSizeMax(properties, SizeInBytes.valueOf(mConf.getMaxLogSize()));
    // the following configurations need to be changed when the single journal entry
    // is unexpectedly big.
    RaftServerConfigKeys.Log.Appender.setBufferByteLimit(properties, SizeInBytes.valueOf(ServerConfiguration.global().getBytes(PropertyKey.MASTER_EMBEDDED_JOURNAL_ENTRY_SIZE_MAX)));
    // this property defines the maximum allowed size of the concurrent journal flush requests.
    // if the total size of the journal entries contained in the flush requests
    // are bigger than the given threshold, Ratis may error out as
    // `Log entry size 117146048 exceeds the max buffer limit of 104857600`
    RaftServerConfigKeys.Write.setByteLimit(properties, SizeInBytes.valueOf(ServerConfiguration.global().getBytes(PropertyKey.MASTER_EMBEDDED_JOURNAL_FLUSH_SIZE_MAX)));
    // this property defines the maximum allowed size of the concurrent journal write IO tasks.
    // if the total size of the journal entries contained in the write IO tasks
    // are bigger than the given threshold, ratis may error out as
    // `SegmentedRaftLogWorker: elementNumBytes = 78215699 > byteLimit = 67108864`
    RaftServerConfigKeys.Log.setQueueByteLimit(properties, (int) ServerConfiguration.global().getBytes(PropertyKey.MASTER_EMBEDDED_JOURNAL_FLUSH_SIZE_MAX));
    // election timeout, heartbeat timeout is automatically 1/2 of the value
    final TimeDuration leaderElectionMinTimeout = TimeDuration.valueOf(mConf.getMinElectionTimeoutMs(), TimeUnit.MILLISECONDS);
    final TimeDuration leaderElectionMaxTimeout = TimeDuration.valueOf(mConf.getMaxElectionTimeoutMs(), TimeUnit.MILLISECONDS);
    RaftServerConfigKeys.Rpc.setTimeoutMin(properties, leaderElectionMinTimeout);
    RaftServerConfigKeys.Rpc.setTimeoutMax(properties, leaderElectionMaxTimeout);
    // request timeout
    RaftServerConfigKeys.Rpc.setRequestTimeout(properties, TimeDuration.valueOf(ServerConfiguration.global().getMs(PropertyKey.MASTER_EMBEDDED_JOURNAL_TRANSPORT_REQUEST_TIMEOUT_MS), TimeUnit.MILLISECONDS));
    RaftServerConfigKeys.RetryCache.setExpiryTime(properties, TimeDuration.valueOf(ServerConfiguration.getMs(PropertyKey.MASTER_EMBEDDED_JOURNAL_RETRY_CACHE_EXPIRY_TIME), TimeUnit.MILLISECONDS));
    // snapshot retention
    RaftServerConfigKeys.Snapshot.setRetentionFileNum(properties, 3);
    // snapshot interval
    RaftServerConfigKeys.Snapshot.setAutoTriggerEnabled(properties, true);
    long snapshotAutoTriggerThreshold = ServerConfiguration.global().getLong(PropertyKey.MASTER_JOURNAL_CHECKPOINT_PERIOD_ENTRIES);
    RaftServerConfigKeys.Snapshot.setAutoTriggerThreshold(properties, snapshotAutoTriggerThreshold);
    RaftServerConfigKeys.Log.Appender.setInstallSnapshotEnabled(properties, false);
    /*
     * Soft disable RPC level safety.
     *
     * Without these overrides, the leader will step down upon detecting a long running GC over
     * 10sec. This is not desirable for a single master cluster. Additionally, reduced safety should
     * be provided via standard leader election in clustered mode.
     */
    RaftServerConfigKeys.Rpc.setSlownessTimeout(properties, TimeDuration.valueOf(Long.MAX_VALUE, TimeUnit.MILLISECONDS));
    RaftServerConfigKeys.LeaderElection.setLeaderStepDownWaitTime(properties, TimeDuration.valueOf(Long.MAX_VALUE, TimeUnit.MILLISECONDS));
    long messageSize = ServerConfiguration.global().getBytes(PropertyKey.MASTER_EMBEDDED_JOURNAL_TRANSPORT_MAX_INBOUND_MESSAGE_SIZE);
    GrpcConfigKeys.setMessageSizeMax(properties, SizeInBytes.valueOf(messageSize));
    RatisDropwizardExports.registerRatisMetricReporters(mRatisMetricsMap);
    // TODO(feng): clean up embedded journal configuration
    // build server
    mServer = RaftServer.newBuilder().setServerId(mPeerId).setGroup(mRaftGroup).setStateMachine(mStateMachine).setProperties(properties).setParameters(parameters).build();
    super.registerMetrics();
    MetricsSystem.registerGaugeIfAbsent(MetricKey.CLUSTER_LEADER_INDEX.getName(), () -> getLeaderIndex());
    MetricsSystem.registerGaugeIfAbsent(MetricKey.MASTER_ROLE_ID.getName(), () -> getRoleId());
    MetricsSystem.registerGaugeIfAbsent(MetricKey.CLUSTER_LEADER_ID.getName(), () -> getLeaderId());
}
Also used : Parameters(org.apache.ratis.conf.Parameters) RaftProperties(org.apache.ratis.conf.RaftProperties) TimeDuration(org.apache.ratis.util.TimeDuration)

Example 4 with TimeDuration

use of org.apache.ratis.util.TimeDuration in project incubator-ratis by apache.

the class LeaderElectionTests method testLateServerStart.

@Test
public void testLateServerStart() throws Exception {
    final int numServer = 3;
    LOG.info("Running testLateServerStart");
    final MiniRaftCluster cluster = newCluster(numServer);
    cluster.initServers();
    // start all except one servers
    final Iterator<RaftServerProxy> i = cluster.getServers().iterator();
    for (int j = 1; j < numServer; j++) {
        i.next().start();
    }
    final RaftServerImpl leader = waitForLeader(cluster);
    final TimeDuration sleepTime = TimeDuration.valueOf(5, TimeUnit.SECONDS);
    LOG.info("sleep " + sleepTime);
    sleepTime.sleep();
    // start the last server
    final RaftServerProxy lastServer = i.next();
    lastServer.start();
    final RaftPeerId lastServerLeaderId = JavaUtils.attempt(() -> getLeader(lastServer.getImpl().getState()), 10, 1000, "getLeaderId", LOG);
    Assert.assertEquals(leader.getId(), lastServerLeaderId);
}
Also used : MiniRaftCluster(org.apache.ratis.MiniRaftCluster) TimeDuration(org.apache.ratis.util.TimeDuration) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 5 with TimeDuration

use of org.apache.ratis.util.TimeDuration in project incubator-ratis by apache.

the class ConfUtils method getTimeDuration.

@SafeVarargs
static TimeDuration getTimeDuration(BiFunction<String, TimeDuration, TimeDuration> getter, String key, TimeDuration defaultValue, BiConsumer<String, TimeDuration>... assertions) {
    final TimeDuration value = get(getter, key, defaultValue, assertions);
    requireNonNegativeTimeDuration().accept(key, value);
    return value;
}
Also used : TimeDuration(org.apache.ratis.util.TimeDuration)

Aggregations

TimeDuration (org.apache.ratis.util.TimeDuration)5 RaftProperties (org.apache.ratis.conf.RaftProperties)2 BaseTest (org.apache.ratis.BaseTest)1 MiniRaftCluster (org.apache.ratis.MiniRaftCluster)1 RaftClient (org.apache.ratis.client.RaftClient)1 Parameters (org.apache.ratis.conf.Parameters)1 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)1 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)1 Test (org.junit.Test)1