org.apache.nifi.components.state.StateManager Java Examples

The following examples show how to use org.apache.nifi.components.state.StateManager. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestState.java    From nifi-scripting-samples with Apache License 2.0 6 votes vote down vote up
/**
 * Demonstrates reading and writing processor state values
 * @throws Exception
 */
@Test
public void testStatePython() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new ExecuteScript());
    runner.setValidateExpressionUsage(false);
    runner.setProperty(SCRIPT_ENGINE, "python");
    runner.setProperty(ScriptingComponentUtils.SCRIPT_FILE, "src/test/resources/executescript/state/state.py");
    runner.setProperty(ScriptingComponentUtils.MODULES, "src/test/resources/executescript");
    runner.assertValid();

    StateManager stateManager = runner.getStateManager();
    stateManager.clear(Scope.CLUSTER);
    Map<String, String> initialStateValues = new HashMap<>();
    initialStateValues.put("some-state", "foo");
    stateManager.setState(initialStateValues, Scope.CLUSTER);

    runner.enqueue("sample text".getBytes(StandardCharsets.UTF_8));
    runner.run();

    runner.assertAllFlowFilesTransferred("success", 1);
    StateMap resultStateValues = stateManager.getState(Scope.CLUSTER);
    Assert.assertEquals("foobar", resultStateValues.get("some-state"));
}
 
Example #2
Source File: TestStandardControllerServiceProvider.java    From nifi with Apache License 2.0 6 votes vote down vote up
private ProcessorNode createProcessor(final StandardProcessScheduler scheduler, final ControllerServiceProvider serviceProvider) {
    final ReloadComponent reloadComponent = Mockito.mock(ReloadComponent.class);

    final Processor processor = new DummyProcessor();
    final MockProcessContext context = new MockProcessContext(processor, Mockito.mock(StateManager.class), variableRegistry);
    final MockProcessorInitializationContext mockInitContext = new MockProcessorInitializationContext(processor, context);
    processor.initialize(mockInitContext);

    final LoggableComponent<Processor> dummyProcessor = new LoggableComponent<>(processor, systemBundle.getBundleDetails().getCoordinate(), null);
    final ProcessorNode procNode = new StandardProcessorNode(dummyProcessor, mockInitContext.getIdentifier(),
            new StandardValidationContextFactory(serviceProvider, null), scheduler, serviceProvider,
            new StandardComponentVariableRegistry(VariableRegistry.EMPTY_REGISTRY), reloadComponent, extensionManager, new SynchronousValidationTrigger());

    final FlowManager flowManager = Mockito.mock(FlowManager.class);
    final FlowController flowController = Mockito.mock(FlowController.class );
    Mockito.when(flowController.getFlowManager()).thenReturn(flowManager);

    final ProcessGroup group = new StandardProcessGroup(UUID.randomUUID().toString(), serviceProvider, scheduler, null, null, flowController,
        new MutableVariableRegistry(variableRegistry));
    group.addProcessor(procNode);
    procNode.setProcessGroup(group);

    return procNode;
}
 
Example #3
Source File: TestPrometheusRecordSink.java    From nifi with Apache License 2.0 6 votes vote down vote up
private PrometheusRecordSink initTask() throws InitializationException {

        final ComponentLog logger = mock(ComponentLog.class);
        final PrometheusRecordSink task = new PrometheusRecordSink();
        ConfigurationContext context = mock(ConfigurationContext.class);
        final StateManager stateManager = new MockStateManager(task);

        final PropertyValue pValue = mock(StandardPropertyValue.class);
        when(context.getProperty(PrometheusMetricsUtil.METRICS_ENDPOINT_PORT)).thenReturn(new MockPropertyValue(portString));
        when(context.getProperty(PrometheusRecordSink.SSL_CONTEXT)).thenReturn(pValue);
        when(pValue.asControllerService(SSLContextService.class)).thenReturn(null);

        final ControllerServiceInitializationContext initContext = new MockControllerServiceInitializationContext(task, UUID.randomUUID().toString(), logger, stateManager);
        task.initialize(initContext);
        task.onScheduled(context);

        return task;
    }
 
