Search in sources :

Example 1 with DistributedMapCacheClientService

use of org.apache.nifi.distributed.cache.client.DistributedMapCacheClientService in project nifi by apache.

the class TestServerAndClient method testClientTermination.

@Test
public void testClientTermination() throws InitializationException, IOException, InterruptedException {
    /**
     * This bypasses the test for build environments in OS X running Java 1.8 due to a JVM bug
     * See:  https://issues.apache.org/jira/browse/NIFI-437
     */
    Assume.assumeFalse("test is skipped due to build environment being OS X with JDK 1.8. See https://issues.apache.org/jira/browse/NIFI-437", SystemUtils.IS_OS_MAC && SystemUtils.IS_JAVA_1_8);
    LOGGER.info("Testing " + Thread.currentThread().getStackTrace()[1].getMethodName());
    // Create server
    final DistributedMapCacheServer server = new MapServer();
    final MockControllerServiceInitializationContext serverInitContext = new MockControllerServiceInitializationContext(server, "server");
    server.initialize(serverInitContext);
    final Map<PropertyDescriptor, String> serverProperties = new HashMap<>();
    final MockConfigurationContext serverContext = new MockConfigurationContext(serverProperties, serverInitContext.getControllerServiceLookup());
    server.startServer(serverContext);
    DistributedMapCacheClientService client = new DistributedMapCacheClientService();
    MockControllerServiceInitializationContext clientInitContext = new MockControllerServiceInitializationContext(client, "client");
    client.initialize(clientInitContext);
    final Map<PropertyDescriptor, String> clientProperties = new HashMap<>();
    clientProperties.put(DistributedMapCacheClientService.HOSTNAME, "localhost");
    clientProperties.put(DistributedMapCacheClientService.COMMUNICATIONS_TIMEOUT, "360 secs");
    MockConfigurationContext clientContext = new MockConfigurationContext(clientProperties, clientInitContext.getControllerServiceLookup());
    client.cacheConfig(clientContext);
    final Serializer<String> valueSerializer = new StringSerializer();
    final Serializer<String> keySerializer = new StringSerializer();
    final Deserializer<String> deserializer = new StringDeserializer();
    final String original = client.getAndPutIfAbsent("testKey", "test", keySerializer, valueSerializer, deserializer);
    assertEquals(null, original);
    final boolean contains = client.containsKey("testKey", keySerializer);
    assertTrue(contains);
    final boolean added = client.putIfAbsent("testKey", "test", keySerializer, valueSerializer);
    assertFalse(added);
    final String originalAfterPut = client.getAndPutIfAbsent("testKey", "test", keySerializer, valueSerializer, deserializer);
    assertEquals("test", originalAfterPut);
    final boolean removed = client.remove("testKey", keySerializer);
    assertTrue(removed);
    final boolean containedAfterRemove = client.containsKey("testKey", keySerializer);
    assertFalse(containedAfterRemove);
    client = null;
    clientInitContext = null;
    clientContext = null;
    Thread.sleep(2000);
    System.gc();
    server.shutdownServer();
}
Also used : PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) DistributedMapCacheServer(org.apache.nifi.distributed.cache.server.map.DistributedMapCacheServer) DistributedMapCacheClientService(org.apache.nifi.distributed.cache.client.DistributedMapCacheClientService) MockControllerServiceInitializationContext(org.apache.nifi.util.MockControllerServiceInitializationContext) MockConfigurationContext(org.apache.nifi.util.MockConfigurationContext) Test(org.junit.Test)

Example 2 with DistributedMapCacheClientService

use of org.apache.nifi.distributed.cache.client.DistributedMapCacheClientService in project nifi by apache.

the class TestServerAndClient method testOptimisticLock.

