NIFI-3972: Ensure that we wait until service state becomes enabled before triggering completable future that says that it's enabled

This commit is contained in:
Mark Payne 2017-05-24 16:15:18 -04:00 committed by joewitt
parent 44fdc0e4ef
commit eb25c8547a
1 changed files with 7 additions and 2 deletions

View File

@ -407,12 +407,13 @@ public class StandardControllerServiceNode extends AbstractConfiguredComponent i
ReflectionUtils.invokeMethodsWithAnnotation(OnEnabled.class, getControllerServiceImplementation(), configContext); ReflectionUtils.invokeMethodsWithAnnotation(OnEnabled.class, getControllerServiceImplementation(), configContext);
} }
future.complete(null);
boolean shouldEnable = false; boolean shouldEnable = false;
synchronized (active) { synchronized (active) {
shouldEnable = active.get() && stateRef.compareAndSet(ControllerServiceState.ENABLING, ControllerServiceState.ENABLED); shouldEnable = active.get() && stateRef.compareAndSet(ControllerServiceState.ENABLING, ControllerServiceState.ENABLED);
} }
future.complete(null);
if (!shouldEnable) { if (!shouldEnable) {
LOG.debug("Disabling service " + this + " after it has been enabled due to disable action being initiated."); LOG.debug("Disabling service " + this + " after it has been enabled due to disable action being initiated.");
// Can only happen if user initiated DISABLE operation before service finished enabling. It's state will be // Can only happen if user initiated DISABLE operation before service finished enabling. It's state will be
@ -421,6 +422,8 @@ public class StandardControllerServiceNode extends AbstractConfiguredComponent i
stateRef.set(ControllerServiceState.DISABLED); stateRef.set(ControllerServiceState.DISABLED);
} }
} catch (Exception e) { } catch (Exception e) {
future.completeExceptionally(e);
final Throwable cause = e instanceof InvocationTargetException ? e.getCause() : e; final Throwable cause = e instanceof InvocationTargetException ? e.getCause() : e;
final ComponentLog componentLog = new SimpleProcessLogger(getIdentifier(), StandardControllerServiceNode.this); final ComponentLog componentLog = new SimpleProcessLogger(getIdentifier(), StandardControllerServiceNode.this);
componentLog.error("Failed to invoke @OnEnabled method due to {}", cause); componentLog.error("Failed to invoke @OnEnabled method due to {}", cause);
@ -438,6 +441,8 @@ public class StandardControllerServiceNode extends AbstractConfiguredComponent i
} }
} }
}); });
} else {
future.complete(null);
} }
return future; return future;