Search in sources :

Example 1 with Node

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

the class KVStateMachineTest method setup.

@Before
public void setup() throws IOException, InterruptedException {
    final Region region = new Region();
    region.setId(1);
    final StoreEngine storeEngine = new MockStoreEngine();
    final KVStoreStateMachine fsm = new KVStoreStateMachine(region, storeEngine);
    final NodeOptions nodeOpts = new NodeOptions();
    final Configuration conf = new Configuration();
    conf.addPeer(PeerId.parsePeer("127.0.0.1:8081"));
    nodeOpts.setInitialConf(conf);
    nodeOpts.setFsm(fsm);
    final String raftDataPath = "raft_st_test";
    this.raftDataPath = new File(raftDataPath);
    if (this.raftDataPath.exists()) {
        FileUtils.forceDelete(this.raftDataPath);
    }
    FileUtils.forceMkdir(this.raftDataPath);
    final Path logUri = Paths.get(raftDataPath, "log");
    nodeOpts.setLogUri(logUri.toString());
    final Path meteUri = Paths.get(raftDataPath, "meta");
    nodeOpts.setRaftMetaUri(meteUri.toString());
    final Path snapshotUri = Paths.get(raftDataPath, "snapshot");
    nodeOpts.setSnapshotUri(snapshotUri.toString());
    final Endpoint serverAddress = new Endpoint("127.0.0.1", 8081);
    final PeerId serverId = new PeerId(serverAddress, 0);
    this.raftGroupService = new RaftGroupService("st_test", serverId, nodeOpts, null, true);
    final Node node = this.raftGroupService.start(false);
    for (int i = 0; i < 100; i++) {
        if (node.isLeader()) {
            break;
        }
        Thread.sleep(100);
    }
    final RawKVStore rawKVStore = storeEngine.getRawKVStore();
    this.raftRawKVStore = new RaftRawKVStore(node, rawKVStore, null);
}
Also used : Path(java.nio.file.Path) Configuration(com.alipay.sofa.jraft.conf.Configuration) RaftGroupService(com.alipay.sofa.jraft.RaftGroupService) Node(com.alipay.sofa.jraft.Node) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) Endpoint(com.alipay.sofa.jraft.util.Endpoint) StoreEngine(com.alipay.sofa.jraft.rhea.StoreEngine) Endpoint(com.alipay.sofa.jraft.util.Endpoint) Region(com.alipay.sofa.jraft.rhea.metadata.Region) File(java.io.File) PeerId(com.alipay.sofa.jraft.entity.PeerId) Before(org.junit.Before)

Example 2 with Node

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

the class BaseCliRequestProcessorTest method testSingleNode.

@Test
public void testSingleNode() {
    Node node = this.mockNode(false);
    this.processor = new MockCliRequestProcessor(null, "test");
    this.processor.handleRequest(asyncContext, TestUtils.createPingRequest());
    ErrorResponse resp = (ErrorResponse) asyncContext.getResponseObject();
    assertNotNull(resp);
    assertSame(this.processor.ctx.node, node);
    assertNotNull(resp);
    assertEquals(0, resp.getErrorCode());
}
Also used : Node(com.alipay.sofa.jraft.Node) ErrorResponse(com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse) Test(org.junit.Test)

Example 3 with Node

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

the class BaseCliRequestProcessorTest method mockNode.

private Node mockNode(boolean disableCli) {
    Node node = Mockito.mock(Node.class);
    Mockito.when(node.getGroupId()).thenReturn("test");
    Mockito.when(node.getNodeId()).thenReturn(new NodeId("test", this.peer.copy()));
    NodeOptions opts = new NodeOptions();
    opts.setDisableCli(disableCli);
    Mockito.when(node.getOptions()).thenReturn(opts);
    NodeManager.getInstance().addAddress(this.peer.getEndpoint());
    NodeManager.getInstance().add(node);
    return node;
}
Also used : Node(com.alipay.sofa.jraft.Node) NodeId(com.alipay.sofa.jraft.entity.NodeId) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions)

Example 4 with Node

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

the class NodeRequestProcessorTest method testOK.