@Test
public void testOptimisticLock() throws Exception {
    /**
     * This bypasses the test for build environments in OS X running Java 1.8 due to a JVM bug
     * See:  https://issues.apache.org/jira/browse/NIFI-437
     */
    Assume.assumeFalse("test is skipped due to build environment being OS X with JDK 1.8. See https://issues.apache.org/jira/browse/NIFI-437", SystemUtils.IS_OS_MAC && SystemUtils.IS_JAVA_1_8);
    LOGGER.info("Testing " + Thread.currentThread().getStackTrace()[1].getMethodName());
    // Create server
    final DistributedMapCacheServer server = new MapServer();
    final TestRunner runner = TestRunners.newTestRunner(Mockito.mock(Processor.class));
    runner.addControllerService("server", server);
    runner.enableControllerService(server);
    DistributedMapCacheClientService client1 = new DistributedMapCacheClientService();
    MockControllerServiceInitializationContext clientInitContext1 = new MockControllerServiceInitializationContext(client1, "client1");
    client1.initialize(clientInitContext1);
    DistributedMapCacheClientService client2 = new DistributedMapCacheClientService();
    MockControllerServiceInitializationContext clientInitContext2 = new MockControllerServiceInitializationContext(client2, "client2");
    client1.initialize(clientInitContext2);
    final Map<PropertyDescriptor, String> clientProperties = new HashMap<>();
    clientProperties.put(DistributedMapCacheClientService.HOSTNAME, "localhost");
    clientProperties.put(DistributedMapCacheClientService.PORT, String.valueOf(server.getPort()));
    clientProperties.put(DistributedMapCacheClientService.COMMUNICATIONS_TIMEOUT, "360 secs");
    MockConfigurationContext clientContext1 = new MockConfigurationContext(clientProperties, clientInitContext1.getControllerServiceLookup());
    client1.cacheConfig(clientContext1);
    MockConfigurationContext clientContext2 = new MockConfigurationContext(clientProperties, clientInitContext2.getControllerServiceLookup());
    client2.cacheConfig(clientContext2);
    final Serializer<String> stringSerializer = new StringSerializer();
    final Deserializer<String> stringDeserializer = new StringDeserializer();
    final String key = "test-optimistic-lock";
    // Ensure there's no existing key
    assertFalse(client1.containsKey(key, stringSerializer));
    assertNull(client1.fetch(key, stringSerializer, stringDeserializer));
    // Client 1 inserts the key.
    client1.put(key, "valueC1-0", stringSerializer, stringSerializer);
    // Client 1 and 2 fetch the key
    AtomicCacheEntry<String, String, Long> c1 = client1.fetch(key, stringSerializer, stringDeserializer);
    AtomicCacheEntry<String, String, Long> c2 = client2.fetch(key, stringSerializer, stringDeserializer);
    assertEquals(new Long(0), c1.getRevision().orElse(0L));
    assertEquals("valueC1-0", c1.getValue());
    assertEquals(new Long(0), c2.getRevision().orElse(0L));
    assertEquals("valueC1-0", c2.getValue());
    // Client 1 replace
    c1.setValue("valueC1-1");
    boolean c1Result = client1.replace(c1, stringSerializer, stringSerializer);
    assertTrue("C1 should be able to replace the key", c1Result);
    // Client 2 replace with the old revision
    c2.setValue("valueC2-1");
    boolean c2Result = client2.replace(c2, stringSerializer, stringSerializer);
    assertFalse("C2 shouldn't be able to replace the key", c2Result);
    // Client 2 fetch the key again
    c2 = client2.fetch(key, stringSerializer, stringDeserializer);
    assertEquals("valueC1-1", c2.getValue());
    assertEquals(new Long(1), c2.getRevision().orElse(0L));
    // Now, Client 2 knows the correct revision so it can replace the key
    c2.setValue("valueC2-2");
    c2Result = client2.replace(c2, stringSerializer, stringSerializer);
    assertTrue("C2 should be able to replace the key", c2Result);
    // Assert the cache
    c2 = client2.fetch(key, stringSerializer, stringDeserializer);
    assertEquals("valueC2-2", c2.getValue());
    assertEquals(new Long(2), c2.getRevision().orElse(0L));
    client1.close();
    client2.close();
    server.shutdownServer();
}
Also used : Processor(org.apache.nifi.processor.Processor) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) TestRunner(org.apache.nifi.util.TestRunner) DistributedMapCacheServer(org.apache.nifi.distributed.cache.server.map.DistributedMapCacheServer) DistributedMapCacheClientService(org.apache.nifi.distributed.cache.client.DistributedMapCacheClientService) MockControllerServiceInitializationContext(org.apache.nifi.util.MockControllerServiceInitializationContext) MockConfigurationContext(org.apache.nifi.util.MockConfigurationContext) Test(org.junit.Test)

Example 3 with DistributedMapCacheClientService

use of org.apache.nifi.distributed.cache.client.DistributedMapCacheClientService in project nifi by apache.

the class TestServerAndClient method testPersistentMapServerAndClientWithLFUEvictions.

