Search in sources :

Example 41 with Region

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

the class RegionRouteTable method findRegionsByKeys.

/**
 * Returns the list of regions to which the keys belongs.
 */
public Map<Region, List<byte[]>> findRegionsByKeys(final List<byte[]> keys) {
    Requires.requireNonNull(keys, "keys");
    final Map<Region, List<byte[]>> regionMap = Maps.newHashMap();
    final StampedLock stampedLock = this.stampedLock;
    final long stamp = stampedLock.readLock();
    try {
        for (final byte[] key : keys) {
            final Region region = findRegionByKeyWithoutLock(key);
            regionMap.computeIfAbsent(region, k -> Lists.newArrayList()).add(key);
        }
        return regionMap;
    } finally {
        stampedLock.unlockRead(stamp);
    }
}
Also used : Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) RouteTableException(com.alipay.sofa.jraft.rhea.errors.RouteTableException) NavigableMap(java.util.NavigableMap) Region(com.alipay.sofa.jraft.rhea.metadata.Region) Lists(com.alipay.sofa.jraft.rhea.util.Lists) List(java.util.List) RegionEpoch(com.alipay.sofa.jraft.rhea.metadata.RegionEpoch) TreeMap(java.util.TreeMap) Maps(com.alipay.sofa.jraft.rhea.util.Maps) Map(java.util.Map) KVEntry(com.alipay.sofa.jraft.rhea.storage.KVEntry) Comparator(java.util.Comparator) StampedLock(java.util.concurrent.locks.StampedLock) Requires(com.alipay.sofa.jraft.util.Requires) CASEntry(com.alipay.sofa.jraft.rhea.storage.CASEntry) BytesUtil(com.alipay.sofa.jraft.util.BytesUtil) Region(com.alipay.sofa.jraft.rhea.metadata.Region) StampedLock(java.util.concurrent.locks.StampedLock) List(java.util.List)

Example 42 with Region

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

the class RegionRouteTable method getRegionById.

public Region getRegionById(final long regionId) {
    final StampedLock stampedLock = this.stampedLock;
    long stamp = stampedLock.tryOptimisticRead();
    // validate() emit a load-fence, but no store-fence.  So you should only have
    // load instructions inside a block of tryOptimisticRead() / validate(),
    // because it is meant to the a read-only operation, and therefore, it is fine
    // to use the loadFence() function to avoid re-ordering.
    Region region = safeCopy(this.regionTable.get(regionId));
    if (!stampedLock.validate(stamp)) {
        stamp = stampedLock.readLock();
        try {
            region = safeCopy(this.regionTable.get(regionId));
        } finally {
            stampedLock.unlockRead(stamp);
        }
    }
    return region;
}
Also used : StampedLock(java.util.concurrent.locks.StampedLock) Region(com.alipay.sofa.jraft.rhea.metadata.Region)

Example 43 with Region

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

the class RegionRouteTable method findRegionsByKeyRange.

/**
 * Returns the list of regions covered by startKey and endKey.
 */
public List<Region> findRegionsByKeyRange(final byte[] startKey, final byte[] endKey) {
    final StampedLock stampedLock = this.stampedLock;
    final long stamp = stampedLock.readLock();
    try {
        final byte[] realStartKey = BytesUtil.nullToEmpty(startKey);
        final NavigableMap<byte[], Long> subRegionMap;
        if (endKey == null) {
            subRegionMap = this.rangeTable.tailMap(realStartKey, false);
        } else {
            subRegionMap = this.rangeTable.subMap(realStartKey, false, endKey, true);
        }
        final List<Region> regionList = Lists.newArrayListWithCapacity(subRegionMap.size() + 1);
        final Map.Entry<byte[], Long> headEntry = this.rangeTable.floorEntry(realStartKey);
        if (headEntry == null) {
            reportFail(startKey);
            throw reject(startKey, "fail to find region by startKey");
        }
        regionList.add(safeCopy(this.regionTable.get(headEntry.getValue())));
        for (final Long regionId : subRegionMap.values()) {
            regionList.add(safeCopy(this.regionTable.get(regionId)));
        }
        return regionList;
    } finally {
        stampedLock.unlockRead(stamp);
    }
}
Also used : StampedLock(java.util.concurrent.locks.StampedLock) Region(com.alipay.sofa.jraft.rhea.metadata.Region) NavigableMap(java.util.NavigableMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 44 with Region

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

the class RegionRouteTable method removeRegion.

public boolean removeRegion(final long regionId) {
    final StampedLock stampedLock = this.stampedLock;
    final long stamp = stampedLock.writeLock();
    try {
        final Region region = this.regionTable.remove(regionId);
        if (region != null) {
            final byte[] startKey = BytesUtil.nullToEmpty(region.getStartKey());
            return this.rangeTable.remove(startKey) != null;
        }
    } finally {
        stampedLock.unlockWrite(stamp);
    }
    return false;
}
Also used : StampedLock(java.util.concurrent.locks.StampedLock) Region(com.alipay.sofa.jraft.rhea.metadata.Region)

Example 45 with Region

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

the class RegionRouteTable method findRegionsByKvEntries.

/**
 * Returns the list of regions to which the keys belongs.
 */
public Map<Region, List<KVEntry>> findRegionsByKvEntries(final List<KVEntry> kvEntries) {
    Requires.requireNonNull(kvEntries, "kvEntries");
    final Map<Region, List<KVEntry>> regionMap = Maps.newHashMap();
    final StampedLock stampedLock = this.stampedLock;
    final long stamp = stampedLock.readLock();
    try {
        for (final KVEntry kvEntry : kvEntries) {
            final Region region = findRegionByKeyWithoutLock(kvEntry.getKey());
            regionMap.computeIfAbsent(region, k -> Lists.newArrayList()).add(kvEntry);
        }
        return regionMap;
    } finally {
        stampedLock.unlockRead(stamp);
    }
}
Also used : Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) RouteTableException(com.alipay.sofa.jraft.rhea.errors.RouteTableException) NavigableMap(java.util.NavigableMap) Region(com.alipay.sofa.jraft.rhea.metadata.Region) Lists(com.alipay.sofa.jraft.rhea.util.Lists) List(java.util.List) RegionEpoch(com.alipay.sofa.jraft.rhea.metadata.RegionEpoch) TreeMap(java.util.TreeMap) Maps(com.alipay.sofa.jraft.rhea.util.Maps) Map(java.util.Map) KVEntry(com.alipay.sofa.jraft.rhea.storage.KVEntry) Comparator(java.util.Comparator) StampedLock(java.util.concurrent.locks.StampedLock) Requires(com.alipay.sofa.jraft.util.Requires) CASEntry(com.alipay.sofa.jraft.rhea.storage.CASEntry) BytesUtil(com.alipay.sofa.jraft.util.BytesUtil) KVEntry(com.alipay.sofa.jraft.rhea.storage.KVEntry) Region(com.alipay.sofa.jraft.rhea.metadata.Region) StampedLock(java.util.concurrent.locks.StampedLock) List(java.util.List)

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