Search in sources :

Example 11 with RaftOptions

use of com.alipay.sofa.jraft.option.RaftOptions in project mmqtt by MrHKing.

the class RaftOptionsBuilder method initRaftOptions.

/**
 * By {@link RaftConfig} creating a {@link RaftOptions}.
 *
 * @param config {@link RaftConfig}
 * @return {@link RaftOptions}
 */
public static RaftOptions initRaftOptions(RaftConfig config) {
    RaftOptions raftOptions = new RaftOptions();
    raftOptions.setReadOnlyOptions(raftReadIndexType(config));
    raftOptions.setMaxByteCountPerRpc(ConvertUtils.toInt(config.getVal(MAX_BYTE_COUNT_PER_RPC), DEFAULT_MAX_BYTE_COUNT_PER_RPC));
    raftOptions.setMaxEntriesSize(ConvertUtils.toInt(config.getVal(MAX_ENTRIES_SIZE), DEFAULT_MAX_ENTRIES_SIZE));
    raftOptions.setMaxBodySize(ConvertUtils.toInt(config.getVal(MAX_BODY_SIZE), DEFAULT_MAX_BODY_SIZE));
    raftOptions.setMaxAppendBufferSize(ConvertUtils.toInt(config.getVal(MAX_APPEND_BUFFER_SIZE), DEFAULT_MAX_APPEND_BUFFER_SIZE));
    raftOptions.setMaxElectionDelayMs(ConvertUtils.toInt(config.getVal(MAX_ELECTION_DELAY_MS), DEFAULT_MAX_ELECTION_DELAY_MS));
    raftOptions.setElectionHeartbeatFactor(ConvertUtils.toInt(config.getVal(ELECTION_HEARTBEAT_FACTOR), DEFAULT_ELECTION_HEARTBEAT_FACTOR));
    raftOptions.setApplyBatch(ConvertUtils.toInt(config.getVal(APPLY_BATCH), DEFAULT_APPLY_BATCH));
    raftOptions.setSync(ConvertUtils.toBoolean(config.getVal(SYNC), DEFAULT_SYNC));
    raftOptions.setSyncMeta(ConvertUtils.toBoolean(config.getVal(SYNC_META), DEFAULT_SYNC_META));
    raftOptions.setDisruptorBufferSize(ConvertUtils.toInt(config.getVal(DISRUPTOR_BUFFER_SIZE), DEFAULT_DISRUPTOR_BUFFER_SIZE));
    raftOptions.setReplicatorPipeline(ConvertUtils.toBoolean(config.getVal(REPLICATOR_PIPELINE), DEFAULT_REPLICATOR_PIPELINE));
    raftOptions.setMaxReplicatorInflightMsgs(ConvertUtils.toInt(config.getVal(MAX_REPLICATOR_INFLIGHT_MSGS), DEFAULT_MAX_REPLICATOR_INFLIGHT_MSGS));
    raftOptions.setEnableLogEntryChecksum(ConvertUtils.toBoolean(config.getVal(ENABLE_LOG_ENTRY_CHECKSUM), DEFAULT_ENABLE_LOG_ENTRY_CHECKSUM));
    return raftOptions;
}
Also used : RaftOptions(com.alipay.sofa.jraft.option.RaftOptions)

Example 12 with RaftOptions

use of com.alipay.sofa.jraft.option.RaftOptions in project nacos by alibaba.

the class JRaftServer method init.

void init(RaftConfig config) {
    this.raftConfig = config;
    this.serializer = SerializeFactory.getDefault();
    Loggers.RAFT.info("Initializes the Raft protocol, raft-config info : {}", config);
    RaftExecutor.init(config);
    final String self = config.getSelfMember();
    String[] info = InternetAddressUtil.splitIPPortStr(self);
    selfIp = info[0];
    selfPort = Integer.parseInt(info[1]);
    localPeerId = PeerId.parsePeer(self);
    nodeOptions = new NodeOptions();
    // Set the election timeout time. The default is 5 seconds.
    int electionTimeout = Math.max(ConvertUtils.toInt(config.getVal(RaftSysConstants.RAFT_ELECTION_TIMEOUT_MS), RaftSysConstants.DEFAULT_ELECTION_TIMEOUT), RaftSysConstants.DEFAULT_ELECTION_TIMEOUT);
    rpcRequestTimeoutMs = ConvertUtils.toInt(raftConfig.getVal(RaftSysConstants.RAFT_RPC_REQUEST_TIMEOUT_MS), RaftSysConstants.DEFAULT_RAFT_RPC_REQUEST_TIMEOUT_MS);
    nodeOptions.setSharedElectionTimer(true);
    nodeOptions.setSharedVoteTimer(true);
    nodeOptions.setSharedStepDownTimer(true);
    nodeOptions.setSharedSnapshotTimer(true);
    nodeOptions.setElectionTimeoutMs(electionTimeout);
    RaftOptions raftOptions = RaftOptionsBuilder.initRaftOptions(raftConfig);
    nodeOptions.setRaftOptions(raftOptions);
    // open jraft node metrics record function
    nodeOptions.setEnableMetrics(true);
    CliOptions cliOptions = new CliOptions();
    this.cliService = RaftServiceFactory.createAndInitCliService(cliOptions);
    this.cliClientService = (CliClientServiceImpl) ((CliServiceImpl) this.cliService).getCliClientService();
}
Also used : RaftOptions(com.alipay.sofa.jraft.option.RaftOptions) CliServiceImpl(com.alipay.sofa.jraft.core.CliServiceImpl) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) Endpoint(com.alipay.sofa.jraft.util.Endpoint) CliOptions(com.alipay.sofa.jraft.option.CliOptions)

