From 6ee4632267fd1ffe645bfc1289def5d20da01500 Mon Sep 17 00:00:00 2001 From: Joe Gresock Date: Thu, 18 May 2023 13:58:26 -0400 Subject: [PATCH] NIFI-11566: Adding updateTimeout argument to parameter commands in CLI Signed-off-by: Pierre Villard This closes #7267. --- .../cli/impl/command/CommandOption.java | 1 + .../AbstractUpdateParamContextCommand.java | 26 ++++++++++++++++--- .../impl/command/nifi/params/DeleteParam.java | 5 +++- .../nifi/params/MergeParamContext.java | 5 +++- .../params/RemoveInheritedParamContexts.java | 5 +++- .../params/SetInheritedParamContexts.java | 5 +++- .../impl/command/nifi/params/SetParam.java | 4 ++- 7 files changed, 42 insertions(+), 9 deletions(-) diff --git a/nifi-toolkit/nifi-toolkit-cli/src/main/java/org/apache/nifi/toolkit/cli/impl/command/CommandOption.java b/nifi-toolkit/nifi-toolkit-cli/src/main/java/org/apache/nifi/toolkit/cli/impl/command/CommandOption.java index 677db3955c..5913871031 100644 --- a/nifi-toolkit/nifi-toolkit-cli/src/main/java/org/apache/nifi/toolkit/cli/impl/command/CommandOption.java +++ b/nifi-toolkit/nifi-toolkit-cli/src/main/java/org/apache/nifi/toolkit/cli/impl/command/CommandOption.java @@ -135,6 +135,7 @@ public enum CommandOption { PARAM_DESC("pd", "paramDescription", "The description of the parameter", true), PARAM_VALUE("pv", "paramValue", "The value of a parameter", true), PARAM_SENSITIVE("ps", "paramSensitive", "Whether or not the parameter is sensitive (true/false)", true), + UPDATE_TIMEOUT("ut", "updateTimeout", "Number of seconds after which a parameter context update will timeout (default: 60, maximum: 600)", true), // Security related KEYSTORE("ks", "keystore", "A keystore to use for TLS/SSL connections", true), diff --git a/nifi-toolkit/nifi-toolkit-cli/src/main/java/org/apache/nifi/toolkit/cli/impl/command/nifi/params/AbstractUpdateParamContextCommand.java b/nifi-toolkit/nifi-toolkit-cli/src/main/java/org/apache/nifi/toolkit/cli/impl/command/nifi/params/AbstractUpdateParamContextCommand.java index 1101377dc3..74ac6729b7 100644 --- a/nifi-toolkit/nifi-toolkit-cli/src/main/java/org/apache/nifi/toolkit/cli/impl/command/nifi/params/AbstractUpdateParamContextCommand.java +++ b/nifi-toolkit/nifi-toolkit-cli/src/main/java/org/apache/nifi/toolkit/cli/impl/command/nifi/params/AbstractUpdateParamContextCommand.java @@ -16,27 +16,36 @@ */ package org.apache.nifi.toolkit.cli.impl.command.nifi.params; +import org.apache.commons.cli.MissingOptionException; import org.apache.commons.lang3.StringUtils; import org.apache.nifi.toolkit.cli.api.Result; import org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClientException; import org.apache.nifi.toolkit.cli.impl.client.nifi.ParamContextClient; +import org.apache.nifi.toolkit.cli.impl.command.CommandOption; import org.apache.nifi.toolkit.cli.impl.command.nifi.AbstractNiFiCommand; import org.apache.nifi.web.api.entity.ParameterContextEntity; import org.apache.nifi.web.api.entity.ParameterContextUpdateRequestEntity; import java.io.IOException; +import java.util.Properties; import java.util.concurrent.atomic.AtomicBoolean; public abstract class AbstractUpdateParamContextCommand extends AbstractNiFiCommand { + private static final int MAX_TIMEOUT_SECONDS = 600; + private static final int DEFAULT_TIMEOUT_SECONDS = 60; + private static final long POLL_INTERVAL_MILLIS = 2000; + public AbstractUpdateParamContextCommand(final String name, final Class resultClass) { super(name, resultClass); } protected ParameterContextUpdateRequestEntity performUpdate(final ParamContextClient client, final ParameterContextEntity parameterContextEntity, - final ParameterContextUpdateRequestEntity updateRequestEntity) + final ParameterContextUpdateRequestEntity updateRequestEntity, final int updateTimeoutSeconds) throws NiFiClientException, IOException { + final int maxPollIterations = Math.min(Math.max(Long.valueOf(updateTimeoutSeconds * 1000L / POLL_INTERVAL_MILLIS).intValue(), 1), MAX_TIMEOUT_SECONDS); + final AtomicBoolean cancelled = new AtomicBoolean(false); // poll the update request for up to 30 seconds to see if it has completed @@ -45,7 +54,7 @@ public abstract class AbstractUpdateParamContextCommand extend final String updateRequestId = updateRequestEntity.getRequest().getRequestId(); try { boolean completed = false; - for (int i = 0; i < 30; i++) { + for (int i = 0; i < maxPollIterations; i++) { final ParameterContextUpdateRequestEntity retrievedUpdateRequest = client.getParamContextUpdateRequest(contextId, updateRequestId); if (retrievedUpdateRequest != null && retrievedUpdateRequest.getRequest().isComplete()) { completed = true; @@ -55,7 +64,7 @@ public abstract class AbstractUpdateParamContextCommand extend if (getContext().isInteractive()) { println("Waiting for update request to complete..."); } - Thread.sleep(2000); + Thread.sleep(POLL_INTERVAL_MILLIS); } catch (InterruptedException e) { e.printStackTrace(); } @@ -75,10 +84,19 @@ public abstract class AbstractUpdateParamContextCommand extend } if (cancelled.get()) { - throw new NiFiClientException("Unable to update parameter context, cancelling update request"); + throw new NiFiClientException("Unable to update parameter context in time, cancelling update request"); } return deleteUpdateRequest; } } + + protected int getUpdateTimeout(final Properties properties) { + try { + final Integer updateTimeoutSeconds = getIntArg(properties, CommandOption.UPDATE_TIMEOUT); + return updateTimeoutSeconds == null ? DEFAULT_TIMEOUT_SECONDS : updateTimeoutSeconds; + } catch (final MissingOptionException e) { + throw new RuntimeException(e.getMessage(), e); + } + } } diff --git a/nifi-toolkit/nifi-toolkit-cli/src/main/java/org/apache/nifi/toolkit/cli/impl/command/nifi/params/DeleteParam.java b/nifi-toolkit/nifi-toolkit-cli/src/main/java/org/apache/nifi/toolkit/cli/impl/command/nifi/params/DeleteParam.java index 2ae667526e..41476ffef3 100644 --- a/nifi-toolkit/nifi-toolkit-cli/src/main/java/org/apache/nifi/toolkit/cli/impl/command/nifi/params/DeleteParam.java +++ b/nifi-toolkit/nifi-toolkit-cli/src/main/java/org/apache/nifi/toolkit/cli/impl/command/nifi/params/DeleteParam.java @@ -61,6 +61,9 @@ public class DeleteParam extends AbstractUpdateParamContextCommand { final String paramContextId = getRequiredArg(properties, CommandOption.PARAM_CONTEXT_ID); final String paramName = getRequiredArg(properties, CommandOption.PARAM_NAME); + // Optional args... + final int updateTimeout = getUpdateTimeout(properties); + // Ensure the context exists... final ParamContextClient paramContextClient = client.getParamContextClient(); final ParameterContextEntity existingEntity = paramContextClient.getParamContext(paramContextId, false); @@ -95,7 +98,7 @@ public class DeleteParam extends AbstractUpdateParamContextCommand { // Submit the update request... final ParameterContextUpdateRequestEntity updateRequestEntity = paramContextClient.updateParamContext(updatedParameterContextEntity); - performUpdate(paramContextClient, updatedParameterContextEntity, updateRequestEntity); + performUpdate(paramContextClient, updatedParameterContextEntity, updateRequestEntity, updateTimeout); if (isInteractive()) { println(); diff --git a/nifi-toolkit/nifi-toolkit-cli/src/main/java/org/apache/nifi/toolkit/cli/impl/command/nifi/params/MergeParamContext.java b/nifi-toolkit/nifi-toolkit-cli/src/main/java/org/apache/nifi/toolkit/cli/impl/command/nifi/params/MergeParamContext.java index 872a5d29eb..610f558d91 100644 --- a/nifi-toolkit/nifi-toolkit-cli/src/main/java/org/apache/nifi/toolkit/cli/impl/command/nifi/params/MergeParamContext.java +++ b/nifi-toolkit/nifi-toolkit-cli/src/main/java/org/apache/nifi/toolkit/cli/impl/command/nifi/params/MergeParamContext.java @@ -63,6 +63,9 @@ public class MergeParamContext extends AbstractUpdateParamContextCommand { addOption(CommandOption.PARAM_DESC.createOption()); addOption(CommandOption.PARAM_VALUE.createOption()); addOption(CommandOption.PARAM_SENSITIVE.createOption()); + addOption(CommandOption.UPDATE_TIMEOUT.createOption()); } @Override @@ -73,6 +74,7 @@ public class SetParam extends AbstractUpdateParamContextCommand { if (!StringUtils.isBlank(paramSensitive) && !"true".equals(paramSensitive) && !"false".equals(paramSensitive)) { throw new IllegalArgumentException("Parameter sensitive flag must be one of 'true' or 'false'"); } + final int updateTimeout = getUpdateTimeout(properties); // Ensure the context exists... final ParamContextClient paramContextClient = client.getParamContextClient(); @@ -126,7 +128,7 @@ public class SetParam extends AbstractUpdateParamContextCommand { // Submit the update request... final ParameterContextUpdateRequestEntity updateRequestEntity = paramContextClient.updateParamContext(updatedParameterContextEntity); - performUpdate(paramContextClient, updatedParameterContextEntity, updateRequestEntity); + performUpdate(paramContextClient, updatedParameterContextEntity, updateRequestEntity, updateTimeout); if (isInteractive()) { println();