Example #4
Source File: GetSplunk.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
private TimeRange loadState(StateManager stateManager) throws IOException {
    final StateMap stateMap = stateManager.getState(Scope.CLUSTER);

    if (stateMap.getVersion() < 0) {
        getLogger().debug("No previous state found");
        return null;
    }

    final String earliest = stateMap.get(EARLIEST_TIME_KEY);
    final String latest = stateMap.get(LATEST_TIME_KEY);
    getLogger().debug("Loaded state with earliestTime of {} and latestTime of {}", new Object[] {earliest, latest});

    if (StringUtils.isBlank(earliest) && StringUtils.isBlank(latest)) {
        return null;
    } else {
        return new TimeRange(earliest, latest);
    }
}
 
Example #5
Source File: GetSplunk.java    From nifi with Apache License 2.0 6 votes vote down vote up
private TimeRange loadState(StateManager stateManager) throws IOException {
    final StateMap stateMap = stateManager.getState(Scope.CLUSTER);

    if (stateMap.getVersion() < 0) {
        getLogger().debug("No previous state found");
        return null;
    }

    final String earliest = stateMap.get(EARLIEST_TIME_KEY);
    final String latest = stateMap.get(LATEST_TIME_KEY);
    getLogger().debug("Loaded state with earliestTime of {} and latestTime of {}", new Object[] {earliest, latest});

    if (StringUtils.isBlank(earliest) && StringUtils.isBlank(latest)) {
        return null;
    } else {
        return new TimeRange(earliest, latest);
    }
}
 
Example #6
Source File: StandardProcessorTestRunner.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Override
public void assertNotValid(final ControllerService service) {
    final StateManager serviceStateManager = controllerServiceStateManagers.get(service.getIdentifier());
    if (serviceStateManager == null) {
        throw new IllegalStateException("Controller Service has not been added to this TestRunner via the #addControllerService method");
    }

    final ValidationContext validationContext = new MockValidationContext(context, serviceStateManager, variableRegistry).getControllerServiceValidationContext(service);
    final Collection<ValidationResult> results = context.getControllerService(service.getIdentifier()).validate(validationContext);

    for (final ValidationResult result : results) {
        if (!result.isValid()) {
            return;
        }
    }

    Assert.fail("Expected Controller Service " + service + " to be invalid but it is valid");
}
 
Example #7
Source File: StandardProcessContext.java    From nifi with Apache License 2.0 6 votes vote down vote up
public StandardProcessContext(final ProcessorNode processorNode, final ControllerServiceProvider controllerServiceProvider, final StringEncryptor encryptor, final StateManager stateManager,
                              final TaskTermination taskTermination) {
    this.procNode = processorNode;
    this.controllerServiceProvider = controllerServiceProvider;
    this.encryptor = encryptor;
    this.stateManager = stateManager;
    this.taskTermination = taskTermination;

    properties = Collections.unmodifiableMap(processorNode.getEffectivePropertyValues());

    preparedQueries = new HashMap<>();
    for (final Map.Entry<PropertyDescriptor, String> entry : processorNode.getRawPropertyValues().entrySet()) {
        final PropertyDescriptor desc = entry.getKey();
        String value = entry.getValue();
        if (value == null) {
            value = desc.getDefaultValue();
        }

        if (value != null) {
            final PreparedQuery pq = Query.prepare(value);
            preparedQueries.put(desc, pq);
        }
    }
}
 
Example #8
Source File: ProvenanceEventConsumer.java    From nifi with Apache License 2.0 6 votes vote down vote up
private long updateLastEventId(final List<ProvenanceEventRecord> events, final StateManager stateManager) {
    if (events == null || events.isEmpty()) {
        return firstEventId;
    }

    // Store the id of the last event so we know where we left off
    final ProvenanceEventRecord lastEvent = events.get(events.size() - 1);
    final String lastEventId = String.valueOf(lastEvent.getEventId());
    try {
        Map<String, String> newMapOfState = new HashMap<>();
        newMapOfState.put(LAST_EVENT_ID_KEY, lastEventId);
        stateManager.setState(newMapOfState, Scope.LOCAL);
    } catch (final IOException ioe) {
        logger.error("Failed to update state to {} due to {}; this could result in events being re-sent after a restart. The message of {} was: {}",
                new Object[]{lastEventId, ioe, ioe, ioe.getMessage()}, ioe);
    }

    return lastEvent.getEventId() + 1;
}
 
