Search in sources :

Example 6 with PerDatasourceShuffleMetrics

use of org.apache.druid.indexing.worker.shuffle.ShuffleMetrics.PerDatasourceShuffleMetrics in project druid by druid-io.

the class ShuffleMetricsTest method testConcurrency.

@Test(timeout = 5000L)
public void testConcurrency() throws ExecutionException, InterruptedException {
    // 2 for write, 1 for read
    final ExecutorService exec = Execs.multiThreaded(3, "shuffle-metrics-test-%d");
    try {
        final ShuffleMetrics metrics = new ShuffleMetrics();
        final String supervisorTask1 = "supervisor1";
        final String supervisorTask2 = "supervisor2";
        final CountDownLatch firstUpdatelatch = new CountDownLatch(2);
        final List<Future<Void>> futures = new ArrayList<>();
        futures.add(exec.submit(() -> {
            metrics.shuffleRequested(supervisorTask1, 1024);
            metrics.shuffleRequested(supervisorTask2, 30);
            firstUpdatelatch.countDown();
            Thread.sleep(ThreadLocalRandom.current().nextInt(10));
            metrics.shuffleRequested(supervisorTask2, 10);
            return null;
        }));
        futures.add(exec.submit(() -> {
            metrics.shuffleRequested(supervisorTask2, 30);
            metrics.shuffleRequested(supervisorTask1, 1024);
            firstUpdatelatch.countDown();
            Thread.sleep(ThreadLocalRandom.current().nextInt(10));
            metrics.shuffleRequested(supervisorTask1, 32);
            return null;
        }));
        final Map<String, PerDatasourceShuffleMetrics> firstSnapshot = exec.submit(() -> {
            firstUpdatelatch.await();
            Thread.sleep(ThreadLocalRandom.current().nextInt(10));
            return metrics.snapshotAndReset();
        }).get();
        int expectedSecondSnapshotSize = 0;
        boolean task1ShouldBeInSecondSnapshot = false;
        boolean task2ShouldBeInSecondSnapshot = false;
        Assert.assertEquals(2, firstSnapshot.size());
        Assert.assertNotNull(firstSnapshot.get(supervisorTask1));
        Assert.assertTrue(2048 == firstSnapshot.get(supervisorTask1).getShuffleBytes() || 2080 == firstSnapshot.get(supervisorTask1).getShuffleBytes());
        Assert.assertTrue(2 == firstSnapshot.get(supervisorTask1).getShuffleRequests() || 3 == firstSnapshot.get(supervisorTask1).getShuffleRequests());
        if (firstSnapshot.get(supervisorTask1).getShuffleRequests() == 2) {
            expectedSecondSnapshotSize++;
            task1ShouldBeInSecondSnapshot = true;
        }
        Assert.assertNotNull(firstSnapshot.get(supervisorTask2));
        Assert.assertTrue(60 == firstSnapshot.get(supervisorTask2).getShuffleBytes() || 70 == firstSnapshot.get(supervisorTask2).getShuffleBytes());
        Assert.assertTrue(2 == firstSnapshot.get(supervisorTask2).getShuffleRequests() || 3 == firstSnapshot.get(supervisorTask2).getShuffleRequests());
        if (firstSnapshot.get(supervisorTask2).getShuffleRequests() == 2) {
            expectedSecondSnapshotSize++;
            task2ShouldBeInSecondSnapshot = true;
        }
        for (Future<Void> future : futures) {
            future.get();
        }
        final Map<String, PerDatasourceShuffleMetrics> secondSnapshot = metrics.snapshotAndReset();
        Assert.assertEquals(expectedSecondSnapshotSize, secondSnapshot.size());
        Assert.assertEquals(task1ShouldBeInSecondSnapshot, secondSnapshot.containsKey(supervisorTask1));
        if (task1ShouldBeInSecondSnapshot) {
            Assert.assertEquals(32, secondSnapshot.get(supervisorTask1).getShuffleBytes());
            Assert.assertEquals(1, secondSnapshot.get(supervisorTask1).getShuffleRequests());
        }
        Assert.assertEquals(task2ShouldBeInSecondSnapshot, secondSnapshot.containsKey(supervisorTask2));
        if (task2ShouldBeInSecondSnapshot) {
            Assert.assertEquals(10, secondSnapshot.get(supervisorTask2).getShuffleBytes());
            Assert.assertEquals(1, secondSnapshot.get(supervisorTask2).getShuffleRequests());
        }
    } finally {
        exec.shutdown();
    }
}
Also used : ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) PerDatasourceShuffleMetrics(org.apache.druid.indexing.worker.shuffle.ShuffleMetrics.PerDatasourceShuffleMetrics) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) PerDatasourceShuffleMetrics(org.apache.druid.indexing.worker.shuffle.ShuffleMetrics.PerDatasourceShuffleMetrics) Test(org.junit.Test)

Aggregations

PerDatasourceShuffleMetrics (org.apache.druid.indexing.worker.shuffle.ShuffleMetrics.PerDatasourceShuffleMetrics)6 Test (org.junit.Test)5 File (java.io.File)1 ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 Response (javax.ws.rs.core.Response)1 Event (org.apache.druid.java.util.emitter.core.Event)1 ServiceMetricEvent (org.apache.druid.java.util.emitter.service.ServiceMetricEvent)1 Builder (org.apache.druid.java.util.emitter.service.ServiceMetricEvent.Builder)1 StubServiceEmitter (org.apache.druid.java.util.metrics.StubServiceEmitter)1 DataSegment (org.apache.druid.timeline.DataSegment)1 Interval (org.joda.time.Interval)1