Search in sources :

Example 1 with RegionEpoch

use of io.dingodb.store.row.metadata.RegionEpoch in project dingo by dingodb.

the class StoreEngine method doSplit.

public void doSplit(final String regionId, final String 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 = "";
        if (this.storeOpts.getStoreDBOptions() != null) {
            baseRaftDataPath = this.storeOpts.getRaftStoreOptions().getDataPath();
        }
        String raftDataPath = JRaftHelper.getRaftDataPath(baseRaftDataPath, region.getId(), getSelfEndpoint().getPort());
        rOpts.setRaftDataPath(raftDataPath);
        rOpts.setRaftStoreOptions(this.storeOpts.getRaftStoreOptions());
        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);
    /**
     * when Region is split, then the cluster info should be update.
     * 1. using the split region to replace the old region
     * 2. insert the split new region
     * 3. call the pdClient to notify the placement driver.
     */
    // todo Huzx
    /*
            {
                Store localStore = this.pdClient.getCurrentStore();
                List<Region> regionList = new ArrayList<>();
                for (Map.Entry<Long, RegionEngine> entry : this.regionEngineTable.entrySet()) {
                    regionList.add(entry.getValue().getRegion().copy());
                }
                localStore.setRegions(regionList);
                this.pdClient.refreshStore(localStore);
            }
             */
    } finally {
        this.splitting.set(false);
    }
}
Also used : Region(io.dingodb.store.row.metadata.Region) RegionEpoch(io.dingodb.store.row.metadata.RegionEpoch)

Example 2 with RegionEpoch

use of io.dingodb.store.row.metadata.RegionEpoch in project dingo by dingodb.

the class AbstractPlacementDriverClient method initRouteTableByRegion.

protected void initRouteTableByRegion(final RegionRouteTableOptions opts) {
    final String regionId = Requires.requireNonNull(opts.getRegionId(), "opts.regionId");
    final byte[] startKey = opts.getStartKeyBytes();
    final byte[] endKey = opts.getEndKeyBytes();
    final String initialServerList = opts.getInitialServerList();
    final Region region = new Region();
    final Configuration conf = new Configuration();
    // region
    region.setId(regionId);
    region.setStartKey(startKey);
    region.setEndKey(endKey);
    region.setRegionEpoch(new RegionEpoch(-1, -1));
    // peers
    Requires.requireTrue(Strings.isNotBlank(initialServerList), "opts.initialServerList is blank");
    conf.parse(initialServerList);
    region.setPeers(JRaftHelper.toPeerList(conf.listPeers()));
    // update raft route table
    RouteTable.getInstance().updateConfiguration(JRaftHelper.getJRaftGroupId(clusterName, regionId), conf);
    this.regionRouteTable.addOrUpdateRegion(region);
}
Also used : Configuration(io.dingodb.raft.conf.Configuration) Region(io.dingodb.store.row.metadata.Region) RegionEpoch(io.dingodb.store.row.metadata.RegionEpoch)

Example 3 with RegionEpoch

use of io.dingodb.store.row.metadata.RegionEpoch in project dingo by dingodb.

the class AbstractPlacementDriverClient method getLocalRegionMetadata.

protected Region getLocalRegionMetadata(final RegionEngineOptions opts) {
    final String regionId = Requires.requireNonNull(opts.getRegionId(), "opts.regionId");
    final byte[] startKey = opts.getStartKeyBytes();
    final byte[] endKey = opts.getEndKeyBytes();
    final String initialServerList = opts.getInitialServerList();
    final Region region = new Region();
    final Configuration conf = new Configuration();
    // region
    region.setId(regionId);
    region.setStartKey(startKey);
    region.setEndKey(endKey);
    region.setRegionEpoch(new RegionEpoch(-1, -1));
    // peers
    Requires.requireTrue(Strings.isNotBlank(initialServerList), "opts.initialServerList is blank");
    conf.parse(initialServerList);
    region.setPeers(JRaftHelper.toPeerList(conf.listPeers()));
    this.regionRouteTable.addOrUpdateRegion(region);
    return region;
}
Also used : Configuration(io.dingodb.raft.conf.Configuration) Region(io.dingodb.store.row.metadata.Region) RegionEpoch(io.dingodb.store.row.metadata.RegionEpoch)

Example 4 with RegionEpoch

use of io.dingodb.store.row.metadata.RegionEpoch in project dingo by dingodb.

the class RegionRouteTable method splitRegion.

