use of com.alipay.sofa.jraft.util.Endpoint in project sofa-jraft by sofastack.
the class NodeTest method testRemoveLeaderWithPriority.
@Test
public void testRemoveLeaderWithPriority() throws Exception {
final List<Integer> priorities = new ArrayList<Integer>();
priorities.add(100);
priorities.add(60);
priorities.add(10);
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
Node leader = cluster.getLeader();
assertNotNull(leader);
assertEquals(100, leader.getNodeTargetPriority());
assertEquals(100, leader.getNodeId().getPeerId().getPriority());
final List<Node> followers = cluster.getFollowers();
assertEquals(2, followers.size());
final PeerId oldLeader = leader.getNodeId().getPeerId().copy();
final Endpoint oldLeaderAddr = oldLeader.getEndpoint();
// remove old leader
LOG.info("Remove old leader {}", oldLeader);
CountDownLatch latch = new CountDownLatch(1);
leader.removePeer(oldLeader, new ExpectClosure(latch));
waitLatch(latch);
assertEquals(60, leader.getNodeTargetPriority());
// stop and clean old leader
LOG.info("Stop and clean old leader {}", oldLeader);
assertTrue(cluster.stop(oldLeaderAddr));
cluster.clean(oldLeaderAddr);
// elect new leader
cluster.waitLeader();
leader = cluster.getLeader();
LOG.info("New leader is {}", leader);
assertNotNull(leader);
assertNotSame(leader, oldLeader);
cluster.stopAll();
}
use of com.alipay.sofa.jraft.util.Endpoint in project sofa-jraft by sofastack.
the class NodeTest method testSingleNodeWithLearner.
@Test
public void testSingleNodeWithLearner() throws Exception {
final Endpoint addr = new Endpoint(TestUtils.getMyIp(), TestUtils.INIT_PORT);
final PeerId peer = new PeerId(addr, 0);
final Endpoint learnerAddr = new Endpoint(TestUtils.getMyIp(), TestUtils.INIT_PORT + 1);
final PeerId learnerPeer = new PeerId(learnerAddr, 0);
NodeManager.getInstance().addAddress(addr);
NodeManager.getInstance().addAddress(learnerAddr);
MockStateMachine learnerFsm = null;
Node learner = null;
RaftGroupService learnerServer = null;
{
// Start learner
final NodeOptions nodeOptions = createNodeOptionsWithSharedTimer();
learnerFsm = new MockStateMachine(learnerAddr);
nodeOptions.setFsm(learnerFsm);
nodeOptions.setLogUri(this.dataPath + File.separator + "log1");
nodeOptions.setRaftMetaUri(this.dataPath + File.separator + "meta1");
nodeOptions.setSnapshotUri(this.dataPath + File.separator + "snapshot1");
nodeOptions.setInitialConf(new Configuration(Collections.singletonList(peer), Collections.singletonList(learnerPeer)));
final RpcServer rpcServer = RaftRpcServerFactory.createRaftRpcServer(learnerAddr);
learnerServer = new RaftGroupService("unittest", new PeerId(learnerAddr, 0), nodeOptions, rpcServer);
learner = learnerServer.start();
}
{
// Start leader
final NodeOptions nodeOptions = createNodeOptionsWithSharedTimer();
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), Collections.singletonList(learnerPeer)));
final Node node = new NodeImpl("unittest", peer);
assertTrue(node.init(nodeOptions));
assertEquals(1, node.listPeers().size());
assertTrue(node.listPeers().contains(peer));
while (!node.isLeader()) {
;
}
sendTestTaskAndWait(node);
assertEquals(10, fsm.getLogs().size());
int i = 0;
for (final ByteBuffer data : fsm.getLogs()) {
assertEquals("hello" + i++, new String(data.array()));
}
// wait for entries to be replicated to learner.
Thread.sleep(1000);
node.shutdown();
node.join();
}
{
// assert learner fsm
assertEquals(10, learnerFsm.getLogs().size());
int i = 0;
for (final ByteBuffer data : learnerFsm.getLogs()) {
assertEquals("hello" + i++, new String(data.array()));
}
learnerServer.shutdown();
learnerServer.join();
}
}
use of com.alipay.sofa.jraft.util.Endpoint in project sofa-jraft by sofastack.
the class PeerIdTest method testIsPriorityNotElected.
@Test
public void testIsPriorityNotElected() {
final Endpoint endpoint1 = new Endpoint("192.168.1.1", 8081);
final PeerId peer1 = new PeerId(endpoint1, 0, 0);
assertEquals("192.168.1.1:8081::0", peer1.toString());
assertTrue(peer1.isPriorityNotElected());
}
use of com.alipay.sofa.jraft.util.Endpoint in project sofa-jraft by sofastack.
the class RheaBenchmarkCluster method start.
protected void start() throws IOException, InterruptedException {
SystemPropertyUtil.setProperty(Configs.NETTY_BUFFER_LOW_WATERMARK, Integer.toString(256 * 1024));
SystemPropertyUtil.setProperty(Configs.NETTY_BUFFER_HIGH_WATERMARK, Integer.toString(512 * 1024));
File file = new File("benchmark_rhea_db");
if (file.exists()) {
FileUtils.forceDelete(file);
}
file = new File("benchmark_rhea_db");
if (file.mkdir()) {
this.tempDbPath = file.getAbsolutePath();
System.out.println("make dir: " + this.tempDbPath);
}
file = new File("benchmark_rhea_raft");
if (file.exists()) {
FileUtils.forceDelete(file);
}
file = new File("benchmark_rhea_raft");
if (file.mkdir()) {
this.tempRaftPath = file.getAbsolutePath();
System.out.println("make dir: " + this.tempRaftPath);
}
for (String c : CONF) {
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
final RheaKVStoreOptions opts = mapper.readValue(new File(c), RheaKVStoreOptions.class);
RheaKVStore rheaKVStore = new DefaultRheaKVStore();
if (rheaKVStore.init(opts)) {
stores.add(rheaKVStore);
} else {
throw new RuntimeException("Fail to init rhea kv store witch conf: " + c);
}
}
PlacementDriverClient pdClient = stores.get(0).getPlacementDriverClient();
Endpoint leader1 = pdClient.getLeader(1, true, 10000);
System.out.println("The region 1 leader is: " + leader1);
// Endpoint leader2 = pdClient.getLeader(2, true, 10000);
// System.out.println("The region 2 leader is: " + leader2);
// Endpoint leader3 = pdClient.getLeader(3, true, 10000);
// System.out.println("The region 3 leader is: " + leader3);
}
use of com.alipay.sofa.jraft.util.Endpoint in project sofa-jraft by sofastack.
the class RemoteFileCopier method init.
public boolean init(String uri, final SnapshotThrottle snapshotThrottle, final SnapshotCopierOptions opts) {
this.rpcService = opts.getRaftClientService();
this.timerManager = opts.getTimerManager();
this.raftOptions = opts.getRaftOptions();
this.snapshotThrottle = snapshotThrottle;
final int prefixSize = Snapshot.REMOTE_SNAPSHOT_URI_SCHEME.length();
if (uri == null || !uri.startsWith(Snapshot.REMOTE_SNAPSHOT_URI_SCHEME)) {
LOG.error("Invalid uri {}.", uri);
return false;
}
uri = uri.substring(prefixSize);
final int slasPos = uri.indexOf('/');
final String ipAndPort = uri.substring(0, slasPos);
uri = uri.substring(slasPos + 1);
try {
this.readId = Long.parseLong(uri);
final String[] ipAndPortStrs = ipAndPort.split(":");
this.endpoint = new Endpoint(ipAndPortStrs[0], Integer.parseInt(ipAndPortStrs[1]));
} catch (final Exception e) {
LOG.error("Fail to parse readerId or endpoint.", e);
return false;
}
if (!this.rpcService.connect(this.endpoint)) {
LOG.error("Fail to init channel to {}.", this.endpoint);
return false;
}
return true;
}
Aggregations