Search in sources :

Example 6 with RegionEpoch

use of com.alipay.sofa.jraft.rhea.metadata.RegionEpoch in project sofa-jraft by sofastack.

the class StoreEngine method doSplit.

public void doSplit(final Long regionId, final Long newRegionId, final byte[] splitKey) {
    try {
        Requires.requireNonNull(regionId, "regionId");
        Requires.requireNonNull(newRegionId, "newRegionId");
        final RegionEngine parent = getRegionEngine(regionId);
        final Region region = parent.getRegion().copy();
        final RegionEngineOptions rOpts = parent.copyRegionOpts();
        region.setId(newRegionId);
        region.setStartKey(splitKey);
        region.setRegionEpoch(new RegionEpoch(-1, -1));
        rOpts.setRegionId(newRegionId);
        rOpts.setStartKeyBytes(region.getStartKey());
        rOpts.setEndKeyBytes(region.getEndKey());
        rOpts.setRaftGroupId(JRaftHelper.getJRaftGroupId(this.pdClient.getClusterName(), newRegionId));
        rOpts.setRaftDataPath(null);
        String baseRaftDataPath = this.storeOpts.getRaftDataPath();
        if (Strings.isBlank(baseRaftDataPath)) {
            baseRaftDataPath = "";
        }
        rOpts.setRaftDataPath(baseRaftDataPath + "raft_data_region_" + region.getId() + "_" + getSelfEndpoint().getPort());
        final RegionEngine engine = new RegionEngine(region, this);
        if (!engine.init(rOpts)) {
            LOG.error("Fail to init [RegionEngine: {}].", region);
            throw Errors.REGION_ENGINE_FAIL.exception();
        }
        // update parent conf
        final Region pRegion = parent.getRegion();
        final RegionEpoch pEpoch = pRegion.getRegionEpoch();
        final long version = pEpoch.getVersion();
        // version + 1
        pEpoch.setVersion(version + 1);
        // update endKey
        pRegion.setEndKey(splitKey);
        // the following two lines of code can make a relation of 'happens-before' for
        // read 'pRegion', because that a write to a ConcurrentMap happens-before every
        // subsequent read of that ConcurrentMap.
        this.regionEngineTable.put(region.getId(), engine);
        registerRegionKVService(new DefaultRegionKVService(engine));
        // update local regionRouteTable
        this.pdClient.getRegionRouteTable().splitRegion(pRegion.getId(), region);
    } finally {
        this.splitting.set(false);
    }
}
Also used : Region(com.alipay.sofa.jraft.rhea.metadata.Region) RegionEngineOptions(com.alipay.sofa.jraft.rhea.options.RegionEngineOptions) RegionEpoch(com.alipay.sofa.jraft.rhea.metadata.RegionEpoch)

Aggregations

Region (com.alipay.sofa.jraft.rhea.metadata.Region)6 RegionEpoch (com.alipay.sofa.jraft.rhea.metadata.RegionEpoch)6 Configuration (com.alipay.sofa.jraft.conf.Configuration)2 MetadataStore (com.alipay.sofa.jraft.rhea.MetadataStore)1 RegionHeartbeatRequest (com.alipay.sofa.jraft.rhea.cmd.pd.RegionHeartbeatRequest)1 RegionStats (com.alipay.sofa.jraft.rhea.metadata.RegionStats)1 TimeInterval (com.alipay.sofa.jraft.rhea.metadata.TimeInterval)1 RegionEngineOptions (com.alipay.sofa.jraft.rhea.options.RegionEngineOptions)1 Pair (com.alipay.sofa.jraft.rhea.util.Pair)1 StampedLock (java.util.concurrent.locks.StampedLock)1