Search in sources :

Example 6 with RaftOptions

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

the class CopySessionTest method setup.

@Before
public void setup() {
    this.timerManager = new TimerManager(5);
    this.copyOpts = new CopyOptions();
    this.rb = RpcRequests.GetFileRequest.newBuilder();
    this.rb.setReaderId(99);
    this.rb.setFilename("data");
    this.raftOpts = new RaftOptions();
    this.session = new CopySession(rpcService, timerManager, null, raftOpts, rb, address);
    this.session.setCopyOptions(copyOpts);
}
Also used : RaftOptions(com.alipay.sofa.jraft.option.RaftOptions) CopyOptions(com.alipay.sofa.jraft.option.CopyOptions) TimerManager(com.alipay.sofa.jraft.core.TimerManager) Before(org.junit.Before)

Example 7 with RaftOptions

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

the class RemoteFileCopierTest method testInit.

@Test
public void testInit() {
    Mockito.when(rpcService.connect(new Endpoint("localhost", 8081))).thenReturn(true);
    assertTrue(copier.init("remote://localhost:8081/999", null, new SnapshotCopierOptions(rpcService, timerManager, new RaftOptions(), new NodeOptions())));
    assertEquals(999, copier.getReaderId());
    Assert.assertEquals("localhost", copier.getEndpoint().getIp());
    Assert.assertEquals(8081, copier.getEndpoint().getPort());
}
Also used : RaftOptions(com.alipay.sofa.jraft.option.RaftOptions) SnapshotCopierOptions(com.alipay.sofa.jraft.option.SnapshotCopierOptions) Endpoint(com.alipay.sofa.jraft.util.Endpoint) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) Test(org.junit.Test)

Example 8 with RaftOptions

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

the class NodeTest method testNodeTaskOverload.

