Search in sources :

Example 26 with Region

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

the class RegionRouteTable method findRegionsByCASEntries.

/**
 * Returns the list of regions to which the keys belongs.
 */
public Map<Region, List<CASEntry>> findRegionsByCASEntries(final List<CASEntry> casEntries) {
    Requires.requireNonNull(casEntries, "casEntries");
    final Map<Region, List<CASEntry>> regionMap = Maps.newHashMap();
    final StampedLock stampedLock = this.stampedLock;
    final long stamp = stampedLock.readLock();
    try {
        for (final CASEntry casEntry : casEntries) {
            final Region region = findRegionByKeyWithoutLock(casEntry.getKey());
            regionMap.computeIfAbsent(region, k -> Lists.newArrayList()).add(casEntry);
        }
        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) CASEntry(com.alipay.sofa.jraft.rhea.storage.CASEntry)

Example 27 with Region

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

the class AbstractRheaKVStoreTest method checkRegion.

private void checkRegion(RheaKVStore store, byte[] key, long expectedRegionId) {
    Region region = store.getPlacementDriverClient().findRegionByKey(key, false);
    assertEquals(expectedRegionId, region.getId());
}
Also used : Region(com.alipay.sofa.jraft.rhea.metadata.Region)

Example 28 with Region

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

the class RegionRouteTableTest method brokenTest.

@Test(expected = RouteTableException.class)
public void brokenTest() {
    RegionRouteTable table = new RegionRouteTable();
    // Region r1 = makeRegion(1, null, KeyValueTool.makeKey("c"));
    Region r2 = makeRegion(2, KeyValueTool.makeKey("c"), KeyValueTool.makeKey("e"));
    Region r3 = makeRegion(3, KeyValueTool.makeKey("e"), KeyValueTool.makeKey("g"));
    Region r4 = makeRegion(4, KeyValueTool.makeKey("g"), KeyValueTool.makeKey("i"));
    Region r5 = makeRegion(5, KeyValueTool.makeKey("i"), KeyValueTool.makeKey("k"));
    Region r6 = makeRegion(6, KeyValueTool.makeKey("k"), KeyValueTool.makeKey("n"));
    Region r7 = makeRegion(7, KeyValueTool.makeKey("n"), null);
    table.addOrUpdateRegion(r2);
    table.addOrUpdateRegion(r3);
    table.addOrUpdateRegion(r4);
    table.addOrUpdateRegion(r5);
    table.addOrUpdateRegion(r6);
    table.addOrUpdateRegion(r7);
    table.findRegionByKey(KeyValueTool.makeKey("a"));
}
Also used : Region(com.alipay.sofa.jraft.rhea.metadata.Region) Test(org.junit.Test)

Example 29 with Region

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

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

the class RegionRouteTableTest method splitRegionTest.

@Test
public void splitRegionTest() {
    RegionRouteTable table = new RegionRouteTable();
    Region region = makeRegion(-1, null, null);
    table.addOrUpdateRegion(region);
    Region newRegion = makeRegion(1, BytesUtil.writeUtf8("t"), null);
    table.splitRegion(-1, newRegion);
    Region found = table.findRegionByKey(BytesUtil.writeUtf8("a"));
    assertEquals(-1, found.getId());
    found = table.findRegionByKey(BytesUtil.writeUtf8("w"));
    assertEquals(1, found.getId());
}
Also used : Region(com.alipay.sofa.jraft.rhea.metadata.Region) Test(org.junit.Test)

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