Search in sources :

Example 6 with StateProvider

use of org.apache.nifi.components.state.StateProvider in project nifi by apache.

the class ITRedisStateProvider method testOnComponentRemoved.

@Test
public void testOnComponentRemoved() throws IOException, InterruptedException {
    final StateProvider provider = getProvider();
    final Map<String, String> newValue = new HashMap<>();
    newValue.put("value", "value");
    provider.setState(newValue, componentId);
    final StateMap stateMap = provider.getState(componentId);
    assertEquals(0L, stateMap.getVersion());
    provider.onComponentRemoved(componentId);
    // wait for the background process to complete
    Thread.sleep(1000L);
    final StateMap stateMapAfterRemoval = provider.getState(componentId);
    // version should be -1 because the state has been removed entirely.
    assertEquals(-1L, stateMapAfterRemoval.getVersion());
}
Also used : StateProvider(org.apache.nifi.components.state.StateProvider) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) StateMap(org.apache.nifi.components.state.StateMap) Test(org.junit.Test)

Example 7 with StateProvider

use of org.apache.nifi.components.state.StateProvider in project nifi by apache.

the class ITRedisStateProvider method testReplaceWithNonExistingValue.

@Test
public void testReplaceWithNonExistingValue() throws Exception {
    final StateProvider provider = getProvider();
    StateMap stateMap = provider.getState(componentId);
    assertNotNull(stateMap);
    final Map<String, String> newValue = new HashMap<>();
    newValue.put("value", "value");
    final boolean replaced = provider.replace(stateMap, newValue, componentId);
    assertFalse(replaced);
}
Also used : StateProvider(org.apache.nifi.components.state.StateProvider) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) StateMap(org.apache.nifi.components.state.StateMap) Test(org.junit.Test)

Example 8 with StateProvider

use of org.apache.nifi.components.state.StateProvider in project nifi by apache.

the class StandardStateManagerProvider method createStateProvider.

