Search in sources :

Example 1 with IteratorItem

use of io.pravega.client.tables.IteratorItem in project pravega by pravega.

the class TableSegmentIteratorTests method testIterator.

@Test
public void testIterator() {
    val items = new TreeMap<ByteBuf, ByteBuf>();
    for (int i = 0; i < 256; i++) {
        val value = Unpooled.wrappedBuffer(new byte[] { (byte) i });
        items.put(value, value);
    }
    val initialArgs = new SegmentIteratorArgs(items.firstKey(), items.lastKey(), 2);
    val tsi = new TableSegmentIterator<>(args -> CompletableFuture.completedFuture(new IteratorItem<>(items.subMap(args.getFromKey(), true, args.getToKey(), true).keySet().stream().limit(args.getMaxItemsAtOnce()).collect(Collectors.toList()))), r -> r, initialArgs);
    val result = new ArrayList<ByteBuf>();
    tsi.collectRemaining(i -> {
        Assert.assertEquals(initialArgs.getMaxItemsAtOnce(), i.getItems().size());
        result.addAll(i.getItems());
        return true;
    }).join();
    val expectedResult = new ArrayList<>(items.keySet());
    AssertExtensions.assertListEquals("Unexpected result.", expectedResult, result, ByteBuf::equals);
    Assert.assertNull("Not expecting any more items.", tsi.getNext().join());
}
Also used : lombok.val(lombok.val) ByteBuf(io.netty.buffer.ByteBuf) TreeMap(java.util.TreeMap) AssertExtensions(io.pravega.test.common.AssertExtensions) lombok.val(lombok.val) IteratorItem(io.pravega.client.tables.IteratorItem) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.Test) Assert(org.junit.Assert) Collectors(java.util.stream.Collectors) Unpooled(io.netty.buffer.Unpooled) ArrayList(java.util.ArrayList) IteratorItem(io.pravega.client.tables.IteratorItem) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 2 with IteratorItem

use of io.pravega.client.tables.IteratorItem in project pravega by pravega.

the class TableSegmentIterator method getNext.

@Override
public CompletableFuture<IteratorItem<T>> getNext() {
    if (this.args.get() == null) {
        // We are done.
        return CompletableFuture.completedFuture(null);
    }
    CompletableFuture<IteratorItem<T>> result = this.fetchNext.apply(this.args.get()).thenApply(r -> {
        if (r == null) {
            // We are done.
            this.args.set(null);
            return null;
        }
        ByteBuf lastKey = r.getItems().isEmpty() ? null : this.getKey.apply(r.getItems().get(r.getItems().size() - 1));
        this.args.set(this.args.get().next(lastKey));
        return r;
    });
    Futures.exceptionListener(result, ObjectClosedException.class, ex -> this.args.set(null));
    return result;
}
Also used : IteratorItem(io.pravega.client.tables.IteratorItem) ByteBuf(io.netty.buffer.ByteBuf)

Example 3 with IteratorItem

use of io.pravega.client.tables.IteratorItem in project pravega by pravega.

the class TableSegmentImplTest method testIterator.

