use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class RouteTableTest method testRefreshConfiguration.
@Test
public void testRefreshConfiguration() throws Exception {
final RouteTable rt = RouteTable.getInstance();
final List<PeerId> partConf = new ArrayList<>();
partConf.add(cluster.getLeader().getLeaderId());
// part of peers conf, only contains leader peer
rt.updateConfiguration(groupId, new Configuration(partConf));
// fetch all conf
final Status st = rt.refreshConfiguration(cliClientService, groupId, 10000);
assertTrue(st.isOk());
final Configuration newCnf = rt.getConfiguration(groupId);
assertArrayEquals(new HashSet<>(cluster.getPeers()).toArray(), new HashSet<>(newCnf.getPeerSet()).toArray());
}
use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class RouteTableTest method testRefreshFail.
@Test
public void testRefreshFail() throws Exception {
cluster.stopAll();
final RouteTable rt = RouteTable.getInstance();
rt.updateConfiguration(groupId, new Configuration(cluster.getPeers()));
final Status status = rt.refreshLeader(cliClientService, groupId, 5000);
assertFalse(status.isOk());
assertTrue(status.getErrorMsg().contains("Fail to init channel"));
}
use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class CliServiceTest method testRebalanceOnLeaderFail.
@Test
public void testRebalanceOnLeaderFail() {
final Set<String> groupIds = new TreeSet<>();
groupIds.add("group_1");
groupIds.add("group_2");
groupIds.add("group_3");
groupIds.add("group_4");
final Configuration conf = new Configuration();
conf.addPeer(new PeerId("host_1", 8080));
conf.addPeer(new PeerId("host_2", 8080));
conf.addPeer(new PeerId("host_3", 8080));
final Map<String, PeerId> rebalancedLeaderIds = new HashMap<>();
final CliService cliService = new MockLeaderFailCliService();
assertEquals("Fail to get leader", cliService.rebalance(groupIds, conf, rebalancedLeaderIds).getErrorMsg());
}
use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class ElectionNode method init.
@Override
public boolean init(final ElectionNodeOptions opts) {
if (this.started) {
LOG.info("[ElectionNode: {}] already started.", opts.getServerAddress());
return true;
}
// node options
NodeOptions nodeOpts = opts.getNodeOptions();
if (nodeOpts == null) {
nodeOpts = new NodeOptions();
}
this.fsm = new ElectionOnlyStateMachine(this.listeners);
nodeOpts.setFsm(this.fsm);
final Configuration initialConf = new Configuration();
if (!initialConf.parse(opts.getInitialServerAddressList())) {
throw new IllegalArgumentException("Fail to parse initConf: " + opts.getInitialServerAddressList());
}
// Set the initial cluster configuration
nodeOpts.setInitialConf(initialConf);
final String dataPath = opts.getDataPath();
try {
FileUtils.forceMkdir(new File(dataPath));
} catch (final IOException e) {
LOG.error("Fail to make dir for dataPath {}.", dataPath);
return false;
}
// Set the data path
// Log, required
nodeOpts.setLogUri(Paths.get(dataPath, "log").toString());
// Metadata, required
nodeOpts.setRaftMetaUri(Paths.get(dataPath, "meta").toString());
// nodeOpts.setSnapshotUri(Paths.get(dataPath, "snapshot").toString());
final String groupId = opts.getGroupId();
final PeerId serverId = new PeerId();
if (!serverId.parse(opts.getServerAddress())) {
throw new IllegalArgumentException("Fail to parse serverId: " + opts.getServerAddress());
}
final RpcServer rpcServer = RaftRpcServerFactory.createRaftRpcServer(serverId.getEndpoint());
this.raftGroupService = new RaftGroupService(groupId, serverId, nodeOpts, rpcServer);
this.node = this.raftGroupService.start();
if (this.node != null) {
this.started = true;
}
return this.started;
}
use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class PriorityElectionNode method init.
@Override
public boolean init(final PriorityElectionNodeOptions opts) {
if (this.started) {
LOG.info("[PriorityElectionNode: {}] already started.", opts.getServerAddress());
return true;
}
// node options
NodeOptions nodeOpts = opts.getNodeOptions();
if (nodeOpts == null) {
nodeOpts = new NodeOptions();
}
this.fsm = new PriorityElectionOnlyStateMachine(this.listeners);
// Set the initial PriorityElectionOnlyStateMachine
nodeOpts.setFsm(this.fsm);
final Configuration initialConf = new Configuration();
if (!initialConf.parse(opts.getInitialServerAddressList())) {
throw new IllegalArgumentException("Fail to parse initConf: " + opts.getInitialServerAddressList());
}
// Set the initial cluster configuration
nodeOpts.setInitialConf(initialConf);
final String dataPath = opts.getDataPath();
try {
FileUtils.forceMkdir(new File(dataPath));
} catch (final IOException e) {
LOG.error("Fail to make dir for dataPath {}.", dataPath);
return false;
}
// Set the data path
// Log, required
nodeOpts.setLogUri(Paths.get(dataPath, "log").toString());
// Metadata, required
nodeOpts.setRaftMetaUri(Paths.get(dataPath, "meta").toString());
// nodeOpts.setSnapshotUri(Paths.get(dataPath, "snapshot").toString());
final String groupId = opts.getGroupId();
final PeerId serverId = new PeerId();
if (!serverId.parse(opts.getServerAddress())) {
throw new IllegalArgumentException("Fail to parse serverId: " + opts.getServerAddress());
}
/**
* Set priority value, required for priority-based election, it must be a positive value when
* enable the feature, some special value meaning:
* <ul>
* <li>-1 : disable priority-based election.</li>
* <li>0: will never participate in election.</li>
* <li>1: minimum value</li>
* </ul>
* value.
*/
nodeOpts.setElectionPriority(serverId.getPriority());
final RpcServer rpcServer = RaftRpcServerFactory.createRaftRpcServer(serverId.getEndpoint());
this.raftGroupService = new RaftGroupService(groupId, serverId, nodeOpts, rpcServer);
this.node = this.raftGroupService.start();
if (this.node != null) {
this.started = true;
}
return this.started;
}
Aggregations