use of org.apache.bookkeeper.clients.impl.internal.api.HashStreamRanges 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.clients.impl.internal.api.HashStreamRanges 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.clients.impl.internal.api.HashStreamRanges in project bookkeeper by apache.
the class TestMetaRangeClientImpl method testGetActiveStreamRangesFailure.
@Test
public void testGetActiveStreamRangesFailure() throws Exception {
CompletableFuture<StorageServerChannel> serviceFuture = FutureUtils.createFuture();
metaRangeClient.getStorageContainerClient().setStorageServerChannelFuture(serviceFuture);
MetaRangeServiceImplBase metaRangeService = new MetaRangeServiceImplBase() {
@Override
public void getActiveRanges(StorageContainerRequest request, StreamObserver<StorageContainerResponse> responseObserver) {
responseObserver.onError(new StatusRuntimeException(Status.INTERNAL));
}
};
serviceRegistry.addService(metaRangeService.bindService());
StorageServerChannel rsChannel = new StorageServerChannel(InProcessChannelBuilder.forName(serverName).directExecutor().build(), Optional.empty());
serviceFuture.complete(rsChannel);
CompletableFuture<HashStreamRanges> getFuture = metaRangeClient.getActiveDataRanges();
try {
getFuture.get();
fail("should fail on rpc failure");
} catch (ExecutionException ee) {
assertNotNull(ee.getCause());
assertTrue(ee.getCause() instanceof StatusRuntimeException);
StatusRuntimeException se = (StatusRuntimeException) ee.getCause();
assertEquals(Status.INTERNAL, se.getStatus());
}
}
use of org.apache.bookkeeper.clients.impl.internal.api.HashStreamRanges in project bookkeeper by apache.
the class PByteBufTableImpl method refreshRangeSpaces.
CompletableFuture<PTable<ByteBuf, ByteBuf>> refreshRangeSpaces(HashStreamRanges newRanges) {
// compare the ranges to see if it requires an update
HashStreamRanges oldRanges = rangeRouter.getRanges();
if (null != oldRanges && oldRanges.getMaxRangeId() >= newRanges.getMaxRangeId()) {
log.info("No new stream ranges found for stream {}.", streamName);
return FutureUtils.value(this);
}
if (log.isInfoEnabled()) {
log.info("Updated the active ranges to {}", newRanges);
}
rangeRouter.setRanges(newRanges);
// add new ranges
Set<Long> activeRanges = Sets.newHashSetWithExpectedSize(newRanges.getRanges().size());
newRanges.getRanges().forEach((rk, range) -> {
activeRanges.add(range.getRangeId());
if (tableRanges.containsKey(range.getRangeId())) {
return;
}
PTable<ByteBuf, ByteBuf> tableRange = trFactory.openTableRange(props, range, executor, opFactory, resultFactory, kvFactory);
if (log.isInfoEnabled()) {
log.info("Create table range client for range {}", range.getRangeId());
}
this.tableRanges.put(range.getRangeId(), tableRange);
});
// remove old ranges
Iterator<Entry<Long, PTable<ByteBuf, ByteBuf>>> rsIter = tableRanges.entrySet().iterator();
while (rsIter.hasNext()) {
Map.Entry<Long, PTable<ByteBuf, ByteBuf>> entry = rsIter.next();
Long rid = entry.getKey();
if (activeRanges.contains(rid)) {
continue;
}
rsIter.remove();
PTable oldRangeSpace = entry.getValue();
oldRangeSpace.close();
}
return FutureUtils.value(this);
}
use of org.apache.bookkeeper.clients.impl.internal.api.HashStreamRanges in project bookkeeper by apache.
the class TestMetaRangeClientImpl method testGetActiveStreamRanges.
@Test
public void testGetActiveStreamRanges() throws Exception {
CompletableFuture<StorageServerChannel> serviceFuture = FutureUtils.createFuture();
metaRangeClient.getStorageContainerClient().setStorageServerChannelFuture(serviceFuture);
// create response
GetActiveRangesResponse getActiveRangesResponse = GetActiveRangesResponse.newBuilder().addRanges(buildRelatedRange(Long.MIN_VALUE, 0L, 123L, 1L, Lists.newArrayList(113L))).addRanges(buildRelatedRange(0L, Long.MAX_VALUE, 124L, 2L, Lists.newArrayList(114L))).build();
StorageContainerResponse response = StorageContainerResponse.newBuilder().setCode(StatusCode.SUCCESS).setGetActiveRangesResp(getActiveRangesResponse).build();
MetaRangeServiceImplBase metaRangeService = new MetaRangeServiceImplBase() {
@Override
public void getActiveRanges(StorageContainerRequest request, StreamObserver<StorageContainerResponse> responseObserver) {
responseObserver.onNext(response);
responseObserver.onCompleted();
}
};
serviceRegistry.addService(metaRangeService.bindService());
StorageServerChannel rsChannel = new StorageServerChannel(InProcessChannelBuilder.forName(serverName).directExecutor().build(), Optional.empty());
serviceFuture.complete(rsChannel);
HashStreamRanges expectedStream = createActiveRanges(response.getGetActiveRangesResp());
CompletableFuture<HashStreamRanges> getFuture = metaRangeClient.getActiveDataRanges();
assertEquals(expectedStream, getFuture.get());
}
Aggregations