Search in sources :

Example 56 with PeerId

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

the class LogEntryV2CodecFactoryTest method createLearners.

private List<PeerId> createLearners(final String... peers) {
    List<PeerId> ret = new ArrayList<>();
    for (String s : peers) {
        PeerId e = new PeerId();
        e.parse(s);
        ret.add(e);
    }
    return ret;
}
Also used : ArrayList(java.util.ArrayList) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 57 with PeerId

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

the class LogEntryV2CodecFactoryTest method testDecodeV1LogEntry.

@Test
public void testDecodeV1LogEntry() {
    ByteBuffer buf = ByteBuffer.wrap("hello".getBytes());
    LogEntry entry = new LogEntry(EnumOutter.EntryType.ENTRY_TYPE_NO_OP);
    entry.setId(new LogId(100, 3));
    entry.setData(buf);
    entry.setPeers(Arrays.asList(new PeerId("localhost", 99, 1), new PeerId("localhost", 100, 2)));
    assertEquals(buf, entry.getData());
    byte[] content = V1Encoder.INSTANCE.encode(entry);
    assertNotNull(content);
    assertTrue(content.length > 0);
    // Decode by auto detect decoder
    LogEntry nentry = this.decoder.decode(content);
    assertNotNull(nentry);
    assertEquals(100, nentry.getId().getIndex());
    assertEquals(3, nentry.getId().getTerm());
    assertEquals(2, nentry.getPeers().size());
    assertEquals("localhost:99:1", nentry.getPeers().get(0).toString());
    assertEquals("localhost:100:2", nentry.getPeers().get(1).toString());
    assertEquals(buf, nentry.getData());
    assertEquals(0, nentry.getData().position());
    assertEquals(5, nentry.getData().remaining());
    assertNull(nentry.getOldPeers());
}
Also used : ByteBuffer(java.nio.ByteBuffer) LogId(com.alipay.sofa.jraft.entity.LogId) LogEntry(com.alipay.sofa.jraft.entity.LogEntry) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test) BaseLogEntryCodecFactoryTest(com.alipay.sofa.jraft.entity.codec.BaseLogEntryCodecFactoryTest)

Example 58 with PeerId

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

the class ConfigurationTest method testCopy.

@Test
public void testCopy() {
    final Configuration conf = JRaftUtils.getConfiguration("localhost:8081,localhost:8082,localhost:8083");
    final Configuration copied = conf.copy();
    assertEquals(conf, copied);
    assertNotSame(conf, copied);
    assertEquals(copied.size(), 3);
    assertEquals("localhost:8081,localhost:8082,localhost:8083", copied.toString());
    final PeerId newPeer = new PeerId("localhost", 8084);
    conf.addPeer(newPeer);
    assertEquals(copied.size(), 3);
    assertEquals(conf.size(), 4);
    assertTrue(conf.contains(newPeer));
    assertFalse(copied.contains(newPeer));
}
Also used : PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 59 with PeerId

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

the class ConfigurationTest method testToStringParseStuffWithPriority.

@Test
public void testToStringParseStuffWithPriority() {
    final String confStr = "localhost:8081:1:100,localhost:8082:1:100,localhost:8083:1:100";
    final Configuration conf = JRaftUtils.getConfiguration(confStr);
    assertEquals(3, conf.size());
    for (final PeerId peer : conf) {
        assertTrue(peer.toString().startsWith("localhost:80"));
        assertEquals(100, peer.getPriority());
        assertEquals(1, peer.getIdx());
    }
    assertFalse(conf.isEmpty());
    assertEquals(confStr, conf.toString());
    final Configuration newConf = new Configuration();
    assertTrue(newConf.parse(conf.toString()));
    assertEquals(3, newConf.getPeerSet().size());
    assertTrue(newConf.contains(new PeerId("localhost", 8081, 1, 100)));
    assertTrue(newConf.contains(new PeerId("localhost", 8082, 1, 100)));
    assertTrue(newConf.contains(new PeerId("localhost", 8083, 1, 100)));
    assertEquals(confStr, newConf.toString());
    assertEquals(conf.hashCode(), newConf.hashCode());
    assertEquals(conf, newConf);
}
Also used : PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 60 with PeerId

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

the class BallotBoxTest method testCommitAt.

@Test
public void testCommitAt() {
    assertFalse(this.box.commitAt(1, 3, new PeerId("localhost", 8081)));
    assertTrue(box.resetPendingIndex(1));
    assertTrue(this.box.appendPendingTask(JRaftUtils.getConfiguration("localhost:8081,localhost:8082,localhost:8083"), JRaftUtils.getConfiguration("localhost:8081"), new Closure() {

        @Override
        public void run(Status status) {
        }
    }));
    assertEquals(0, this.box.getLastCommittedIndex());
    try {
        this.box.commitAt(1, 3, new PeerId("localhost", 8081));
        fail();
    } catch (ArrayIndexOutOfBoundsException e) {
    }
    assertTrue(this.box.commitAt(1, 1, new PeerId("localhost", 8081)));
    assertEquals(0, this.box.getLastCommittedIndex());
    assertEquals(1, this.box.getPendingIndex());
    assertTrue(this.box.commitAt(1, 1, new PeerId("localhost", 8082)));
    assertEquals(1, this.box.getLastCommittedIndex());
    assertEquals(2, this.box.getPendingIndex());
    Mockito.verify(this.waiter, Mockito.only()).onCommitted(1);
}
Also used : Status(com.alipay.sofa.jraft.Status) Closure(com.alipay.sofa.jraft.Closure) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Aggregations

PeerId (com.alipay.sofa.jraft.entity.PeerId)236 Test (org.junit.Test)107 Node (com.alipay.sofa.jraft.Node)70 Configuration (com.alipay.sofa.jraft.conf.Configuration)54 Status (com.alipay.sofa.jraft.Status)49 Endpoint (com.alipay.sofa.jraft.util.Endpoint)43 ArrayList (java.util.ArrayList)32 CountDownLatch (java.util.concurrent.CountDownLatch)28 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)24 LogId (com.alipay.sofa.jraft.entity.LogId)17 Message (com.google.protobuf.Message)15 ByteBuffer (java.nio.ByteBuffer)15 Task (com.alipay.sofa.jraft.entity.Task)13 File (java.io.File)12 SynchronizedClosure (com.alipay.sofa.jraft.closure.SynchronizedClosure)11 LogEntry (com.alipay.sofa.jraft.entity.LogEntry)11 JRaftException (com.alipay.sofa.jraft.error.JRaftException)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 RpcServer (com.alipay.sofa.jraft.rpc.RpcServer)9 RaftGroupService (com.alipay.sofa.jraft.RaftGroupService)8