public void splitRegion(final String leftId, final Region right) {
    Requires.requireNonNull(right, "right");
    Requires.requireNonNull(right.getRegionEpoch(), "right.regionEpoch");
    final StampedLock stampedLock = this.stampedLock;
    final long stamp = stampedLock.writeLock();
    try {
        final Region left = this.regionTable.get(leftId);
        Requires.requireNonNull(left, "left");
        final byte[] leftStartKey = BytesUtil.nullToEmpty(left.getStartKey());
        final byte[] leftEndKey = left.getEndKey();
        final String rightId = right.getId();
        final byte[] rightStartKey = right.getStartKey();
        final byte[] rightEndKey = right.getEndKey();
        Requires.requireNonNull(rightStartKey, "rightStartKey");
        Requires.requireTrue(BytesUtil.compare(leftStartKey, rightStartKey) < 0, "leftStartKey must < rightStartKey");
        if (leftEndKey == null || rightEndKey == null) {
            Requires.requireTrue(leftEndKey == rightEndKey, "leftEndKey must == rightEndKey");
        } else {
            Requires.requireTrue(BytesUtil.compare(leftEndKey, rightEndKey) == 0, "leftEndKey must == rightEndKey");
            Requires.requireTrue(BytesUtil.compare(rightStartKey, rightEndKey) < 0, "rightStartKey must < rightEndKey");
        }
        final RegionEpoch leftEpoch = left.getRegionEpoch();
        leftEpoch.setVersion(leftEpoch.getVersion() + 1);
        left.setEndKey(rightStartKey);
        this.regionTable.put(rightId, right.copy());
        this.rangeTable.put(rightStartKey, rightId);
    } finally {
        stampedLock.unlockWrite(stamp);
    }
}
Also used : StampedLock(java.util.concurrent.locks.StampedLock) Region(io.dingodb.store.row.metadata.Region) RegionEpoch(io.dingodb.store.row.metadata.RegionEpoch)

Example 5 with RegionEpoch

use of io.dingodb.store.row.metadata.RegionEpoch in project dingo by dingodb.

the class RowStoreMetaAdaptorImpl method mapping.

public Region mapping(RegionApp regionApp) {
    if (regionApp == null) {
        return null;
    }
    String regionId = regionApp.regionId();
    RegionView regionView = scheduleMetaAdaptor.regionView(regionApp.view());
    List<Peer> peerIds = regionView.nodeResources().stream().map(id -> new Peer(regionId, id.toString(), GeneralIdHelper.storeEndpoint(id))).collect(Collectors.toList());
    return new Region(regionId, regionApp.startKey(), regionApp.endKey(), new RegionEpoch(regionApp.version(), regionView.confVer()), peerIds);
}
Also used : ExecutorView(io.dingodb.server.coordinator.resource.impl.ExecutorView) StoreStats(io.dingodb.store.row.metadata.StoreStats) GeneralId(io.dingodb.server.coordinator.GeneralId) RegionView(io.dingodb.server.coordinator.app.impl.RegionView) StoreLabel(io.dingodb.store.row.metadata.StoreLabel) BigDecimal(java.math.BigDecimal) RowStoreMetaAdaptor(io.dingodb.server.coordinator.meta.RowStoreMetaAdaptor) Map(java.util.Map) Optional(io.dingodb.common.util.Optional) RegionEpoch(io.dingodb.store.row.metadata.RegionEpoch) Store(io.dingodb.store.row.metadata.Store) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Region(io.dingodb.store.row.metadata.Region) Set(java.util.Set) GeneralIdHelper(io.dingodb.server.coordinator.meta.GeneralIdHelper) Peer(io.dingodb.store.row.metadata.Peer) Collectors(java.util.stream.Collectors) Cluster(io.dingodb.store.row.metadata.Cluster) ScheduleMetaAdaptor(io.dingodb.server.coordinator.meta.ScheduleMetaAdaptor) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) ConcurrentHashSet(com.alipay.remoting.util.ConcurrentHashSet) RegionStats(io.dingodb.store.row.metadata.RegionStats) RegionApp(io.dingodb.server.coordinator.app.impl.RegionApp) Endpoint(io.dingodb.raft.util.Endpoint) RegionView(io.dingodb.server.coordinator.app.impl.RegionView) Peer(io.dingodb.store.row.metadata.Peer) Region(io.dingodb.store.row.metadata.Region) RegionEpoch(io.dingodb.store.row.metadata.RegionEpoch)

Aggregations

Region (io.dingodb.store.row.metadata.Region)5 RegionEpoch (io.dingodb.store.row.metadata.RegionEpoch)5 Configuration (io.dingodb.raft.conf.Configuration)2 ConcurrentHashSet (com.alipay.remoting.util.ConcurrentHashSet)1 Optional (io.dingodb.common.util.Optional)1 Endpoint (io.dingodb.raft.util.Endpoint)1 GeneralId (io.dingodb.server.coordinator.GeneralId)1 RegionApp (io.dingodb.server.coordinator.app.impl.RegionApp)1 RegionView (io.dingodb.server.coordinator.app.impl.RegionView)1 GeneralIdHelper (io.dingodb.server.coordinator.meta.GeneralIdHelper)1 RowStoreMetaAdaptor (io.dingodb.server.coordinator.meta.RowStoreMetaAdaptor)1 ScheduleMetaAdaptor (io.dingodb.server.coordinator.meta.ScheduleMetaAdaptor)1 ExecutorView (io.dingodb.server.coordinator.resource.impl.ExecutorView)1 Cluster (io.dingodb.store.row.metadata.Cluster)1 Peer (io.dingodb.store.row.metadata.Peer)1 RegionStats (io.dingodb.store.row.metadata.RegionStats)1 Store (io.dingodb.store.row.metadata.Store)1 StoreLabel (io.dingodb.store.row.metadata.StoreLabel)1 StoreStats (io.dingodb.store.row.metadata.StoreStats)1 BigDecimal (java.math.BigDecimal)1