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());
}
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;
}
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;
}
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);
}
}
}
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))));
}
Aggregations