private <T> void testIterator(Function<SegmentIteratorArgs, AsyncIterator<IteratorItem<T>>> newIterator, Supplier<ByteBuf> getLastRequestFromKey, Supplier<ByteBuf> getLastRequestToKey, Function<TableSegmentEntry, T> getItemFromEntry, Consumer<List<T>> sendReply, BiPredicate<T, T> checkItemEquality) throws Exception {
    val suggestedKeyCount = 3;
    // Generate 100 Entries and split them into batches.
    val allEntries = IntStream.range(0, 100).mapToObj(i -> versionedEntry(i * 10L, Integer.toString(i * 10), 1L)).collect(Collectors.toList());
    val inputEntries = splitIteratorInputs(allEntries);
    // Do an full iteration as well.
    inputEntries.add(allEntries);
    // Check regular iteration.
    for (int i = 0; i < inputEntries.size(); i++) {
        val entryList = inputEntries.get(i);
        SegmentIteratorArgs args = SegmentIteratorArgs.builder().fromKey(entryList.get(0).getKey().getKey()).toKey(entryList.get(entryList.size() - 1).getKey().getKey()).maxItemsAtOnce(suggestedKeyCount).build();
        // We collect iterated items in this list.
        val actualItems = new ArrayList<T>();
        val itemsToReturn = entryList.iterator();
        val tableIterator = newIterator.apply(args);
        while (itemsToReturn.hasNext()) {
            val iteratorFuture = tableIterator.getNext();
            // Verify the wire command got sent as expected.
            val requestFromKey = getLastRequestFromKey.get();
            Assert.assertEquals("Unexpected fromKey sent.", args.getFromKey(), requestFromKey);
            val requestToKey = getLastRequestToKey.get();
            Assert.assertEquals("Unexpected toKey sent.", args.getToKey(), requestToKey);
            // Send a reply.
            val expectedResult = new ArrayList<T>();
            int count = suggestedKeyCount;
            while (itemsToReturn.hasNext() && count > 0) {
                val next = itemsToReturn.next();
                expectedResult.add(getItemFromEntry.apply(next));
                args = args.next(next.getKey().getKey());
                count--;
            }
            sendReply.accept(expectedResult);
            // Check the partial result.
            val iteratorResult = iteratorFuture.get(SHORT_TIMEOUT, TimeUnit.MILLISECONDS);
            AssertExtensions.assertListEquals("Unexpected partial result.", expectedResult, iteratorResult.getItems(), checkItemEquality);
            actualItems.addAll(iteratorResult.getItems());
        }
        // Then the final result.
        val expected = entryList.stream().map(getItemFromEntry).collect(Collectors.toList());
        AssertExtensions.assertListEquals("Unexpected result.", expected, actualItems, checkItemEquality);
    }
}
Also used : lombok.val(lombok.val) Arrays(java.util.Arrays) NoSuchKeyException(io.pravega.client.tables.NoSuchKeyException) AssertExtensions(io.pravega.test.common.AssertExtensions) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Cleanup(lombok.Cleanup) Unpooled(io.netty.buffer.Unpooled) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.connection.impl.ClientConnection) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) IteratorItem(io.pravega.client.tables.IteratorItem) Request(io.pravega.shared.protocol.netty.Request) DelegationTokenProviderFactory(io.pravega.client.security.auth.DelegationTokenProviderFactory) Collectors(java.util.stream.Collectors) RetriesExhaustedException(io.pravega.common.util.RetriesExhaustedException) List(java.util.List) ThreadPooledTestSuite(io.pravega.test.common.ThreadPooledTestSuite) TestUtils(io.pravega.test.common.TestUtils) BadKeyVersionException(io.pravega.client.tables.BadKeyVersionException) IntStream(java.util.stream.IntStream) Segment(io.pravega.client.segment.impl.Segment) Setter(lombok.Setter) Getter(lombok.Getter) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException) Reply(io.pravega.shared.protocol.netty.Reply) Exceptions(io.pravega.common.Exceptions) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) Deque(java.util.Deque) Append(io.pravega.shared.protocol.netty.Append) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Iterators(com.google.common.collect.Iterators) ArrayList(java.util.ArrayList) BiPredicate(java.util.function.BiPredicate) Lists(com.google.common.collect.Lists) ByteBuf(io.netty.buffer.ByteBuf) Charsets(com.google.common.base.Charsets) LongStream(java.util.stream.LongStream) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) lombok.val(lombok.val) AsyncIterator(io.pravega.common.util.AsyncIterator) Test(org.junit.Test) AuthenticationException(io.pravega.auth.AuthenticationException) WireCommands(io.pravega.shared.protocol.netty.WireCommands) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) WireCommand(io.pravega.shared.protocol.netty.WireCommand) AbstractMap(java.util.AbstractMap) KeyValueTableClientConfiguration(io.pravega.client.tables.KeyValueTableClientConfiguration) ArrayDeque(java.util.ArrayDeque) Assert(org.junit.Assert) Collections(java.util.Collections) ArrayList(java.util.ArrayList)

Example 4 with IteratorItem

use of io.pravega.client.tables.IteratorItem in project pravega by pravega.

the class KeyValueTableIteratorImplTests method testIteratorSingleSegment.

/**
 * Tests the iterator when there is a single segment involved.
 */
