Search in sources :

Example 1 with RaftMessagesFactory

use of org.apache.ignite.raft.jraft.RaftMessagesFactory in project ignite-3 by apache.

the class MessageFileTest method testSaveLoad.

@Test
public void testSaveLoad() throws Exception {
    File tempFile = File.createTempFile("test", "msgfile");
    String path = tempFile.getAbsolutePath();
    tempFile.delete();
    MessageFile file = new MessageFile(path);
    assertNull(file.load());
    LocalFileMetaOutter.LocalFileMeta msg = new RaftMessagesFactory().localFileMeta().checksum("test").source(LocalFileMetaOutter.FileSource.FILE_SOURCE_REFERENCE).build();
    assertTrue(file.save(msg, true));
    MessageFile newFile = new MessageFile(path);
    LocalFileMetaOutter.LocalFileMeta loadedMsg = newFile.load();
    assertNotNull(loadedMsg);
    assertEquals("test", loadedMsg.checksum());
    assertEquals(LocalFileMetaOutter.FileSource.FILE_SOURCE_REFERENCE, loadedMsg.source());
    new File(path).delete();
    assertNull(newFile.load());
}
Also used : LocalFileMetaOutter(org.apache.ignite.raft.jraft.entity.LocalFileMetaOutter) RaftMessagesFactory(org.apache.ignite.raft.jraft.RaftMessagesFactory) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 2 with RaftMessagesFactory

use of org.apache.ignite.raft.jraft.RaftMessagesFactory in project ignite-3 by apache.

the class PingRequestProcessorTest method testHandlePing.

@Test
public void testHandlePing() throws Exception {
    PingRequestProcessor processor = new PingRequestProcessor(null, new RaftMessagesFactory());
    MockAsyncContext ctx = new MockAsyncContext();
    processor.handleRequest(ctx, TestUtils.createPingRequest());
    ErrorResponse response = (ErrorResponse) ctx.getResponseObject();
    assertEquals(0, response.errorCode());
}
Also used : MockAsyncContext(org.apache.ignite.raft.jraft.test.MockAsyncContext) RaftMessagesFactory(org.apache.ignite.raft.jraft.RaftMessagesFactory) ErrorResponse(org.apache.ignite.raft.jraft.rpc.RpcRequests.ErrorResponse) Test(org.junit.jupiter.api.Test)

Example 3 with RaftMessagesFactory

use of org.apache.ignite.raft.jraft.RaftMessagesFactory in project ignite-3 by apache.

the class SnapshotExecutorTest method testInstallSnapshot.

