Search in sources :

Example 1 with RepeatedTimer

use of org.apache.ignite.raft.jraft.util.RepeatedTimer in project ignite-3 by apache.

the class NodeImpl method initTimers.

/**
 * Initialize timer pools.
 * @param opts The options.
 */
private void initTimers(final NodeOptions opts) {
    if (opts.getScheduler() == null && validateOption(opts, "scheduler"))
        opts.setScheduler(JRaftUtils.createScheduler(opts));
    String name = "JRaft-VoteTimer";
    if (opts.getVoteTimer() == null && validateOption(opts, "voteTimer")) {
        opts.setVoteTimer(JRaftUtils.createTimer(opts, name));
    }
    this.voteTimer = new RepeatedTimer(name, NodeImpl.this.options.getElectionTimeoutMs(), opts.getVoteTimer()) {

        @Override
        protected void onTrigger() {
            handleVoteTimeout();
        }

        @Override
        protected int adjustTimeout(final int timeoutMs) {
            return randomTimeout(timeoutMs);
        }
    };
    name = "JRaft-ElectionTimer";
    if (opts.getElectionTimer() == null && validateOption(opts, "electionTimer"))
        opts.setElectionTimer(JRaftUtils.createTimer(opts, name));
    electionTimer = new RepeatedTimer(name, NodeImpl.this.options.getElectionTimeoutMs(), opts.getElectionTimer()) {

        @Override
        protected void onTrigger() {
            handleElectionTimeout();
        }

        @Override
        protected int adjustTimeout(final int timeoutMs) {
            return randomTimeout(timeoutMs);
        }
    };
    name = "JRaft-StepDownTimer";
    if (opts.getStepDownTimer() == null && validateOption(opts, "stepDownTimer"))
        opts.setStepDownTimer(JRaftUtils.createTimer(opts, name));
    stepDownTimer = new RepeatedTimer(name, NodeImpl.this.options.getElectionTimeoutMs() >> 1, opts.getStepDownTimer()) {

        @Override
        protected void onTrigger() {
            handleStepDownTimeout();
        }
    };
    name = "JRaft-SnapshotTimer";
    if (opts.getSnapshotTimer() == null && validateOption(opts, "snapshotTimer"))
        opts.setSnapshotTimer(JRaftUtils.createTimer(opts, name));
    snapshotTimer = new RepeatedTimer(name, NodeImpl.this.options.getSnapshotIntervalSecs() * 1000, opts.getSnapshotTimer()) {

        private volatile boolean firstSchedule = true;

        @Override
        protected void onTrigger() {
            handleSnapshotTimeout();
        }

        @Override
        protected int adjustTimeout(final int timeoutMs) {
            if (!this.firstSchedule) {
                return timeoutMs;
            }
            // Randomize the first snapshot trigger timeout
            this.firstSchedule = false;
            if (timeoutMs > 0) {
                int half = timeoutMs / 2;
                return half + ThreadLocalRandom.current().nextInt(half);
            } else {
                return timeoutMs;
            }
        }
    };
}
Also used : ByteString(org.apache.ignite.raft.jraft.util.ByteString) RepeatedTimer(org.apache.ignite.raft.jraft.util.RepeatedTimer)

Aggregations

ByteString (org.apache.ignite.raft.jraft.util.ByteString)1 RepeatedTimer (org.apache.ignite.raft.jraft.util.RepeatedTimer)1