@Test
public void testOK() {
    Node node = Mockito.mock(Node.class, withSettings().extraInterfaces(RaftServerService.class));
    Mockito.when(node.getGroupId()).thenReturn("test");
    PeerId peerId = new PeerId("localhost", 8081);
    Mockito.when(node.getNodeId()).thenReturn(new NodeId("test", peerId));
    NodeManager.getInstance().addAddress(peerId.getEndpoint());
    NodeManager.getInstance().add(node);
    this.processor.handleRequest(asyncContext, TestUtils.createPingRequest());
    ErrorResponse resp = (ErrorResponse) asyncContext.getResponseObject();
    assertNotNull(resp);
    assertEquals(0, resp.getErrorCode());
}
Also used : Node(com.alipay.sofa.jraft.Node) NodeId(com.alipay.sofa.jraft.entity.NodeId) RaftServerService(com.alipay.sofa.jraft.rpc.RaftServerService) PeerId(com.alipay.sofa.jraft.entity.PeerId) ErrorResponse(com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse) Test(org.junit.Test)

Example 5 with Node

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

the class TestCluster method start.

public boolean start(final Endpoint listenAddr, final boolean emptyPeers, final int snapshotIntervalSecs, final boolean enableMetrics, final SnapshotThrottle snapshotThrottle, final RaftOptions raftOptions) throws IOException {
    if (this.serverMap.get(listenAddr.toString()) != null) {
        return true;
    }
    final NodeOptions nodeOptions = new NodeOptions();
    nodeOptions.setElectionTimeoutMs(this.electionTimeoutMs);
    nodeOptions.setEnableMetrics(enableMetrics);
    nodeOptions.setSnapshotThrottle(snapshotThrottle);
    nodeOptions.setSnapshotIntervalSecs(snapshotIntervalSecs);
    if (raftOptions != null) {
        nodeOptions.setRaftOptions(raftOptions);
    }
    final String serverDataPath = this.dataPath + File.separator + listenAddr.toString().replace(':', '_');
    FileUtils.forceMkdir(new File(serverDataPath));
    nodeOptions.setLogUri(serverDataPath + File.separator + "logs");
    nodeOptions.setRaftMetaUri(serverDataPath + File.separator + "meta");
    nodeOptions.setSnapshotUri(serverDataPath + File.separator + "snapshot");
    final MockStateMachine fsm = new MockStateMachine(listenAddr);
    nodeOptions.setFsm(fsm);
    if (!emptyPeers) {
        nodeOptions.setInitialConf(new Configuration(this.peers, this.learners));
    }
    final RpcServer rpcServer = RaftRpcServerFactory.createRaftRpcServer(listenAddr);
    final RaftGroupService server = new RaftGroupService(this.name, new PeerId(listenAddr, 0), nodeOptions, rpcServer);
    this.lock.lock();
    try {
        if (this.serverMap.put(listenAddr.toString(), server) == null) {
            final Node node = server.start();
            this.fsms.put(new PeerId(listenAddr, 0), fsm);
            this.nodes.add((NodeImpl) node);
            return true;
        }
    } finally {
        this.lock.unlock();
    }
    return false;
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) RpcServer(com.alipay.sofa.jraft.rpc.RpcServer) RaftGroupService(com.alipay.sofa.jraft.RaftGroupService) Node(com.alipay.sofa.jraft.Node) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) File(java.io.File) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Aggregations

Node (com.alipay.sofa.jraft.Node)88 PeerId (com.alipay.sofa.jraft.entity.PeerId)73 Test (org.junit.Test)64 Endpoint (com.alipay.sofa.jraft.util.Endpoint)41 CountDownLatch (java.util.concurrent.CountDownLatch)32 Configuration (com.alipay.sofa.jraft.conf.Configuration)24 ArrayList (java.util.ArrayList)21 Status (com.alipay.sofa.jraft.Status)20 Task (com.alipay.sofa.jraft.entity.Task)17 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)17 SynchronizedClosure (com.alipay.sofa.jraft.closure.SynchronizedClosure)14 ByteBuffer (java.nio.ByteBuffer)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)13 ReadIndexClosure (com.alipay.sofa.jraft.closure.ReadIndexClosure)10 RaftGroupService (com.alipay.sofa.jraft.RaftGroupService)7 RaftException (com.alipay.sofa.jraft.error.RaftException)7 LinkedHashSet (java.util.LinkedHashSet)7 NodeId (com.alipay.sofa.jraft.entity.NodeId)6 LogIndexOutOfBoundsException (com.alipay.sofa.jraft.error.LogIndexOutOfBoundsException)6 LogNotFoundException (com.alipay.sofa.jraft.error.LogNotFoundException)6