@Test
public void testPersistentMapServerAndClientWithLFUEvictions() throws InitializationException, IOException {
    /**
     * This bypasses the test for build environments in OS X running Java 1.8 due to a JVM bug
     * See:  https://issues.apache.org/jira/browse/NIFI-437
     */
    Assume.assumeFalse("test is skipped due to build environment being OS X with JDK 1.8. See https://issues.apache.org/jira/browse/NIFI-437", SystemUtils.IS_OS_MAC && SystemUtils.IS_JAVA_1_8);
    LOGGER.info("Testing " + Thread.currentThread().getStackTrace()[1].getMethodName());
    // Create server
    final File dataFile = new File("target/cache-data");
    deleteRecursively(dataFile);
    // Create server
    final TestRunner runner = TestRunners.newTestRunner(Mockito.mock(Processor.class));
    final DistributedMapCacheServer server = new MapServer();
    runner.addControllerService("server", server);
    runner.setProperty(server, DistributedMapCacheServer.PERSISTENCE_PATH, dataFile.getAbsolutePath());
    runner.setProperty(server, DistributedMapCacheServer.MAX_CACHE_ENTRIES, "3");
    runner.setProperty(server, DistributedMapCacheServer.EVICTION_POLICY, DistributedMapCacheServer.EVICTION_STRATEGY_LFU);
    runner.enableControllerService(server);
    DistributedMapCacheClientService client = createMapClient(server.getPort());
    final Serializer<String> serializer = new StringSerializer();
    final boolean added = client.putIfAbsent("test", "1", serializer, serializer);
    waitABit();
    final boolean added2 = client.putIfAbsent("test2", "2", serializer, serializer);
    waitABit();
    final boolean added3 = client.putIfAbsent("test3", "3", serializer, serializer);
    waitABit();
    assertTrue(added);
    assertTrue(added2);
    assertTrue(added3);
    final boolean contains = client.containsKey("test", serializer);
    final boolean contains2 = client.containsKey("test2", serializer);
    assertTrue(contains);
    assertTrue(contains2);
    final Deserializer<String> deserializer = new StringDeserializer();
    final Set<String> keys = client.keySet(deserializer);
    assertEquals(3, keys.size());
    assertTrue(keys.contains("test"));
    assertTrue(keys.contains("test2"));
    assertTrue(keys.contains("test3"));
    final boolean addedAgain = client.putIfAbsent("test", "1", serializer, serializer);
    assertFalse(addedAgain);
    final boolean added4 = client.putIfAbsent("test4", "4", serializer, serializer);
    assertTrue(added4);
    // ensure that added3 was evicted because it was used least frequently
    assertFalse(client.containsKey("test3", serializer));
    server.shutdownServer();
    final DistributedMapCacheServer newServer = new MapServer();
    runner.addControllerService("server2", newServer);
    runner.setProperty(newServer, DistributedMapCacheServer.PERSISTENCE_PATH, dataFile.getAbsolutePath());
    runner.enableControllerService(newServer);
    client.close();
    client = createMapClient(newServer.getPort());
    assertTrue(client.containsKey("test", serializer));
    assertTrue(client.containsKey("test2", serializer));
    assertFalse(client.containsKey("test3", serializer));
    assertTrue(client.containsKey("test4", serializer));
    // Test removeByPattern, the first two should be removed and the last should remain
    client.put("test.1", "1", serializer, serializer);
    client.put("test.2", "2", serializer, serializer);
    client.put("test3", "2", serializer, serializer);
    final long removedTwo = client.removeByPattern("test\\..*");
    assertEquals(2L, removedTwo);
    assertFalse(client.containsKey("test.1", serializer));
    assertFalse(client.containsKey("test.2", serializer));
    assertTrue(client.containsKey("test3", serializer));
    // test removeByPatternAndGet
    client.put("test.1", "1", serializer, serializer);
    client.put("test.2", "2", serializer, serializer);
    Map<String, String> removed = client.removeByPatternAndGet("test\\..*", deserializer, deserializer);
    assertEquals(2, removed.size());
    assertTrue(removed.containsKey("test.1"));
    assertTrue(removed.containsKey("test.2"));
    assertFalse(client.containsKey("test.1", serializer));
    assertFalse(client.containsKey("test.2", serializer));
    assertTrue(client.containsKey("test3", serializer));
    removed = client.removeByPatternAndGet("test\\..*", deserializer, deserializer);
    assertEquals(0, removed.size());
    newServer.shutdownServer();
    client.close();
}
Also used : Processor(org.apache.nifi.processor.Processor) TestRunner(org.apache.nifi.util.TestRunner) DistributedMapCacheServer(org.apache.nifi.distributed.cache.server.map.DistributedMapCacheServer) DistributedMapCacheClientService(org.apache.nifi.distributed.cache.client.DistributedMapCacheClientService) File(java.io.File) Test(org.junit.Test)

