Search in sources :

Example 1 with Configuration

use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.

the class ChaosTestCluster method addPeer.

public synchronized void addPeer(final PeerId peerId) {
    if (this.peerIds.contains(peerId)) {
        throw new RuntimeException("peerId is exist: " + peerId);
    }
    this.peerIds.add(peerId);
    final Configuration conf = new Configuration(this.peerIds);
    final String initialServerList = conf.toString();
    final PlacementDriverOptions pdOpts = // use a fake pd
    PlacementDriverOptionsConfigured.newConfigured().withFake(true).config();
    final StoreEngineOptions storeOpts = // 
    StoreEngineOptionsConfigured.newConfigured().withStorageType(// 
    this.storageType).withRocksDBOptions(// 
    RocksDBOptionsConfigured.newConfigured().withDbPath(DB_PATH).config()).withRaftDataPath(// 
    RAFT_DATA_PATH).withServerAddress(// 
    peerId.getEndpoint()).config();
    final RheaKVStoreOptions opts = // 
    RheaKVStoreOptionsConfigured.newConfigured().withClusterName(// 
    "chaos_test").withInitialServerList(initialServerList).withStoreEngineOptions(// 
    storeOpts).withPlacementDriverOptions(// 
    pdOpts).config();
    BatchingOptions batchingOptions = opts.getBatchingOptions();
    if (batchingOptions == null) {
        batchingOptions = new BatchingOptions();
    }
    batchingOptions.setAllowBatching(this.allowBatching);
    opts.setBatchingOptions(batchingOptions);
    final RheaKVStore store = new DefaultRheaKVStore();
    if (!store.init(opts)) {
        throw new IllegalStateException("fail to init store with options: " + opts);
    }
    final RheaKVStore leader = getLeaderStore();
    final PlacementDriverClient pdClient = leader.getPlacementDriverClient();
    if (!pdClient.addReplica(Constants.DEFAULT_REGION_ID, JRaftHelper.toPeer(peerId), true)) {
        throw new RuntimeException("fail to add peer: " + peerId);
    }
    this.stores.add(store);
    awaitLeader();
}
Also used : RheaKVStoreOptions(com.alipay.sofa.jraft.rhea.options.RheaKVStoreOptions) RheaKVStore(com.alipay.sofa.jraft.rhea.client.RheaKVStore) DefaultRheaKVStore(com.alipay.sofa.jraft.rhea.client.DefaultRheaKVStore) BatchingOptions(com.alipay.sofa.jraft.rhea.options.BatchingOptions) PlacementDriverOptions(com.alipay.sofa.jraft.rhea.options.PlacementDriverOptions) Configuration(com.alipay.sofa.jraft.conf.Configuration) PlacementDriverClient(com.alipay.sofa.jraft.rhea.client.pd.PlacementDriverClient) StoreEngineOptions(com.alipay.sofa.jraft.rhea.options.StoreEngineOptions) DefaultRheaKVStore(com.alipay.sofa.jraft.rhea.client.DefaultRheaKVStore)

Example 2 with Configuration

use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.

the class ChaosTestCluster method start.

public synchronized void start() {
    deleteFiles();
    final Configuration conf = new Configuration(this.peerIds);
    final String initialServerList = conf.toString();
    for (final PeerId p : conf.listPeers()) {
        final PlacementDriverOptions pdOpts = // use a fake pd
        PlacementDriverOptionsConfigured.newConfigured().withFake(true).config();
        final StoreEngineOptions storeOpts = // 
        StoreEngineOptionsConfigured.newConfigured().withStorageType(// 
        this.storageType).withRocksDBOptions(// 
        RocksDBOptionsConfigured.newConfigured().withDbPath(DB_PATH).config()).withRaftDataPath(// 
        RAFT_DATA_PATH).withServerAddress(// 
        p.getEndpoint()).withLeastKeysOnSplit(// 
        10).config();
        final RheaKVStoreOptions opts = // 
        RheaKVStoreOptionsConfigured.newConfigured().withClusterName(// 
        CLUSTER_NAME).withInitialServerList(initialServerList).withOnlyLeaderRead(// 
        this.onlyLeaderRead).withStoreEngineOptions(// 
        storeOpts).withPlacementDriverOptions(// 
        pdOpts).withFailoverRetries(// 
        30).withFutureTimeoutMillis(// 
        TimeUnit.SECONDS.toMillis(60)).config();
        BatchingOptions batchingOptions = opts.getBatchingOptions();
        if (batchingOptions == null) {
            batchingOptions = new BatchingOptions();
        }
        batchingOptions.setAllowBatching(this.allowBatching);
        opts.setBatchingOptions(batchingOptions);
        final RheaKVStore store = new DefaultRheaKVStore();
        if (!store.init(opts)) {
            throw new IllegalStateException("fail to init store with options: " + opts);
        }
        this.stores.add(store);
    }
    awaitLeader();
}
Also used : RheaKVStoreOptions(com.alipay.sofa.jraft.rhea.options.RheaKVStoreOptions) RheaKVStore(com.alipay.sofa.jraft.rhea.client.RheaKVStore) DefaultRheaKVStore(com.alipay.sofa.jraft.rhea.client.DefaultRheaKVStore) BatchingOptions(com.alipay.sofa.jraft.rhea.options.BatchingOptions) PlacementDriverOptions(com.alipay.sofa.jraft.rhea.options.PlacementDriverOptions) Configuration(com.alipay.sofa.jraft.conf.Configuration) StoreEngineOptions(com.alipay.sofa.jraft.rhea.options.StoreEngineOptions) DefaultRheaKVStore(com.alipay.sofa.jraft.rhea.client.DefaultRheaKVStore) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 3 with Configuration