Example 13 with RaftOptions

use of com.alipay.sofa.jraft.option.RaftOptions in project nacos by alibaba.

the class RaftOptionsBuilder method initRaftOptions.

/**
 * By {@link RaftConfig} creating a {@link RaftOptions}.
 *
 * @param config {@link RaftConfig}
 * @return {@link RaftOptions}
 */
public static RaftOptions initRaftOptions(RaftConfig config) {
    RaftOptions raftOptions = new RaftOptions();
    raftOptions.setReadOnlyOptions(raftReadIndexType(config));
    raftOptions.setMaxByteCountPerRpc(ConvertUtils.toInt(config.getVal(MAX_BYTE_COUNT_PER_RPC), DEFAULT_MAX_BYTE_COUNT_PER_RPC));
    raftOptions.setMaxEntriesSize(ConvertUtils.toInt(config.getVal(MAX_ENTRIES_SIZE), DEFAULT_MAX_ENTRIES_SIZE));
    raftOptions.setMaxBodySize(ConvertUtils.toInt(config.getVal(MAX_BODY_SIZE), DEFAULT_MAX_BODY_SIZE));
    raftOptions.setMaxAppendBufferSize(ConvertUtils.toInt(config.getVal(MAX_APPEND_BUFFER_SIZE), DEFAULT_MAX_APPEND_BUFFER_SIZE));
    raftOptions.setMaxElectionDelayMs(ConvertUtils.toInt(config.getVal(MAX_ELECTION_DELAY_MS), DEFAULT_MAX_ELECTION_DELAY_MS));
    raftOptions.setElectionHeartbeatFactor(ConvertUtils.toInt(config.getVal(ELECTION_HEARTBEAT_FACTOR), DEFAULT_ELECTION_HEARTBEAT_FACTOR));
    raftOptions.setApplyBatch(ConvertUtils.toInt(config.getVal(APPLY_BATCH), DEFAULT_APPLY_BATCH));
    raftOptions.setSync(ConvertUtils.toBoolean(config.getVal(SYNC), DEFAULT_SYNC));
    raftOptions.setSyncMeta(ConvertUtils.toBoolean(config.getVal(SYNC_META), DEFAULT_SYNC_META));
    raftOptions.setDisruptorBufferSize(ConvertUtils.toInt(config.getVal(DISRUPTOR_BUFFER_SIZE), DEFAULT_DISRUPTOR_BUFFER_SIZE));
    raftOptions.setReplicatorPipeline(ConvertUtils.toBoolean(config.getVal(REPLICATOR_PIPELINE), DEFAULT_REPLICATOR_PIPELINE));
    raftOptions.setMaxReplicatorInflightMsgs(ConvertUtils.toInt(config.getVal(MAX_REPLICATOR_INFLIGHT_MSGS), DEFAULT_MAX_REPLICATOR_INFLIGHT_MSGS));
    raftOptions.setEnableLogEntryChecksum(ConvertUtils.toBoolean(config.getVal(ENABLE_LOG_ENTRY_CHECKSUM), DEFAULT_ENABLE_LOG_ENTRY_CHECKSUM));
    return raftOptions;
}
Also used : RaftOptions(com.alipay.sofa.jraft.option.RaftOptions)

Example 14 with RaftOptions

use of com.alipay.sofa.jraft.option.RaftOptions in project sofa-jraft by sofastack.

the class NodeTest method testInstallLargeSnapshot.

