Search in sources :

Example 1 with RegionEpoch

use of com.alipay.sofa.jraft.rhea.metadata.RegionEpoch 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 RegionEpoch

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

the class AbstractPlacementDriverClient method getLocalRegionMetadata.

protected Region getLocalRegionMetadata(final RegionEngineOptions opts) {
    final long regionId = Requires.requireNonNull(opts.getRegionId(), "opts.regionId");
    Requires.requireTrue(regionId >= Region.MIN_ID_WITH_MANUAL_CONF, "opts.regionId must >= " + Region.MIN_ID_WITH_MANUAL_CONF);
    Requires.requireTrue(regionId < Region.MAX_ID_WITH_MANUAL_CONF, "opts.regionId must < " + Region.MAX_ID_WITH_MANUAL_CONF);
    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(com.alipay.sofa.jraft.conf.Configuration) Region(com.alipay.sofa.jraft.rhea.metadata.Region) RegionEpoch(com.alipay.sofa.jraft.rhea.metadata.RegionEpoch)

Example 3 with RegionEpoch

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

the class RegionRouteTableTest method makeRegion.

Region makeRegion(long id, byte[] startKey, byte[] endKey) {
    Region region = new Region();
    region.setId(id);
    region.setStartKey(startKey);
    region.setEndKey(endKey);
    region.setRegionEpoch(new RegionEpoch(-1, -1));
    return region;
}
Also used : Region(com.alipay.sofa.jraft.rhea.metadata.Region) RegionEpoch(com.alipay.sofa.jraft.rhea.metadata.RegionEpoch)

Example 4 with RegionEpoch

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

the class AbstractPlacementDriverClient method initRouteTableByRegion.

protected void initRouteTableByRegion(final RegionRouteTableOptions opts) {
    final long 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(com.alipay.sofa.jraft.conf.Configuration) Region(com.alipay.sofa.jraft.rhea.metadata.Region) RegionEpoch(com.alipay.sofa.jraft.rhea.metadata.RegionEpoch)

Example 5 with RegionEpoch

use of com.alipay.sofa.jraft.rhea.metadata.RegionEpoch 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);
    }
}
Also used : StampedLock(java.util.concurrent.locks.StampedLock) Region(com.alipay.sofa.jraft.rhea.metadata.Region) RegionEpoch(com.alipay.sofa.jraft.rhea.metadata.RegionEpoch)

Aggregations

Region (com.alipay.sofa.jraft.rhea.metadata.Region)6 RegionEpoch (com.alipay.sofa.jraft.rhea.metadata.RegionEpoch)6 Configuration (com.alipay.sofa.jraft.conf.Configuration)2 MetadataStore (com.alipay.sofa.jraft.rhea.MetadataStore)1 RegionHeartbeatRequest (com.alipay.sofa.jraft.rhea.cmd.pd.RegionHeartbeatRequest)1 RegionStats (com.alipay.sofa.jraft.rhea.metadata.RegionStats)1 TimeInterval (com.alipay.sofa.jraft.rhea.metadata.TimeInterval)1 RegionEngineOptions (com.alipay.sofa.jraft.rhea.options.RegionEngineOptions)1 Pair (com.alipay.sofa.jraft.rhea.util.Pair)1 StampedLock (java.util.concurrent.locks.StampedLock)1