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);
}
}
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);
}
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;
}
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);
}
}
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);
}
Aggregations