use of com.alipay.sofa.jraft.rhea.metadata.RegionStats in project sofa-jraft by sofastack.
the class RegionStatsValidator method readMessage.
@Override
public void readMessage(final HandlerContext ctx, final RegionPingEvent event) throws Exception {
final MetadataStore metadataStore = event.getMetadataStore();
final RegionHeartbeatRequest request = event.getMessage();
final List<Pair<Region, RegionStats>> regionStatsList = request.getRegionStatsList();
if (regionStatsList == null || regionStatsList.isEmpty()) {
LOG.error("Empty [RegionStatsList] by event: {}.", event);
throw Errors.INVALID_REGION_STATS.exception();
}
for (final Pair<Region, RegionStats> pair : regionStatsList) {
final Region region = pair.getKey();
if (region == null) {
LOG.error("Empty [Region] by event: {}.", event);
throw Errors.INVALID_REGION_STATS.exception();
}
final RegionEpoch regionEpoch = region.getRegionEpoch();
if (regionEpoch == null) {
LOG.error("Empty [RegionEpoch] by event: {}.", event);
throw Errors.INVALID_REGION_STATS.exception();
}
final RegionStats regionStats = pair.getValue();
if (regionStats == null) {
LOG.error("Empty [RegionStats] by event: {}.", event);
throw Errors.INVALID_REGION_STATS.exception();
}
final Pair<Region, RegionStats> currentRegionInfo = metadataStore.getRegionStats(request.getClusterId(), region);
if (currentRegionInfo == null) {
// new data
return;
}
final Region currentRegion = currentRegionInfo.getKey();
if (regionEpoch.compareTo(currentRegion.getRegionEpoch()) < 0) {
LOG.error("The region epoch is out of date: {}.", event);
throw Errors.REGION_HEARTBEAT_OUT_OF_DATE.exception();
}
final TimeInterval interval = regionStats.getInterval();
if (interval == null) {
LOG.error("Empty [TimeInterval] by event: {}.", event);
throw Errors.INVALID_REGION_STATS.exception();
}
final TimeInterval currentInterval = currentRegionInfo.getValue().getInterval();
if (interval.getEndTimestamp() < currentInterval.getEndTimestamp()) {
LOG.error("The [TimeInterval] is out of date: {}.", event);
throw Errors.REGION_HEARTBEAT_OUT_OF_DATE.exception();
}
}
}
use of com.alipay.sofa.jraft.rhea.metadata.RegionStats in project sofa-jraft by sofastack.
the class SplittingJudgeByApproximateKeysHandler method readMessage.
@Override
public void readMessage(final HandlerContext ctx, final RegionPingEvent event) throws Exception {
if (event.isReady()) {
return;
}
final MetadataStore metadataStore = event.getMetadataStore();
final RegionHeartbeatRequest request = event.getMessage();
final long clusterId = request.getClusterId();
final ClusterStatsManager clusterStatsManager = ClusterStatsManager.getInstance(clusterId);
clusterStatsManager.addOrUpdateRegionStats(request.getRegionStatsList());
final Set<Long> stores = metadataStore.unsafeGetStoreIds(clusterId);
if (stores == null || stores.isEmpty()) {
return;
}
if (clusterStatsManager.regionSize() >= stores.size()) {
// one store one region is perfect
return;
}
final Pair<Region, RegionStats> modelWorker = clusterStatsManager.findModelWorkerRegion();
if (!isSplitNeeded(request, modelWorker)) {
return;
}
LOG.info("[Cluster: {}] model worker region is: {}.", clusterId, modelWorker);
final Long newRegionId = metadataStore.createRegionId(clusterId);
final Instruction.RangeSplit rangeSplit = new Instruction.RangeSplit();
rangeSplit.setNewRegionId(newRegionId);
final Instruction instruction = new Instruction();
instruction.setRegion(modelWorker.getKey().copy());
instruction.setRangeSplit(rangeSplit);
event.addInstruction(instruction);
}
use of com.alipay.sofa.jraft.rhea.metadata.RegionStats in project sofa-jraft by sofastack.
the class DefaultMetadataStore method batchUpdateRegionStats.
@Override
public CompletableFuture<Boolean> batchUpdateRegionStats(final long clusterId, final List<Pair<Region, RegionStats>> regionStatsList) {
final List<KVEntry> entries = Lists.newArrayListWithCapacity(regionStatsList.size());
for (final Pair<Region, RegionStats> p : regionStatsList) {
final String key = MetadataKeyHelper.getRegionStatsKey(clusterId, p.getKey().getId());
final byte[] bytes = this.serializer.writeObject(p);
entries.add(new KVEntry(BytesUtil.writeUtf8(key), bytes));
}
return this.rheaKVStore.put(entries);
}
use of com.alipay.sofa.jraft.rhea.metadata.RegionStats 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.RegionStats in project sofa-jraft by sofastack.
the class StatsCollector method collectRegionStats.
public RegionStats collectRegionStats(final Region region, final TimeInterval timeInterval) {
final RegionStats stats = new RegionStats();
stats.setRegionId(region.getId());
// Leader Peer sending the heartbeat
stats.setLeader(new Peer(region.getId(), this.storeEngine.getStoreId(), this.storeEngine.getSelfEndpoint()));
// Leader considers that these peers are down
// TODO
// Pending peers are the peers that the leader can't consider as working followers
// TODO
// Bytes written for the region during this period
stats.setBytesWritten(getRegionBytesWritten(region, true));
// Bytes read for the region during this period
stats.setBytesRead(getRegionBytesRead(region, true));
// Keys written for the region during this period
stats.setKeysWritten(getRegionKeysWritten(region, true));
// Keys read for the region during this period
stats.setKeysRead(getRegionKeysRead(region, true));
// Approximate region size
// TODO very important
// Approximate number of keys
stats.setApproximateKeys(this.rawKVStore.getApproximateKeysInRange(region.getStartKey(), region.getEndKey()));
// Actually reported time interval
stats.setInterval(timeInterval);
LOG.info("Collect [RegionStats]: {}.", stats);
return stats;
}
Aggregations