Example 4 with DistributedMapCacheClientService

use of org.apache.nifi.distributed.cache.client.DistributedMapCacheClientService in project nifi by apache.

the class TestServerAndClient method testNonPersistentMapServerAndClient.

@Test
public void testNonPersistentMapServerAndClient() throws InitializationException, IOException, InterruptedException {
    /**
     * This bypasses the test for build environments in OS X running Java 1.8 due to a JVM bug
     * See:  https://issues.apache.org/jira/browse/NIFI-437
     */
    Assume.assumeFalse("test is skipped due to build environment being OS X with JDK 1.8. See https://issues.apache.org/jira/browse/NIFI-437", SystemUtils.IS_OS_MAC && SystemUtils.IS_JAVA_1_8);
    LOGGER.info("Testing " + Thread.currentThread().getStackTrace()[1].getMethodName());
    // Create server
    final DistributedMapCacheServer server = new MapServer();
    final TestRunner runner = TestRunners.newTestRunner(Mockito.mock(Processor.class));
    runner.addControllerService("server", server);
    runner.enableControllerService(server);
    DistributedMapCacheClientService client = new DistributedMapCacheClientService();
    MockControllerServiceInitializationContext clientInitContext = new MockControllerServiceInitializationContext(client, "client");
    client.initialize(clientInitContext);
    final Map<PropertyDescriptor, String> clientProperties = new HashMap<>();
    clientProperties.put(DistributedMapCacheClientService.HOSTNAME, "localhost");
    clientProperties.put(DistributedMapCacheClientService.PORT, String.valueOf(server.getPort()));
    clientProperties.put(DistributedMapCacheClientService.COMMUNICATIONS_TIMEOUT, "360 secs");
    MockConfigurationContext clientContext = new MockConfigurationContext(clientProperties, clientInitContext.getControllerServiceLookup());
    client.cacheConfig(clientContext);
    final Serializer<String> valueSerializer = new StringSerializer();
    final Serializer<String> keySerializer = new StringSerializer();
    final Deserializer<String> deserializer = new StringDeserializer();
    final String original = client.getAndPutIfAbsent("testKey", "test", keySerializer, valueSerializer, deserializer);
    assertEquals(null, original);
    LOGGER.debug("end getAndPutIfAbsent");
    final boolean contains = client.containsKey("testKey", keySerializer);
    assertTrue(contains);
    LOGGER.debug("end containsKey");
    final boolean added = client.putIfAbsent("testKey", "test", keySerializer, valueSerializer);
    assertFalse(added);
    LOGGER.debug("end putIfAbsent");
    final String originalAfterPut = client.getAndPutIfAbsent("testKey", "test", keySerializer, valueSerializer, deserializer);
    assertEquals("test", originalAfterPut);
    LOGGER.debug("end getAndPutIfAbsent");
    final boolean removed = client.remove("testKey", keySerializer);
    assertTrue(removed);
    LOGGER.debug("end remove");
    client.put("testKey", "testValue", keySerializer, valueSerializer);
    assertTrue(client.containsKey("testKey", keySerializer));
    String removedValue = client.removeAndGet("testKey", keySerializer, deserializer);
    assertEquals("testValue", removedValue);
    removedValue = client.removeAndGet("testKey", keySerializer, deserializer);
    assertNull(removedValue);
    final Set<String> keys = client.keySet(deserializer);
    assertEquals(0, keys.size());
    // Test removeByPattern, the first two should be removed and the last should remain
    client.put("test.1", "1", keySerializer, keySerializer);
    client.put("test.2", "2", keySerializer, keySerializer);
    client.put("test3", "2", keySerializer, keySerializer);
    final long removedTwo = client.removeByPattern("test\\..*");
    assertEquals(2L, removedTwo);
    assertFalse(client.containsKey("test.1", keySerializer));
    assertFalse(client.containsKey("test.2", keySerializer));
    assertTrue(client.containsKey("test3", keySerializer));
    final boolean containedAfterRemove = client.containsKey("testKey", keySerializer);
    assertFalse(containedAfterRemove);
    client.putIfAbsent("testKey", "test", keySerializer, valueSerializer);
    client.close();
    try {
        client.containsKey("testKey", keySerializer);
        fail("Should be closed and not accessible");
    } catch (final Exception e) {
    }
    DistributedMapCacheClientService client2 = new DistributedMapCacheClientService();
    MockControllerServiceInitializationContext clientInitContext2 = new MockControllerServiceInitializationContext(client2, "client2");
    client2.initialize(clientInitContext2);
    MockConfigurationContext clientContext2 = new MockConfigurationContext(clientProperties, clientInitContext2.getControllerServiceLookup());
    client2.cacheConfig(clientContext2);
    assertFalse(client2.putIfAbsent("testKey", "test", keySerializer, valueSerializer));
    assertTrue(client2.containsKey("testKey", keySerializer));
    server.shutdownServer();
    Thread.sleep(1000);
    try {
        client2.containsKey("testKey", keySerializer);
        fail("Should have blown exception!");
    } catch (final ConnectException e) {
        client2 = null;
        clientContext2 = null;
        clientInitContext2 = null;
    }
    LOGGER.debug("end testNonPersistentMapServerAndClient");
}
Also used : Processor(org.apache.nifi.processor.Processor) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) TestRunner(org.apache.nifi.util.TestRunner) DistributedMapCacheServer(org.apache.nifi.distributed.cache.server.map.DistributedMapCacheServer) InitializationException(org.apache.nifi.reporting.InitializationException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) SerializationException(org.apache.commons.lang3.SerializationException) DeserializationException(org.apache.nifi.distributed.cache.client.exception.DeserializationException) DistributedMapCacheClientService(org.apache.nifi.distributed.cache.client.DistributedMapCacheClientService) MockControllerServiceInitializationContext(org.apache.nifi.util.MockControllerServiceInitializationContext) MockConfigurationContext(org.apache.nifi.util.MockConfigurationContext) ConnectException(java.net.ConnectException) Test(org.junit.Test)