Example #9
Source File: TestState.java    From nifi-scripting-samples with Apache License 2.0 6 votes vote down vote up
/**
 * Demonstrates reading and writing processor state values
 * @throws Exception
 */
@Test
public void testStateJavascript() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new ExecuteScript());
    runner.setValidateExpressionUsage(false);
    runner.setProperty(SCRIPT_ENGINE, "ECMAScript");
    runner.setProperty(ScriptingComponentUtils.SCRIPT_FILE, "src/test/resources/executescript/state/state.js");
    runner.setProperty(ScriptingComponentUtils.MODULES, "src/test/resources/executescript");
    runner.assertValid();

    StateManager stateManager = runner.getStateManager();
    stateManager.clear(Scope.CLUSTER);
    Map<String, String> initialStateValues = new HashMap<>();
    initialStateValues.put("some-state", "foo");
    stateManager.setState(initialStateValues, Scope.CLUSTER);

    runner.enqueue("sample text".getBytes(StandardCharsets.UTF_8));
    runner.run();

    runner.assertAllFlowFilesTransferred("success", 1);
    StateMap resultStateValues = stateManager.getState(Scope.CLUSTER);
    Assert.assertEquals("foobar", resultStateValues.get("some-state"));
}
 
Example #10
Source File: StandardProcessorTestRunner.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Override
public void assertNotValid(final ControllerService service) {
    final StateManager serviceStateManager = controllerServiceStateManagers.get(service.getIdentifier());
    if (serviceStateManager == null) {
        throw new IllegalStateException("Controller Service has not been added to this TestRunner via the #addControllerService method");
    }

    final ValidationContext validationContext = new MockValidationContext(context, serviceStateManager, variableRegistry).getControllerServiceValidationContext(service);
    final Collection<ValidationResult> results = context.getControllerService(service.getIdentifier()).validate(validationContext);

    for (final ValidationResult result : results) {
        if (!result.isValid()) {
            return;
        }
    }

    Assert.fail("Expected Controller Service " + service + " to be invalid but it is valid");
}
 
Example #11
Source File: StandardProcessorTestRunner.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Override
public void assertValid(final ControllerService service) {
    final StateManager serviceStateManager = controllerServiceStateManagers.get(service.getIdentifier());
    if (serviceStateManager == null) {
        throw new IllegalStateException("Controller Service has not been added to this TestRunner via the #addControllerService method");
    }

    final ValidationContext validationContext = new MockValidationContext(context, serviceStateManager, variableRegistry).getControllerServiceValidationContext(service);
    final Collection<ValidationResult> results = context.getControllerService(service.getIdentifier()).validate(validationContext);

    for (final ValidationResult result : results) {
        if (!result.isValid()) {
            Assert.fail("Expected Controller Service to be valid but it is invalid due to: " + result.toString());
        }
    }
}
 
Example #12
Source File: AttributeRollingWindow.java    From nifi with Apache License 2.0 6 votes vote down vote up
@OnScheduled
public void onScheduled(final ProcessContext context) throws IOException {
    timeWindow = context.getProperty(TIME_WINDOW).asTimePeriod(TimeUnit.MILLISECONDS);
    microBatchTime = context.getProperty(SUB_WINDOW_LENGTH).asTimePeriod(TimeUnit.MILLISECONDS);

    if(microBatchTime == null || microBatchTime == 0) {
        StateManager stateManager = context.getStateManager();
        StateMap state = stateManager.getState(SCOPE);
        HashMap<String, String> tempMap = new HashMap<>();
        tempMap.putAll(state.toMap());
        if (!tempMap.containsKey(COUNT_KEY)) {
            tempMap.put(COUNT_KEY, "0");
            context.getStateManager().setState(tempMap, SCOPE);
        }
    }
}
 
