Search in sources :

Example 6 with Counter

use of org.opennms.newts.api.Counter in project opennms by OpenNMS.

the class NewtsWriterTest method samplesAreDroppedWhenRingBufferIsFull.

/**
     * Fills the ring buffer and locks all of the writer threads to verify
     * that samples additional samples are dropped.
     */
@Test
public void samplesAreDroppedWhenRingBufferIsFull() throws Exception {
    Resource x = new Resource("x");
    int ringBufferSize = 1024;
    int numWriterThreads = 8;
    Lock lock = new ReentrantLock();
    LockedSampleRepository sampleRepo = new LockedSampleRepository(lock);
    MetricRegistry registry = new MetricRegistry();
    NewtsWriter writer = new NewtsWriter(1, ringBufferSize, numWriterThreads, registry);
    writer.setSampleRepository(sampleRepo);
    lock.lock();
    for (int i = 0; i < ringBufferSize; i++) {
        Sample s = new Sample(Timestamp.now(), x, "y", MetricType.COUNTER, new Counter(i));
        writer.insert(Lists.newArrayList(s));
    }
    // The ring buffer should be full, and all of the threads should be locked
    Thread.sleep(250);
    assertEquals(numWriterThreads, sampleRepo.getNumThreadsLocked());
    // Attempt to insert another batch of samples
    for (int i = 0; i < 8; i++) {
        Sample s = new Sample(Timestamp.now(), x, "y", MetricType.COUNTER, new Counter(i));
        writer.insert(Lists.newArrayList(s));
    }
    ;
    // Unlock the writer threads and wait for the ring buffer to drain
    lock.unlock();
    writer.destroy();
    // Verify the number of inserted samples
    assertEquals(0, sampleRepo.getNumThreadsLocked());
    assertEquals(ringBufferSize, sampleRepo.getNumSamplesInserted());
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Counter(org.opennms.newts.api.Counter) Sample(org.opennms.newts.api.Sample) MetricRegistry(com.codahale.metrics.MetricRegistry) Resource(org.opennms.newts.api.Resource) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock) Test(org.junit.Test)

Aggregations

Counter (org.opennms.newts.api.Counter)6 Sample (org.opennms.newts.api.Sample)6 Test (org.junit.Test)5 MetricRegistry (com.codahale.metrics.MetricRegistry)3 Resource (org.opennms.newts.api.Resource)3 Rate (org.opennms.newts.aggregate.Rate)2 Gauge (org.opennms.newts.api.Gauge)2 Results (org.opennms.newts.api.Results)2 Row (org.opennms.newts.api.Results.Row)2 Timestamp (org.opennms.newts.api.Timestamp)2 BoundStatement (com.datastax.driver.core.BoundStatement)1 PreparedStatement (com.datastax.driver.core.PreparedStatement)1 RegularStatement (com.datastax.driver.core.RegularStatement)1 ResultSetFuture (com.datastax.driver.core.ResultSetFuture)1 Statement (com.datastax.driver.core.Statement)1 List (java.util.List)1 Lock (java.util.concurrent.locks.Lock)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 Duration (org.opennms.newts.api.Duration)1 MetricType (org.opennms.newts.api.MetricType)1