Example 5 with DistributedMapCacheClientService

use of org.apache.nifi.distributed.cache.client.DistributedMapCacheClientService in project nifi by apache.

the class TestServerAndClient method createMapClient.

private DistributedMapCacheClientService createMapClient(final int port) throws InitializationException {
    final DistributedMapCacheClientService client = new DistributedMapCacheClientService();
    final MockControllerServiceInitializationContext clientInitContext = new MockControllerServiceInitializationContext(client, "client");
    client.initialize(clientInitContext);
    final Map<PropertyDescriptor, String> clientProperties = new HashMap<>();
    clientProperties.put(DistributedMapCacheClientService.HOSTNAME, "localhost");
    clientProperties.put(DistributedMapCacheClientService.PORT, String.valueOf(port));
    final MockConfigurationContext clientContext = new MockConfigurationContext(clientProperties, clientInitContext.getControllerServiceLookup());
    client.cacheConfig(clientContext);
    return client;
}
Also used : DistributedMapCacheClientService(org.apache.nifi.distributed.cache.client.DistributedMapCacheClientService) MockControllerServiceInitializationContext(org.apache.nifi.util.MockControllerServiceInitializationContext) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) MockConfigurationContext(org.apache.nifi.util.MockConfigurationContext) HashMap(java.util.HashMap)

Aggregations

DistributedMapCacheClientService (org.apache.nifi.distributed.cache.client.DistributedMapCacheClientService)6 HashMap (java.util.HashMap)5 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)5 DistributedMapCacheServer (org.apache.nifi.distributed.cache.server.map.DistributedMapCacheServer)5 MockConfigurationContext (org.apache.nifi.util.MockConfigurationContext)5 MockControllerServiceInitializationContext (org.apache.nifi.util.MockControllerServiceInitializationContext)5 Test (org.junit.Test)5 Processor (org.apache.nifi.processor.Processor)4 TestRunner (org.apache.nifi.util.TestRunner)4 File (java.io.File)2 IOException (java.io.IOException)1 ConnectException (java.net.ConnectException)1 SSLContext (javax.net.ssl.SSLContext)1 SerializationException (org.apache.commons.lang3.SerializationException)1 AtomicCacheEntry (org.apache.nifi.distributed.cache.client.AtomicCacheEntry)1 DeserializationException (org.apache.nifi.distributed.cache.client.exception.DeserializationException)1 MapCacheServer (org.apache.nifi.distributed.cache.server.map.MapCacheServer)1 StandardVersionNegotiator (org.apache.nifi.remote.StandardVersionNegotiator)1 InitializationException (org.apache.nifi.reporting.InitializationException)1