Example #13
Source File: StandardProcessContext.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
public StandardProcessContext(final ProcessorNode processorNode, final ControllerServiceProvider controllerServiceProvider, final StringEncryptor encryptor, final StateManager stateManager,
                              final VariableRegistry variableRegistry) {
    this.procNode = processorNode;
    this.controllerServiceProvider = controllerServiceProvider;
    this.encryptor = encryptor;
    this.stateManager = stateManager;
    this.variableRegistry = variableRegistry;

    preparedQueries = new HashMap<>();
    for (final Map.Entry<PropertyDescriptor, String> entry : procNode.getProperties().entrySet()) {
        final PropertyDescriptor desc = entry.getKey();
        String value = entry.getValue();
        if (value == null) {
            value = desc.getDefaultValue();
        }

        if (value != null) {
            final PreparedQuery pq = Query.prepare(value);
            preparedQueries.put(desc, pq);
        }
    }
}
 
Example #14
Source File: QueryDatabaseTableRecordTest.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetQueryUsingPhoenixAdapter() throws Exception {
    Map<String, String> maxValues = new HashMap<>();
    StateManager stateManager = runner.getStateManager();
    processor.putColumnType("mytable" + AbstractDatabaseFetchProcessor.NAMESPACE_DELIMITER + "id", Types.INTEGER);
    processor.putColumnType("mytable" + AbstractDatabaseFetchProcessor.NAMESPACE_DELIMITER + "time_created", Types.TIME);
    processor.putColumnType("mytable" + AbstractDatabaseFetchProcessor.NAMESPACE_DELIMITER + "date_created", Types.TIMESTAMP);

    maxValues.put("id", "509");
    maxValues.put("time_created", "12:34:57");
    maxValues.put("date_created", "2016-03-07 12:34:56");
    stateManager.setState(maxValues, Scope.CLUSTER);

    dbAdapter = new PhoenixDatabaseAdapter();
    String query = processor.getQuery(dbAdapter, "myTable", null, Arrays.asList("id", "DATE_CREATED", "TIME_CREATED"), "type = \"CUSTOMER\"", stateManager.getState(Scope.CLUSTER).toMap());
    assertEquals("SELECT * FROM myTable WHERE id > 509 AND DATE_CREATED >= timestamp '2016-03-07 12:34:56' AND TIME_CREATED >= time '12:34:57' AND (type = \"CUSTOMER\")", query);
    // Cover the other path
    dbAdapter = new GenericDatabaseAdapter();
    query = processor.getQuery(dbAdapter, "myTable", null, Arrays.asList("id", "DATE_CREATED", "TIME_CREATED"), "type = \"CUSTOMER\"", stateManager.getState(Scope.CLUSTER).toMap());
    assertEquals("SELECT * FROM myTable WHERE id > 509 AND DATE_CREATED >= '2016-03-07 12:34:56' AND TIME_CREATED >= '12:34:57' AND (type = \"CUSTOMER\")", query);
}
 
Example #15
Source File: StatelessProcessContext.java    From nifi with Apache License 2.0 5 votes vote down vote up
public StatelessProcessContext(final ConfigurableComponent component, final StatelessControllerServiceLookup lookup, final String componentName,
                               final SLF4JComponentLog logger, final StateManager stateManager, final VariableRegistry variableRegistry, final ParameterContext parameterContext) {
    this.component = Objects.requireNonNull(component);
    this.componentName = componentName == null ? "" : componentName;
    this.inputRequirement = component.getClass().getAnnotation(InputRequirement.class);
    this.lookup = lookup;
    this.stateManager = stateManager;
    this.variableRegistry = variableRegistry;
    this.identifier = component.getIdentifier();
    this.logger = logger;
    this.parameterContext = parameterContext;
}
 
Example #16
Source File: MockProcessContext.java    From nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new MockProcessContext for the given Processor with given name
 *
 * @param component being mocked
 * @param componentName the name to be given the component;
 * @param stateManager state manager
 * @param variableRegistry variableRegistry
 */