use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.

the class KVStateMachineTest method setup.

@Before
public void setup() throws IOException, InterruptedException {
    final Region region = new Region();
    region.setId(1);
    final StoreEngine storeEngine = new MockStoreEngine();
    final KVStoreStateMachine fsm = new KVStoreStateMachine(region, storeEngine);
    final NodeOptions nodeOpts = new NodeOptions();
    final Configuration conf = new Configuration();
    conf.addPeer(PeerId.parsePeer("127.0.0.1:8081"));
    nodeOpts.setInitialConf(conf);
    nodeOpts.setFsm(fsm);
    final String raftDataPath = "raft_st_test";
    this.raftDataPath = new File(raftDataPath);
    if (this.raftDataPath.exists()) {
        FileUtils.forceDelete(this.raftDataPath);
    }
    FileUtils.forceMkdir(this.raftDataPath);
    final Path logUri = Paths.get(raftDataPath, "log");
    nodeOpts.setLogUri(logUri.toString());
    final Path meteUri = Paths.get(raftDataPath, "meta");
    nodeOpts.setRaftMetaUri(meteUri.toString());
    final Path snapshotUri = Paths.get(raftDataPath, "snapshot");
    nodeOpts.setSnapshotUri(snapshotUri.toString());
    final Endpoint serverAddress = new Endpoint("127.0.0.1", 8081);
    final PeerId serverId = new PeerId(serverAddress, 0);
    this.raftGroupService = new RaftGroupService("st_test", serverId, nodeOpts, null, true);
    final Node node = this.raftGroupService.start(false);
    for (int i = 0; i < 100; i++) {
        if (node.isLeader()) {
            break;
        }
        Thread.sleep(100);
    }
    final RawKVStore rawKVStore = storeEngine.getRawKVStore();
    this.raftRawKVStore = new RaftRawKVStore(node, rawKVStore, null);
}
Also used : Path(java.nio.file.Path) Configuration(com.alipay.sofa.jraft.conf.Configuration) RaftGroupService(com.alipay.sofa.jraft.RaftGroupService) Node(com.alipay.sofa.jraft.Node) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) Endpoint(com.alipay.sofa.jraft.util.Endpoint) StoreEngine(com.alipay.sofa.jraft.rhea.StoreEngine) Endpoint(com.alipay.sofa.jraft.util.Endpoint) Region(com.alipay.sofa.jraft.rhea.metadata.Region) File(java.io.File) PeerId(com.alipay.sofa.jraft.entity.PeerId) Before(org.junit.Before)

Example 4 with Configuration

use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.

the class AbstractChaosTest method chaosSplittingTest.