@Test
public void testInstallLargeSnapshot() throws Exception {
    final List<PeerId> peers = TestUtils.generatePeers(4);
    final TestCluster cluster = new TestCluster("unitest", this.dataPath, peers.subList(0, 3));
    for (int i = 0; i < peers.size() - 1; i++) {
        final PeerId peer = peers.get(i);
        final boolean started = cluster.start(peer.getEndpoint(), false, 200, false);
        assertTrue(started);
    }
    cluster.waitLeader();
    // get leader
    final Node leader = cluster.getLeader();
    assertNotNull(leader);
    // apply tasks to leader
    sendTestTaskAndWait(leader, 0, RaftError.SUCCESS);
    cluster.ensureSame();
    // apply something more
    for (int i = 1; i < 100; i++) {
        sendTestTaskAndWait(leader, i * 10, RaftError.SUCCESS);
    }
    Thread.sleep(1000);
    // trigger leader snapshot
    triggerLeaderSnapshot(cluster, leader);
    // apply something more
    for (int i = 100; i < 200; i++) {
        sendTestTaskAndWait(leader, i * 10, RaftError.SUCCESS);
    }
    // trigger leader snapshot
    triggerLeaderSnapshot(cluster, leader, 2);
    // wait leader to compact logs
    Thread.sleep(1000);
    // add follower
    final PeerId newPeer = peers.get(3);
    final RaftOptions raftOptions = new RaftOptions();
    raftOptions.setMaxByteCountPerRpc(128);
    final boolean started = cluster.start(newPeer.getEndpoint(), true, 300, false, null, raftOptions);
    assertTrue(started);
    final CountDownLatch latch = new CountDownLatch(1);
    leader.addPeer(newPeer, status -> {
        assertTrue(status.toString(), status.isOk());
        latch.countDown();
    });
    waitLatch(latch);
    cluster.ensureSame();
    assertEquals(4, cluster.getFsms().size());
    for (final MockStateMachine fsm : cluster.getFsms()) {
        assertEquals(2000, fsm.getLogs().size());
    }
    cluster.stopAll();
}
Also used : RaftOptions(com.alipay.sofa.jraft.option.RaftOptions) Node(com.alipay.sofa.jraft.Node) CountDownLatch(java.util.concurrent.CountDownLatch) Endpoint(com.alipay.sofa.jraft.util.Endpoint) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 15 with RaftOptions

use of com.alipay.sofa.jraft.option.RaftOptions in project sofa-jraft by sofastack.

the class LocalRaftMetaStorageTest method testGetAndSetReload.

@Test
public void testGetAndSetReload() {
    assertEquals(0, this.raftMetaStorage.getTerm());
    assertTrue(this.raftMetaStorage.getVotedFor().isEmpty());
    this.raftMetaStorage.setTerm(99);
    assertEquals(99, this.raftMetaStorage.getTerm());
    assertTrue(this.raftMetaStorage.getVotedFor().isEmpty());
    assertTrue(this.raftMetaStorage.setVotedFor(new PeerId("localhost", 8081)));
    assertEquals(99, this.raftMetaStorage.getTerm());
    Assert.assertEquals(new PeerId("localhost", 8081), this.raftMetaStorage.getVotedFor());
    assertTrue(this.raftMetaStorage.setTermAndVotedFor(100, new PeerId("localhost", 8083)));
    assertEquals(100, this.raftMetaStorage.getTerm());
    Assert.assertEquals(new PeerId("localhost", 8083), this.raftMetaStorage.getVotedFor());
    this.raftMetaStorage = new LocalRaftMetaStorage(this.path, new RaftOptions());
    Mockito.when(this.node.getNodeMetrics()).thenReturn(null);
    this.raftMetaStorage.init(newOptions());
    assertEquals(100, this.raftMetaStorage.getTerm());
    Assert.assertEquals(new PeerId("localhost", 8083), this.raftMetaStorage.getVotedFor());
}
Also used : RaftOptions(com.alipay.sofa.jraft.option.RaftOptions) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test) BaseStorageTest(com.alipay.sofa.jraft.storage.BaseStorageTest)

Aggregations

RaftOptions (com.alipay.sofa.jraft.option.RaftOptions)25 Test (org.junit.Test)11 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)9 Endpoint (com.alipay.sofa.jraft.util.Endpoint)9 Before (org.junit.Before)8 File (java.io.File)6 PeerId (com.alipay.sofa.jraft.entity.PeerId)5 LocalFileMetaOutter (com.alipay.sofa.jraft.entity.LocalFileMetaOutter)4 TimerManager (com.alipay.sofa.jraft.core.TimerManager)3 CopyOptions (com.alipay.sofa.jraft.option.CopyOptions)3 LogStorageOptions (com.alipay.sofa.jraft.option.LogStorageOptions)3 SnapshotCopierOptions (com.alipay.sofa.jraft.option.SnapshotCopierOptions)3 BaseStorageTest (com.alipay.sofa.jraft.storage.BaseStorageTest)3 RocksDBSegmentLogStorage (com.alipay.sofa.jraft.storage.log.RocksDBSegmentLogStorage)3 ByteBuffer (java.nio.ByteBuffer)3 Node (com.alipay.sofa.jraft.Node)2 Configuration (com.alipay.sofa.jraft.conf.Configuration)2 ConfigurationManager (com.alipay.sofa.jraft.conf.ConfigurationManager)2 CliServiceImpl (com.alipay.sofa.jraft.core.CliServiceImpl)2 CliOptions (com.alipay.sofa.jraft.option.CliOptions)2