@Test
public void testIteratorSingleSegment() {
    val pk = newBuffer(DEFAULT_CONFIG.getPrimaryKeyLength());
    val sk1 = newBuffer(DEFAULT_CONFIG.getSecondaryKeyLength());
    val sk2 = newBuffer(DEFAULT_CONFIG.getSecondaryKeyLength());
    val maxIterationSize = 10;
    val allEntries = IntStream.range(0, maxIterationSize).mapToObj(i -> new AbstractMap.SimpleImmutableEntry<>(newBuffer(TOTAL_KEY_LENGTH), newBuffer(5))).collect(Collectors.toList());
    val mockSegment = mock(TableSegment.class);
    when(mockSegment.keyIterator(any())).thenAnswer(arg -> {
        val iteratorArgs = (SegmentIteratorArgs) arg.getArgument(0);
        checkSegmentIteratorArgs(iteratorArgs, pk, sk1, sk2, maxIterationSize);
        val keys = allEntries.stream().map(e -> new TableSegmentKey(Unpooled.wrappedBuffer(e.getKey()), TableSegmentKeyVersion.NO_VERSION)).collect(Collectors.toList());
        return AsyncIterator.singleton(new IteratorItem<>(keys));
    });
    when(mockSegment.entryIterator(any())).thenAnswer(arg -> {
        val iteratorArgs = (SegmentIteratorArgs) arg.getArgument(0);
        checkSegmentIteratorArgs(iteratorArgs, pk, sk1, sk2, maxIterationSize);
        val entries = allEntries.stream().map(e -> new TableSegmentEntry(new TableSegmentKey(Unpooled.wrappedBuffer(e.getKey()), TableSegmentKeyVersion.NO_VERSION), Unpooled.wrappedBuffer(e.getValue()))).collect(Collectors.toList());
        return AsyncIterator.singleton(new IteratorItem<>(entries));
    });
    val selector = mock(SegmentSelector.class);
    when(selector.getKvt()).thenReturn(KVT);
    when(selector.getSegmentCount()).thenReturn(DEFAULT_CONFIG.getPartitionCount());
    val segmentRequests = new HashSet<ByteBuffer>();
    when(selector.getTableSegment(any())).thenAnswer(arg -> {
        val key = (ByteBuffer) arg.getArgument(0);
        segmentRequests.add(key.duplicate());
        return mockSegment;
    });
    val entryHelper = new TableEntryHelper(selector, DEFAULT_CONFIG);
    val b = new KeyValueTableIteratorImpl.Builder(DEFAULT_CONFIG, entryHelper, executorService()).maxIterationSize(maxIterationSize);
    // Everything up until here has been verified in the Builder unit tests (no need to do it again).
    // Create a Primary Key range iterator.
    val iterator = b.forPrimaryKey(pk, sk1, sk2);
    // Issue a Key iterator and then an Entry iterator, then collect all the results.
    val iteratorKeys = new ArrayList<TableKey>();
    iterator.keys().collectRemaining(ii -> iteratorKeys.addAll(ii.getItems())).join();
    val iteratorEntries = new ArrayList<TableEntry>();
    iterator.entries().collectRemaining(ii -> iteratorEntries.addAll(ii.getItems())).join();
    // Validate the results are as expected.
    Assert.assertEquals(allEntries.size(), iteratorKeys.size());
    Assert.assertEquals(allEntries.size(), iteratorEntries.size());
    for (int i = 0; i < allEntries.size(); i++) {
        val expected = allEntries.get(i);
        val actualKey = iteratorKeys.get(i);
        val actualEntry = iteratorEntries.get(i);
        Assert.assertEquals(Unpooled.wrappedBuffer(expected.getKey()), entryHelper.serializeKey(actualKey.getPrimaryKey(), actualKey.getSecondaryKey()));
        Assert.assertEquals(actualKey, actualEntry.getKey());
        Assert.assertEquals(expected.getValue(), actualEntry.getValue());
    }
    Assert.assertEquals("Only expecting 1 segment to be requested.", 1, segmentRequests.size());
}
Also used : lombok.val(lombok.val) IntStream(java.util.stream.IntStream) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Arrays(java.util.Arrays) AssertExtensions(io.pravega.test.common.AssertExtensions) Random(java.util.Random) CompletableFuture(java.util.concurrent.CompletableFuture) KeyValueTableConfiguration(io.pravega.client.tables.KeyValueTableConfiguration) Function(java.util.function.Function) ByteBuffer(java.nio.ByteBuffer) Unpooled(io.netty.buffer.Unpooled) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ByteBufferUtils(io.pravega.common.util.ByteBufferUtils) KeyValueTableInfo(io.pravega.client.admin.KeyValueTableInfo) LeakDetectorTestSuite(io.pravega.test.common.LeakDetectorTestSuite) TableEntry(io.pravega.client.tables.TableEntry) KeyValueTableIterator(io.pravega.client.tables.KeyValueTableIterator) lombok.val(lombok.val) IteratorItem(io.pravega.client.tables.IteratorItem) AsyncIterator(io.pravega.common.util.AsyncIterator) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) AbstractMap(java.util.AbstractMap) List(java.util.List) TableKey(io.pravega.client.tables.TableKey) Assert(org.junit.Assert) Mockito.mock(org.mockito.Mockito.mock) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with IteratorItem

