Search in sources :

Example 11 with CompositeByteArraySegment

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

the class WriteQueueTests method testAdd.

/**
 * Tests the basic functionality of the add() method.
 */
@Test
public void testAdd() {
    // Just over 1ms.
    final int timeIncrement = 1234 * 1000;
    AtomicLong time = new AtomicLong();
    val q = new WriteQueue(time::get);
    val initialStats = q.getStatistics();
    Assert.assertEquals("Unexpected getSize on empty queue.", 0, initialStats.getSize());
    Assert.assertEquals("Unexpected getAverageFillRate on empty queue.", 0, initialStats.getAverageItemFillRatio(), 0);
    Assert.assertEquals("Unexpected getExpectedProcessingTimeMillis on empty queue.", 0, initialStats.getExpectedProcessingTimeMillis());
    int expectedSize = 0;
    long firstItemTime = 0;
    val writeResults = new ArrayList<CompletableFuture<LogAddress>>();
    for (int i = 0; i < ITEM_COUNT; i++) {
        time.addAndGet(timeIncrement);
        if (i == 0) {
            firstItemTime = time.get();
        }
        int writeSize = i * 10000;
        val writeResult = new CompletableFuture<LogAddress>();
        q.add(new Write(new CompositeByteArraySegment(writeSize), new TestWriteLedger(i), writeResult));
        writeResults.add(writeResult);
        expectedSize += writeSize;
        q.removeFinishedWrites();
        val stats = q.getStatistics();
        val expectedFillRatio = (double) expectedSize / stats.getSize() / BookKeeperConfig.MAX_APPEND_LENGTH;
        val expectedProcTime = (time.get() - firstItemTime) / AbstractTimer.NANOS_TO_MILLIS;
        Assert.assertEquals("Unexpected getSize.", i + 1, stats.getSize());
        Assert.assertEquals("Unexpected getAverageFillRate.", expectedFillRatio, stats.getAverageItemFillRatio(), 0.01);
        Assert.assertEquals("Unexpected getExpectedProcessingTimeMillis.", expectedProcTime, stats.getExpectedProcessingTimeMillis());
    }
    // Now verify the stats are also updated when finishing writes.
    for (int i = 0; i < writeResults.size(); i++) {
        writeResults.get(i).complete(null);
        val cs = q.removeFinishedWrites();
        Assert.assertEquals("Unexpected number of removed items", 1, cs.getRemovedCount());
        Assert.assertEquals("Unexpected size after removing " + (i + 1), ITEM_COUNT - i - 1, q.getStatistics().getSize());
    }
}
Also used : lombok.val(lombok.val) CompositeByteArraySegment(io.pravega.common.util.CompositeByteArraySegment) AtomicLong(java.util.concurrent.atomic.AtomicLong) LogAddress(io.pravega.segmentstore.storage.LogAddress) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 12 with CompositeByteArraySegment

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

the class WriteQueueTests method testClose.

/**
 * Tests the close() method.
 */
@Test
public void testClose() {
    val q = new WriteQueue();
    val expectedWrites = new ArrayList<Write>();
    for (int i = 0; i < ITEM_COUNT; i++) {
        val w = new Write(new CompositeByteArraySegment(i), new TestWriteLedger(i), CompletableFuture.completedFuture(null));
        q.add(w);
        expectedWrites.add(w);
    }
    val removedWrites = q.close();
    AssertExtensions.assertListEquals("Unexpected writes removed.", expectedWrites, removedWrites, Object::equals);
    val clearStats = q.getStatistics();
    Assert.assertEquals("Unexpected getSize after clear.", 0, clearStats.getSize());
    Assert.assertEquals("Unexpected getAverageFillRate after clear.", 0, clearStats.getAverageItemFillRatio(), 0);
    Assert.assertEquals("Unexpected getExpectedProcessingTimeMillis after clear.", 0, clearStats.getExpectedProcessingTimeMillis());
    AssertExtensions.assertThrows("add() worked after close().", () -> q.add(new Write(new CompositeByteArraySegment(1), new TestWriteLedger(0), CompletableFuture.completedFuture(null))), ex -> ex instanceof ObjectClosedException);
    AssertExtensions.assertThrows("getWritesToExecute() worked after close().", () -> q.getWritesToExecute(1), ex -> ex instanceof ObjectClosedException);
    AssertExtensions.assertThrows("removeFinishedWrites() worked after close().", q::removeFinishedWrites, ex -> ex instanceof ObjectClosedException);
}
Also used : lombok.val(lombok.val) CompositeByteArraySegment(io.pravega.common.util.CompositeByteArraySegment) ObjectClosedException(io.pravega.common.ObjectClosedException) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 13 with CompositeByteArraySegment

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