public MockProcessContext(final ConfigurableComponent component,
                          final String componentName,
                          final StateManager stateManager,
                          final VariableRegistry variableRegistry) {
    this.component = Objects.requireNonNull(component);
    this.componentName = componentName == null ? "" : componentName;
    this.inputRequirement = component.getClass().getAnnotation(InputRequirement.class);
    this.stateManager = stateManager;
    this.variableRegistry = variableRegistry;
}
 
Example #17
Source File: StandardComponentStateDAO.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
private void clearState(final String componentId) {
    try {
        final StateManager manager = stateManagerProvider.getStateManager(componentId);
        if (manager == null) {
            throw new ResourceNotFoundException(String.format("State for the specified component %s could not be found.", componentId));
        }

        // clear both state's at the same time
        manager.clear(Scope.CLUSTER);
        manager.clear(Scope.LOCAL);
    } catch (final IOException ioe) {
        throw new IllegalStateException(String.format("Unable to clear the state for the specified component %s: %s", componentId, ioe), ioe);
    }
}
 
Example #18
Source File: MockControllerServiceInitializationContext.java    From nifi with Apache License 2.0 5 votes vote down vote up
public MockControllerServiceInitializationContext(final ControllerService controllerService, final String identifier, final ComponentLog logger, final StateManager stateManager,
                                                  final KerberosContext kerberosContext) {
    this.identifier = identifier;
    this.logger = logger;
    this.stateManager = stateManager;
    this.kerberosContext = kerberosContext;
    addControllerService(controllerService, identifier);
}
 
Example #19
Source File: StandardControllerServiceInitializationContext.java    From nifi with Apache License 2.0 5 votes vote down vote up
public StandardControllerServiceInitializationContext(
        final String identifier, final ComponentLog logger,
        final ControllerServiceProvider serviceProvider, final StateManager stateManager,
        final KerberosConfig kerberosConfig, final NodeTypeProvider nodeTypeProvider) {
    this.id = identifier;
    this.logger = logger;
    this.serviceProvider = serviceProvider;
    this.stateManager = stateManager;
    this.kerberosConfig = kerberosConfig;
    this.nodeTypeProvider = nodeTypeProvider;
}
 
Example #20
Source File: GetSplunk.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
private void saveState(StateManager stateManager, TimeRange timeRange) throws IOException {
    final String earliest = StringUtils.isBlank(timeRange.getEarliestTime()) ? "" : timeRange.getEarliestTime();
    final String latest = StringUtils.isBlank(timeRange.getLatestTime()) ? "" : timeRange.getLatestTime();

    Map<String,String> state = new HashMap<>(2);
    state.put(EARLIEST_TIME_KEY, earliest);
    state.put(LATEST_TIME_KEY, latest);

    getLogger().debug("Saving state with earliestTime of {} and latestTime of {}", new Object[] {earliest, latest});
    stateManager.setState(state, Scope.CLUSTER);
}
 
Example #21
Source File: GetHBase.java    From nifi with Apache License 2.0 5 votes vote down vote up
private ScanResult getState(final StateManager stateManager) throws IOException {
    final StateMap stateMap = stateManager.getState(Scope.CLUSTER);
    if (stateMap.getVersion() < 0) {
        return null;
    }

    return ScanResult.fromFlatMap(stateMap.toMap());
}
 
Example #22
Source File: StandardStateManagerProvider.java    From nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the State Manager that has been created for the given component ID, or <code>null</code> if none exists
 *
 * @return the StateManager that can be used by the component with the given ID, or <code>null</code> if none exists
 */
@Override
public synchronized StateManager getStateManager(final String componentId) {
    StateManager stateManager = stateManagers.get(componentId);
    if (stateManager != null) {
        return stateManager;
    }

    stateManager = new StandardStateManager(localStateProvider, clusterStateProvider, componentId);
    stateManagers.put(componentId, stateManager);
    return stateManager;
}
 
