Search in sources :

Example 21 with Transaction

use of com.apple.foundationdb.Transaction in project fdb-record-layer by FoundationDB.

the class BunchedMapScanTest method testScanMulti.

private void testScanMulti(int limit, boolean reverse, List<List<Tuple>> keyLists, @Nonnull BiFunction<Transaction, byte[], BunchedMapMultiIterator<Tuple, Tuple, Long>> iteratorFunction) {
    try (Transaction tr = db.createTransaction()) {
        byte[] continuation = null;
        List<BunchedMapScanEntry<Tuple, Tuple, Long>> entryList = new ArrayList<>();
        BunchedMapScanEntry<Tuple, Tuple, Long> lastEntry = null;
        do {
            BunchedMapMultiIterator<Tuple, Tuple, Long> iterator = iteratorFunction.apply(tr, continuation);
            int returned = 0;
            while (iterator.hasNext()) {
                BunchedMapScanEntry<Tuple, Tuple, Long> toAdd = iterator.peek();
                assertEquals(toAdd, iterator.next());
                if (lastEntry != null) {
                    if (toAdd.getSubspaceTag().equals(lastEntry.getSubspaceTag())) {
                        assertEquals(reverse ? 1 : -1, Integer.signum(lastEntry.getKey().compareTo(toAdd.getKey())));
                    } else {
                        assertEquals(reverse ? 1 : -1, Integer.signum(lastEntry.getSubspaceTag().compareTo(toAdd.getSubspaceTag())));
                    }
                }
                entryList.add(toAdd);
                lastEntry = toAdd;
                returned++;
            }
            continuation = iterator.getContinuation();
            if (limit == ReadTransaction.ROW_LIMIT_UNLIMITED || returned < limit) {
                assertNull(continuation);
            } else {
                assertNotNull(continuation);
            }
        } while (continuation != null);
        if (reverse) {
            entryList = Lists.reverse(entryList);
        }
        Long tag = null;
        int pos = 0;
        int totalRead = 0;
        for (BunchedMapScanEntry<Tuple, Tuple, Long> entry : entryList) {
            if (tag == null || !tag.equals(entry.getSubspaceTag())) {
                if (tag != null) {
                    assertEquals(tag + 1, entry.getSubspaceTag().longValue());
                }
                tag = entry.getSubspaceTag();
                pos = 0;
            }
            assertEquals(keyLists.get(tag.intValue()).get(pos), entry.getKey());
            assertEquals(value, entry.getValue());
            assertEquals(subSubspaces.get(tag.intValue()), entry.getSubspace());
            pos++;
            totalRead++;
        }
        int totalToSee = keyLists.stream().mapToInt(List::size).sum();
        assertEquals(totalToSee, totalRead);
    }
}
Also used : Transaction(com.apple.foundationdb.Transaction) ReadTransaction(com.apple.foundationdb.ReadTransaction) ArrayList(java.util.ArrayList) Tuple(com.apple.foundationdb.tuple.Tuple)

Example 22 with Transaction

use of com.apple.foundationdb.Transaction in project fdb-record-layer by FoundationDB.

the class BunchedMapScanTest method continuationWithDeletes.

private void continuationWithDeletes(int limit, boolean reverse) {
    try (Transaction tr = db.createTransaction()) {
        byte[] continuation = null;
        List<Tuple> readKeys = new ArrayList<>();
        do {
            List<Tuple> mostRecentReadKeys = new ArrayList<>();
            int returned = 0;
            BunchedMapIterator<Tuple, Tuple> bunchedMapIterator = map.scan(tr, subSubspaces.get(1), continuation, limit, reverse);
            while (bunchedMapIterator.hasNext()) {
                Tuple toAdd = bunchedMapIterator.peek().getKey();
                assertEquals(toAdd, bunchedMapIterator.next().getKey());
                mostRecentReadKeys.add(toAdd);
                returned += 1;
            }
            assertFalse(bunchedMapIterator.hasNext());
            assertThrows(NoSuchElementException.class, bunchedMapIterator::peek);
            assertThrows(NoSuchElementException.class, bunchedMapIterator::next);
            continuation = bunchedMapIterator.getContinuation();
            if (returned != limit) {
                assertNull(continuation);
            } else {
                assertNotNull(continuation);
            }
            // Remove all of the keys that were most recently read.
            mostRecentReadKeys.forEach(k -> map.remove(tr, subSubspaces.get(1), k).join());
            readKeys.addAll(mostRecentReadKeys);
        } while (continuation != null);
        if (reverse) {
            readKeys = Lists.reverse(readKeys);
        }
        List<Tuple> expectedKeys = IntStream.range(0, keys.size()).filter(i -> i % subSubspaces.size() == 1).mapToObj(keys::get).collect(Collectors.toList());
        assertEquals(expectedKeys, readKeys);
        tr.cancel();
    }
}
Also used : Transaction(com.apple.foundationdb.Transaction) ReadTransaction(com.apple.foundationdb.ReadTransaction) ArrayList(java.util.ArrayList) Tuple(com.apple.foundationdb.tuple.Tuple)