@Test
public void testInstallSnapshot() throws Exception {
    RaftMessagesFactory msgFactory = raftOptions.getRaftMessagesFactory();
    final RpcRequests.InstallSnapshotRequest irb = msgFactory.installSnapshotRequest().groupId("test").peerId(addr.toString()).serverId("localhost:8080").uri("remote://localhost:8080/99").term(0).meta(msgFactory.snapshotMeta().lastIncludedIndex(1).lastIncludedTerm(2).build()).build();
    Mockito.when(raftClientService.connect(new Endpoint("localhost", 8080))).thenReturn(true);
    final CompletableFuture<Message> fut = new CompletableFuture<>();
    final GetFileRequestBuilder rb = msgFactory.getFileRequest().readerId(99).filename(Snapshot.JRAFT_SNAPSHOT_META_FILE).count(Integer.MAX_VALUE).offset(0).readPartly(true);
    // Mock get metadata
    ArgumentCaptor<RpcResponseClosure> argument = ArgumentCaptor.forClass(RpcResponseClosure.class);
    Mockito.when(raftClientService.getFile(eq(new Endpoint("localhost", 8080)), eq(rb.build()), eq(copyOpts.getTimeoutMs()), argument.capture())).thenReturn(fut);
    Future<?> snapFut = Utils.runInThread(ForkJoinPool.commonPool(), () -> executor.installSnapshot(irb, msgFactory.installSnapshotResponse(), new RpcRequestClosure(asyncCtx, msgFactory)));
    assertTrue(TestUtils.waitForArgumentCapture(argument, 5_000));
    RpcResponseClosure<RpcRequests.GetFileResponse> closure = argument.getValue();
    final ByteBuffer metaBuf = table.saveToByteBufferAsRemote();
    closure.setResponse(msgFactory.getFileResponse().readSize(metaBuf.remaining()).eof(true).data(new ByteString(metaBuf)).build());
    // Mock get file
    argument = ArgumentCaptor.forClass(RpcResponseClosure.class);
    rb.filename("testFile");
    rb.count(raftOptions.getMaxByteCountPerRpc());
    Mockito.when(raftClientService.getFile(eq(new Endpoint("localhost", 8080)), eq(rb.build()), eq(copyOpts.getTimeoutMs()), argument.capture())).thenReturn(fut);
    closure.run(Status.OK());
    assertTrue(TestUtils.waitForArgumentCapture(argument, 5_000));
    closure = argument.getValue();
    closure.setResponse(msgFactory.getFileResponse().readSize(100).eof(true).data(new ByteString(new byte[100])).build());
    ArgumentCaptor<LoadSnapshotClosure> loadSnapshotArg = ArgumentCaptor.forClass(LoadSnapshotClosure.class);
    Mockito.when(fSMCaller.onSnapshotLoad(loadSnapshotArg.capture())).thenReturn(true);
    closure.run(Status.OK());
    assertTrue(TestUtils.waitForArgumentCapture(loadSnapshotArg, 5_000));
    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());
    executor.join();
    assertTrue(snapFut.isDone());
    assertEquals(2, executor.getLastSnapshotTerm());
    assertEquals(1, executor.getLastSnapshotIndex());
}
Also used : Message(org.apache.ignite.raft.jraft.rpc.Message) ByteString(org.apache.ignite.raft.jraft.util.ByteString) RpcResponseClosure(org.apache.ignite.raft.jraft.rpc.RpcResponseClosure) RaftMessagesFactory(org.apache.ignite.raft.jraft.RaftMessagesFactory) RpcRequests(org.apache.ignite.raft.jraft.rpc.RpcRequests) ByteBuffer(java.nio.ByteBuffer) RpcRequestClosure(org.apache.ignite.raft.jraft.rpc.RpcRequestClosure) LoadSnapshotClosure(org.apache.ignite.raft.jraft.closure.LoadSnapshotClosure) CompletableFuture(java.util.concurrent.CompletableFuture) GetFileRequestBuilder(org.apache.ignite.raft.jraft.rpc.GetFileRequestBuilder) Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) SnapshotReader(org.apache.ignite.raft.jraft.storage.snapshot.SnapshotReader) LocalSnapshotReader(org.apache.ignite.raft.jraft.storage.snapshot.local.LocalSnapshotReader) Test(org.junit.jupiter.api.Test)

Example 4 with RaftMessagesFactory

use of org.apache.ignite.raft.jraft.RaftMessagesFactory in project ignite-3 by apache.

the class SnapshotExecutorTest method testInterruptInstalling.

@Test
public void testInterruptInstalling() throws Exception {
    RaftMessagesFactory msgFactory = raftOptions.getRaftMessagesFactory();
    final RpcRequests.InstallSnapshotRequest irb = msgFactory.installSnapshotRequest().groupId("test").peerId(addr.toString()).serverId("localhost:8080").uri("remote://localhost:8080/99").term(0).meta(msgFactory.snapshotMeta().lastIncludedIndex(1).lastIncludedTerm(1).build()).build();
    Mockito.lenient().when(raftClientService.connect(new Endpoint("localhost", 8080))).thenReturn(true);
    final CompletableFuture<Message> future = new CompletableFuture<>();
    final RpcRequests.GetFileRequest rb = msgFactory.getFileRequest().readerId(99).filename(Snapshot.JRAFT_SNAPSHOT_META_FILE).count(Integer.MAX_VALUE).offset(0).readPartly(true).build();
    // Mock get metadata
    final ArgumentCaptor<RpcResponseClosure> argument = ArgumentCaptor.forClass(RpcResponseClosure.class);
    Mockito.lenient().when(raftClientService.getFile(eq(new Endpoint("localhost", 8080)), eq(rb), eq(copyOpts.getTimeoutMs()), argument.capture())).thenReturn(future);
    ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
    executorService = singleThreadExecutor;
    Utils.runInThread(singleThreadExecutor, () -> executor.installSnapshot(irb, msgFactory.installSnapshotResponse(), new RpcRequestClosure(asyncCtx, msgFactory)));
    executor.interruptDownloadingSnapshots(1);
    executor.join();
    assertEquals(0, executor.getLastSnapshotTerm());
    assertEquals(0, executor.getLastSnapshotIndex());
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) Message(org.apache.ignite.raft.jraft.rpc.Message) RpcResponseClosure(org.apache.ignite.raft.jraft.rpc.RpcResponseClosure) ExecutorService(java.util.concurrent.ExecutorService) RaftMessagesFactory(org.apache.ignite.raft.jraft.RaftMessagesFactory) RpcRequests(org.apache.ignite.raft.jraft.rpc.RpcRequests) RpcRequestClosure(org.apache.ignite.raft.jraft.rpc.RpcRequestClosure) Test(org.junit.jupiter.api.Test)

