use of com.alipay.sofa.jraft.util.Endpoint in project sofa-jraft by sofastack.
the class JRaftHelper method toJRaftPeerId.
public static PeerId toJRaftPeerId(final Peer peer) {
Requires.requireNonNull(peer, "peer");
final Endpoint endpoint = peer.getEndpoint();
Requires.requireNonNull(endpoint, "peer.endpoint");
return new PeerId(endpoint, 0);
}
use of com.alipay.sofa.jraft.util.Endpoint in project sofa-jraft by sofastack.
the class TestCluster method stopAll.
public void stopAll() throws InterruptedException {
final List<Endpoint> addrs = getAllNodes();
final List<Node> nodes = new ArrayList<>();
for (final Endpoint addr : addrs) {
final Node node = removeNode(addr);
node.shutdown();
nodes.add(node);
this.serverMap.remove(addr.toString()).shutdown();
}
for (final Node node : nodes) {
node.join();
}
CLUSTERS.remove(this);
}
use of com.alipay.sofa.jraft.util.Endpoint in project sofa-jraft by sofastack.
the class TestCluster method removeNode.
public Node removeNode(final Endpoint addr) {
Node ret = null;
this.lock.lock();
try {
for (int i = 0; i < this.nodes.size(); i++) {
if (this.nodes.get(i).getNodeId().getPeerId().getEndpoint().equals(addr)) {
ret = this.nodes.remove(i);
this.fsms.remove(ret.getNodeId().getPeerId());
break;
}
}
} finally {
this.lock.unlock();
}
return ret;
}
use of com.alipay.sofa.jraft.util.Endpoint in project sofa-jraft by sofastack.
the class NodeTest method testRecoverFollower.
@Test
public void testRecoverFollower() throws Exception {
final List<PeerId> peers = TestUtils.generatePeers(3);
final TestCluster cluster = new TestCluster("unitest", this.dataPath, peers);
for (final PeerId peer : peers) {
assertTrue(cluster.start(peer.getEndpoint()));
}
cluster.waitLeader();
final Node leader = cluster.getLeader();
assertNotNull(leader);
Thread.sleep(100);
final List<Node> followers = cluster.getFollowers();
assertEquals(2, followers.size());
final Endpoint followerAddr = followers.get(0).getNodeId().getPeerId().getEndpoint().copy();
assertTrue(cluster.stop(followerAddr));
this.sendTestTaskAndWait(leader);
for (int i = 10; i < 30; i++) {
final ByteBuffer data = ByteBuffer.wrap(("no clusre" + i).getBytes());
final Task task = new Task(data, null);
leader.apply(task);
}
// wait leader to compact logs
Thread.sleep(5000);
// restart follower
assertTrue(cluster.start(followerAddr));
assertTrue(cluster.ensureSame(30));
assertEquals(3, cluster.getFsms().size());
for (final MockStateMachine fsm : cluster.getFsms()) {
assertEquals(30, fsm.getLogs().size());
}
cluster.stopAll();
}
use of com.alipay.sofa.jraft.util.Endpoint 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();
}
}
Aggregations