Search in sources :

Example 1 with NodeImpl

use of com.alipay.sofa.jraft.core.NodeImpl in project nacos by alibaba.

the class JRaftServerTest method testCommit.

@Test
public void testCommit() throws NoSuchFieldException, IllegalAccessException, TimeoutException, InterruptedException {
    WriteRequest.Builder writeRequestBuilder = WriteRequest.newBuilder();
    WriteRequest writeRequest = writeRequestBuilder.build();
    // No group is set, and make sure that an IllegalArgumentException will be thrown.
    CompletableFuture<Response> future = server.commit(groupId, writeRequest, this.future);
    verify(future).completeExceptionally(any(IllegalArgumentException.class));
    // Set an group.
    Collection<RequestProcessor4CP> processors = Collections.singletonList(mockProcessor4CP);
    server.createMultiRaftGroup(processors);
    Field cliClientServiceField = JRaftServer.class.getDeclaredField("cliClientService");
    cliClientServiceField.setAccessible(true);
    cliClientServiceField.set(server, cliClientServiceMock);
    // Make the node leader and verify the invokeToLeader is never called.
    NodeImpl node = (NodeImpl) server.findNodeByGroup(groupId);
    Field stateField = NodeImpl.class.getDeclaredField("state");
    stateField.setAccessible(true);
    stateField.set(node, State.STATE_LEADER);
    server.commit(groupId, writeRequest, future);
    verify(cliClientServiceMock, never()).getRpcClient();
    // make the node follower and verify the invokeToLeader is called.
    node = (NodeImpl) server.findNodeByGroup(groupId);
    stateField.setAccessible(true);
    stateField.set(node, State.STATE_FOLLOWER);
    RouteTable.getInstance().updateLeader(groupId, peerId1);
    server.commit(groupId, writeRequest, future);
    verify(cliClientServiceMock).getRpcClient();
}
Also used : Response(com.alibaba.nacos.consistency.entity.Response) Field(java.lang.reflect.Field) NodeImpl(com.alipay.sofa.jraft.core.NodeImpl) WriteRequest(com.alibaba.nacos.consistency.entity.WriteRequest) RequestProcessor4CP(com.alibaba.nacos.consistency.cp.RequestProcessor4CP) Test(org.junit.Test)

Example 2 with NodeImpl

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

the class JRaftUtils method bootstrap.

/**
 * Bootstrap a non-empty raft node.
 *
 * @param opts options of bootstrap
 * @return true if bootstrap success
 */
public static boolean bootstrap(final BootstrapOptions opts) throws InterruptedException {
    final NodeImpl node = new NodeImpl();
    final boolean ret = node.bootstrap(opts);
    node.shutdown();
    node.join();
    return ret;
}
Also used : NodeImpl(com.alipay.sofa.jraft.core.NodeImpl)

Aggregations

NodeImpl (com.alipay.sofa.jraft.core.NodeImpl)2 RequestProcessor4CP (com.alibaba.nacos.consistency.cp.RequestProcessor4CP)1 Response (com.alibaba.nacos.consistency.entity.Response)1 WriteRequest (com.alibaba.nacos.consistency.entity.WriteRequest)1 Field (java.lang.reflect.Field)1 Test (org.junit.Test)1