|
| 1 | +package com.marklogic.client.fastfunctest.datamovement; |
| 2 | + |
| 3 | +import com.marklogic.client.datamovement.DataMovementManager; |
| 4 | +import com.marklogic.client.datamovement.QueryBatcher; |
| 5 | +import com.marklogic.client.fastfunctest.AbstractFunctionalTest; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.Collections; |
| 10 | +import java.util.HashSet; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Set; |
| 13 | +import java.util.concurrent.atomic.AtomicInteger; |
| 14 | + |
| 15 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 16 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 17 | + |
| 18 | +public class AdjustQueryBatcherThreadCountTest extends AbstractFunctionalTest { |
| 19 | + |
| 20 | + @Test |
| 21 | + void increaseThreadCount() { |
| 22 | + List<String> uris = writeJsonDocs(20); |
| 23 | + Set<String> threadNames = Collections.synchronizedSet(new HashSet<>()); |
| 24 | + AtomicInteger uriCount = new AtomicInteger(); |
| 25 | + |
| 26 | + DataMovementManager dmm = client.newDataMovementManager(); |
| 27 | + QueryBatcher qb = dmm.newQueryBatcher(uris.iterator()) |
| 28 | + .withThreadCount(1) |
| 29 | + .withBatchSize(1) |
| 30 | + .onUrisReady(batch -> { |
| 31 | + waitFor(50); |
| 32 | + threadNames.add(Thread.currentThread().getName()); |
| 33 | + uriCount.addAndGet(batch.getItems().length); |
| 34 | + }); |
| 35 | + |
| 36 | + dmm.startJob(qb); |
| 37 | + waitFor(100); |
| 38 | + qb.withThreadCount(3); |
| 39 | + qb.awaitCompletion(); |
| 40 | + dmm.stopJob(qb); |
| 41 | + |
| 42 | + assertEquals(20, uriCount.get()); |
| 43 | + assertEquals(3, threadNames.size(), "3 threads should have processed all the batches, as the thread count " + |
| 44 | + "was increased from 1 to 3 100ms into the job."); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void reduceThreadCount() { |
| 49 | + List<String> uris = writeJsonDocs(20); |
| 50 | + List<String> threadNames = Collections.synchronizedList(new ArrayList<>()); |
| 51 | + |
| 52 | + DataMovementManager dmm = client.newDataMovementManager(); |
| 53 | + QueryBatcher qb = dmm.newQueryBatcher(uris.iterator()) |
| 54 | + .withThreadCount(4) |
| 55 | + .withBatchSize(1) |
| 56 | + .onUrisReady(batch -> { |
| 57 | + waitFor(50); |
| 58 | + threadNames.add(Thread.currentThread().getName()); |
| 59 | + }); |
| 60 | + |
| 61 | + dmm.startJob(qb); |
| 62 | + waitFor(100); |
| 63 | + qb.withThreadCount(2); |
| 64 | + qb.awaitCompletion(); |
| 65 | + dmm.stopJob(qb); |
| 66 | + |
| 67 | + assertEquals(20, threadNames.size(), "With 20 docs and a batch size of 1, the onUrisReady listener should " + |
| 68 | + "have been called 20 times and thus captured 20 names."); |
| 69 | + |
| 70 | + Set<String> lastEightThreadNames = new HashSet<>(threadNames.subList(12, 19)); |
| 71 | + assertEquals(2, lastEightThreadNames.size(), "Since the thread count was reduced from 4 to 2 100ms into the " + |
| 72 | + "job, then we can assume that 8 batches of size 1 were processed by 4 threads during those first 100ms, " + |
| 73 | + "as there's a 50ms pause in the onUrisReady listener. The thread count would have been reduced to 2. In " + |
| 74 | + "theory, the last 12 batches should have only been processed by those 2 threads. But just to be safe, " + |
| 75 | + "we verify that the last 8 batches were only processed by 2 threads in case it took the thread pool a " + |
| 76 | + "little bit of time to switch from 4 to 2 threads."); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void setThreadCountToOneAndThenHigher() { |
| 81 | + List<String> uris = writeJsonDocs(20); |
| 82 | + AtomicInteger uriCount = new AtomicInteger(); |
| 83 | + |
| 84 | + DataMovementManager dmm = client.newDataMovementManager(); |
| 85 | + QueryBatcher qb = dmm.newQueryBatcher(uris.iterator()) |
| 86 | + .withThreadCount(4) |
| 87 | + .withBatchSize(1) |
| 88 | + .onUrisReady(batch -> { |
| 89 | + waitFor(50); |
| 90 | + uriCount.addAndGet(batch.getItems().length); |
| 91 | + }); |
| 92 | + |
| 93 | + dmm.startJob(qb); |
| 94 | + waitFor(100); |
| 95 | + qb.withThreadCount(1); |
| 96 | + qb.withThreadCount(8); |
| 97 | + qb.awaitCompletion(); |
| 98 | + dmm.stopJob(qb); |
| 99 | + |
| 100 | + assertEquals(20, uriCount.get(), "The purpose of this test is to verify that if the thread count is set to 1, " + |
| 101 | + "the thread pool doesn't stop or throw an error. It may pause execution (for reasons that aren't known), " + |
| 102 | + "but testing has shown that increasing it to a value greater than 1 will cause execution to resume if it " + |
| 103 | + "was in fact paused."); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + void setThreadCountToZero() { |
| 108 | + List<String> uris = writeJsonDocs(20); |
| 109 | + AtomicInteger uriCount = new AtomicInteger(); |
| 110 | + |
| 111 | + DataMovementManager dmm = client.newDataMovementManager(); |
| 112 | + QueryBatcher qb = dmm.newQueryBatcher(uris.iterator()) |
| 113 | + .withThreadCount(4) |
| 114 | + .withBatchSize(1) |
| 115 | + .onUrisReady(batch -> { |
| 116 | + waitFor(50); |
| 117 | + uriCount.addAndGet(batch.getItems().length); |
| 118 | + }); |
| 119 | + |
| 120 | + dmm.startJob(qb); |
| 121 | + waitFor(100); |
| 122 | + |
| 123 | + IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> qb.withThreadCount(0)); |
| 124 | + assertEquals("threadCount must be 1 or greater", ex.getMessage()); |
| 125 | + assertEquals(4, qb.getThreadCount(), "The thread count should not have been adjusted since the input was invalid"); |
| 126 | + |
| 127 | + qb.awaitCompletion(); |
| 128 | + dmm.stopJob(qb); |
| 129 | + |
| 130 | + assertEquals(20, uriCount.get(), "All 20 URIs should still have been retrieved"); |
| 131 | + } |
| 132 | +} |
0 commit comments