Search in sources :

Example 1 with RangeProperties

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());
}
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 RangeProperties

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);
}
Also used : RangeProperties(org.apache.bookkeeper.stream.proto.RangeProperties)

Example 3 with RangeProperties

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);
}
Also used : RangeProperties(org.apache.bookkeeper.stream.proto.RangeProperties) RelatedRanges(org.apache.bookkeeper.stream.proto.storage.RelatedRanges)

Example 4 with RangeProperties

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

Example 5 with RangeProperties

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));
    }
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) HashRouter(org.apache.bookkeeper.common.router.HashRouter) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) MetaRangeClient(org.apache.bookkeeper.clients.impl.internal.api.MetaRangeClient) RangeKeyType(org.apache.bookkeeper.stream.proto.RangeKeyType) ClientException(org.apache.bookkeeper.clients.exceptions.ClientException) OptionFactoryImpl(org.apache.bookkeeper.api.kv.impl.options.OptionFactoryImpl) Unpooled(io.netty.buffer.Unpooled) ConcurrentMap(java.util.concurrent.ConcurrentMap) RangeRouter(org.apache.bookkeeper.clients.impl.routing.RangeRouter) Lists(com.google.common.collect.Lists) ByteBuf(io.netty.buffer.ByteBuf) RangeProperties(org.apache.bookkeeper.stream.proto.RangeProperties) TestName(org.junit.rules.TestName) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Assert.fail(org.junit.Assert.fail) HashStreamRanges(org.apache.bookkeeper.clients.impl.internal.api.HashStreamRanges) DEFAULT_STREAM_CONF(org.apache.bookkeeper.stream.protocol.ProtocolConstants.DEFAULT_STREAM_CONF) Before(org.junit.Before) OrderedScheduler(org.apache.bookkeeper.common.util.OrderedScheduler) Txn(org.apache.bookkeeper.api.kv.Txn) StorageServerClientManager(org.apache.bookkeeper.clients.impl.internal.api.StorageServerClientManager) Assert.assertTrue(org.junit.Assert.assertTrue) DeleteOption(org.apache.bookkeeper.api.kv.options.DeleteOption) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Bytes(org.apache.bookkeeper.common.util.Bytes) NavigableMap(java.util.NavigableMap) PTable(org.apache.bookkeeper.api.kv.PTable) FutureUtils(org.apache.bookkeeper.common.concurrent.FutureUtils) Maps(com.google.common.collect.Maps) OptionFactory(org.apache.bookkeeper.api.kv.options.OptionFactory) Mockito.verify(org.mockito.Mockito.verify) RangeOption(org.apache.bookkeeper.api.kv.options.RangeOption) ByteBufUtil(io.netty.buffer.ByteBufUtil) List(java.util.List) Rule(org.junit.Rule) ProtoUtils(org.apache.bookkeeper.stream.protocol.util.ProtoUtils) StreamProperties(org.apache.bookkeeper.stream.proto.StreamProperties) Optional(java.util.Optional) PutOption(org.apache.bookkeeper.api.kv.options.PutOption) IncrementOption(org.apache.bookkeeper.api.kv.options.IncrementOption) Assert.assertEquals(org.junit.Assert.assertEquals) ArgumentMatchers.same(org.mockito.ArgumentMatchers.same) Mockito.mock(org.mockito.Mockito.mock) HashStreamRanges(org.apache.bookkeeper.clients.impl.internal.api.HashStreamRanges) RangeProperties(org.apache.bookkeeper.stream.proto.RangeProperties) ByteBuf(io.netty.buffer.ByteBuf) PTable(org.apache.bookkeeper.api.kv.PTable) Test(org.junit.Test)

Aggregations

RangeProperties (org.apache.bookkeeper.stream.proto.RangeProperties)12 Test (org.junit.Test)6 HashStreamRanges (org.apache.bookkeeper.clients.impl.internal.api.HashStreamRanges)3 Lists (com.google.common.collect.Lists)2 Maps (com.google.common.collect.Maps)2 List (java.util.List)2 NavigableMap (java.util.NavigableMap)2 FutureUtils (org.apache.bookkeeper.common.concurrent.FutureUtils)2 Bytes (org.apache.bookkeeper.common.util.Bytes)2 StreamProperties (org.apache.bookkeeper.stream.proto.StreamProperties)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 ImmutableList (com.google.common.collect.ImmutableList)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 ByteBuf (io.netty.buffer.ByteBuf)1 ByteBufUtil (io.netty.buffer.ByteBufUtil)1 Unpooled (io.netty.buffer.Unpooled)1 Map (java.util.Map)1 Optional (java.util.Optional)1