Search in sources :

Example 36 with Configuration

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());
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) ArrayList(java.util.ArrayList) PeerId(com.alipay.sofa.jraft.entity.PeerId) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 37 with Configuration

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"));
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) Test(org.junit.Test)

Example 38 with Configuration

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());
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) CliService(com.alipay.sofa.jraft.CliService) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 39 with Configuration

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;
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) RpcServer(com.alipay.sofa.jraft.rpc.RpcServer) RaftGroupService(com.alipay.sofa.jraft.RaftGroupService) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) IOException(java.io.IOException) File(java.io.File) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 40 with Configuration

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;
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) RpcServer(com.alipay.sofa.jraft.rpc.RpcServer) RaftGroupService(com.alipay.sofa.jraft.RaftGroupService) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) IOException(java.io.IOException) File(java.io.File) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Aggregations

Configuration (com.alipay.sofa.jraft.conf.Configuration)81 PeerId (com.alipay.sofa.jraft.entity.PeerId)54 Test (org.junit.Test)28 Node (com.alipay.sofa.jraft.Node)20 Endpoint (com.alipay.sofa.jraft.util.Endpoint)20 Status (com.alipay.sofa.jraft.Status)18 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)18 RaftGroupService (com.alipay.sofa.jraft.RaftGroupService)9 SynchronizedClosure (com.alipay.sofa.jraft.closure.SynchronizedClosure)8 File (java.io.File)8 ArrayList (java.util.ArrayList)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 RpcServer (com.alipay.sofa.jraft.rpc.RpcServer)7 ConfigurationEntry (com.alipay.sofa.jraft.conf.ConfigurationEntry)5 Task (com.alipay.sofa.jraft.entity.Task)5 CliOptions (com.alipay.sofa.jraft.option.CliOptions)5 LogId (com.alipay.sofa.jraft.entity.LogId)4 RheaKVStore (com.alipay.sofa.jraft.rhea.client.RheaKVStore)4 LogEntry (com.alipay.sofa.jraft.entity.LogEntry)3 RaftException (com.alipay.sofa.jraft.error.RaftException)3