Search in sources :

Example 11 with Status

use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.

the class LogManagerTest method testGetConfiguration.

@Test
public void testGetConfiguration() throws Exception {
    assertTrue(this.logManager.getConfiguration(1).isEmpty());
    final List<LogEntry> entries = new ArrayList<>(2);
    final LogEntry confEntry1 = new LogEntry(EnumOutter.EntryType.ENTRY_TYPE_CONFIGURATION);
    confEntry1.setId(new LogId(0, 1));
    confEntry1.setPeers(JRaftUtils.getConfiguration("localhost:8081,localhost:8082").listPeers());
    final LogEntry confEntry2 = new LogEntry(EnumOutter.EntryType.ENTRY_TYPE_CONFIGURATION);
    confEntry2.setId(new LogId(0, 2));
    confEntry2.setPeers(JRaftUtils.getConfiguration("localhost:8081,localhost:8082,localhost:8083").listPeers());
    confEntry2.setOldPeers(confEntry1.getPeers());
    entries.add(confEntry1);
    entries.add(confEntry2);
    final CountDownLatch latch = new CountDownLatch(1);
    this.logManager.appendEntries(new ArrayList<>(entries), new LogManager.StableClosure() {

        @Override
        public void run(final Status status) {
            assertTrue(status.isOk());
            latch.countDown();
        }
    });
    latch.await();
    ConfigurationEntry entry = this.logManager.getConfiguration(1);
    assertEquals("localhost:8081,localhost:8082", entry.getConf().toString());
    assertTrue(entry.getOldConf().isEmpty());
    entry = this.logManager.getConfiguration(2);
    assertEquals("localhost:8081,localhost:8082,localhost:8083", entry.getConf().toString());
    assertEquals("localhost:8081,localhost:8082", entry.getOldConf().toString());
}
Also used : Status(com.alipay.sofa.jraft.Status) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) LogId(com.alipay.sofa.jraft.entity.LogId) LogManager(com.alipay.sofa.jraft.storage.LogManager) ConfigurationEntry(com.alipay.sofa.jraft.conf.ConfigurationEntry) LogEntry(com.alipay.sofa.jraft.entity.LogEntry) Test(org.junit.Test) BaseStorageTest(com.alipay.sofa.jraft.storage.BaseStorageTest)

Example 12 with Status

use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.

the class LogManagerTest method mockAddEntries.

private List<LogEntry> mockAddEntries() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    final List<LogEntry> mockEntries = TestUtils.mockEntries(10);
    this.logManager.appendEntries(new ArrayList<>(mockEntries), new LogManager.StableClosure() {

        @Override
        public void run(final Status status) {
            assertTrue(status.isOk());
            latch.countDown();
        }
    });
    latch.await();
    return mockEntries;
}
Also used : Status(com.alipay.sofa.jraft.Status) CountDownLatch(java.util.concurrent.CountDownLatch) LogManager(com.alipay.sofa.jraft.storage.LogManager) LogEntry(com.alipay.sofa.jraft.entity.LogEntry)

Example 13 with Status

use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.

the class ProtobufSerializerTest method testEncodeDecodeResponseContent.

@Test
public void testEncodeDecodeResponseContent() throws Exception {
    final PingRequest reqObject = TestUtils.createPingRequest();
    final RpcRequestCommand request = cmdFactory.createRequestCommand(reqObject);
    final ErrorResponse respObject = (ErrorResponse) RpcFactoryHelper.responseFactory().newResponse(null, new Status(-1, "test"));
    final RpcResponseCommand response = cmdFactory.createResponse(respObject, request);
    response.setResponseClass(ErrorResponse.class.getName());
    assertTrue(serializer.serializeContent(response));
    response.setResponseObject(null);
    assertTrue(serializer.deserializeContent(response, null));
    assertNotNull(response.getResponseObject());
    assertEquals(respObject, response.getResponseObject());
    assertNotSame(respObject, response.getResponseObject());
}
Also used : Status(com.alipay.sofa.jraft.Status) PingRequest(com.alipay.sofa.jraft.rpc.RpcRequests.PingRequest) RpcResponseCommand(com.alipay.remoting.rpc.protocol.RpcResponseCommand) RpcRequestCommand(com.alipay.remoting.rpc.protocol.RpcRequestCommand) ErrorResponse(com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse) Test(org.junit.Test)

Example 14 with Status

use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.

the class RpcResponseFactoryTest method testNewResponseWithErrorStatus.

