Search in sources :

Example 76 with BufferView

use of io.pravega.common.util.BufferView in project pravega by pravega.

the class BenchmarkTests method testSequentialOperations.

private SequentialResult testSequentialOperations(CacheStorage s) {
    val writeBuffer = new ByteArraySegment(new byte[ENTRY_SIZE]);
    val appendBuffer = new ByteArraySegment(writeBuffer.array(), 0, s.getAppendableLength(ENTRY_SIZE));
    val readBuffer = new byte[ENTRY_SIZE];
    this.random.nextBytes(writeBuffer.array());
    int[] ids = new int[ENTRY_COUNT];
    val insert = measure(() -> {
        for (int i = 0; i < ENTRY_COUNT; i++) {
            ids[i] = s.insert(writeBuffer);
        }
    });
    val append = measure(() -> {
        for (int i = 0; i < ENTRY_COUNT; i++) {
            s.append(ids[i], writeBuffer.getLength(), appendBuffer);
        }
    });
    val replace = measure(() -> {
        for (int i = 0; i < ENTRY_COUNT; i++) {
            ids[i] = s.replace(ids[i], writeBuffer);
        }
    });
    val get = measure(() -> {
        for (int i = 0; i < ENTRY_COUNT; i++) {
            BufferView result = s.get(ids[i]);
            result.copyTo(ByteBuffer.wrap(readBuffer));
        }
    });
    val delete = measure(() -> {
        for (int i = 0; i < ENTRY_COUNT; i++) {
            s.delete(ids[i]);
        }
    });
    return new SequentialResult(insert, replace, append, get, delete);
}
Also used : lombok.val(lombok.val) ByteArraySegment(io.pravega.common.util.ByteArraySegment) BufferView(io.pravega.common.util.BufferView)

Example 77 with BufferView

use of io.pravega.common.util.BufferView in project pravega by pravega.

the class DirectMemoryCacheTests method testInsertErrors.

/**
 * Tests the ability to roll back any modifications that may have been performed while inserting. The easiest one to
 * trigger is {@link CacheFullException}s.
 */
@Test
public void testInsertErrors() {
    final int cleanAfterInvocations = DirectMemoryCache.MAX_CLEANUP_ATTEMPTS;
    // 2-buffer cache.
    final int maxSize = 2 * LAYOUT.bufferSize();
    // Leave 3 blocks empty from first cache.
    final int firstWriteSize = LAYOUT.bufferSize() - 4 * LAYOUT.blockSize();
    // Second write would attempt to overfill the cache.
    final int secondWriteSize = maxSize - firstWriteSize;
    final BufferView toInsert = new ByteArraySegment(new byte[1]);
    @Cleanup val c = new TestCache(maxSize);
    val firstWrite = new byte[firstWriteSize];
    rnd.nextBytes(firstWrite);
    val address = c.insert(new ByteArraySegment(firstWrite));
    checkSnapshot(c, (long) firstWriteSize, (long) firstWriteSize + LAYOUT.blockSize(), (long) LAYOUT.blockSize(), (long) LAYOUT.bufferSize(), (long) maxSize);
    AssertExtensions.assertThrows("Expected CacheFullException when inserting entry that would overfill.", () -> c.insert(new ByteArraySegment(new byte[secondWriteSize])), ex -> ex instanceof CacheFullException);
    AssertExtensions.assertThrows("Expected CacheFullException when replacing entry with entry that would overfill.", () -> c.replace(address, new ByteArraySegment(new byte[secondWriteSize])), ex -> ex instanceof CacheFullException);
    // We should still have the same amount of bytes stored, but we should have allocated the second buffer.
    checkSnapshot(c, (long) firstWriteSize, firstWriteSize + LAYOUT.blockSize() * 2L, LAYOUT.blockSize() * 2L, (long) maxSize, (long) maxSize);
    // Verify our original write is still intact.
    checkData(c, address, firstWrite, 0, firstWrite.length);
}
Also used : lombok.val(lombok.val) ByteArraySegment(io.pravega.common.util.ByteArraySegment) BufferView(io.pravega.common.util.BufferView) Cleanup(lombok.Cleanup) Test(org.junit.Test)

Aggregations

BufferView (io.pravega.common.util.BufferView)77 ArrayList (java.util.ArrayList)49 lombok.val (lombok.val)49 ByteArraySegment (io.pravega.common.util.ByteArraySegment)44 Cleanup (lombok.Cleanup)42 Duration (java.time.Duration)39 Test (org.junit.Test)39 List (java.util.List)37 CompletableFuture (java.util.concurrent.CompletableFuture)34 AssertExtensions (io.pravega.test.common.AssertExtensions)29 HashMap (java.util.HashMap)29 Assert (org.junit.Assert)29 ThreadPooledTestSuite (io.pravega.test.common.ThreadPooledTestSuite)28 TimeUnit (java.util.concurrent.TimeUnit)28 AtomicReference (java.util.concurrent.atomic.AtomicReference)26 Collectors (java.util.stream.Collectors)26 AtomicLong (java.util.concurrent.atomic.AtomicLong)25 Exceptions (io.pravega.common.Exceptions)24 TableEntry (io.pravega.segmentstore.contracts.tables.TableEntry)24 Map (java.util.Map)22