Example 5 with RaftMessagesFactory

use of org.apache.ignite.raft.jraft.RaftMessagesFactory in project ignite-3 by apache.

the class FSMCallerTest method setup.

@BeforeEach
public void setup() {
    this.fsmCaller = new FSMCallerImpl();
    NodeOptions options = new NodeOptions();
    executor = JRaftUtils.createExecutor("test-executor-", Utils.cpus());
    options.setCommonExecutor(executor);
    this.closureQueue = new ClosureQueueImpl(options);
    opts = new FSMCallerOptions();
    Mockito.when(this.node.getNodeMetrics()).thenReturn(new NodeMetrics(false));
    Mockito.when(this.node.getOptions()).thenReturn(options);
    opts.setNode(this.node);
    opts.setFsm(this.fsm);
    opts.setLogManager(this.logManager);
    opts.setBootstrapId(new LogId(10, 1));
    opts.setClosureQueue(this.closureQueue);
    opts.setRaftMessagesFactory(new RaftMessagesFactory());
    opts.setGroupId("TestSrv");
    opts.setfSMCallerExecutorDisruptor(disruptor = new StripedDisruptor<>("TestFSMDisruptor", 1024, () -> new FSMCallerImpl.ApplyTask(), 1));
    assertTrue(this.fsmCaller.init(opts));
}
Also used : FSMCallerOptions(org.apache.ignite.raft.jraft.option.FSMCallerOptions) ClosureQueueImpl(org.apache.ignite.raft.jraft.closure.ClosureQueueImpl) RaftMessagesFactory(org.apache.ignite.raft.jraft.RaftMessagesFactory) NodeOptions(org.apache.ignite.raft.jraft.option.NodeOptions) LogId(org.apache.ignite.raft.jraft.entity.LogId) StripedDisruptor(org.apache.ignite.raft.jraft.disruptor.StripedDisruptor) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

RaftMessagesFactory (org.apache.ignite.raft.jraft.RaftMessagesFactory)5 Test (org.junit.jupiter.api.Test)4 CompletableFuture (java.util.concurrent.CompletableFuture)2 Message (org.apache.ignite.raft.jraft.rpc.Message)2 RpcRequestClosure (org.apache.ignite.raft.jraft.rpc.RpcRequestClosure)2 RpcRequests (org.apache.ignite.raft.jraft.rpc.RpcRequests)2 RpcResponseClosure (org.apache.ignite.raft.jraft.rpc.RpcResponseClosure)2 Endpoint (org.apache.ignite.raft.jraft.util.Endpoint)2 File (java.io.File)1 ByteBuffer (java.nio.ByteBuffer)1 ExecutorService (java.util.concurrent.ExecutorService)1 ClosureQueueImpl (org.apache.ignite.raft.jraft.closure.ClosureQueueImpl)1 LoadSnapshotClosure (org.apache.ignite.raft.jraft.closure.LoadSnapshotClosure)1 StripedDisruptor (org.apache.ignite.raft.jraft.disruptor.StripedDisruptor)1 LocalFileMetaOutter (org.apache.ignite.raft.jraft.entity.LocalFileMetaOutter)1 LogId (org.apache.ignite.raft.jraft.entity.LogId)1 FSMCallerOptions (org.apache.ignite.raft.jraft.option.FSMCallerOptions)1 NodeOptions (org.apache.ignite.raft.jraft.option.NodeOptions)1 GetFileRequestBuilder (org.apache.ignite.raft.jraft.rpc.GetFileRequestBuilder)1 ErrorResponse (org.apache.ignite.raft.jraft.rpc.RpcRequests.ErrorResponse)1