@Test
public void testNewResponseWithErrorStatus() {
    ErrorResponse response = (ErrorResponse) RpcFactoryHelper.responseFactory().newResponse(null, new Status(300, "test"));
    assertEquals(response.getErrorCode(), 300);
    assertEquals(response.getErrorMsg(), "test");
}
Also used : Status(com.alipay.sofa.jraft.Status) ErrorResponse(com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse) Test(org.junit.Test)

Example 15 with Status

use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.

the class LogManagerTest method testAppendEntresConflicts.

@Test
public void testAppendEntresConflicts() throws Exception {
    // Append 0-10
    List<LogEntry> mockEntries = TestUtils.mockEntries(10);
    for (int i = 0; i < 10; i++) {
        mockEntries.get(i).getId().setTerm(1);
    }
    final CountDownLatch latch1 = new CountDownLatch(1);
    this.logManager.appendEntries(new ArrayList<>(mockEntries), new LogManager.StableClosure() {

        @Override
        public void run(final Status status) {
            assertTrue(status.isOk());
            latch1.countDown();
        }
    });
    latch1.await();
    assertEquals(1, this.logManager.getFirstLogIndex());
    assertEquals(10, this.logManager.getLastLogIndex());
    // Append 11-20
    final CountDownLatch latch2 = new CountDownLatch(1);
    mockEntries = TestUtils.mockEntries(10);
    for (int i = 0; i < 10; i++) {
        mockEntries.get(i).getId().setIndex(11 + i);
        mockEntries.get(i).getId().setTerm(1);
    }
    this.logManager.appendEntries(new ArrayList<>(mockEntries), new LogManager.StableClosure() {

        @Override
        public void run(final Status status) {
            assertTrue(status.isOk());
            latch2.countDown();
        }
    });
    latch2.await();
    assertEquals(1, this.logManager.getFirstLogIndex());
    assertEquals(20, this.logManager.getLastLogIndex());
    // Re-adds 11-30, but 15 has different term, it will truncate [14,lastIndex] logs
    mockEntries = TestUtils.mockEntries(20);
    for (int i = 0; i < 20; i++) {
        if (11 + i >= 15) {
            mockEntries.get(i).getId().setTerm(2);
        } else {
            mockEntries.get(i).getId().setTerm(1);
        }
        mockEntries.get(i).getId().setIndex(11 + i);
    }
    final CountDownLatch latch3 = new CountDownLatch(1);
    this.logManager.appendEntries(new ArrayList<>(mockEntries), new LogManager.StableClosure() {

        @Override
        public void run(final Status status) {
            assertTrue(status.isOk());
            latch3.countDown();
        }
    });
    latch3.await();
    assertEquals(1, this.logManager.getFirstLogIndex());
    assertEquals(30, this.logManager.getLastLogIndex());
    for (int i = 0; i < 30; i++) {
        final LogEntry entry = (this.logManager.getEntry(i + 1));
        assertEquals(i + 1, entry.getId().getIndex());
        if (i + 1 >= 15) {
            assertEquals(2, entry.getId().getTerm());
        } else {
            assertEquals(1, entry.getId().getTerm());
        }
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) CountDownLatch(java.util.concurrent.CountDownLatch) LogManager(com.alipay.sofa.jraft.storage.LogManager) LogEntry(com.alipay.sofa.jraft.entity.LogEntry) Test(org.junit.Test) BaseStorageTest(com.alipay.sofa.jraft.storage.BaseStorageTest)

Aggregations

Status (com.alipay.sofa.jraft.Status)213 Test (org.junit.Test)63 PeerId (com.alipay.sofa.jraft.entity.PeerId)55 CountDownLatch (java.util.concurrent.CountDownLatch)36 BaseKVStoreClosure (com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure)33 Configuration (com.alipay.sofa.jraft.conf.Configuration)25 Message (com.google.protobuf.Message)24 ReadIndexClosure (com.alipay.sofa.jraft.closure.ReadIndexClosure)22 ArrayList (java.util.ArrayList)22 Node (com.alipay.sofa.jraft.Node)21 Closure (com.alipay.sofa.jraft.Closure)17 Task (com.alipay.sofa.jraft.entity.Task)17 ByteBuffer (java.nio.ByteBuffer)16 Endpoint (com.alipay.sofa.jraft.util.Endpoint)15 List (java.util.List)15 RaftException (com.alipay.sofa.jraft.error.RaftException)14 LogEntry (com.alipay.sofa.jraft.entity.LogEntry)12 LogId (com.alipay.sofa.jraft.entity.LogId)12 KVStoreClosure (com.alipay.sofa.jraft.rhea.storage.KVStoreClosure)12 JRaftException (com.alipay.sofa.jraft.error.JRaftException)11