Search in sources :

Example 1 with TimeInterval

use of com.alipay.sofa.jraft.rhea.metadata.TimeInterval 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();
        }
    }
}
Also used : MetadataStore(com.alipay.sofa.jraft.rhea.MetadataStore) RegionStats(com.alipay.sofa.jraft.rhea.metadata.RegionStats) TimeInterval(com.alipay.sofa.jraft.rhea.metadata.TimeInterval) Region(com.alipay.sofa.jraft.rhea.metadata.Region) RegionEpoch(com.alipay.sofa.jraft.rhea.metadata.RegionEpoch) RegionHeartbeatRequest(com.alipay.sofa.jraft.rhea.cmd.pd.RegionHeartbeatRequest) Pair(com.alipay.sofa.jraft.rhea.util.Pair)

Example 2 with TimeInterval

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

the class StoreStatsValidator method readMessage.

@Override
public void readMessage(final HandlerContext ctx, final StorePingEvent event) throws Exception {
    final MetadataStore metadataStore = event.getMetadataStore();
    final StoreHeartbeatRequest request = event.getMessage();
    final StoreStats storeStats = request.getStats();
    if (storeStats == null) {
        LOG.error("Empty [StoreStats] by event: {}.", event);
        throw Errors.INVALID_STORE_STATS.exception();
    }
    final StoreStats currentStoreStats = metadataStore.getStoreStats(request.getClusterId(), storeStats.getStoreId());
    if (currentStoreStats == null) {
        // new data
        return;
    }
    final TimeInterval interval = storeStats.getInterval();
    if (interval == null) {
        LOG.error("Empty [TimeInterval] by event: {}.", event);
        throw Errors.INVALID_STORE_STATS.exception();
    }
    final TimeInterval currentInterval = currentStoreStats.getInterval();
    if (interval.getEndTimestamp() < currentInterval.getEndTimestamp()) {
        LOG.error("The [TimeInterval] is out of date: {}.", event);
        throw Errors.STORE_HEARTBEAT_OUT_OF_DATE.exception();
    }
}
Also used : MetadataStore(com.alipay.sofa.jraft.rhea.MetadataStore) StoreHeartbeatRequest(com.alipay.sofa.jraft.rhea.cmd.pd.StoreHeartbeatRequest) StoreStats(com.alipay.sofa.jraft.rhea.metadata.StoreStats) TimeInterval(com.alipay.sofa.jraft.rhea.metadata.TimeInterval)

Example 3 with TimeInterval

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

the class HeartbeatSender method sendStoreHeartbeat.

private void sendStoreHeartbeat(final long nextDelay, final boolean forceRefreshLeader, final long lastTime) {
    final long now = System.currentTimeMillis();
    final StoreHeartbeatRequest request = new StoreHeartbeatRequest();
    request.setClusterId(this.storeEngine.getClusterId());
    final TimeInterval timeInterval = new TimeInterval(lastTime, now);
    final StoreStats stats = this.statsCollector.collectStoreStats(timeInterval);
    request.setStats(stats);
    final HeartbeatClosure<Object> closure = new HeartbeatClosure<Object>() {

        @Override
        public void run(final Status status) {
            final boolean forceRefresh = !status.isOk() && ErrorsHelper.isInvalidPeer(getError());
            final StoreHeartbeatTask nexTask = new StoreHeartbeatTask(nextDelay, now, forceRefresh);
            heartbeatTimer.newTimeout(nexTask, nexTask.getNextDelay(), TimeUnit.SECONDS);
        }
    };
    final Endpoint endpoint = this.pdClient.getPdLeader(forceRefreshLeader, this.heartbeatRpcTimeoutMillis);
    callAsyncWithRpc(endpoint, request, closure);
}
Also used : StoreHeartbeatRequest(com.alipay.sofa.jraft.rhea.cmd.pd.StoreHeartbeatRequest) Status(com.alipay.sofa.jraft.Status) StoreStats(com.alipay.sofa.jraft.rhea.metadata.StoreStats) TimeInterval(com.alipay.sofa.jraft.rhea.metadata.TimeInterval) Endpoint(com.alipay.sofa.jraft.util.Endpoint)

Example 4 with TimeInterval

use of com.alipay.sofa.jraft.rhea.metadata.TimeInterval 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);
}
Also used : Status(com.alipay.sofa.jraft.Status) TimeInterval(com.alipay.sofa.jraft.rhea.metadata.TimeInterval) RegionStats(com.alipay.sofa.jraft.rhea.metadata.RegionStats) Instruction(com.alipay.sofa.jraft.rhea.metadata.Instruction) RegionHeartbeatRequest(com.alipay.sofa.jraft.rhea.cmd.pd.RegionHeartbeatRequest) Endpoint(com.alipay.sofa.jraft.util.Endpoint) Region(com.alipay.sofa.jraft.rhea.metadata.Region) List(java.util.List) Pair(com.alipay.sofa.jraft.rhea.util.Pair)

Aggregations

TimeInterval (com.alipay.sofa.jraft.rhea.metadata.TimeInterval)4 Status (com.alipay.sofa.jraft.Status)2 MetadataStore (com.alipay.sofa.jraft.rhea.MetadataStore)2 RegionHeartbeatRequest (com.alipay.sofa.jraft.rhea.cmd.pd.RegionHeartbeatRequest)2 StoreHeartbeatRequest (com.alipay.sofa.jraft.rhea.cmd.pd.StoreHeartbeatRequest)2 Region (com.alipay.sofa.jraft.rhea.metadata.Region)2 RegionStats (com.alipay.sofa.jraft.rhea.metadata.RegionStats)2 StoreStats (com.alipay.sofa.jraft.rhea.metadata.StoreStats)2 Pair (com.alipay.sofa.jraft.rhea.util.Pair)2 Endpoint (com.alipay.sofa.jraft.util.Endpoint)2 Instruction (com.alipay.sofa.jraft.rhea.metadata.Instruction)1 RegionEpoch (com.alipay.sofa.jraft.rhea.metadata.RegionEpoch)1 List (java.util.List)1