Search in sources :

Example 61 with PeerId

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

the class BaseLogEntryCodecFactoryTest method testEncodeDecodeWithData.

@Test
public void testEncodeDecodeWithData() {
    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 = this.encoder.encode(entry);
    assertNotNull(content);
    assertTrue(content.length > 0);
    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)

Example 62 with PeerId

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

the class AtomicRangeGroup method redirect.

/**
 * Redirect request to new leader
 * @return
 */
public BooleanCommand redirect() {
    final BooleanCommand response = new BooleanCommand();
    response.setSuccess(false);
    response.setErrorMsg("Not leader");
    if (node != null) {
        final PeerId leader = node.getLeaderId();
        if (leader != null) {
            response.setRedirect(leader.toString());
        }
    }
    return response;
}
Also used : BooleanCommand(com.alipay.sofa.jraft.test.atomic.command.BooleanCommand) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 63 with PeerId

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

the class AtomicRangeGroup method start.

public static AtomicRangeGroup start(StartupConf conf, RpcServer rpcServer) throws IOException {
    final NodeOptions nodeOptions = new NodeOptions();
    // Set election timeout to 1 second
    nodeOptions.setElectionTimeoutMs(1000);
    // Close cli service
    nodeOptions.setDisableCli(false);
    // A snapshot saving would be triggered every 30 seconds
    // nodeOptions.setSnapshotIntervalSecs(30);
    // Parsing Options
    final PeerId serverId = new PeerId();
    if (!serverId.parse(conf.getServerAddress())) {
        throw new IllegalArgumentException("Fail to parse serverId:" + conf.getServerAddress());
    }
    final Configuration initConf = new Configuration();
    if (!initConf.parse(conf.getConf())) {
        throw new IllegalArgumentException("Fail to parse initConf:" + conf.getConf());
    }
    // Set the initial cluster configuration
    nodeOptions.setInitialConf(initConf);
    // Startup node
    final AtomicRangeGroup node = new AtomicRangeGroup(conf.getDataPath(), conf.getGroupId(), serverId, conf.getMinSlot(), conf.getMaxSlot(), nodeOptions, rpcServer);
    LOG.info("Started range node[{}-{}] at port:{}", conf.getMinSlot(), conf.getMaxSlot(), node.getNode().getNodeId().getPeerId().getPort());
    return node;
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 64 with PeerId

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

the class AtomicClient method start.

public void start() throws InterruptedException, TimeoutException {
    cliOptions = new CliOptions();
    this.cliClientService.init(cliOptions);
    this.rpcClient = this.cliClientService.getRpcClient();
    if (conf != null) {
        final Set<PeerId> peers = conf.getPeerSet();
        for (final PeerId peer : peers) {
            try {
                final BooleanCommand cmd = (BooleanCommand) this.rpcClient.invokeSync(peer.getEndpoint(), new GetSlotsCommand(), cliOptions.getRpcDefaultTimeout());
                if (cmd instanceof SlotsResponseCommand) {
                    groups = ((SlotsResponseCommand) cmd).getMap();
                    break;
                } else {
                    LOG.warn("Fail to get slots from peer {}, error: {}", peer, cmd.getErrorMsg());
                }
            } catch (final Throwable t) {
                LOG.warn("Fail to get slots from peer {}, error: {}", peer, t.getMessage());
            // continue;
            }
        }
        if (groups == null || groups.isEmpty()) {
            throw new IllegalArgumentException("Can't get slots from any peers");
        } else {
            LOG.info("All groups  is {}", groups);
        }
        for (final String groupId : groups.values()) {
            RouteTable.getInstance().updateConfiguration(groupId, conf);
            refreshLeader(groupId);
            refreshConf(groupId);
        }
    }
}
Also used : GetSlotsCommand(com.alipay.sofa.jraft.test.atomic.command.GetSlotsCommand) BooleanCommand(com.alipay.sofa.jraft.test.atomic.command.BooleanCommand) SlotsResponseCommand(com.alipay.sofa.jraft.test.atomic.command.SlotsResponseCommand) CliOptions(com.alipay.sofa.jraft.option.CliOptions) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 65 with PeerId

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

the class ConfigurationEntryTest method testStuffMethodsWithPriority.

@Test
public void testStuffMethodsWithPriority() {
    ConfigurationEntry entry = TestUtils.getConfEntry("localhost:8081::100,localhost:8082::100,localhost:8083::100", null);
    assertTrue(entry.isStable());
    assertFalse(entry.isEmpty());
    assertTrue(entry.contains(new PeerId("localhost", 8081, 0, 100)));
    assertTrue(entry.contains(new PeerId("localhost", 8082, 0, 100)));
    assertTrue(entry.contains(new PeerId("localhost", 8083, 0, 100)));
    assertEquals(entry.listPeers(), new HashSet<>(Arrays.asList(new PeerId("localhost", 8081, 0, 100), new PeerId("localhost", 8082, 0, 100), new PeerId("localhost", 8083, 0, 100))));
}
Also used : 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