Search in sources :

Example 1 with Endpoint

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);
}
Also used : RheaKVStore(com.alipay.sofa.jraft.rhea.client.RheaKVStore) DefaultRheaKVStore(com.alipay.sofa.jraft.rhea.client.DefaultRheaKVStore) Endpoint(com.alipay.sofa.jraft.util.Endpoint)

Example 2 with Endpoint

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());
}
Also used : RheaKVStore(com.alipay.sofa.jraft.rhea.client.RheaKVStore) DefaultRheaKVStore(com.alipay.sofa.jraft.rhea.client.DefaultRheaKVStore) Endpoint(com.alipay.sofa.jraft.util.Endpoint) Peer(com.alipay.sofa.jraft.rhea.metadata.Peer) PlacementDriverClient(com.alipay.sofa.jraft.rhea.client.pd.PlacementDriverClient)

Example 3 with Endpoint

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);
}
Also used : Path(java.nio.file.Path) Configuration(com.alipay.sofa.jraft.conf.Configuration) RaftGroupService(com.alipay.sofa.jraft.RaftGroupService) Node(com.alipay.sofa.jraft.Node) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) Endpoint(com.alipay.sofa.jraft.util.Endpoint) StoreEngine(com.alipay.sofa.jraft.rhea.StoreEngine) Endpoint(com.alipay.sofa.jraft.util.Endpoint) Region(com.alipay.sofa.jraft.rhea.metadata.Region) File(java.io.File) PeerId(com.alipay.sofa.jraft.entity.PeerId) Before(org.junit.Before)

Example 4 with Endpoint

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));
}
Also used : RaftOptions(com.alipay.sofa.jraft.option.RaftOptions) Endpoint(com.alipay.sofa.jraft.util.Endpoint) LocalSnapshotMetaTable(com.alipay.sofa.jraft.storage.snapshot.local.LocalSnapshotMetaTable) LocalSnapshotReader(com.alipay.sofa.jraft.storage.snapshot.local.LocalSnapshotReader) SnapshotExecutorImpl(com.alipay.sofa.jraft.storage.snapshot.SnapshotExecutorImpl) CopyOptions(com.alipay.sofa.jraft.option.CopyOptions) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) TimerManager(com.alipay.sofa.jraft.core.TimerManager) LocalSnapshotWriter(com.alipay.sofa.jraft.storage.snapshot.local.LocalSnapshotWriter) SnapshotExecutorOptions(com.alipay.sofa.jraft.option.SnapshotExecutorOptions) Before(org.junit.Before)

Example 5 with Endpoint

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());
}
Also used : Message(com.google.protobuf.Message) RpcResponseClosure(com.alipay.sofa.jraft.rpc.RpcResponseClosure) ByteBuffer(java.nio.ByteBuffer) RpcRequestClosure(com.alipay.sofa.jraft.rpc.RpcRequestClosure) LoadSnapshotClosure(com.alipay.sofa.jraft.closure.LoadSnapshotClosure) Endpoint(com.alipay.sofa.jraft.util.Endpoint) FutureImpl(com.alipay.sofa.jraft.rpc.impl.FutureImpl) LocalSnapshotReader(com.alipay.sofa.jraft.storage.snapshot.local.LocalSnapshotReader) SnapshotReader(com.alipay.sofa.jraft.storage.snapshot.SnapshotReader) Test(org.junit.Test)

Aggregations

Endpoint (com.alipay.sofa.jraft.util.Endpoint)77 Test (org.junit.Test)34 PeerId (com.alipay.sofa.jraft.entity.PeerId)28 Node (com.alipay.sofa.jraft.Node)23 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)18 Configuration (com.alipay.sofa.jraft.conf.Configuration)12 CountDownLatch (java.util.concurrent.CountDownLatch)11 Status (com.alipay.sofa.jraft.Status)8 File (java.io.File)7 ByteBuffer (java.nio.ByteBuffer)7 RaftOptions (com.alipay.sofa.jraft.option.RaftOptions)6 Region (com.alipay.sofa.jraft.rhea.metadata.Region)6 PlacementDriverClient (com.alipay.sofa.jraft.rhea.client.pd.PlacementDriverClient)5 RheaKVStoreOptions (com.alipay.sofa.jraft.rhea.options.RheaKVStoreOptions)5 RpcResponseClosure (com.alipay.sofa.jraft.rpc.RpcResponseClosure)5 RpcServer (com.alipay.sofa.jraft.rpc.RpcServer)5 FutureImpl (com.alipay.sofa.jraft.rpc.impl.FutureImpl)5 SnapshotReader (com.alipay.sofa.jraft.storage.snapshot.SnapshotReader)5 Message (com.google.protobuf.Message)5 RaftGroupService (com.alipay.sofa.jraft.RaftGroupService)4