@Test
public void testNodeTaskOverload() throws Exception {
    final Endpoint addr = new Endpoint(TestUtils.getMyIp(), TestUtils.INIT_PORT);
    final PeerId peer = new PeerId(addr, 0);
    NodeManager.getInstance().addAddress(addr);
    final NodeOptions nodeOptions = createNodeOptionsWithSharedTimer();
    final RaftOptions raftOptions = new RaftOptions();
    raftOptions.setDisruptorBufferSize(2);
    nodeOptions.setRaftOptions(raftOptions);
    final MockStateMachine fsm = new MockStateMachine(addr);
    nodeOptions.setFsm(fsm);
    nodeOptions.setLogUri(this.dataPath + File.separator + "log");
    nodeOptions.setRaftMetaUri(this.dataPath + File.separator + "meta");
    nodeOptions.setSnapshotUri(this.dataPath + File.separator + "snapshot");
    nodeOptions.setInitialConf(new Configuration(Collections.singletonList(peer)));
    final Node node = new NodeImpl("unittest", peer);
    assertTrue(node.init(nodeOptions));
    assertEquals(1, node.listPeers().size());
    assertTrue(node.listPeers().contains(peer));
    while (!node.isLeader()) {
        ;
    }
    final List<Task> tasks = new ArrayList<>();
    final AtomicInteger c = new AtomicInteger(0);
    for (int i = 0; i < 10; i++) {
        final ByteBuffer data = ByteBuffer.wrap(("hello" + i).getBytes());
        final Task task = new Task(data, new JoinableClosure(status -> {
            System.out.println(status);
            if (!status.isOk()) {
                assertTrue(status.getRaftError() == RaftError.EBUSY || status.getRaftError() == RaftError.EPERM);
            }
            c.incrementAndGet();
        }));
        node.apply(task);
        tasks.add(task);
    }
    try {
        Task.joinAll(tasks, TimeUnit.SECONDS.toMillis(30));
        assertEquals(10, c.get());
    } finally {
        node.shutdown();
        node.join();
    }
}
Also used : Arrays(java.util.Arrays) RaftOptions(com.alipay.sofa.jraft.option.RaftOptions) Assert.assertNotSame(org.junit.Assert.assertNotSame) LoggerFactory(org.slf4j.LoggerFactory) ByteBuffer(java.nio.ByteBuffer) Future(java.util.concurrent.Future) Vector(java.util.Vector) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) TestUtils(com.alipay.sofa.jraft.test.TestUtils) Endpoint(com.alipay.sofa.jraft.util.Endpoint) Assert.fail(org.junit.Assert.fail) AfterClass(org.junit.AfterClass) PeerId(com.alipay.sofa.jraft.entity.PeerId) Configuration(com.alipay.sofa.jraft.conf.Configuration) StateMachine(com.alipay.sofa.jraft.StateMachine) SynchronizedClosure(com.alipay.sofa.jraft.closure.SynchronizedClosure) RpcServer(com.alipay.sofa.jraft.rpc.RpcServer) BootstrapOptions(com.alipay.sofa.jraft.option.BootstrapOptions) Set(java.util.Set) ReadIndexClosure(com.alipay.sofa.jraft.closure.ReadIndexClosure) SnapshotThrottle(com.alipay.sofa.jraft.storage.SnapshotThrottle) JRaftUtils(com.alipay.sofa.jraft.JRaftUtils) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) RaftGroupService(com.alipay.sofa.jraft.RaftGroupService) Assert.assertFalse(org.junit.Assert.assertFalse) RaftException(com.alipay.sofa.jraft.error.RaftException) LogIndexOutOfBoundsException(com.alipay.sofa.jraft.error.LogIndexOutOfBoundsException) ThroughputSnapshotThrottle(com.alipay.sofa.jraft.storage.snapshot.ThroughputSnapshotThrottle) UserLog(com.alipay.sofa.jraft.entity.UserLog) BeforeClass(org.junit.BeforeClass) JoinableClosure(com.alipay.sofa.jraft.closure.JoinableClosure) Bits(com.alipay.sofa.jraft.util.Bits) LogNotFoundException(com.alipay.sofa.jraft.error.LogNotFoundException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Utils(com.alipay.sofa.jraft.util.Utils) EnumOutter(com.alipay.sofa.jraft.entity.EnumOutter) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Assert.assertSame(org.junit.Assert.assertSame) SizeUnit(org.rocksdb.util.SizeUnit) StorageOptionsFactory(com.alipay.sofa.jraft.util.StorageOptionsFactory) NodeManager(com.alipay.sofa.jraft.NodeManager) TestName(org.junit.rules.TestName) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) RaftError(com.alipay.sofa.jraft.error.RaftError) TaskClosure(com.alipay.sofa.jraft.closure.TaskClosure) Iterator(com.alipay.sofa.jraft.Iterator) LinkedHashSet(java.util.LinkedHashSet) Before(org.junit.Before) Logger(org.slf4j.Logger) Assert.assertNotNull(org.junit.Assert.assertNotNull) RaftRpcServerFactory(com.alipay.sofa.jraft.rpc.RaftRpcServerFactory) Assert.assertTrue(org.junit.Assert.assertTrue) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) Status(com.alipay.sofa.jraft.Status) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) Rule(org.junit.Rule) Task(com.alipay.sofa.jraft.entity.Task) Assert.assertNull(org.junit.Assert.assertNull) Node(com.alipay.sofa.jraft.Node) ConsoleReporter(com.codahale.metrics.ConsoleReporter) SnapshotReader(com.alipay.sofa.jraft.storage.snapshot.SnapshotReader) Assert(org.junit.Assert) RocksDBLogStorage(com.alipay.sofa.jraft.storage.impl.RocksDBLogStorage) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) RaftOptions(com.alipay.sofa.jraft.option.RaftOptions) Task(com.alipay.sofa.jraft.entity.Task) Configuration(com.alipay.sofa.jraft.conf.Configuration) Node(com.alipay.sofa.jraft.Node) ArrayList(java.util.ArrayList) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) ByteBuffer(java.nio.ByteBuffer) Endpoint(com.alipay.sofa.jraft.util.Endpoint) Endpoint(com.alipay.sofa.jraft.util.Endpoint) JoinableClosure(com.alipay.sofa.jraft.closure.JoinableClosure) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 9 with RaftOptions

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

the class ReadOnlyServiceTest method setup.