use of io.pravega.client.tables.IteratorItem in project pravega by pravega.

the class KeyValueTableIteratorImplTests method testMergeAsyncIterator.

/**
 * Tests the {@link KeyValueTableIteratorImpl.MergeAsyncIterator} class.
 */
@Test
public void testMergeAsyncIterator() {
    val segmentCount = 5;
    val minItemsPerSegment = 11;
    val maxItemsPerSegment = 101;
    val iterationSize = 3;
    // Generate test data.
    val c = new KeyValueTableIteratorImpl.TableKeyComparator();
    // Sorted.
    val segmentIterators = new ArrayList<AsyncIterator<IteratorItem<TableKey>>>();
    // Sorted.
    val expectedData = new ArrayList<TableKey>();
    for (int i = 0; i < segmentCount; i++) {
        val count = minItemsPerSegment + random.nextInt(maxItemsPerSegment - minItemsPerSegment);
        val segmentBatches = new ArrayList<List<TableKey>>();
        // Generate segment contents.
        val segmentSortedContents = IntStream.range(0, count).mapToObj(x -> new TableKey(newBuffer(DEFAULT_CONFIG.getPrimaryKeyLength()), newBuffer(DEFAULT_CONFIG.getSecondaryKeyLength()))).sorted(c).collect(Collectors.toList());
        // Break it down into batches and create a "segment iterator" from them.
        int index = 0;
        while (index < count) {
            int batchCount = Math.min(iterationSize, count - index);
            segmentBatches.add(segmentSortedContents.subList(index, index + batchCount));
            index += batchCount;
        }
        segmentIterators.add(createAsyncIterator(segmentBatches));
        expectedData.addAll(segmentSortedContents);
    }
    expectedData.sort(c);
    // Create a merge iterator and collect its contents.
    val mergeIterator = new KeyValueTableIteratorImpl.MergeAsyncIterator<>(segmentIterators.iterator(), k -> k, iterationSize, executorService());
    val actualData = new ArrayList<TableKey>();
    mergeIterator.collectRemaining(ii -> {
        val expected = Math.min(iterationSize, expectedData.size() - actualData.size());
        Assert.assertEquals(expected, ii.getItems().size());
        return actualData.addAll(ii.getItems());
    });
    // Verify it returns the correct items.
    AssertExtensions.assertListEquals("", expectedData, actualData, TableKey::equals);
}
Also used : lombok.val(lombok.val) IteratorItem(io.pravega.client.tables.IteratorItem) ArrayList(java.util.ArrayList) TableKey(io.pravega.client.tables.TableKey) Test(org.junit.Test)

Aggregations

IteratorItem (io.pravega.client.tables.IteratorItem)6 ArrayList (java.util.ArrayList)5 lombok.val (lombok.val)5 Test (org.junit.Test)5 Unpooled (io.netty.buffer.Unpooled)4 AssertExtensions (io.pravega.test.common.AssertExtensions)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 Collectors (java.util.stream.Collectors)4 Assert (org.junit.Assert)4 ByteBuf (io.netty.buffer.ByteBuf)3 TableKey (io.pravega.client.tables.TableKey)3 AsyncIterator (io.pravega.common.util.AsyncIterator)3 AbstractMap (java.util.AbstractMap)3 Arrays (java.util.Arrays)3 List (java.util.List)3 Function (java.util.function.Function)3 IntStream (java.util.stream.IntStream)3 KeyValueTableInfo (io.pravega.client.admin.KeyValueTableInfo)2 KeyValueTableConfiguration (io.pravega.client.tables.KeyValueTableConfiguration)2 KeyValueTableIterator (io.pravega.client.tables.KeyValueTableIterator)2