Search in sources :

Example 1 with RegionEngineOptions

use of com.alipay.sofa.jraft.rhea.options.RegionEngineOptions in project sofa-jraft by sofastack.

the class StoreEngine method initAllRegionEngine.

private boolean initAllRegionEngine(final StoreEngineOptions opts, final Store store) {
    Requires.requireNonNull(opts, "opts");
    Requires.requireNonNull(store, "store");
    String baseRaftDataPath = opts.getRaftDataPath();
    if (Strings.isNotBlank(baseRaftDataPath)) {
        try {
            FileUtils.forceMkdir(new File(baseRaftDataPath));
        } catch (final Throwable t) {
            LOG.error("Fail to make dir for raftDataPath: {}.", baseRaftDataPath);
            return false;
        }
    } else {
        baseRaftDataPath = "";
    }
    final Endpoint serverAddress = opts.getServerAddress();
    final List<RegionEngineOptions> rOptsList = opts.getRegionEngineOptionsList();
    final List<Region> regionList = store.getRegions();
    Requires.requireTrue(rOptsList.size() == regionList.size());
    for (int i = 0; i < rOptsList.size(); i++) {
        final RegionEngineOptions rOpts = rOptsList.get(i);
        if (!inConfiguration(rOpts.getServerAddress().toString(), rOpts.getInitialServerList())) {
            continue;
        }
        final Region region = regionList.get(i);
        if (Strings.isBlank(rOpts.getRaftDataPath())) {
            final String childPath = "raft_data_region_" + region.getId() + "_" + serverAddress.getPort();
            rOpts.setRaftDataPath(Paths.get(baseRaftDataPath, childPath).toString());
        }
        Requires.requireNonNull(region.getRegionEpoch(), "regionEpoch");
        final RegionEngine engine = new RegionEngine(region, this);
        if (engine.init(rOpts)) {
            final RegionKVService regionKVService = new DefaultRegionKVService(engine);
            registerRegionKVService(regionKVService);
            this.regionEngineTable.put(region.getId(), engine);
        } else {
            LOG.error("Fail to init [RegionEngine: {}].", region);
            return false;
        }
    }
    return true;
}
Also used : Endpoint(com.alipay.sofa.jraft.util.Endpoint) Region(com.alipay.sofa.jraft.rhea.metadata.Region) RegionEngineOptions(com.alipay.sofa.jraft.rhea.options.RegionEngineOptions) File(java.io.File) Endpoint(com.alipay.sofa.jraft.util.Endpoint)

Example 2 with RegionEngineOptions

use of com.alipay.sofa.jraft.rhea.options.RegionEngineOptions in project sofa-jraft by sofastack.

the class MultiRegionEngineOptionsConfigured method getOrCreateOptsById.

private RegionEngineOptions getOrCreateOptsById(final Long regionId) {
    Requires.requireNonNull(regionId, "regionId");
    RegionEngineOptions opts = this.optsTable.get(regionId);
    if (opts != null) {
        return opts;
    }
    opts = new RegionEngineOptions();
    opts.setRegionId(regionId);
    this.optsTable.put(regionId, opts);
    return opts;
}
Also used : RegionEngineOptions(com.alipay.sofa.jraft.rhea.options.RegionEngineOptions)

Example 3 with RegionEngineOptions

use of com.alipay.sofa.jraft.rhea.options.RegionEngineOptions in project sofa-jraft by sofastack.

the class RheaKVTestCluster method start.

protected void start() throws IOException, InterruptedException {
    System.out.println("RheaKVTestCluster init ...");
    File file = new File("rhea_pd_db");
    if (file.exists()) {
        FileUtils.forceDelete(file);
    }
    file = new File("rhea_pd_db");
    if (file.mkdir()) {
        this.tempDbPath = file.getAbsolutePath();
        System.out.println("make dir: " + this.tempDbPath);
    }
    file = new File("rhea_pd_raft");
    if (file.exists()) {
        FileUtils.forceDelete(file);
    }
    file = new File("rhea_pd_raft");
    if (file.mkdir()) {
        this.tempRaftPath = file.getAbsolutePath();
        System.out.println("make dir: " + this.tempRaftPath);
    }
    final Set<Long> regionIds = new HashSet<>();
    for (final String c : CONF) {
        final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
        final InputStream in = RheaKVTestCluster.class.getResourceAsStream(c);
        final RheaKVStoreOptions opts = mapper.readValue(in, RheaKVStoreOptions.class);
        for (final RegionEngineOptions rOpts : opts.getStoreEngineOptions().getRegionEngineOptionsList()) {
            regionIds.add(rOpts.getRegionId());
        }
        final RheaKVStore rheaKVStore = new DefaultRheaKVStore();
        if (rheaKVStore.init(opts)) {
            stores.add(rheaKVStore);
        } else {
            System.err.println("Fail to init rhea kv store witch conf: " + c);
        }
    }
    final PlacementDriverClient pdClient = stores.get(0).getPlacementDriverClient();
    for (final Long regionId : regionIds) {
        final Endpoint leader = pdClient.getLeader(regionId, true, 10000);
        System.out.println("The region " + regionId + " leader is: " + leader);
    }
}
Also used : DefaultRheaKVStore(com.alipay.sofa.jraft.rhea.client.DefaultRheaKVStore) RheaKVStore(com.alipay.sofa.jraft.rhea.client.RheaKVStore) InputStream(java.io.InputStream) PlacementDriverClient(com.alipay.sofa.jraft.rhea.client.pd.PlacementDriverClient) RegionEngineOptions(com.alipay.sofa.jraft.rhea.options.RegionEngineOptions) DefaultRheaKVStore(com.alipay.sofa.jraft.rhea.client.DefaultRheaKVStore) RheaKVStoreOptions(com.alipay.sofa.jraft.rhea.options.RheaKVStoreOptions) Endpoint(com.alipay.sofa.jraft.util.Endpoint) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HashSet(java.util.HashSet)

