use of com.alipay.sofa.jraft.util.Endpoint in project sofa-jraft by sofastack.
the class ChaosTestCluster method getByStorePeer.
public synchronized RheaKVStore getByStorePeer(final PeerId peerId) {
awaitLeader();
final Endpoint endpoint = JRaftHelper.toPeer(peerId).getEndpoint();
for (final RheaKVStore store : this.stores) {
if (endpoint.equals(getSelfEndpoint(store))) {
return store;
}
}
throw new RuntimeException("fail to get peer: " + peerId);
}
use of com.alipay.sofa.jraft.util.Endpoint in project sofa-jraft by sofastack.
the class ChaosTestCluster method randomTransferLeader.
public synchronized void randomTransferLeader() {
final RheaKVStore leader = getLeaderStore();
final Endpoint leaderEndpoint = getSelfEndpoint(leader);
final PlacementDriverClient pdClient = leader.getPlacementDriverClient();
final Peer randomPeer = JRaftHelper.toPeer(getRandomPeer());
boolean result = pdClient.transferLeader(Constants.DEFAULT_REGION_ID, randomPeer, false);
if (!result) {
throw new RuntimeException("fail to transfer leader [" + leaderEndpoint + " --> " + randomPeer);
}
LOG.info("Transfer leader from {} to {}", leaderEndpoint, randomPeer.getEndpoint());
}
use of com.alipay.sofa.jraft.util.Endpoint 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);
}
use of com.alipay.sofa.jraft.util.Endpoint in project sofa-jraft by sofastack.
the class SnapshotExecutorTest method setup.
@Override
@Before
public void setup() throws Exception {
super.setup();
this.timerManager = new TimerManager(5);
this.raftOptions = new RaftOptions();
this.writer = new LocalSnapshotWriter(this.path, this.snapshotStorage, this.raftOptions);
this.reader = new LocalSnapshotReader(this.snapshotStorage, null, new Endpoint("localhost", 8081), this.raftOptions, this.path);
Mockito.when(this.snapshotStorage.open()).thenReturn(this.reader);
Mockito.when(this.snapshotStorage.create(true)).thenReturn(this.writer);
this.table = new LocalSnapshotMetaTable(this.raftOptions);
this.table.addFile("testFile", LocalFileMetaOutter.LocalFileMeta.newBuilder().setChecksum("test").build());
this.table.setMeta(RaftOutter.SnapshotMeta.newBuilder().setLastIncludedIndex(1).setLastIncludedTerm(1).build());
this.uri = "remote://" + this.hostPort + "/" + this.readerId;
this.copyOpts = new CopyOptions();
Mockito.when(this.node.getRaftOptions()).thenReturn(new RaftOptions());
Mockito.when(this.node.getOptions()).thenReturn(new NodeOptions());
Mockito.when(this.node.getRpcService()).thenReturn(this.raftClientService);
Mockito.when(this.node.getTimerManager()).thenReturn(this.timerManager);
Mockito.when(this.node.getServiceFactory()).thenReturn(DefaultJRaftServiceFactory.newInstance());
this.executor = new SnapshotExecutorImpl();
final SnapshotExecutorOptions opts = new SnapshotExecutorOptions();
opts.setFsmCaller(this.fSMCaller);
opts.setInitTerm(0);
opts.setNode(this.node);
opts.setLogManager(this.logManager);
opts.setUri(this.path);
this.addr = new Endpoint("localhost", 8081);
opts.setAddr(this.addr);
assertTrue(this.executor.init(opts));
}
use of com.alipay.sofa.jraft.util.Endpoint in project sofa-jraft by sofastack.
the class SnapshotExecutorTest method testInstallSnapshot.
@Test
public void testInstallSnapshot() throws Exception {
final RpcRequests.InstallSnapshotRequest.Builder irb = RpcRequests.InstallSnapshotRequest.newBuilder();
irb.setGroupId("test");
irb.setPeerId(this.addr.toString());
irb.setServerId("localhost:8080");
irb.setUri("remote://localhost:8080/99");
irb.setTerm(0);
irb.setMeta(RaftOutter.SnapshotMeta.newBuilder().setLastIncludedIndex(1).setLastIncludedTerm(2));
Mockito.when(this.raftClientService.connect(new Endpoint("localhost", 8080))).thenReturn(true);
final FutureImpl<Message> future = new FutureImpl<>();
final RpcRequests.GetFileRequest.Builder rb = RpcRequests.GetFileRequest.newBuilder().setReaderId(99).setFilename(Snapshot.JRAFT_SNAPSHOT_META_FILE).setCount(Integer.MAX_VALUE).setOffset(0).setReadPartly(true);
// mock get metadata
ArgumentCaptor<RpcResponseClosure> argument = ArgumentCaptor.forClass(RpcResponseClosure.class);
Mockito.when(this.raftClientService.getFile(eq(new Endpoint("localhost", 8080)), eq(rb.build()), eq(this.copyOpts.getTimeoutMs()), argument.capture())).thenReturn(future);
Utils.runInThread(new Runnable() {
@Override
public void run() {
SnapshotExecutorTest.this.executor.installSnapshot(irb.build(), RpcRequests.InstallSnapshotResponse.newBuilder(), new RpcRequestClosure(SnapshotExecutorTest.this.asyncCtx));
}
});
Thread.sleep(500);
RpcResponseClosure<RpcRequests.GetFileResponse> closure = argument.getValue();
final ByteBuffer metaBuf = this.table.saveToByteBufferAsRemote();
closure.setResponse(RpcRequests.GetFileResponse.newBuilder().setReadSize(metaBuf.remaining()).setEof(true).setData(ByteString.copyFrom(metaBuf)).build());
// mock get file
argument = ArgumentCaptor.forClass(RpcResponseClosure.class);
rb.setFilename("testFile");
rb.setCount(this.raftOptions.getMaxByteCountPerRpc());
Mockito.when(this.raftClientService.getFile(eq(new Endpoint("localhost", 8080)), eq(rb.build()), eq(this.copyOpts.getTimeoutMs()), argument.capture())).thenReturn(future);
closure.run(Status.OK());
Thread.sleep(500);
closure = argument.getValue();
closure.setResponse(RpcRequests.GetFileResponse.newBuilder().setReadSize(100).setEof(true).setData(ByteString.copyFrom(new byte[100])).build());
final ArgumentCaptor<LoadSnapshotClosure> loadSnapshotArg = ArgumentCaptor.forClass(LoadSnapshotClosure.class);
Mockito.when(this.fSMCaller.onSnapshotLoad(loadSnapshotArg.capture())).thenReturn(true);
closure.run(Status.OK());
Thread.sleep(500);
final LoadSnapshotClosure done = loadSnapshotArg.getValue();
final SnapshotReader reader = done.start();
assertNotNull(reader);
assertEquals(1, reader.listFiles().size());
assertTrue(reader.listFiles().contains("testFile"));
done.run(Status.OK());
this.executor.join();
assertEquals(2, this.executor.getLastSnapshotTerm());
assertEquals(1, this.executor.getLastSnapshotIndex());
}
Aggregations