Search in sources :

Example 81 with Node

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

the class NodeTest method testNodesWithPriorityElection.

@Test
public void testNodesWithPriorityElection() throws Exception {
    List<Integer> priorities = new ArrayList<>();
    priorities.add(100);
    priorities.add(40);
    priorities.add(40);
    final List<PeerId> peers = TestUtils.generatePriorityPeers(3, priorities);
    final TestCluster cluster = new TestCluster("unittest", this.dataPath, peers);
    for (final PeerId peer : peers) {
        assertTrue(cluster.start(peer.getEndpoint(), peer.getPriority()));
    }
    // elect leader
    cluster.waitLeader();
    // get leader
    final Node leader = cluster.getLeader();
    assertNotNull(leader);
    assertEquals(3, leader.listPeers().size());
    assertEquals(100, leader.getNodeTargetPriority());
    assertEquals(100, leader.getLeaderId().getPriority());
    assertEquals(2, cluster.getFollowers().size());
    cluster.stopAll();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Node(com.alipay.sofa.jraft.Node) ArrayList(java.util.ArrayList) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 82 with Node

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

the class BaseCliRequestProcessorTest method testManyNodes.

@Test
public void testManyNodes() {
    Node node1 = Mockito.mock(Node.class);
    Mockito.when(node1.getGroupId()).thenReturn("test");
    Mockito.when(node1.getNodeId()).thenReturn(new NodeId("test", new PeerId("localhost", 8081)));
    NodeOptions opts = new NodeOptions();
    Mockito.when(node1.getOptions()).thenReturn(opts);
    NodeManager.getInstance().addAddress(new Endpoint("localhost", 8081));
    NodeManager.getInstance().add(node1);
    Node node2 = Mockito.mock(Node.class);
    Mockito.when(node2.getGroupId()).thenReturn("test");
    Mockito.when(node2.getNodeId()).thenReturn(new NodeId("test", new PeerId("localhost", 8082)));
    Mockito.when(node2.getOptions()).thenReturn(opts);
    NodeManager.getInstance().addAddress(new Endpoint("localhost", 8082));
    NodeManager.getInstance().add(node2);
    this.processor = new MockCliRequestProcessor(null, "test");
    this.processor.handleRequest(asyncContext, TestUtils.createPingRequest());
    ErrorResponse resp = (ErrorResponse) asyncContext.getResponseObject();
    assertNotNull(resp);
    assertEquals(RaftError.EINVAL.getNumber(), resp.getErrorCode());
    assertEquals("Peer must be specified since there're 2 nodes in group test", resp.getErrorMsg());
}
Also used : Endpoint(com.alipay.sofa.jraft.util.Endpoint) Node(com.alipay.sofa.jraft.Node) NodeId(com.alipay.sofa.jraft.entity.NodeId) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) PeerId(com.alipay.sofa.jraft.entity.PeerId) ErrorResponse(com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse) Test(org.junit.Test)

Example 83 with Node

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

the class BaseCliRequestProcessorTest method testOK.

@Test
public void testOK() {
    Node node = mockNode(false);
    this.processor.handleRequest(asyncContext, TestUtils.createPingRequest());
    ErrorResponse resp = (ErrorResponse) asyncContext.getResponseObject();
    assertNotNull(this.processor.done);
    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 84 with Node

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

the class AppendEntriesRequestProcessor method getOrCreatePeerRequestContext.

@SuppressWarnings("unchecked")
PeerRequestContext getOrCreatePeerRequestContext(final String groupId, final PeerPair pair, final Connection conn) {
    ConcurrentMap<PeerPair, PeerRequestContext> groupContexts = this.peerRequestContexts.get(groupId);
    if (groupContexts == null) {
        groupContexts = new ConcurrentHashMap<>();
        final ConcurrentMap<PeerPair, PeerRequestContext> existsCtxs = this.peerRequestContexts.putIfAbsent(groupId, groupContexts);
        if (existsCtxs != null) {
            groupContexts = existsCtxs;
        }
    }
    PeerRequestContext peerCtx = groupContexts.get(pair);
    if (peerCtx == null) {
        synchronized (Utils.withLockObject(groupContexts)) {
            peerCtx = groupContexts.get(pair);
            // double check in lock
            if (peerCtx == null) {
                // only one thread to process append entries for every jraft node
                final PeerId peer = new PeerId();
                final boolean parsed = peer.parse(pair.local);
                assert (parsed);
                final Node node = NodeManager.getInstance().get(groupId, peer);
                assert (node != null);
                peerCtx = new PeerRequestContext(groupId, pair, node.getRaftOptions().getMaxReplicatorInflightMsgs());
                groupContexts.put(pair, peerCtx);
            }
        }
    }
    // Add the pair to connection attribute metadata.
    if (conn != null) {
        Set<PeerPair> pairs;
        if ((pairs = (Set<AppendEntriesRequestProcessor.PeerPair>) conn.getAttribute(PAIR_ATTR)) == null) {
            pairs = new ConcurrentHashSet<>();
            Set<PeerPair> existsPairs = (Set<PeerPair>) conn.setAttributeIfAbsent(PAIR_ATTR, pairs);
            if (existsPairs != null) {
                pairs = existsPairs;
            }
        }
        pairs.add(pair);
    }
    return peerCtx;
}
Also used : ConcurrentHashSet(com.alipay.sofa.jraft.util.concurrent.ConcurrentHashSet) Set(java.util.Set) Node(com.alipay.sofa.jraft.Node) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 85 with Node

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

the class AppendEntriesRequestProcessor method processRequest0.

@Override
public Message processRequest0(final RaftServerService service, final AppendEntriesRequest request, final RpcRequestClosure done) {
    final Node node = (Node) service;
    if (node.getRaftOptions().isReplicatorPipeline()) {
        final String groupId = request.getGroupId();
        final PeerPair pair = pairOf(request.getPeerId(), request.getServerId());
        boolean isHeartbeat = isHeartbeatRequest(request);
        int reqSequence = -1;
        if (!isHeartbeat) {
            reqSequence = getAndIncrementSequence(groupId, pair, done.getRpcCtx().getConnection());
        }
        final Message response = service.handleAppendEntriesRequest(request, new SequenceRpcRequestClosure(done, defaultResp(), groupId, pair, reqSequence, isHeartbeat));
        if (response != null) {
            if (isHeartbeat) {
                done.getRpcCtx().sendResponse(response);
            } else {
                sendSequenceResponse(groupId, pair, reqSequence, done.getRpcCtx(), response);
            }
        }
        return null;
    } else {
        return service.handleAppendEntriesRequest(request, done);
    }
}
Also used : Message(com.google.protobuf.Message) Node(com.alipay.sofa.jraft.Node)

Aggregations

Node (com.alipay.sofa.jraft.Node)90 PeerId (com.alipay.sofa.jraft.entity.PeerId)74 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)25 ArrayList (java.util.ArrayList)22 Status (com.alipay.sofa.jraft.Status)21 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)14 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)13 ReadIndexClosure (com.alipay.sofa.jraft.closure.ReadIndexClosure)10 RaftException (com.alipay.sofa.jraft.error.RaftException)8 RaftGroupService (com.alipay.sofa.jraft.RaftGroupService)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