@Before
public void setup() {
    this.readOnlyServiceImpl = new ReadOnlyServiceImpl();
    final ReadOnlyServiceOptions opts = new ReadOnlyServiceOptions();
    opts.setFsmCaller(this.fsmCaller);
    opts.setNode(this.node);
    opts.setRaftOptions(new RaftOptions());
    Mockito.when(this.node.getNodeMetrics()).thenReturn(new NodeMetrics(false));
    Mockito.when(this.node.getOptions()).thenReturn(new NodeOptions());
    Mockito.when(this.node.getGroupId()).thenReturn("test");
    Mockito.when(this.node.getServerId()).thenReturn(new PeerId("localhost:8081", 0));
    assertTrue(this.readOnlyServiceImpl.init(opts));
}
Also used : RaftOptions(com.alipay.sofa.jraft.option.RaftOptions) ReadOnlyServiceOptions(com.alipay.sofa.jraft.option.ReadOnlyServiceOptions) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) PeerId(com.alipay.sofa.jraft.entity.PeerId) Before(org.junit.Before)

Example 10 with RaftOptions

use of com.alipay.sofa.jraft.option.RaftOptions in project incubator-hugegraph by apache.

the class RaftSharedContext method nodeOptions.

public NodeOptions nodeOptions() throws IOException {
    HugeConfig config = this.config();
    PeerId selfId = new PeerId();
    selfId.parse(config.get(CoreOptions.RAFT_ENDPOINT));
    NodeOptions nodeOptions = new NodeOptions();
    nodeOptions.setEnableMetrics(false);
    nodeOptions.setRpcProcessorThreadPoolSize(config.get(CoreOptions.RAFT_RPC_THREADS));
    nodeOptions.setRpcConnectTimeoutMs(config.get(CoreOptions.RAFT_RPC_CONNECT_TIMEOUT));
    nodeOptions.setRpcDefaultTimeout(config.get(CoreOptions.RAFT_RPC_TIMEOUT));
    int electionTimeout = config.get(CoreOptions.RAFT_ELECTION_TIMEOUT);
    nodeOptions.setElectionTimeoutMs(electionTimeout);
    nodeOptions.setDisableCli(false);
    int snapshotInterval = config.get(CoreOptions.RAFT_SNAPSHOT_INTERVAL);
    nodeOptions.setSnapshotIntervalSecs(snapshotInterval);
    Configuration groupPeers = new Configuration();
    String groupPeersStr = config.get(CoreOptions.RAFT_GROUP_PEERS);
    if (!groupPeers.parse(groupPeersStr)) {
        throw new HugeException("Failed to parse group peers %s", groupPeersStr);
    }
    nodeOptions.setInitialConf(groupPeers);
    String raftPath = config.get(CoreOptions.RAFT_PATH);
    String logUri = Paths.get(raftPath, "log").toString();
    FileUtils.forceMkdir(new File(logUri));
    nodeOptions.setLogUri(logUri);
    String metaUri = Paths.get(raftPath, "meta").toString();
    FileUtils.forceMkdir(new File(metaUri));
    nodeOptions.setRaftMetaUri(metaUri);
    if (config.get(CoreOptions.RAFT_USE_SNAPSHOT)) {
        String snapshotUri = Paths.get(raftPath, "snapshot").toString();
        FileUtils.forceMkdir(new File(snapshotUri));
        nodeOptions.setSnapshotUri(snapshotUri);
    }
    RaftOptions raftOptions = nodeOptions.getRaftOptions();
    /*
         * NOTE: if buffer size is too small(<=1024), will throw exception
         * "LogManager is busy, disk queue overload"
         */
    raftOptions.setApplyBatch(config.get(CoreOptions.RAFT_APPLY_BATCH));
    raftOptions.setDisruptorBufferSize(config.get(CoreOptions.RAFT_QUEUE_SIZE));
    raftOptions.setDisruptorPublishEventWaitTimeoutSecs(config.get(CoreOptions.RAFT_QUEUE_PUBLISH_TIMEOUT));
    raftOptions.setReplicatorPipeline(config.get(CoreOptions.RAFT_REPLICATOR_PIPELINE));
    raftOptions.setOpenStatistics(false);
    raftOptions.setReadOnlyOptions(ReadOnlyOption.valueOf(config.get(CoreOptions.RAFT_READ_STRATEGY)));
    return nodeOptions;
}
Also used : RaftOptions(com.alipay.sofa.jraft.option.RaftOptions) Configuration(com.alipay.sofa.jraft.conf.Configuration) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) HugeConfig(com.baidu.hugegraph.config.HugeConfig) HugeException(com.baidu.hugegraph.HugeException) File(java.io.File) PeerId(com.alipay.sofa.jraft.entity.PeerId)

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