the class BookKeeperLogUnitTests method testHandleWriteException.

private static Write testHandleWriteException(Throwable ex, Class expectedErrorType) {
    Write result = new Write(new CompositeByteArraySegment(new byte[0]), mock(WriteLedger.class), new CompletableFuture<>());
    BookKeeperLog bookKeeperLog = mock(BookKeeperLog.class);
    BookKeeperLog.handleWriteException(ex, result, bookKeeperLog);
    assertTrue("Unexpected failure cause " + result.getFailureCause(), expectedErrorType.isInstance(result.getFailureCause()));
    assertSame(ex, result.getFailureCause().getCause());
    return result;
}
Also used : CompositeByteArraySegment(io.pravega.common.util.CompositeByteArraySegment)

Example 14 with CompositeByteArraySegment

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

the class DurableDataLogTestBase method populate.

protected TreeMap<LogAddress, byte[]> populate(DurableDataLog log, int writeCount) {
    TreeMap<LogAddress, byte[]> writtenData = new TreeMap<>(Comparator.comparingLong(LogAddress::getSequence));
    val data = new ArrayList<byte[]>();
    val futures = new ArrayList<CompletableFuture<LogAddress>>();
    for (int i = 0; i < writeCount; i++) {
        byte[] writeData = getWriteData();
        futures.add(log.append(new CompositeByteArraySegment(writeData), TIMEOUT));
        data.add(writeData);
    }
    val addresses = Futures.allOfWithResults(futures).join();
    for (int i = 0; i < data.size(); i++) {
        writtenData.put(addresses.get(i), data.get(i));
    }
    return writtenData;
}
Also used : lombok.val(lombok.val) CompositeByteArraySegment(io.pravega.common.util.CompositeByteArraySegment) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap)

Example 15 with CompositeByteArraySegment

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

the class DurableDataLogTestBase method testRegisterQueueStateListener.

/**
 * Tests the ability to register a {@link ThrottleSourceListener} and notify it of updates.
 * @throws Exception If an error occurred.
 */
@Test(timeout = 30000)
public void testRegisterQueueStateListener() throws Exception {
    val listener = new TestThrottleSourceListener();
    try (DurableDataLog log = createDurableDataLog()) {
        log.initialize(TIMEOUT);
        log.registerQueueStateChangeListener(listener);
        // Only verify sequence number monotonicity. We'll verify reads in its own test.
        int writeCount = getWriteCount();
        for (int i = 0; i < writeCount; i++) {
            log.append(new CompositeByteArraySegment(getWriteData()), TIMEOUT).join();
        }
        // Verify the correct number of invocations.
        TestUtils.await(() -> listener.getCount() == writeCount, 10, TIMEOUT.toMillis());
        // Verify that the listener is unregistered when closed.
        listener.close();
        log.append(new CompositeByteArraySegment(getWriteData()), TIMEOUT).join();
        try {
            TestUtils.await(() -> listener.getCount() > writeCount, 10, 50);
            Assert.fail("Listener's count was updated after it was closed.");
        } catch (TimeoutException tex) {
        // This is expected. We do not want our condition to hold true.
        }
        // This should have no effect as it's already closed.
        log.registerQueueStateChangeListener(listener);
        Assert.assertFalse("Not expected the listener to have been notified after closing.", listener.wasNotifiedWhenClosed());
    }
}
Also used : lombok.val(lombok.val) CompositeByteArraySegment(io.pravega.common.util.CompositeByteArraySegment) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Aggregations

CompositeByteArraySegment (io.pravega.common.util.CompositeByteArraySegment)17 Test (org.junit.Test)15 lombok.val (lombok.val)14 ArrayList (java.util.ArrayList)11 DurableDataLog (io.pravega.segmentstore.storage.DurableDataLog)6 ObjectClosedException (io.pravega.common.ObjectClosedException)5 RetriesExhaustedException (io.pravega.common.util.RetriesExhaustedException)4 LogAddress (io.pravega.segmentstore.storage.LogAddress)4 TreeMap (java.util.TreeMap)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 Cleanup (lombok.Cleanup)4 DataLogCorruptedException (io.pravega.segmentstore.storage.DataLogCorruptedException)3 DataLogNotAvailableException (io.pravega.segmentstore.storage.DataLogNotAvailableException)3 DurableDataLogException (io.pravega.segmentstore.storage.DurableDataLogException)3 HashMap (java.util.HashMap)3 CancellationException (java.util.concurrent.CancellationException)3 BookKeeper (org.apache.bookkeeper.client.api.BookKeeper)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 Lists (com.google.common.collect.Lists)2 Sets (com.google.common.collect.Sets)2