Search in sources :

Example 1 with ScanLimitReachedException

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

the class FDBRecordStoreScanLimitTest method assertNumberOfRecordsScanned.

private void assertNumberOfRecordsScanned(int expected, Function<byte[], RecordCursor<FDBQueriedRecord<Message>>> cursorFunction, boolean failOnLimitReached, String message) throws Exception {
    try (FDBRecordContext context = openContext()) {
        openSimpleRecordStore(context);
        if (context.getTimer() != null) {
            context.getTimer().reset();
        }
        try (RecordCursor<FDBQueriedRecord<Message>> cursor = cursorFunction.apply(null)) {
            boolean caughtScanLimitReached = false;
            RecordCursorResult<FDBQueriedRecord<Message>> result = null;
            try {
                do {
                    result = cursor.getNext();
                } while (result.hasNext());
            } catch (RecordCoreException ex) {
                if (failOnLimitReached && ex.getCause() instanceof ScanLimitReachedException) {
                    caughtScanLimitReached = true;
                } else {
                    throw ex;
                }
            }
            if (failOnLimitReached && !caughtScanLimitReached) {
                assertNotEquals(RecordCursor.NoNextReason.SCAN_LIMIT_REACHED, result.getNoNextReason());
            }
            Optional<Integer> scanned = getRecordScanned(context);
            if (context.getTimer() != null) {
                context.getTimer().reset();
            }
            int overrun = BaseCursorCountVisitor.getCount(cursor);
            scanned.ifPresent(value -> assertThat(message, value, lessThanOrEqualTo(expected + overrun)));
        }
    }
}
Also used : RecordCoreException(com.apple.foundationdb.record.RecordCoreException) FDBQueriedRecord(com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) ScanLimitReachedException(com.apple.foundationdb.record.ScanLimitReachedException)

Example 2 with ScanLimitReachedException

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

the class CursorLimitManagerTest method testRecordScanLimiterBase.

public void testRecordScanLimiterBase(boolean failOnLimitReached) {
    final int numberOfScans = 12;
    final RecordScanLimiter recordScanLimiter = RecordScanLimiterFactory.enforce(numberOfScans);
    final CursorLimitManager manager = new CursorLimitManager(recordScanLimiter, failOnLimitReached, null, null);
    for (int i = 0; i < numberOfScans; i++) {
        assertTrue(manager.tryRecordScan());
        assertFalse(manager.isStopped());
        assertFalse(manager.getStoppedReason().isPresent());
    }
    if (failOnLimitReached) {
        try {
            TestHelpers.assertThrows("limit on number of key-values scanned per transaction reached", ScanLimitReachedException.class, manager::tryRecordScan, "no_next_reason", RecordCursor.NoNextReason.SCAN_LIMIT_REACHED.toString());
        } catch (Exception e) {
            fail("Unexpected ScanLimitReachedException.");
        }
    } else {
        assertFalse(manager.tryRecordScan());
        assertTrue(manager.isStopped());
        assertEquals(RecordCursor.NoNextReason.SCAN_LIMIT_REACHED, manager.getStoppedReason().get());
    }
}
Also used : RecordScanLimiter(com.apple.foundationdb.record.RecordScanLimiter) ScanLimitReachedException(com.apple.foundationdb.record.ScanLimitReachedException)

Aggregations

ScanLimitReachedException (com.apple.foundationdb.record.ScanLimitReachedException)2 RecordCoreException (com.apple.foundationdb.record.RecordCoreException)1 RecordScanLimiter (com.apple.foundationdb.record.RecordScanLimiter)1 FDBQueriedRecord (com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord)1 FDBRecordContext (com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext)1