Example 23 with Transaction

use of com.apple.foundationdb.Transaction in project fdb-record-layer by FoundationDB.

the class BunchedMapScanTest method getKeysContinuation.

@Test
public void getKeysContinuation() throws InterruptedException, ExecutionException {
    clearAndPopulate();
    try (Transaction tr = db.createTransaction()) {
        getKeysContinuation(tr, false);
        getKeysContinuation(tr, true);
    }
}
Also used : Transaction(com.apple.foundationdb.Transaction) ReadTransaction(com.apple.foundationdb.ReadTransaction) Test(org.junit.jupiter.api.Test)

Example 24 with Transaction

use of com.apple.foundationdb.Transaction in project fdb-record-layer by FoundationDB.

the class BunchedMapScanTest method scanWithConflict.

@Test
public void scanWithConflict() throws InterruptedException, ExecutionException {
    clearAndPopulate();
    try (Transaction tr1 = db.createTransaction();
        Transaction tr2 = db.createTransaction()) {
        CompletableFuture.allOf(tr1.getReadVersion(), tr2.getReadVersion()).get();
        BunchedMapIterator<Tuple, Tuple> iterator = map.scan(tr1, bmSubspace);
        int count = MoreAsyncUtil.reduce(iterator, 0, (oldCount, item) -> oldCount + 1).get();
        assertEquals(keys.size(), count);
        tr1.addWriteConflictKey(Tuple.from(count).pack());
        assertFalse(map.put(tr2, bmSubspace, Tuple.from(keys.get(keys.size() - 1).getLong(0) + 1), value).get().isPresent());
        tr2.commit().get();
        CompletionException e = assertThrows(CompletionException.class, () -> tr1.commit().join());
        assertNotNull(e.getCause());
        assertTrue(e.getCause() instanceof FDBException);
        FDBException fdbE = (FDBException) e.getCause();
        assertEquals(FDBError.NOT_COMMITTED.code(), fdbE.getCode());
    }
    byte[] continuation = null;
    for (int i = 0; i < keys.size(); i++) {
    }
    try (Transaction tr1 = db.createTransaction();
        Transaction tr2 = db.createTransaction()) {
        CompletableFuture.allOf(tr1.getReadVersion(), tr2.getReadVersion()).get();
        BunchedMapIterator<Tuple, Tuple> iterator = map.scan(tr1, bmSubspace);
    }
}
Also used : IntStream(java.util.stream.IntStream) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) FDB(com.apple.foundationdb.FDB) Arrays(java.util.Arrays) FDBTestBase(com.apple.foundationdb.FDBTestBase) BiFunction(java.util.function.BiFunction) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) CompletableFuture(java.util.concurrent.CompletableFuture) Database(com.apple.foundationdb.Database) AsyncUtil(com.apple.foundationdb.async.AsyncUtil) Supplier(java.util.function.Supplier) Subspace(com.apple.foundationdb.subspace.Subspace) ArrayList(java.util.ArrayList) AfterAll(org.junit.jupiter.api.AfterAll) Transaction(com.apple.foundationdb.Transaction) Tuple(com.apple.foundationdb.tuple.Tuple) Lists(com.google.common.collect.Lists) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) FDBError(com.apple.foundationdb.FDBError) BeforeAll(org.junit.jupiter.api.BeforeAll) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Tag(org.junit.jupiter.api.Tag) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) NoSuchElementException(java.util.NoSuchElementException) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) ByteArrayUtil2(com.apple.foundationdb.tuple.ByteArrayUtil2) MoreAsyncUtil(com.apple.foundationdb.async.MoreAsyncUtil) ByteArrayUtil(com.apple.foundationdb.tuple.ByteArrayUtil) LongStream(java.util.stream.LongStream) Tags(com.apple.test.Tags) DirectoryLayer(com.apple.foundationdb.directory.DirectoryLayer) CompletionException(java.util.concurrent.CompletionException) Collectors(java.util.stream.Collectors) ReadTransaction(com.apple.foundationdb.ReadTransaction) Test(org.junit.jupiter.api.Test) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) PathUtil(com.apple.foundationdb.directory.PathUtil) Stream(java.util.stream.Stream) FDBException(com.apple.foundationdb.FDBException) TupleHelpers(com.apple.foundationdb.tuple.TupleHelpers) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Comparator(java.util.Comparator) Collections(java.util.Collections) AsyncPeekIterator(com.apple.foundationdb.async.AsyncPeekIterator) Transaction(com.apple.foundationdb.Transaction) ReadTransaction(com.apple.foundationdb.ReadTransaction) CompletionException(java.util.concurrent.CompletionException) FDBException(com.apple.foundationdb.FDBException) Tuple(com.apple.foundationdb.tuple.Tuple) Test(org.junit.jupiter.api.Test)

