Search in sources :

Example 1 with PeerId

use of com.alipay.sofa.jraft.entity.PeerId 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 2 with PeerId

use of com.alipay.sofa.jraft.entity.PeerId 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 3 with PeerId

use of com.alipay.sofa.jraft.entity.PeerId 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 4 with PeerId

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

the class RouteTable method selectLeader.

/**
 * Get the cached leader of the group, return it when found, null otherwise.
 * Make sure calls {@link #refreshLeader(CliClientService, String, int)} already
 * before invoke this method.
 *
 * @param groupId raft group id
 * @return peer of leader
 */
public PeerId selectLeader(final String groupId) {
    Requires.requireTrue(!StringUtils.isBlank(groupId), "Blank group id");
    final GroupConf gc = this.groupConfTable.get(groupId);
    if (gc == null) {
        return null;
    }
    final StampedLock stampedLock = gc.stampedLock;
    long stamp = stampedLock.tryOptimisticRead();
    PeerId leader = gc.leader;
    if (!stampedLock.validate(stamp)) {
        stamp = stampedLock.readLock();
        try {
            leader = gc.leader;
        } finally {
            stampedLock.unlockRead(stamp);
        }
    }
    return leader;
}
Also used : StampedLock(java.util.concurrent.locks.StampedLock) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 5 with PeerId

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

the class Configuration method toString.

@Override
public String toString() {
    final StringBuilder sb = new StringBuilder();
    final List<PeerId> peers = listPeers();
    int i = 0;
    int size = peers.size();
    for (final PeerId peer : peers) {
        sb.append(peer);
        if (i < size - 1 || !this.learners.isEmpty()) {
            sb.append(",");
        }
        i++;
    }
    size = this.learners.size();
    i = 0;
    for (final PeerId peer : this.learners) {
        sb.append(peer).append(LEARNER_POSTFIX);
        if (i < size - 1) {
            sb.append(",");
        }
        i++;
    }
    return sb.toString();
}
Also used : PeerId(com.alipay.sofa.jraft.entity.PeerId)

Aggregations

PeerId (com.alipay.sofa.jraft.entity.PeerId)211 Test (org.junit.Test)103 Node (com.alipay.sofa.jraft.Node)69 Configuration (com.alipay.sofa.jraft.conf.Configuration)51 Status (com.alipay.sofa.jraft.Status)45 Endpoint (com.alipay.sofa.jraft.util.Endpoint)42 ArrayList (java.util.ArrayList)31 CountDownLatch (java.util.concurrent.CountDownLatch)28 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)22 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 SynchronizedClosure (com.alipay.sofa.jraft.closure.SynchronizedClosure)11 LogEntry (com.alipay.sofa.jraft.entity.LogEntry)11 File (java.io.File)11 JRaftException (com.alipay.sofa.jraft.error.JRaftException)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 RaftGroupService (com.alipay.sofa.jraft.RaftGroupService)8 Closure (com.alipay.sofa.jraft.Closure)7