private static StateProvider createStateProvider(final File configFile, final Scope scope, final NiFiProperties properties, final VariableRegistry variableRegistry) throws ConfigParseException, IOException {
    final String providerId;
    final String providerIdPropertyName;
    final String providerDescription;
    final String providerXmlElementName;
    final String oppositeScopeXmlElementName;
    switch(scope) {
        case CLUSTER:
            providerId = properties.getClusterStateProviderId();
            providerIdPropertyName = NiFiProperties.STATE_MANAGEMENT_CLUSTER_PROVIDER_ID;
            providerDescription = "Cluster State Provider";
            providerXmlElementName = "cluster-provider";
            oppositeScopeXmlElementName = "local-provider";
            break;
        case LOCAL:
            providerId = properties.getLocalStateProviderId();
            providerIdPropertyName = NiFiProperties.STATE_MANAGEMENT_LOCAL_PROVIDER_ID;
            providerDescription = "Local State Provider";
            providerXmlElementName = "local-provider";
            oppositeScopeXmlElementName = "cluster-provider";
            break;
        default:
            throw new AssertionError("Attempted to create State Provider for unknown Scope: " + scope);
    }
    if (!configFile.exists()) {
        throw new IllegalStateException("Cannot create " + providerDescription + " because the State Management Configuration File " + configFile + " does not exist");
    }
    if (!configFile.canRead()) {
        throw new IllegalStateException("Cannot create " + providerDescription + " because the State Management Configuration File " + configFile + " cannot be read");
    }
    if (providerId == null) {
        if (scope == Scope.CLUSTER) {
            throw new IllegalStateException("Cannot create Cluster State Provider because the '" + providerIdPropertyName + "' property is missing from the NiFi Properties file. In order to run NiFi in a cluster, the " + providerIdPropertyName + " property must be configured in nifi.properties");
        }
        throw new IllegalStateException("Cannot create " + providerDescription + " because the '" + providerIdPropertyName + "' property is missing from the NiFi Properties file");
    }
    if (providerId.trim().isEmpty()) {
        throw new IllegalStateException("Cannot create " + providerDescription + " because the '" + providerIdPropertyName + "' property in the NiFi Properties file has no value set. This is a required property and must reference the identifier of one of the " + providerXmlElementName + " elements in the State Management Configuration File (" + configFile + ")");
    }
    final StateManagerConfiguration config = StateManagerConfiguration.parse(configFile);
    final StateProviderConfiguration providerConfig = config.getStateProviderConfiguration(providerId);
    if (providerConfig == null) {
        throw new IllegalStateException("Cannot create " + providerDescription + " because the '" + providerIdPropertyName + "' property in the NiFi Properties file is set to '" + providerId + "', but there is no " + providerXmlElementName + " entry in the State Management Configuration File (" + configFile + ") with this id");
    }
    if (providerConfig.getScope() != scope) {
        throw new IllegalStateException("Cannot create " + providerDescription + " because the '" + providerIdPropertyName + "' property in the NiFi Properties file is set to '" + providerId + "', but this id is assigned to a " + oppositeScopeXmlElementName + " entry in the State Management Configuration File (" + configFile + "), rather than a " + providerXmlElementName + " entry");
    }
    final String providerClassName = providerConfig.getClassName();
    final StateProvider provider;
    try {
        provider = instantiateStateProvider(providerClassName);
    } catch (final Exception e) {
        throw new RuntimeException("Cannot create " + providerDescription + " of type " + providerClassName, e);
    }
    if (!ArrayUtils.contains(provider.getSupportedScopes(), scope)) {
        throw new RuntimeException("Cannot use " + providerDescription + " (" + providerClassName + ") as it only supports scope(s) " + ArrayUtils.toString(provider.getSupportedScopes()) + " but " + "instance" + " is configured to use scope " + scope);
    }
    // create variable registry
    final Map<PropertyDescriptor, PropertyValue> propertyMap = new HashMap<>();
    final Map<PropertyDescriptor, String> propertyStringMap = new HashMap<>();
    for (final PropertyDescriptor descriptor : provider.getPropertyDescriptors()) {
        propertyMap.put(descriptor, new StandardPropertyValue(descriptor.getDefaultValue(), null, variableRegistry));
        propertyStringMap.put(descriptor, descriptor.getDefaultValue());
    }
    for (final Map.Entry<String, String> entry : providerConfig.getProperties().entrySet()) {
        final PropertyDescriptor descriptor = provider.getPropertyDescriptor(entry.getKey());
        propertyStringMap.put(descriptor, entry.getValue());
        propertyMap.put(descriptor, new StandardPropertyValue(entry.getValue(), null, variableRegistry));
    }
    final SSLContext sslContext = SslContextFactory.createSslContext(properties, false);
    final ComponentLog logger = new SimpleProcessLogger(providerId, provider);
    final StateProviderInitializationContext initContext = new StandardStateProviderInitializationContext(providerId, propertyMap, sslContext, logger);
    synchronized (provider) {
        provider.initialize(initContext);
    }
    final ValidationContext validationContext = new StandardValidationContext(null, propertyStringMap, null, null, null, variableRegistry);
    final Collection<ValidationResult> results = provider.validate(validationContext);
    final StringBuilder validationFailures = new StringBuilder();
    int invalidCount = 0;
    for (final ValidationResult result : results) {
        if (!result.isValid()) {
            validationFailures.append(result.toString()).append("\n");
            invalidCount++;
        }
    }
    if (invalidCount > 0) {
        throw new IllegalStateException("Could not initialize State Providers because the " + providerDescription + " is not valid. The following " + invalidCount + " Validation Errors occurred:\n" + validationFailures.toString() + "\nPlease check the configuration of the " + providerDescription + " with ID [" + providerId.trim() + "] in the file " + configFile.getAbsolutePath());
    }
    return provider;
}
Also used : StandardStateProviderInitializationContext(org.apache.nifi.controller.state.StandardStateProviderInitializationContext) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) StandardPropertyValue(org.apache.nifi.attribute.expression.language.StandardPropertyValue) ValidationResult(org.apache.nifi.components.ValidationResult) SimpleProcessLogger(org.apache.nifi.processor.SimpleProcessLogger) StateProviderInitializationContext(org.apache.nifi.components.state.StateProviderInitializationContext) StandardStateProviderInitializationContext(org.apache.nifi.controller.state.StandardStateProviderInitializationContext) StandardValidationContext(org.apache.nifi.processor.StandardValidationContext) StateManagerConfiguration(org.apache.nifi.controller.state.config.StateManagerConfiguration) StateProviderConfiguration(org.apache.nifi.controller.state.config.StateProviderConfiguration) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) StateProvider(org.apache.nifi.components.state.StateProvider) PropertyValue(org.apache.nifi.components.PropertyValue) StandardPropertyValue(org.apache.nifi.attribute.expression.language.StandardPropertyValue) SSLContext(javax.net.ssl.SSLContext) ComponentLog(org.apache.nifi.logging.ComponentLog) IOException(java.io.IOException) ConfigParseException(org.apache.nifi.controller.state.ConfigParseException) ValidationContext(org.apache.nifi.components.ValidationContext) StandardValidationContext(org.apache.nifi.processor.StandardValidationContext) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) StateMap(org.apache.nifi.components.state.StateMap)