Example 25 with Transaction

use of com.apple.foundationdb.Transaction in project fdb-record-layer by FoundationDB.

the class BunchedMapTest method inconsistentScan.

private static List<KeyValue> inconsistentScan(@Nonnull Database db, @Nonnull Subspace subspace) {
    // Note that tr is mutated in the block, hence not using try-with-resources
    Transaction tr = db.createTransaction();
    try {
        KeySelector begin = KeySelector.firstGreaterOrEqual(subspace.range().begin);
        KeySelector end = KeySelector.firstGreaterOrEqual(subspace.range().end);
        KeyValue lastSeen = null;
        AsyncIterator<KeyValue> rangeIterator = tr.getRange(begin, end).iterator();
        List<KeyValue> rangeKVs = new ArrayList<>();
        boolean done = false;
        while (!done) {
            // Might loop if there are timeouts encountered within loop.
            try {
                while (rangeIterator.hasNext()) {
                    KeyValue next = rangeIterator.next();
                    rangeKVs.add(next);
                    lastSeen = next;
                }
                done = true;
            } catch (RuntimeException e) {
                FDBException fdbE = unwrapException(e);
                if (fdbE == null || fdbE.getCode() != FDBError.TRANSACTION_TOO_OLD.code()) {
                    throw e;
                } else {
                    // Timed out. Restart transaction and keep going.
                    tr.close();
                    tr = db.createTransaction();
                    if (lastSeen != null) {
                        // Update begin if we have any results.
                        begin = KeySelector.firstGreaterThan(lastSeen.getKey());
                        lastSeen = null;
                    }
                    rangeIterator = tr.getRange(begin, end).iterator();
                }
            }
        }
        return rangeKVs;
    } finally {
        tr.close();
    }
}
Also used : KeyValue(com.apple.foundationdb.KeyValue) Transaction(com.apple.foundationdb.Transaction) ArrayList(java.util.ArrayList) FDBException(com.apple.foundationdb.FDBException) KeySelector(com.apple.foundationdb.KeySelector)

Aggregations

Transaction (com.apple.foundationdb.Transaction)84 ReadTransaction (com.apple.foundationdb.ReadTransaction)34 Tuple (com.apple.foundationdb.tuple.Tuple)34 Test (org.junit.jupiter.api.Test)33 Nonnull (javax.annotation.Nonnull)28 ArrayList (java.util.ArrayList)26 List (java.util.List)26 CompletableFuture (java.util.concurrent.CompletableFuture)26 AsyncUtil (com.apple.foundationdb.async.AsyncUtil)22 Subspace (com.apple.foundationdb.subspace.Subspace)21 Collectors (java.util.stream.Collectors)19 Nullable (javax.annotation.Nullable)19 KeyValue (com.apple.foundationdb.KeyValue)18 Map (java.util.Map)18 KeyValueLogMessage (com.apple.foundationdb.record.logging.KeyValueLogMessage)17 Range (com.apple.foundationdb.Range)16 RecordCoreException (com.apple.foundationdb.record.RecordCoreException)16 AtomicReference (java.util.concurrent.atomic.AtomicReference)16 Collections (java.util.Collections)15 RecordCursor (com.apple.foundationdb.record.RecordCursor)14