Search in sources :

Example 1 with CONSUMED_COUNT_NAME

use of org.apache.nifi.processors.standard.WaitNotifyProtocol.CONSUMED_COUNT_NAME in project nifi by apache.

the class TestWaitNotifyProtocol method testReleaseCandidateTotal.

@Test
public void testReleaseCandidateTotal() throws Exception {
    final List<Integer> candidates = IntStream.range(0, 10).boxed().collect(Collectors.toList());
    final Signal signal = new Signal();
    final List<Integer> released = new ArrayList<>();
    final List<Integer> waiting = new ArrayList<>();
    // Test empty counter name, should use total counters.
    final String emptyCounterName = null;
    final BiConsumer<Long, Integer> releaseCandidate = (requiredCountForPass, releasableCandidatePerPass) -> {
        released.clear();
        waiting.clear();
        signal.releaseCandidates(emptyCounterName, requiredCountForPass, releasableCandidatePerPass, candidates, r -> released.addAll(r), w -> waiting.addAll(w));
    };
    final String counterA = "counterA";
    final String counterB = "counterB";
    final String counterC = "counterC";
    final Field releasableCount = Signal.class.getDeclaredField("releasableCount");
    releasableCount.setAccessible(true);
    // No counter, should wait.
    releaseCandidate.accept(3L, 1);
    assertEquals(0, released.size());
    assertEquals(10, waiting.size());
    assertEquals(0, signal.getCount(emptyCounterName));
    assertEquals(0, signal.getCount(CONSUMED_COUNT_NAME));
    assertEquals(0, releasableCount.getInt(signal));
    // Counter is not enough yet.
    signal.getCounts().put(counterA, 1L);
    signal.getCounts().remove(CONSUMED_COUNT_NAME);
    releaseCandidate.accept(3L, 1);
    assertEquals(0, released.size());
    assertEquals(10, waiting.size());
    // Counter incremented, but not enough
    assertEquals(1, signal.getCount(emptyCounterName));
    assertEquals(0, signal.getCount(CONSUMED_COUNT_NAME));
    assertEquals(0, releasableCount.getInt(signal));
    // Counter reached the target.
    signal.getCounts().put(counterA, 1L);
    signal.getCounts().put(counterB, 1L);
    signal.getCounts().put(counterC, 1L);
    signal.getCounts().remove(CONSUMED_COUNT_NAME);
    releaseCandidate.accept(3L, 1);
    assertEquals(1, released.size());
    assertEquals(9, waiting.size());
    // Counter 3 was converted into 1 release
    assertEquals(0, signal.getCount(emptyCounterName));
    assertEquals(-3, signal.getCount(CONSUMED_COUNT_NAME));
    assertEquals(0, releasableCount.getInt(signal));
    // Counter reached the target for two candidates.
    signal.getCounts().put(counterA, 1L);
    signal.getCounts().put(counterB, 2L);
    signal.getCounts().put(counterC, 3L);
    signal.getCounts().remove(CONSUMED_COUNT_NAME);
    releaseCandidate.accept(3L, 1);
    assertEquals(2, released.size());
    assertEquals(8, waiting.size());
    // Counter 3 was converted into 1 release
    assertEquals(0, signal.getCount(emptyCounterName));
    assertEquals(-6, signal.getCount(CONSUMED_COUNT_NAME));
    assertEquals(0, releasableCount.getInt(signal));
    // Counter reached the target for two candidates, and reminder is 2.
    signal.getCounts().put(counterA, 3L);
    signal.getCounts().put(counterB, 3L);
    signal.getCounts().put(counterC, 5L);
    signal.getCounts().remove(CONSUMED_COUNT_NAME);
    releaseCandidate.accept(3L, 1);
    // 11 / 3 = 3
    assertEquals(3, released.size());
    assertEquals(7, waiting.size());
    assertEquals(2, signal.getCount(emptyCounterName));
    assertEquals(-9, signal.getCount(CONSUMED_COUNT_NAME));
    assertEquals(0, releasableCount.getInt(signal));
    // Counter reached the target for two pass count and each pass can release 2 candidates.
    signal.getCounts().put(counterA, 1L);
    signal.getCounts().put(counterB, 2L);
    signal.getCounts().put(counterC, 3L);
    signal.getCounts().remove(CONSUMED_COUNT_NAME);
    releaseCandidate.accept(3L, 2);
    // (6 / 3) * 2 = 4
    assertEquals(4, released.size());
    assertEquals(6, waiting.size());
    assertEquals(0, signal.getCount(emptyCounterName));
    assertEquals(-6, signal.getCount(CONSUMED_COUNT_NAME));
    assertEquals(0, releasableCount.getInt(signal));
    // If there are counts more than enough to release current candidates, unused releasableCount should remain.
    signal.getCounts().put(counterA, 10L);
    signal.getCounts().put(counterB, 20L);
    signal.getCounts().put(counterC, 20L);
    signal.getCounts().remove(CONSUMED_COUNT_NAME);
    releaseCandidate.accept(3L, 2);
    // (50 / 3) * 2 = 32. Used 10.
    assertEquals(10, released.size());
    assertEquals(0, waiting.size());
    // 50 % 3 = 2.
    assertEquals(2, signal.getCount(emptyCounterName));
    // 50 % 3 = 2.
    assertEquals(-48, signal.getCount(CONSUMED_COUNT_NAME));
    // 32 - 10 = 22.
    assertEquals(22, releasableCount.getInt(signal));
}
Also used : ByteArrayOutputStream(org.apache.activemq.util.ByteArrayOutputStream) IntStream(java.util.stream.IntStream) AtomicCacheEntry(org.apache.nifi.distributed.cache.client.AtomicCacheEntry) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Answer(org.mockito.stubbing.Answer) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) BiConsumer(java.util.function.BiConsumer) Assert.fail(org.junit.Assert.fail) AtomicDistributedMapCacheClient(org.apache.nifi.distributed.cache.client.AtomicDistributedMapCacheClient) Before(org.junit.Before) Signal(org.apache.nifi.processors.standard.WaitNotifyProtocol.Signal) DEFAULT_COUNT_NAME(org.apache.nifi.processors.standard.WaitNotifyProtocol.DEFAULT_COUNT_NAME) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) CONSUMED_COUNT_NAME(org.apache.nifi.processors.standard.WaitNotifyProtocol.CONSUMED_COUNT_NAME) StandardCharsets(java.nio.charset.StandardCharsets) Matchers.any(org.mockito.Matchers.any) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) DeserializationException(org.apache.nifi.distributed.cache.client.exception.DeserializationException) FlowFileAttributesSerializer(org.apache.nifi.processors.standard.util.FlowFileAttributesSerializer) ConcurrentModificationException(java.util.ConcurrentModificationException) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) Field(java.lang.reflect.Field) Signal(org.apache.nifi.processors.standard.WaitNotifyProtocol.Signal) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

Field (java.lang.reflect.Field)1 StandardCharsets (java.nio.charset.StandardCharsets)1 ArrayList (java.util.ArrayList)1 ConcurrentModificationException (java.util.ConcurrentModificationException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 BiConsumer (java.util.function.BiConsumer)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 ByteArrayOutputStream (org.apache.activemq.util.ByteArrayOutputStream)1 AtomicCacheEntry (org.apache.nifi.distributed.cache.client.AtomicCacheEntry)1 AtomicDistributedMapCacheClient (org.apache.nifi.distributed.cache.client.AtomicDistributedMapCacheClient)1 DeserializationException (org.apache.nifi.distributed.cache.client.exception.DeserializationException)1 CONSUMED_COUNT_NAME (org.apache.nifi.processors.standard.WaitNotifyProtocol.CONSUMED_COUNT_NAME)1 DEFAULT_COUNT_NAME (org.apache.nifi.processors.standard.WaitNotifyProtocol.DEFAULT_COUNT_NAME)1 Signal (org.apache.nifi.processors.standard.WaitNotifyProtocol.Signal)1 FlowFileAttributesSerializer (org.apache.nifi.processors.standard.util.FlowFileAttributesSerializer)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertFalse (org.junit.Assert.assertFalse)1