Example 4 with RegionEngineOptions

use of com.alipay.sofa.jraft.rhea.options.RegionEngineOptions in project sofa-jraft by sofastack.

the class FakePlacementDriverClient method getStoreMetadata.

@Override
public Store getStoreMetadata(final StoreEngineOptions opts) {
    final Store store = new Store();
    final List<RegionEngineOptions> rOptsList = opts.getRegionEngineOptionsList();
    final List<Region> regionList = Lists.newArrayListWithCapacity(rOptsList.size());
    store.setId(-1);
    store.setEndpoint(opts.getServerAddress());
    for (final RegionEngineOptions rOpts : rOptsList) {
        regionList.add(getLocalRegionMetadata(rOpts));
    }
    store.setRegions(regionList);
    return store;
}
Also used : Store(com.alipay.sofa.jraft.rhea.metadata.Store) Region(com.alipay.sofa.jraft.rhea.metadata.Region) RegionEngineOptions(com.alipay.sofa.jraft.rhea.options.RegionEngineOptions)

Example 5 with RegionEngineOptions

use of com.alipay.sofa.jraft.rhea.options.RegionEngineOptions in project sofa-jraft by sofastack.

the class RemotePlacementDriverClient method getStoreMetadata.

@Override
public Store getStoreMetadata(final StoreEngineOptions opts) {
    final Endpoint selfEndpoint = opts.getServerAddress();
    // remote conf is the preferred
    final Store remoteStore = this.metadataRpcClient.getStoreInfo(this.clusterId, selfEndpoint);
    if (!remoteStore.isEmpty()) {
        final List<Region> regions = remoteStore.getRegions();
        for (final Region region : regions) {
            super.regionRouteTable.addOrUpdateRegion(region);
        }
        return remoteStore;
    }
    // local conf
    final Store localStore = new Store();
    final List<RegionEngineOptions> rOptsList = opts.getRegionEngineOptionsList();
    final List<Region> regionList = Lists.newArrayListWithCapacity(rOptsList.size());
    localStore.setId(remoteStore.getId());
    localStore.setEndpoint(selfEndpoint);
    for (final RegionEngineOptions rOpts : rOptsList) {
        regionList.add(getLocalRegionMetadata(rOpts));
    }
    localStore.setRegions(regionList);
    this.metadataRpcClient.updateStoreInfo(this.clusterId, localStore);
    return localStore;
}
Also used : Endpoint(com.alipay.sofa.jraft.util.Endpoint) Store(com.alipay.sofa.jraft.rhea.metadata.Store) Region(com.alipay.sofa.jraft.rhea.metadata.Region) RegionEngineOptions(com.alipay.sofa.jraft.rhea.options.RegionEngineOptions)

Aggregations

RegionEngineOptions (com.alipay.sofa.jraft.rhea.options.RegionEngineOptions)7 Region (com.alipay.sofa.jraft.rhea.metadata.Region)4 Endpoint (com.alipay.sofa.jraft.util.Endpoint)4 Store (com.alipay.sofa.jraft.rhea.metadata.Store)3 File (java.io.File)2 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)1 DefaultRheaKVStore (com.alipay.sofa.jraft.rhea.client.DefaultRheaKVStore)1 RheaKVStore (com.alipay.sofa.jraft.rhea.client.RheaKVStore)1 HeartbeatSender (com.alipay.sofa.jraft.rhea.client.pd.HeartbeatSender)1 PlacementDriverClient (com.alipay.sofa.jraft.rhea.client.pd.PlacementDriverClient)1 RemotePlacementDriverClient (com.alipay.sofa.jraft.rhea.client.pd.RemotePlacementDriverClient)1 RegionEpoch (com.alipay.sofa.jraft.rhea.metadata.RegionEpoch)1 HeartbeatOptions (com.alipay.sofa.jraft.rhea.options.HeartbeatOptions)1 RheaKVStoreOptions (com.alipay.sofa.jraft.rhea.options.RheaKVStoreOptions)1 BatchRawKVStore (com.alipay.sofa.jraft.rhea.storage.BatchRawKVStore)1 MemoryRawKVStore (com.alipay.sofa.jraft.rhea.storage.MemoryRawKVStore)1 RocksRawKVStore (com.alipay.sofa.jraft.rhea.storage.RocksRawKVStore)1 Describer (com.alipay.sofa.jraft.util.Describer)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 YAMLFactory (com.fasterxml.jackson.dataformat.yaml.YAMLFactory)1