Example #23
Source File: MockValidationContext.java    From nifi with Apache License 2.0 5 votes vote down vote up
public MockValidationContext(final MockProcessContext processContext, final StateManager stateManager, final VariableRegistry variableRegistry) {
    this.context = processContext;
    this.stateManager = stateManager;
    this.variableRegistry = variableRegistry;

    final Map<PropertyDescriptor, String> properties = processContext.getProperties();
    expressionLanguageSupported = new HashMap<>(properties.size());
    for (final PropertyDescriptor descriptor : properties.keySet()) {
        expressionLanguageSupported.put(descriptor.getName(), descriptor.isExpressionLanguageSupported());
    }
}
 
Example #24
Source File: ScriptedActionHandlerTest.java    From nifi with Apache License 2.0 5 votes vote down vote up
private MockScriptedActionHandler initTask(String scriptFile) throws InitializationException {

        final MockScriptedActionHandler actionHandler = new MockScriptedActionHandler();
        context = mock(ConfigurationContext.class);
        StateManager stateManager = new MockStateManager(actionHandler);

        final ComponentLog logger = mock(ComponentLog.class);
        final ControllerServiceInitializationContext initContext = new MockControllerServiceInitializationContext(actionHandler, UUID.randomUUID().toString(), logger, stateManager);
        actionHandler.initialize(initContext);

        // Call something that sets up the ScriptingComponentHelper, so we can mock it
        actionHandler.getSupportedPropertyDescriptors();

        Map<PropertyDescriptor, String> properties = new HashMap<>();
        properties.put(actionHandler.getScriptingComponentHelper().SCRIPT_ENGINE, actionHandler.getScriptingComponentHelper().SCRIPT_ENGINE.getName());
        properties.put(ScriptingComponentUtils.SCRIPT_FILE, ScriptingComponentUtils.SCRIPT_FILE.getName());
        properties.put(ScriptingComponentUtils.SCRIPT_BODY, ScriptingComponentUtils.SCRIPT_BODY.getName());
        properties.put(ScriptingComponentUtils.MODULES, ScriptingComponentUtils.MODULES.getName());
        when(context.getProperties()).thenReturn(properties);

        when(context.getProperty(actionHandler.getScriptingComponentHelper().SCRIPT_ENGINE))
                .thenReturn(new MockPropertyValue("Groovy"));
        when(context.getProperty(ScriptingComponentUtils.SCRIPT_FILE))
                .thenReturn(new MockPropertyValue(scriptFile));
        when(context.getProperty(ScriptingComponentUtils.SCRIPT_BODY))
                .thenReturn(new MockPropertyValue(null));
        when(context.getProperty(ScriptingComponentUtils.MODULES))
                .thenReturn(new MockPropertyValue(null));
        try {
            actionHandler.onEnabled(context);
        } catch (Exception e) {
            e.printStackTrace();
            fail("onEnabled error: " + e.getMessage());
        }
        return actionHandler;
    }
 
Example #25
Source File: StandardStateManagerProvider.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the State Manager that has been created for the given component ID, or <code>null</code> if none exists
 *
 * @return the StateManager that can be used by the component with the given ID, or <code>null</code> if none exists
 */
@Override
public synchronized StateManager getStateManager(final String componentId) {
    StateManager stateManager = stateManagers.get(componentId);
    if (stateManager != null) {
        return stateManager;
    }

    stateManager = new StandardStateManager(localStateProvider, clusterStateProvider, componentId);
    stateManagers.put(componentId, stateManager);
    return stateManager;
}
 
Example #26
Source File: StandardControllerServiceInitializationContext.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
public StandardControllerServiceInitializationContext(
        final String identifier, final ComponentLog logger,
        final ControllerServiceProvider serviceProvider, final StateManager stateManager,
        final NiFiProperties nifiProperties) {
    this.id = identifier;
    this.logger = logger;
    this.serviceProvider = serviceProvider;
    this.stateManager = stateManager;
    this.nifiProperties = nifiProperties;
}
 