@Test
public void chaosSplittingTest() {
    final List<PeerId> peerIds = TestUtil.generatePeers(INITIAL_PEER_COUNT);
    final CliOptions opts = new CliOptions();
    opts.setTimeoutMs(30000);
    final RheaKVCliService cliService = RheaKVServiceFactory.createAndInitRheaKVCliService(opts);
    final long regionId = Constants.DEFAULT_REGION_ID;
    final long newRegionId = 2;
    final String groupId = JRaftHelper.getJRaftGroupId(ChaosTestCluster.CLUSTER_NAME, regionId);
    final Configuration conf = new Configuration(peerIds);
    ChaosTestCluster cluster = null;
    for (int l = 0; l < RETRIES; l++) {
        final ExecutorService executor = Executors.newCachedThreadPool(new NamedThreadFactory("chaos-splitting-test", true));
        final List<Future<?>> allFutures = new CopyOnWriteArrayList<>();
        try {
            cluster = new ChaosTestCluster(peerIds, getStorageType(), isAllowBatching(), isOnlyLeaderRead());
            cluster.start();
            final RheaKVStore store = cluster.getLeaderStore();
            // for least keys on split
            for (int j = 0; j < LOOP_2; j++) {
                store.bPut(j + "_split_", VALUE);
            }
            for (int i = 0; i < LOOP_1; i++) {
                final int index = i;
                final Future<?> f = executor.submit(() -> {
                    for (int j = 0; j < LOOP_2; j++) {
                        store.bPut(index + "_split_test_" + j, VALUE);
                    }
                });
                allFutures.add(f);
            }
            final Status st = cliService.rangeSplit(regionId, newRegionId, groupId, conf);
            if (!st.isOk()) {
                System.err.println("Status:" + st);
                throw new RuntimeException(st.toString());
            }
            // wait for all writes finished
            for (final Future<?> f : allFutures) {
                f.get(30, TimeUnit.SECONDS);
            }
            break;
        } catch (final Exception e) {
            System.err.println("Fail to put data, try again...");
            e.printStackTrace();
            for (final Future<?> f : allFutures) {
                f.cancel(true);
            }
            if (cluster != null) {
                cluster.stopAll();
            }
            cluster = null;
        } finally {
            ExecutorServiceHelper.shutdownAndAwaitTermination(executor);
        }
    }
    if (cluster == null) {
        throw new RuntimeException("fail to put data, can not check data");
    }
    try {
        chaosSplittingCheckData(cluster);
    } finally {
        cluster.stopAll();
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) RheaKVStore(com.alipay.sofa.jraft.rhea.client.RheaKVStore) RheaKVCliService(com.alipay.sofa.jraft.rhea.client.RheaKVCliService) Configuration(com.alipay.sofa.jraft.conf.Configuration) NamedThreadFactory(com.alipay.sofa.jraft.util.NamedThreadFactory) CliOptions(com.alipay.sofa.jraft.option.CliOptions) ExecutorService(java.util.concurrent.ExecutorService) CompletableFuture(java.util.concurrent.CompletableFuture) Future(java.util.concurrent.Future) PeerId(com.alipay.sofa.jraft.entity.PeerId) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.Test)

Example 5 with Configuration

use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.

the class RouteTable method updateConfiguration.

/**
 * Update configuration of group in route table.
 *
 * @param groupId raft group id
 * @param confStr configuration string
 * @return true on success
 */
public boolean updateConfiguration(final String groupId, final String confStr) {
    Requires.requireTrue(!StringUtils.isBlank(groupId), "Blank group id");
    Requires.requireTrue(!StringUtils.isBlank(confStr), "Blank configuration");
    final Configuration conf = new Configuration();
    if (conf.parse(confStr)) {
        return updateConfiguration(groupId, conf);
    } else {
        LOG.error("Fail to parse confStr: {}", confStr);
        return false;
    }
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration)

Aggregations

Configuration (com.alipay.sofa.jraft.conf.Configuration)76 PeerId (com.alipay.sofa.jraft.entity.PeerId)51 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)17 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)17 RaftGroupService (com.alipay.sofa.jraft.RaftGroupService)9 SynchronizedClosure (com.alipay.sofa.jraft.closure.SynchronizedClosure)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 RpcServer (com.alipay.sofa.jraft.rpc.RpcServer)7 File (java.io.File)7 ArrayList (java.util.ArrayList)7 ConfigurationEntry (com.alipay.sofa.jraft.conf.ConfigurationEntry)5 Task (com.alipay.sofa.jraft.entity.Task)5 LogId (com.alipay.sofa.jraft.entity.LogId)4 CliOptions (com.alipay.sofa.jraft.option.CliOptions)4 RheaKVStore (com.alipay.sofa.jraft.rhea.client.RheaKVStore)4 CliService (com.alipay.sofa.jraft.CliService)3 LogEntry (com.alipay.sofa.jraft.entity.LogEntry)3