use of com.alipay.sofa.jraft.rhea.metadata.Region 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;
}
use of com.alipay.sofa.jraft.rhea.metadata.Region in project sofa-jraft by sofastack.
the class HeartbeatSender method sendRegionHeartbeat.
private void sendRegionHeartbeat(final long nextDelay, final long lastTime, final boolean forceRefreshLeader) {
final long now = System.currentTimeMillis();
final RegionHeartbeatRequest request = new RegionHeartbeatRequest();
request.setClusterId(this.storeEngine.getClusterId());
request.setStoreId(this.storeEngine.getStoreId());
request.setLeastKeysOnSplit(this.storeEngine.getStoreOpts().getLeastKeysOnSplit());
final List<Long> regionIdList = this.storeEngine.getLeaderRegionIds();
if (regionIdList.isEmpty()) {
// So sad, there is no even a region leader :(
final RegionHeartbeatTask nextTask = new RegionHeartbeatTask(nextDelay, now, false);
this.heartbeatTimer.newTimeout(nextTask, nextTask.getNextDelay(), TimeUnit.SECONDS);
if (LOG.isInfoEnabled()) {
LOG.info("So sad, there is no even a region leader on [clusterId:{}, storeId: {}, endpoint:{}].", this.storeEngine.getClusterId(), this.storeEngine.getStoreId(), this.storeEngine.getSelfEndpoint());
}
return;
}
final List<Pair<Region, RegionStats>> regionStatsList = Lists.newArrayListWithCapacity(regionIdList.size());
final TimeInterval timeInterval = new TimeInterval(lastTime, now);
for (final Long regionId : regionIdList) {
final Region region = this.pdClient.getRegionById(regionId);
final RegionStats stats = this.statsCollector.collectRegionStats(region, timeInterval);
if (stats == null) {
continue;
}
regionStatsList.add(Pair.of(region, stats));
}
request.setRegionStatsList(regionStatsList);
final HeartbeatClosure<List<Instruction>> closure = new HeartbeatClosure<List<Instruction>>() {
@Override
public void run(final Status status) {
final boolean isOk = status.isOk();
if (isOk) {
final List<Instruction> instructions = getResult();
if (instructions != null && !instructions.isEmpty()) {
instructionProcessor.process(instructions);
}
}
final boolean forceRefresh = !isOk && ErrorsHelper.isInvalidPeer(getError());
final RegionHeartbeatTask nextTask = new RegionHeartbeatTask(nextDelay, now, forceRefresh);
heartbeatTimer.newTimeout(nextTask, nextTask.getNextDelay(), TimeUnit.SECONDS);
}
};
final Endpoint endpoint = this.pdClient.getPdLeader(forceRefreshLeader, this.heartbeatRpcTimeoutMillis);
callAsyncWithRpc(endpoint, request, closure);
}
use of com.alipay.sofa.jraft.rhea.metadata.Region in project sofa-jraft by sofastack.
the class MemoryKVStoreSnapshotFile method doSnapshotLoad.
@Override
void doSnapshotLoad(final String snapshotPath, final LocalFileMeta meta, final Region region) throws Exception {
final File file = new File(snapshotPath);
if (!file.exists()) {
throw new StorageException("Snapshot file [" + snapshotPath + "] not exists");
}
final Region snapshotRegion = readMetadata(meta, Region.class);
if (!RegionHelper.isSameRange(region, snapshotRegion)) {
throw new StorageException("Invalid snapshot region: " + snapshotRegion + ", current region is: " + region);
}
this.kvStore.doSnapshotLoad(this, snapshotPath);
}
use of com.alipay.sofa.jraft.rhea.metadata.Region 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;
}
use of com.alipay.sofa.jraft.rhea.metadata.Region in project sofa-jraft by sofastack.
the class RegionRouteTable method splitRegion.
public void splitRegion(final long 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 long 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);
}
}
Aggregations