Example #27
Source File: SiteToSiteUtils.java    From nifi with Apache License 2.0 5 votes vote down vote up
public static SiteToSiteClient getClient(PropertyContext reportContext, ComponentLog logger, StateManager stateManager) {
    final SSLContextService sslContextService = reportContext.getProperty(SiteToSiteUtils.SSL_CONTEXT).asControllerService(SSLContextService.class);
    final SSLContext sslContext = sslContextService == null ? null : sslContextService.createSSLContext(SslContextFactory.ClientAuth.REQUIRED);
    final EventReporter eventReporter = (EventReporter) (severity, category, message) -> {
        switch (severity) {
            case WARNING:
                logger.warn(message);
                break;
            case ERROR:
                logger.error(message);
                break;
            default:
                break;
        }
    };
    final String destinationUrl = reportContext.getProperty(SiteToSiteUtils.DESTINATION_URL).evaluateAttributeExpressions().getValue();

    final SiteToSiteTransportProtocol mode = SiteToSiteTransportProtocol.valueOf(reportContext.getProperty(SiteToSiteUtils.TRANSPORT_PROTOCOL).getValue());
    final HttpProxy httpProxy = mode.equals(SiteToSiteTransportProtocol.RAW) || StringUtils.isEmpty(reportContext.getProperty(SiteToSiteUtils.HTTP_PROXY_HOSTNAME).getValue()) ? null
            : new HttpProxy(reportContext.getProperty(SiteToSiteUtils.HTTP_PROXY_HOSTNAME).getValue(), reportContext.getProperty(SiteToSiteUtils.HTTP_PROXY_PORT).asInteger(),
            reportContext.getProperty(SiteToSiteUtils.HTTP_PROXY_USERNAME).getValue(), reportContext.getProperty(SiteToSiteUtils.HTTP_PROXY_PASSWORD).getValue());

    // If no state manager was provided and this context supports retrieving it, do so
    if (stateManager == null && reportContext instanceof ReportingContext) {
        stateManager = ((ReportingContext) reportContext).getStateManager();
    }
    return new SiteToSiteClient.Builder()
            .urls(SiteToSiteRestApiClient.parseClusterUrls(destinationUrl))
            .portName(reportContext.getProperty(SiteToSiteUtils.PORT_NAME).getValue())
            .useCompression(reportContext.getProperty(SiteToSiteUtils.COMPRESS).asBoolean())
            .eventReporter(eventReporter)
            .sslContext(sslContext)
            .timeout(reportContext.getProperty(SiteToSiteUtils.TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS)
            .transportProtocol(mode)
            .httpProxy(httpProxy)
            .stateManager(stateManager)
            .build();
}
 
Example #28
Source File: StandardComponentStateDAO.java    From nifi with Apache License 2.0 5 votes vote down vote up
private StateMap getState(final String componentId, final Scope scope) {
    try {
        final StateManager manager = stateManagerProvider.getStateManager(componentId);
        if (manager == null) {
            throw new ResourceNotFoundException(String.format("State for the specified component %s could not be found.", componentId));
        }

        return manager.getState(scope);
    } catch (final IOException ioe) {
        throw new IllegalStateException(String.format("Unable to get the state for the specified component %s: %s", componentId, ioe), ioe);
    }
}
 
Example #29
Source File: CaptureChangeMySQL.java    From nifi with Apache License 2.0 5 votes vote down vote up
private void updateState(StateManager stateManager, String binlogFile, long binlogPosition, long sequenceId) throws IOException {
    // Update state with latest values
    if (stateManager != null) {
        Map<String, String> newStateMap = new HashMap<>(stateManager.getState(Scope.CLUSTER).toMap());

        // Save current binlog filename and position to the state map
        if (binlogFile != null) {
            newStateMap.put(BinlogEventInfo.BINLOG_FILENAME_KEY, binlogFile);
        }
        newStateMap.put(BinlogEventInfo.BINLOG_POSITION_KEY, Long.toString(binlogPosition));
        newStateMap.put(EventWriter.SEQUENCE_ID_KEY, String.valueOf(sequenceId));
        stateManager.setState(newStateMap, Scope.CLUSTER);
    }
}
 
Example #30
Source File: ScrollElasticsearchHttp.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
private void finishQuery(StateManager stateManager) throws IOException {

        Map<String, String> state = new HashMap<>(2);
        state.put(FINISHED_QUERY_STATE, "true");

        getLogger().debug("Saving state with finishedQuery = true");
        stateManager.setState(state, Scope.LOCAL);
    }