Search in sources :

Example 1 with HashStreamRanges

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());
}
Also used : HashStreamRanges(org.apache.bookkeeper.clients.impl.internal.api.HashStreamRanges) RangeProperties(org.apache.bookkeeper.stream.proto.RangeProperties) GetActiveRangesResponse(org.apache.bookkeeper.stream.proto.storage.GetActiveRangesResponse) Test(org.junit.Test)

Example 2 with HashStreamRanges

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();
}
Also used : HashStreamRanges(org.apache.bookkeeper.clients.impl.internal.api.HashStreamRanges) RangeProperties(org.apache.bookkeeper.stream.proto.RangeProperties) Map(java.util.Map)

Example 3 with HashStreamRanges

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());
    }
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) HashStreamRanges(org.apache.bookkeeper.clients.impl.internal.api.HashStreamRanges) StorageContainerRequest(org.apache.bookkeeper.stream.proto.storage.StorageContainerRequest) StorageServerChannel(org.apache.bookkeeper.clients.impl.channel.StorageServerChannel) StatusRuntimeException(io.grpc.StatusRuntimeException) ExecutionException(java.util.concurrent.ExecutionException) MetaRangeServiceImplBase(org.apache.bookkeeper.stream.proto.storage.MetaRangeServiceGrpc.MetaRangeServiceImplBase) Test(org.junit.Test)

Example 4 with HashStreamRanges

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);
}
Also used : HashStreamRanges(org.apache.bookkeeper.clients.impl.internal.api.HashStreamRanges) Entry(java.util.Map.Entry) ByteBuf(io.netty.buffer.ByteBuf) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PTable(org.apache.bookkeeper.api.kv.PTable)

Example 5 with HashStreamRanges

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());
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) HashStreamRanges(org.apache.bookkeeper.clients.impl.internal.api.HashStreamRanges) StorageContainerRequest(org.apache.bookkeeper.stream.proto.storage.StorageContainerRequest) GetActiveRangesResponse(org.apache.bookkeeper.stream.proto.storage.GetActiveRangesResponse) StorageServerChannel(org.apache.bookkeeper.clients.impl.channel.StorageServerChannel) MetaRangeServiceImplBase(org.apache.bookkeeper.stream.proto.storage.MetaRangeServiceGrpc.MetaRangeServiceImplBase) StorageContainerResponse(org.apache.bookkeeper.stream.proto.storage.StorageContainerResponse) Test(org.junit.Test)

Aggregations

HashStreamRanges (org.apache.bookkeeper.clients.impl.internal.api.HashStreamRanges)7 Test (org.junit.Test)3 StreamObserver (io.grpc.stub.StreamObserver)2 Map (java.util.Map)2 StorageServerChannel (org.apache.bookkeeper.clients.impl.channel.StorageServerChannel)2 RangeProperties (org.apache.bookkeeper.stream.proto.RangeProperties)2 GetActiveRangesResponse (org.apache.bookkeeper.stream.proto.storage.GetActiveRangesResponse)2 MetaRangeServiceImplBase (org.apache.bookkeeper.stream.proto.storage.MetaRangeServiceGrpc.MetaRangeServiceImplBase)2 StorageContainerRequest (org.apache.bookkeeper.stream.proto.storage.StorageContainerRequest)2 StatusRuntimeException (io.grpc.StatusRuntimeException)1 ByteBuf (io.netty.buffer.ByteBuf)1 Entry (java.util.Map.Entry)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 ExecutionException (java.util.concurrent.ExecutionException)1 PTable (org.apache.bookkeeper.api.kv.PTable)1 StorageContainerResponse (org.apache.bookkeeper.stream.proto.storage.StorageContainerResponse)1