Example 9 with StateProvider

use of org.apache.nifi.components.state.StateProvider in project nifi by apache.

the class AbstractTestStateProvider method testToMap.

@Test
public void testToMap() throws IOException {
    final String key = "testKeySet";
    final StateProvider provider = getProvider();
    Map<String, String> map = provider.getState(componentId).toMap();
    assertNotNull(map);
    assertTrue(map.isEmpty());
    provider.setState(Collections.singletonMap(key, "value"), componentId);
    map = provider.getState(componentId).toMap();
    assertNotNull(map);
    assertEquals(1, map.size());
    assertEquals("value", map.get(key));
    provider.setState(Collections.<String, String>emptyMap(), componentId);
    final StateMap stateMap = provider.getState(componentId);
    map = stateMap.toMap();
    assertNotNull(map);
    assertTrue(map.isEmpty());
    assertEquals(1, stateMap.getVersion());
}
Also used : StateProvider(org.apache.nifi.components.state.StateProvider) StateMap(org.apache.nifi.components.state.StateMap) Test(org.junit.Test)

Example 10 with StateProvider

use of org.apache.nifi.components.state.StateProvider in project nifi by apache.

the class AbstractTestStateProvider method testReplaceSuccessful.

@Test
public void testReplaceSuccessful() throws IOException {
    final String key = "testReplaceSuccessful";
    final StateProvider provider = getProvider();
    StateMap map = provider.getState(componentId);
    assertNotNull(map);
    assertEquals(-1, map.getVersion());
    assertNotNull(map.toMap());
    assertTrue(map.toMap().isEmpty());
    provider.setState(Collections.singletonMap(key, "value1"), componentId);
    map = provider.getState(componentId);
    assertNotNull(map);
    assertEquals(0, map.getVersion());
    assertEquals("value1", map.get(key));
    assertEquals("value1", map.toMap().get(key));
    final Map<String, String> newMap = new HashMap<>(map.toMap());
    newMap.put(key, "value2");
    assertTrue(provider.replace(map, newMap, componentId));
    map = provider.getState(componentId);
    assertEquals("value2", map.get(key));
    assertEquals(1L, map.getVersion());
}
Also used : StateProvider(org.apache.nifi.components.state.StateProvider) HashMap(java.util.HashMap) StateMap(org.apache.nifi.components.state.StateMap) Test(org.junit.Test)

Aggregations

StateProvider (org.apache.nifi.components.state.StateProvider)16 StateMap (org.apache.nifi.components.state.StateMap)15 Test (org.junit.Test)14 HashMap (java.util.HashMap)9 LinkedHashMap (java.util.LinkedHashMap)4 IOException (java.io.IOException)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 SSLContext (javax.net.ssl.SSLContext)1 StandardPropertyValue (org.apache.nifi.attribute.expression.language.StandardPropertyValue)1 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)1 PropertyValue (org.apache.nifi.components.PropertyValue)1 ValidationContext (org.apache.nifi.components.ValidationContext)1 ValidationResult (org.apache.nifi.components.ValidationResult)1 StateProviderInitializationContext (org.apache.nifi.components.state.StateProviderInitializationContext)1 ConfigParseException (org.apache.nifi.controller.state.ConfigParseException)1 StandardStateProviderInitializationContext (org.apache.nifi.controller.state.StandardStateProviderInitializationContext)1 StateManagerConfiguration (org.apache.nifi.controller.state.config.StateManagerConfiguration)1 StateProviderConfiguration (org.apache.nifi.controller.state.config.StateProviderConfiguration)1