use of org.apache.bookkeeper.stream.proto.RangeProperties in project bookkeeper by apache.
the class TestProtocolInternalUtils method testCreateActiveRanges.
//
// Test Meta KeyRange Server Requests
//
@Test
public void testCreateActiveRanges() {
GetActiveRangesResponse.Builder responseBuilder = GetActiveRangesResponse.newBuilder();
responseBuilder.addRanges(RelatedRanges.newBuilder().setProps(RangeProperties.newBuilder().setStartHashKey(Long.MIN_VALUE).setEndHashKey(0L).setRangeId(1L).setStorageContainerId(1L)).setType(RelationType.PARENTS).addRelatedRanges(INVALID_RANGE_ID)).addRanges(RelatedRanges.newBuilder().setProps(RangeProperties.newBuilder().setStartHashKey(0L).setEndHashKey(Long.MAX_VALUE).setRangeId(2L).setStorageContainerId(2L)).setType(RelationType.PARENTS).addRelatedRanges(INVALID_RANGE_ID));
GetActiveRangesResponse response = responseBuilder.build();
HashStreamRanges hsr = createActiveRanges(response);
TreeMap<Long, RangeProperties> activeRanges = Maps.newTreeMap();
activeRanges.put(Long.MIN_VALUE, response.getRanges(0).getProps());
activeRanges.put(0L, response.getRanges(1).getProps());
HashStreamRanges expectedHSR = HashStreamRanges.ofHash(RangeKeyType.HASH, activeRanges);
assertEquals(expectedHSR, hsr);
assertEquals(2L, hsr.getMaxRangeId());
}
use of org.apache.bookkeeper.stream.proto.RangeProperties in project bookkeeper by apache.
the class StreamRanges method ofHash.
public static HashStreamRanges ofHash(RangeKeyType keyType, NavigableMap<Long, RangeProperties> ranges) {
checkArgument(RangeKeyType.HASH == keyType, "Only hash routing is supported now. %s is not supported.", keyType);
NavigableMap<Long, RangeProperties> readOnlyRanges = Collections.unmodifiableNavigableMap(ranges);
long maxRangeId = 0L;
for (RangeProperties props : ranges.values()) {
maxRangeId = Math.max(maxRangeId, props.getRangeId());
}
return new HashStreamRanges(readOnlyRanges, maxRangeId);
}
use of org.apache.bookkeeper.stream.proto.RangeProperties in project bookkeeper by apache.
the class ProtocolInternalUtils method createActiveRanges.
static HashStreamRanges createActiveRanges(GetActiveRangesResponse response) {
TreeMap<Long, RangeProperties> ranges = Maps.newTreeMap();
long lastEndKey = Long.MIN_VALUE;
for (RelatedRanges rr : response.getRangesList()) {
RangeProperties range = rr.getProps();
long startKey = range.getStartHashKey();
long endKey = range.getEndHashKey();
checkState(lastEndKey == startKey, "Invalid range key found : expected = %s, actual = %s", lastEndKey, startKey);
ranges.put(startKey, range);
lastEndKey = endKey;
}
checkState(Long.MAX_VALUE == lastEndKey, "Missing key range [%s - %s)", lastEndKey, Long.MAX_VALUE);
checkState(ranges.size() > 0, "No active range found");
return HashStreamRanges.ofHash(RangeKeyType.HASH, ranges);
}
use of org.apache.bookkeeper.stream.proto.RangeProperties in project bookkeeper by apache.
the class RangeRouter method getRange.
/**
* Get the range to route the given {@code key}.
*
* <p>If <i>key</i> is null, a range is picked randomly. Otherwise, the range is picked
* according to the hash code of {@code key}.
*
* <p>This function should be called after {@link #setRanges(HashStreamRanges)}.
*
* @param key the key to route
* @return the range to write.
* @throws IllegalStateException if ranges is empty.
*/
public Long getRange(@Nullable K key) {
long routingKey;
if (null != key) {
routingKey = keyRouter.getRoutingKey(key);
} else {
routingKey = ThreadLocalRandom.current().nextLong();
}
HashStreamRanges rs;
long stamp = lock.tryOptimisticRead();
rs = ranges;
if (!lock.validate(stamp)) {
stamp = lock.readLock();
try {
rs = ranges;
} finally {
lock.unlockRead(stamp);
}
}
checkState(null != rs, "No range is available");
Map.Entry<Long, RangeProperties> ceilingEntry = rs.getRanges().floorEntry(routingKey);
return ceilingEntry.getValue().getRangeId();
}
use of org.apache.bookkeeper.stream.proto.RangeProperties in project bookkeeper by apache.
the class TestPByteBufTableImpl method testBasicOperations.
@SuppressWarnings("unchecked")
@Test
public void testBasicOperations() throws Exception {
when(mockMetaRangeClient.getActiveDataRanges()).thenReturn(FutureUtils.value(streamRanges1));
ConcurrentMap<Long, PTable<ByteBuf, ByteBuf>> tableRanges = Maps.newConcurrentMap();
for (RangeProperties rangeProps : streamRanges1.getRanges().values()) {
tableRanges.put(rangeProps.getRangeId(), mock(PTable.class));
}
RangeRouter<ByteBuf> mockRouter = mock(RangeRouter.class);
when(mockRouter.getRange(any(ByteBuf.class))).thenAnswer(invocationOnMock -> {
ByteBuf key = invocationOnMock.getArgument(0);
byte[] keyData = ByteBufUtil.getBytes(key);
return Bytes.toLong(keyData, 0);
});
TableRangeFactory<ByteBuf, ByteBuf> trFactory = (streamProps1, rangeProps, executor, opFactory, resultFactory, kvFactory) -> tableRanges.get(rangeProps.getRangeId());
PByteBufTableImpl table = new PByteBufTableImpl(runtime.getMethodName(), streamProps, mockClientManager, scheduler.chooseThread(), trFactory, Optional.of(mockRouter));
assertEquals(0, table.getTableRanges().size());
verify(mockRouter, times(0)).setRanges(any(HashStreamRanges.class));
// initialize the table
assertTrue(table == FutureUtils.result(table.initialize()));
verify(mockRouter, times(1)).setRanges(eq(streamRanges1));
assertEquals(4, table.getTableRanges().size());
// test get
for (RangeProperties rangeProps : streamRanges1.getRanges().values()) {
ByteBuf pkey = Unpooled.wrappedBuffer(Bytes.toBytes(rangeProps.getRangeId()));
ByteBuf lkey = Unpooled.wrappedBuffer(Bytes.toBytes(rangeProps.getRangeId()));
try (RangeOption<ByteBuf> option = optionFactory.newRangeOption().build()) {
table.get(pkey, lkey, option);
verify(tableRanges.get(rangeProps.getRangeId()), times(1)).get(eq(pkey), eq(lkey), eq(option));
}
}
// test put
for (RangeProperties rangeProps : streamRanges1.getRanges().values()) {
ByteBuf pkey = Unpooled.wrappedBuffer(Bytes.toBytes(rangeProps.getRangeId()));
ByteBuf lkey = Unpooled.wrappedBuffer(Bytes.toBytes(rangeProps.getRangeId()));
ByteBuf value = Unpooled.wrappedBuffer(Bytes.toBytes(rangeProps.getRangeId()));
try (PutOption<ByteBuf> option = optionFactory.newPutOption().build()) {
table.put(pkey, lkey, value, option);
verify(tableRanges.get(rangeProps.getRangeId()), times(1)).put(eq(pkey), eq(lkey), eq(value), eq(option));
}
}
// test increment
for (RangeProperties rangeProps : streamRanges1.getRanges().values()) {
ByteBuf pkey = Unpooled.wrappedBuffer(Bytes.toBytes(rangeProps.getRangeId()));
ByteBuf lkey = Unpooled.wrappedBuffer(Bytes.toBytes(rangeProps.getRangeId()));
long amount = 100L;
try (IncrementOption<ByteBuf> option = optionFactory.newIncrementOption().build()) {
table.increment(pkey, lkey, amount, option);
verify(tableRanges.get(rangeProps.getRangeId()), times(1)).increment(eq(pkey), eq(lkey), eq(amount), same(option));
}
}
// test delete
for (RangeProperties rangeProps : streamRanges1.getRanges().values()) {
ByteBuf pkey = Unpooled.wrappedBuffer(Bytes.toBytes(rangeProps.getRangeId()));
ByteBuf lkey = Unpooled.wrappedBuffer(Bytes.toBytes(rangeProps.getRangeId()));
try (DeleteOption<ByteBuf> option = optionFactory.newDeleteOption().build()) {
table.delete(pkey, lkey, option);
verify(tableRanges.get(rangeProps.getRangeId()), times(1)).delete(eq(pkey), eq(lkey), eq(option));
}
}
// test txn
for (RangeProperties rangeProps : streamRanges1.getRanges().values()) {
ByteBuf pkey = Unpooled.wrappedBuffer(Bytes.toBytes(rangeProps.getRangeId()));
Txn<ByteBuf, ByteBuf> txn = table.txn(pkey);
verify(tableRanges.get(rangeProps.getRangeId()), times(1)).txn(eq(pkey));
}
}
Aggregations