use of io.pravega.client.tables.TableModification in project pravega by pravega.
the class KeyValueTableTestBase method testLargeBatchUpdates.
/**
* Verifies that overflowing a single {@link TableSegment} limits are rejected. No batch updates, removals or retrievals
* may exceed the {@link TableSegment#MAXIMUM_BATCH_KEY_COUNT} or {@link TableSegment#MAXIMUM_BATCH_LENGTH} limits.
*/
@Test
public void testLargeBatchUpdates() {
val rnd = new Random(0);
@Cleanup val kvt = createKeyValueTable();
// Exceeding by batch count.
val keys = IntStream.range(0, TableSegment.MAXIMUM_BATCH_KEY_COUNT + 1).mapToObj(i -> new TableKey(ByteBuffer.allocate(getPrimaryKeyLength()).putLong(i), ByteBuffer.allocate(getSecondaryKeyLength()).putInt(i))).collect(Collectors.toList());
val getBatchCountExceeded = keys.stream().map(k -> new TableKey(k.getPrimaryKey().duplicate(), k.getSecondaryKey().duplicate())).collect(Collectors.toList());
AssertExtensions.assertSuppliedFutureThrows("Get batch exceeded max count.", () -> kvt.getAll(getBatchCountExceeded), ex -> ex instanceof IllegalArgumentException);
List<TableModification> putBatchCountExceeded = keys.stream().map(a -> new Put(new TableKey(a.getPrimaryKey().duplicate(), a.getSecondaryKey().duplicate()), a.getSecondaryKey().duplicate())).collect(Collectors.toList());
AssertExtensions.assertSuppliedFutureThrows("Update batch exceeded max count.", () -> kvt.update(putBatchCountExceeded), ex -> ex instanceof IllegalArgumentException);
List<TableModification> removeBatchCountExceeded = keys.stream().map(k -> new Remove(new TableKey(k.getPrimaryKey().duplicate(), k.getSecondaryKey().duplicate()))).collect(Collectors.toList());
AssertExtensions.assertSuppliedFutureThrows("Remove batch exceeded max count.", () -> kvt.update(removeBatchCountExceeded), ex -> ex instanceof IllegalArgumentException);
// Exceed by serialization size.
// It is impossible to exceed the serialization size for retrievals or removals (due to the max key constraint),
// so the only request we can verify is the update one.
val limitValue = new byte[KeyValueTable.MAXIMUM_VALUE_LENGTH];
rnd.nextBytes(limitValue);
val putBatchSizeExceeded = new ArrayList<TableModification>();
int estimatedSize = 0;
while (estimatedSize < TableSegment.MAXIMUM_BATCH_LENGTH) {
val pk = new byte[getPrimaryKeyLength()];
val sk = new byte[getSecondaryKeyLength()];
rnd.nextBytes(pk);
rnd.nextBytes(sk);
val e = new Put(new TableKey(ByteBuffer.wrap(pk), ByteBuffer.wrap(sk)), ByteBuffer.wrap(limitValue));
putBatchSizeExceeded.add(e);
estimatedSize += pk.length + sk.length + limitValue.length;
}
AssertExtensions.assertSuppliedFutureThrows("Put batch exceeded max size.", () -> kvt.update(putBatchSizeExceeded), ex -> ex instanceof IllegalArgumentException);
}
use of io.pravega.client.tables.TableModification in project pravega by pravega.
the class KeyValueTableTestBase method testMultiKeyOperations.
/**
* Tests the ability to perform multi-key updates, replacements and removals. These methods should be exercised:
* - {@link KeyValueTable#update(Iterable)} with {@link Insert} and {@link Put} instances.
* - {@link KeyValueTable#update(Iterable)} with {@link Remove}.
* - {@link KeyValueTable#getAll}
*/
@Test
public void testMultiKeyOperations() {
val versions = new Versions();
@Cleanup val kvt = createKeyValueTable();
// Conditional Insert.
val iteration = new AtomicInteger(0);
forEveryPrimaryKey((pk, secondaryKeys) -> {
List<TableModification> inserts = secondaryKeys.stream().map(sk -> new Insert(new TableKey(pk, sk), getValue(pk, sk, iteration.get()))).collect(Collectors.toList());
val keyVersions = kvt.update(inserts).join();
val hint = getUniqueKeyId(pk);
Assert.assertEquals("Unexpected result size" + hint, inserts.size(), keyVersions.size());
for (int i = 0; i < inserts.size(); i++) {
versions.add(getUniqueKeyId(pk, inserts.get(i).getKey().getSecondaryKey()), keyVersions.get(i));
}
});
checkValues(iteration.get(), versions, kvt);
// Unconditional update.
iteration.incrementAndGet();
forEveryPrimaryKey((pk, secondaryKeys) -> {
List<TableModification> puts = secondaryKeys.stream().map(sk -> new Put(new TableKey(pk, sk), getValue(pk, sk, iteration.get()))).collect(Collectors.toList());
val keyVersions = kvt.update(puts).join();
val hint = getUniqueKeyId(pk);
Assert.assertEquals("Unexpected result size" + hint, puts.size(), keyVersions.size());
for (int i = 0; i < puts.size(); i++) {
versions.add(getUniqueKeyId(pk, puts.get(i).getKey().getSecondaryKey()), keyVersions.get(i));
}
});
checkValues(iteration.get(), versions, kvt);
// Conditional replace.
iteration.incrementAndGet();
forEveryPrimaryKey((pk, secondaryKeys) -> {
// Failed update (bad version).
val pkValue = Math.abs(PK_SERIALIZER.deserialize(pk));
List<TableModification> badPuts = secondaryKeys.stream().map(sk -> new Put(new TableKey(pk, sk), getValue(pk, sk, iteration.get()), alterVersion(versions.get(getUniqueKeyId(pk, sk)), pkValue % 2 == 0, pkValue % 2 == 1))).collect(Collectors.toList());
AssertExtensions.assertSuppliedFutureThrows("update(Put) did not throw for bad version.", () -> kvt.update(badPuts), ex -> ex instanceof BadKeyVersionException);
// Correct update.
List<TableModification> puts = secondaryKeys.stream().map(sk -> new Put(new TableKey(pk, sk), getValue(pk, sk, iteration.get()), versions.get(getUniqueKeyId(pk, sk)))).collect(Collectors.toList());
val keyVersions = kvt.update(puts).join();
val hint = getUniqueKeyId(pk);
Assert.assertEquals("Unexpected result size" + hint, puts.size(), keyVersions.size());
for (int i = 0; i < puts.size(); i++) {
versions.add(getUniqueKeyId(pk, puts.get(i).getKey().getSecondaryKey()), keyVersions.get(i));
}
});
checkValues(iteration.get(), versions, kvt);
// Conditional removal.
iteration.incrementAndGet();
forEveryPrimaryKey((pk, secondaryKeys) -> {
val hint = getUniqueKeyId(pk);
// Failed update (bad version).
val pkValue = Math.abs(PK_SERIALIZER.deserialize(pk));
List<TableModification> badRemovals = secondaryKeys.stream().map(sk -> new Remove(new TableKey(pk, sk), alterVersion(versions.get(getUniqueKeyId(pk, sk)), pkValue % 2 == 0, pkValue % 2 == 1))).collect(Collectors.toList());
AssertExtensions.assertSuppliedFutureThrows("update(Remove) did not throw for bad version." + hint, () -> kvt.update(badRemovals), ex -> ex instanceof BadKeyVersionException);
// Correct update.
List<TableModification> removals = secondaryKeys.stream().map(sk -> new Remove(new TableKey(pk, sk), versions.get(getUniqueKeyId(pk, sk)))).collect(Collectors.toList());
kvt.update(removals).join();
for (val sk : secondaryKeys) {
versions.remove(getUniqueKeyId(pk, sk));
}
});
Assert.assertTrue("Expected all keys to have been removed.", versions.isEmpty());
checkValues(iteration.get(), versions, kvt);
// Reinsert (conditionally)
iteration.incrementAndGet();
forEveryPrimaryKey((pk, secondaryKeys) -> {
val hint = getUniqueKeyId(pk);
List<TableModification> entries = secondaryKeys.stream().map(sk -> new Insert(new TableKey(pk, sk), getValue(pk, sk, iteration.get()))).collect(Collectors.toList());
val keyVersions = kvt.update(entries).join();
Assert.assertEquals("Unexpected result size" + hint, entries.size(), keyVersions.size());
for (int i = 0; i < entries.size(); i++) {
versions.add(getUniqueKeyId(pk, entries.get(i).getKey().getSecondaryKey()), keyVersions.get(i));
}
});
checkValues(iteration.get(), versions, kvt);
}
use of io.pravega.client.tables.TableModification in project pravega by pravega.
the class KeyValueTableTestBase method testIterators.
@Test
public void testIterators() {
@Cleanup val kvt = createKeyValueTable();
val iteration = new AtomicInteger(0);
// Populate everything.
val versions = new Versions();
forEveryPrimaryKey((pk, secondaryKeys) -> {
List<TableModification> inserts = secondaryKeys.stream().map(sk -> new Insert(new TableKey(pk, sk), getValue(pk, sk, iteration.get()))).collect(Collectors.toList());
val keyVersions = kvt.update(inserts).join();
for (int i = 0; i < inserts.size(); i++) {
versions.add(getUniqueKeyId(pk, inserts.get(i).getKey().getSecondaryKey()), keyVersions.get(i));
}
});
// Check the Primary Key iterator.
checkPrimaryKeyIterator(kvt, versions, iteration.get());
// Check the Global iterators.
checkGlobalIterator(kvt, versions, iteration.get());
}
Aggregations