Search in sources :

Example 36 with Region

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;
}
Also used : Store(com.alipay.sofa.jraft.rhea.metadata.Store) Region(com.alipay.sofa.jraft.rhea.metadata.Region) RegionEngineOptions(com.alipay.sofa.jraft.rhea.options.RegionEngineOptions)

Example 37 with Region

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

Example 38 with Region

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);
}
Also used : Region(com.alipay.sofa.jraft.rhea.metadata.Region) File(java.io.File) StorageException(com.alipay.sofa.jraft.rhea.errors.StorageException)

Example 39 with Region

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;
}
Also used : Endpoint(com.alipay.sofa.jraft.util.Endpoint) Store(com.alipay.sofa.jraft.rhea.metadata.Store) Region(com.alipay.sofa.jraft.rhea.metadata.Region) RegionEngineOptions(com.alipay.sofa.jraft.rhea.options.RegionEngineOptions)

Example 40 with Region

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);
    }
}
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)67 KVEntry (com.alipay.sofa.jraft.rhea.storage.KVEntry)35 List (java.util.List)35 Map (java.util.Map)35 Status (com.alipay.sofa.jraft.Status)34 Lists (com.alipay.sofa.jraft.rhea.util.Lists)34 BytesUtil (com.alipay.sofa.jraft.util.BytesUtil)34 Endpoint (com.alipay.sofa.jraft.util.Endpoint)33 KVIterator (com.alipay.sofa.jraft.rhea.storage.KVIterator)31 KVStoreClosure (com.alipay.sofa.jraft.rhea.storage.KVStoreClosure)31 RawKVStore (com.alipay.sofa.jraft.rhea.storage.RawKVStore)31 Sequence (com.alipay.sofa.jraft.rhea.storage.Sequence)31 ByteArray (com.alipay.sofa.jraft.rhea.util.ByteArray)31 DistributedLock (com.alipay.sofa.jraft.rhea.util.concurrent.DistributedLock)31 TimeUnit (java.util.concurrent.TimeUnit)31 CASEntry (com.alipay.sofa.jraft.rhea.storage.CASEntry)30 Requires (com.alipay.sofa.jraft.util.Requires)30 Logger (org.slf4j.Logger)30 LoggerFactory (org.slf4j.LoggerFactory)30 RegionEngine (com.alipay.sofa.jraft.rhea.RegionEngine)29