Search in sources :

Example 1 with Sample

use of org.apache.cassandra.metrics.Sampler.Sample in project cassandra by apache.

the class SamplerTest method sampleLoadshedding.

@Test
public void sampleLoadshedding() throws Exception {
    // dont need to run this in children tests
    if (sampler != null)
        return;
    AtomicInteger called = new AtomicInteger();
    CountDownLatch latch = new CountDownLatch(1);
    Sampler<String> waitSampler = new Sampler<String>() {

        protected void insert(String item, long value) {
            called.incrementAndGet();
            try {
                latch.await(1, TimeUnit.MINUTES);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        public boolean isEnabled() {
            return true;
        }

        public void beginSampling(int capacity, int durationMillis) {
        }

        public List<Sample<String>> finishSampling(int count) {
            return null;
        }

        public String toString(String value) {
            return "";
        }
    };
    // 1000 queued, 1 in progress, 1 to drop
    for (int i = 0; i < 1002; i++) {
        waitSampler.addSample("TEST", 1);
    }
    latch.countDown();
    waitForEmpty(1000);
    Assert.assertEquals(1001, called.get());
    Assert.assertEquals(1, MessagingService.instance().getDroppedMessages().get("_SAMPLE").intValue());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Sample(org.apache.cassandra.metrics.Sampler.Sample) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with Sample

use of org.apache.cassandra.metrics.Sampler.Sample in project cassandra by apache.

the class ColumnFamilyStore method finishLocalSampling.

@SuppressWarnings({ "rawtypes", "unchecked" })
public List<CompositeData> finishLocalSampling(String sampler, int count) throws OpenDataException {
    Sampler samplerImpl = metric.samplers.get(SamplerType.valueOf(sampler));
    List<Sample> samplerResults = samplerImpl.finishSampling(count);
    List<CompositeData> result = new ArrayList<>(count);
    for (Sample counter : samplerResults) {
        // Not duplicating the buffer for safety because AbstractSerializer and ByteBufferUtil.bytesToHex
        // don't modify position or limit
        result.add(new CompositeDataSupport(COUNTER_COMPOSITE_TYPE, COUNTER_NAMES, new Object[] { keyspace.getName() + "." + name, counter.count, counter.error, // string
        samplerImpl.toString(counter.value) }));
    }
    return result;
}
Also used : Sample(org.apache.cassandra.metrics.Sampler.Sample) Sampler(org.apache.cassandra.metrics.Sampler)

Aggregations

Sample (org.apache.cassandra.metrics.Sampler.Sample)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Sampler (org.apache.cassandra.metrics.Sampler)1 Test (org.junit.Test)1