http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/f7f03580/api/src/main/java/brooklyn/management/TaskAdaptable.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/brooklyn/management/TaskAdaptable.java b/api/src/main/java/brooklyn/management/TaskAdaptable.java deleted file mode 100644 index c3d4e23..0000000 --- a/api/src/main/java/brooklyn/management/TaskAdaptable.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package brooklyn.management; - -/** marker interface for something which can be adapted to a task */ -public interface TaskAdaptable<T> { - Task<T> asTask(); -}
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/f7f03580/api/src/main/java/brooklyn/management/TaskFactory.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/brooklyn/management/TaskFactory.java b/api/src/main/java/brooklyn/management/TaskFactory.java deleted file mode 100644 index 0f4c828..0000000 --- a/api/src/main/java/brooklyn/management/TaskFactory.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package brooklyn.management; - - -/** Interface for something which can generate tasks (or task wrappers) */ -public interface TaskFactory<T extends TaskAdaptable<?>> { - T newTask(); -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/f7f03580/api/src/main/java/brooklyn/management/TaskQueueingContext.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/brooklyn/management/TaskQueueingContext.java b/api/src/main/java/brooklyn/management/TaskQueueingContext.java deleted file mode 100644 index 65faea4..0000000 --- a/api/src/main/java/brooklyn/management/TaskQueueingContext.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package brooklyn.management; - -import java.util.List; - -import brooklyn.util.time.Duration; - -import com.google.common.annotations.Beta; - -/** - * Marks a place where tasks can be added, e.g. a task which allows children to be added (including after it is activated); - * if the implementer of this is also a task, then it may be picked up by hierarchical methods (e.g. in DynamicTasks). - * - * @since 0.6.0 - */ -@Beta -public interface TaskQueueingContext { - - /** queues the task for submission as part of this queueing context - * <p> - * implementations should mark it as queued but not yet submitted. - * note the task may have already been submitted, and is being queued here for informational purposes, - * in which case the implementation should not run it. */ - public void queue(Task<?> t); - - /** returns a list of queued tasks (immutable copy) */ - public List<Task<?>> getQueue(); - - /** Drains the task queue for this context to complete, ie waits for this context to complete (or terminate early) - * @param optionalTimeout null to run forever - * @param includePrimaryThread whether the parent (this context) should also be joined on; - * should only be true if invoking this from another task, as otherwise it will be waiting for itself! - * @param throwFirstError whether to throw the first exception encountered - * <p> - * Also note that this waits on tasks so that blocking details on the caller are meaningful. - */ - public void drain(Duration optionalTimeout, boolean includePrimaryThread, boolean throwFirstError); - - /** Returns the task which is this queueing context */ - public Task<?> asTask(); - - /** causes subsequent children failures not to fail the parent */ - public void swallowChildrenFailures(); - -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/f7f03580/api/src/main/java/brooklyn/management/TaskWrapper.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/brooklyn/management/TaskWrapper.java b/api/src/main/java/brooklyn/management/TaskWrapper.java deleted file mode 100644 index 5c885ec..0000000 --- a/api/src/main/java/brooklyn/management/TaskWrapper.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package brooklyn.management; - -/** - * Interface for something which is not a task, but which is closely linked to one, ie. it has a task. - * - * @since 0.6.0 - */ -public interface TaskWrapper<T> extends TaskAdaptable<T> { - Task<T> getTask(); -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/f7f03580/api/src/main/java/brooklyn/management/classloading/BrooklynClassLoadingContext.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/brooklyn/management/classloading/BrooklynClassLoadingContext.java b/api/src/main/java/brooklyn/management/classloading/BrooklynClassLoadingContext.java deleted file mode 100644 index cf4c320..0000000 --- a/api/src/main/java/brooklyn/management/classloading/BrooklynClassLoadingContext.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package brooklyn.management.classloading; - -import java.net.URL; - -import javax.annotation.Nullable; - -import brooklyn.management.ManagementContext; -import brooklyn.util.guava.Maybe; - -/** - * Provides functionality for loading classes based on the current context - * (e.g. catalog item, entity, etc). - */ -public interface BrooklynClassLoadingContext { - - public ManagementContext getManagementContext(); - public Class<?> loadClass(String className); - public <T> Class<? extends T> loadClass(String className, @Nullable Class<T> supertype); - - public Maybe<Class<?>> tryLoadClass(String className); - public <T> Maybe<Class<? extends T>> tryLoadClass(String className, @Nullable Class<T> supertype); - - /** As {@link ClassLoader#getResource(String)} */ - public URL getResource(String name); - - /** - * As {@link ClassLoader#getResources(String)} but returning an {@link Iterable} rather than - * an {@link java.util.Enumeration}. - */ - public Iterable<URL> getResources(String name); - -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/f7f03580/api/src/main/java/brooklyn/management/entitlement/EntitlementClass.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/brooklyn/management/entitlement/EntitlementClass.java b/api/src/main/java/brooklyn/management/entitlement/EntitlementClass.java deleted file mode 100644 index bc44c67..0000000 --- a/api/src/main/java/brooklyn/management/entitlement/EntitlementClass.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package brooklyn.management.entitlement; - -import com.google.common.reflect.TypeToken; - -/** @see EntitlementManager */ -public interface EntitlementClass<T> { - String entitlementClassIdentifier(); - TypeToken<T> entitlementClassArgumentType(); -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/f7f03580/api/src/main/java/brooklyn/management/entitlement/EntitlementContext.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/brooklyn/management/entitlement/EntitlementContext.java b/api/src/main/java/brooklyn/management/entitlement/EntitlementContext.java deleted file mode 100644 index 8cac063..0000000 --- a/api/src/main/java/brooklyn/management/entitlement/EntitlementContext.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package brooklyn.management.entitlement; - -/** @see EntitlementManager */ -public interface EntitlementContext { - String user(); -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/f7f03580/api/src/main/java/brooklyn/management/entitlement/EntitlementManager.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/brooklyn/management/entitlement/EntitlementManager.java b/api/src/main/java/brooklyn/management/entitlement/EntitlementManager.java deleted file mode 100644 index 76b0a33..0000000 --- a/api/src/main/java/brooklyn/management/entitlement/EntitlementManager.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package brooklyn.management.entitlement; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -import com.google.common.annotations.Beta; - -/** - * Entitlement lookup relies on: - * <li>an "entitlement context", consisting of at minimum a string identifier of the user/actor for which entitlement is being requested - * <li>an "entitlement class", representing the category of activity for which entitlement is being requested - * <li>an "entitlement class argument", representing the specifics of the activity for which entitlement is being requested - * <p> - * Instances of this class typically have a 1-arg constructor taking a BrooklynProperties object - * (configuration injected by the Brooklyn framework) - * or a 0-arg constructor (if no external configuration is needed). - * <p> - * An EntitlementManagerAdapter class is available to do dispatch to common methods. - * <p> - * Instantiation is done e.g. by Entitlements.newManager. - * @since 0.7.0 */ -@Beta -public interface EntitlementManager { - - public <T> boolean isEntitled(@Nullable EntitlementContext context, @Nonnull EntitlementClass<T> entitlementClass, @Nullable T entitlementClassArgument); - -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/f7f03580/api/src/main/java/brooklyn/management/ha/HighAvailabilityManager.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/brooklyn/management/ha/HighAvailabilityManager.java b/api/src/main/java/brooklyn/management/ha/HighAvailabilityManager.java deleted file mode 100644 index a525601..0000000 --- a/api/src/main/java/brooklyn/management/ha/HighAvailabilityManager.java +++ /dev/null @@ -1,129 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package brooklyn.management.ha; - -import java.util.Map; - -import com.google.common.annotations.Beta; -import com.google.common.annotations.VisibleForTesting; - -/** - * Monitors other management nodes (via the {@link ManagementPlaneSyncRecordPersister}) to detect - * if the current master has failed or stopped. If so, then deterministically chooses a new master. - * If that master is self, then promotes. - - * Users are not expected to implement this class, or to call methods on it directly. - * - * Expected lifecycle of methods calls on this is: - * <ol> - * <li>{@link #setPersister(ManagementPlaneSyncRecordPersister)} - * <li>Exactly one of {@link #disabled()} or {@link #start(HighAvailabilityMode)} - * <li>{@link #stop()} - * </ol> - * - * @since 0.7.0 - */ -@Beta -public interface HighAvailabilityManager { - - ManagementNodeState getNodeState(); - - /** The time in milliseconds when the state was last changed. -1 if no state transition has occurred yet.*/ - long getLastStateChange(); - - /** - * @param persister - * @return self - */ - HighAvailabilityManager setPersister(ManagementPlaneSyncRecordPersister persister); - - /** - * Indicates that HA is disabled: this node will act as the only management node in this management plane, - * and will not persist HA meta-information (meaning other nodes cannot join). - * <p> - * Subsequently can expect {@link #getNodeState()} to be {@link ManagementNodeState#MASTER} - * and {@link #loadManagementPlaneSyncRecord(boolean)} to show just this one node -- - * as if it were running HA with just one node -- - * but {@link #isRunning()} will return false. - * <p> - * Currently this method is intended to be called early in the lifecycle, - * instead of {@link #start(HighAvailabilityMode)}. It may be an error if - * this is called after this HA Manager is started. - */ - @Beta - void disabled(); - - /** Whether HA mode is operational */ - boolean isRunning(); - - /** - * Starts the monitoring of other nodes (and thus potential promotion of this node from standby to master). - * <p> - * When this method returns, the status of this node will be set, - * either {@link ManagementNodeState#MASTER} if appropriate - * or {@link ManagementNodeState#STANDBY} / {@link ManagementNodeState#HOT_STANDBY} / {@link ManagementNodeState#HOT_BACKUP}. - * - * @param startMode mode to start with - * @throws IllegalStateException if current state of the management-plane doesn't match that desired by {@code startMode} - */ - void start(HighAvailabilityMode startMode); - - /** - * Stops this node, then publishes own status (via {@link ManagementPlaneSyncRecordPersister} of {@link ManagementNodeState#TERMINATED}. - */ - void stop(); - - /** changes the mode that this HA server is running in - * <p> - * note it will be an error to {@link #changeMode(HighAvailabilityMode)} to {@link ManagementNodeState#MASTER} - * when there is already a master; to promote a node explicitly set its priority higher than - * the others and invoke {@link #changeMode(HighAvailabilityMode)} to a standby mode on the existing master */ - void changeMode(HighAvailabilityMode mode); - - /** sets the priority, and publishes it synchronously so it is canonical */ - void setPriority(long priority); - - long getPriority(); - - /** deletes non-master node records; active nodes (including this) will republish, - * so this provides a simple way to clean out the cache of dead brooklyn nodes */ - @Beta - void publishClearNonMaster(); - - /** - * Returns a snapshot of the management-plane's current / most-recently-known status, - * as last read from {@link #loadManagementPlaneSyncRecord(boolean)}, or null if none read. - */ - ManagementPlaneSyncRecord getLastManagementPlaneSyncRecord(); - - /** - * @param useLocalKnowledgeForThisNode - if true, the record for this mgmt node will be replaced with the - * actual current status known in this JVM (may be more recent than what is persisted); - * for most purposes there is little difference but in some cases the local node being updated - * may be explicitly wanted or not wanted - */ - ManagementPlaneSyncRecord loadManagementPlaneSyncRecord(boolean useLocalKnowledgeForThisNode); - - @VisibleForTesting - ManagementPlaneSyncRecordPersister getPersister(); - - /** Returns a collection of metrics */ - Map<String,Object> getMetrics(); - -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/f7f03580/api/src/main/java/brooklyn/management/ha/HighAvailabilityMode.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/brooklyn/management/ha/HighAvailabilityMode.java b/api/src/main/java/brooklyn/management/ha/HighAvailabilityMode.java deleted file mode 100644 index 2e593b1..0000000 --- a/api/src/main/java/brooklyn/management/ha/HighAvailabilityMode.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package brooklyn.management.ha; - -/** Specifies the HA mode that a mgmt node should run in */ -public enum HighAvailabilityMode { - /** - * Means HA mode should not be operational. - * <p> - * When specified for the initial HA mode, this simply turns off HA. - * <p> - * However if being used to {@link HighAvailabilityManager#changeMode(HighAvailabilityMode)}, - * this will cause the node to transition to a {@link ManagementNodeState#FAILED} state. - * Switching to a "master-but-not-part-of-HA" state is not currently supported, as this would be - * problematic if another node (which was part of HA) then tries to become master, - * and the more common use of this at runtime is to disable a node from being part of the HA plane. - */ - DISABLED, - - /** - * Means auto-detect whether to be master or standby; if there is already a master then start as standby, - * otherwise start as master. - */ - AUTO, - - /** - * Means node must be lukewarm standby; if there is not already a master then fail fast on startup. - * See {@link ManagementNodeState#STANDBY}. - */ - STANDBY, - - /** - * Means node must be hot standby; if there is not already a master then fail fast on startup. - * See {@link ManagementNodeState#HOT_STANDBY}. - */ - HOT_STANDBY, - - /** - * Means node must be hot backup; do not attempt to become master (but it <i>can</i> start without a master). - * See {@link ManagementNodeState#HOT_BACKUP}. - */ - HOT_BACKUP, - - /** - * Means node must be master; if there is already a master then fail fast on startup. - * See {@link ManagementNodeState#MASTER}. - */ - // TODO when multi-master supported we will of course not fail fast on startup when there is already a master; - // instead the responsibility for master entities will be divided among masters - MASTER; -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/f7f03580/api/src/main/java/brooklyn/management/ha/ManagementNodeState.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/brooklyn/management/ha/ManagementNodeState.java b/api/src/main/java/brooklyn/management/ha/ManagementNodeState.java deleted file mode 100644 index ac54ef2..0000000 --- a/api/src/main/java/brooklyn/management/ha/ManagementNodeState.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package brooklyn.management.ha; - -import brooklyn.util.guava.Maybe; - -public enum ManagementNodeState { - /** Node is either coming online, or is in some kind of recovery/transitioning mode */ - INITIALIZING, - - /** Node is in "lukewarm standby" mode, where it is available to be promoted to master, - * but does not have entities loaded and will require some effort to be promoted */ - STANDBY, - /** Node is acting as read-only proxy available to be promoted to master on existing master failure */ - HOT_STANDBY, - /** Node is acting as a read-only proxy but not making itself available for promotion to master */ - HOT_BACKUP, - /** Node is running as primary/master, able to manage entities and create new ones */ - // the semantics are intended to support multi-master here; we could have multiple master nodes, - // but we need to look up who is master for any given entity - MASTER, - - /** Node has failed and requires maintenance attention */ - FAILED, - /** Node has gone away; maintenance not possible */ - TERMINATED; - - /** Converts a {@link HighAvailabilityMode} to a {@link ManagementNodeState}, if possible */ - public static Maybe<ManagementNodeState> of(HighAvailabilityMode startMode) { - switch (startMode) { - case AUTO: - case DISABLED: - return Maybe.absent("Requested "+HighAvailabilityMode.class+" mode "+startMode+" cannot be converted to "+ManagementNodeState.class); - case HOT_BACKUP: - return Maybe.of(HOT_BACKUP); - case HOT_STANDBY: - return Maybe.of(HOT_STANDBY); - case MASTER: - return Maybe.of(MASTER); - case STANDBY: - return Maybe.of(STANDBY); - } - // above should be exhaustive - return Maybe.absent("Requested "+HighAvailabilityMode.class+" mode "+startMode+" was not expected"); - } - - /** true for hot non-master modes, where we are proxying the data from the persistent store */ - public static boolean isHotProxy(ManagementNodeState state) { - return state==HOT_BACKUP || state==HOT_STANDBY; - } - - /** true for non-master modes which can be promoted to master */ - public static boolean isStandby(ManagementNodeState state) { - return state==ManagementNodeState.STANDBY || state==ManagementNodeState.HOT_STANDBY; - } -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/f7f03580/api/src/main/java/brooklyn/management/ha/ManagementNodeSyncRecord.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/brooklyn/management/ha/ManagementNodeSyncRecord.java b/api/src/main/java/brooklyn/management/ha/ManagementNodeSyncRecord.java deleted file mode 100644 index 38150bf..0000000 --- a/api/src/main/java/brooklyn/management/ha/ManagementNodeSyncRecord.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package brooklyn.management.ha; - -import java.net.URI; - -import com.google.common.annotations.Beta; - -/** - * Represents the state of a management-node. - * - * @see {@link ManagementPlaneSyncRecord#getManagementNodes()} - * - * @since 0.7.0 - * - * @author aled - */ -@Beta -public interface ManagementNodeSyncRecord { - - // TODO Not setting URI currently; ManagementContext doesn't know its URI; only have one if web-console was enabled. - - // TODO Add getPlaneId(); but first need to set it in a sensible way - - String getBrooklynVersion(); - - String getNodeId(); - - URI getUri(); - - ManagementNodeState getStatus(); - - Long getPriority(); - - /** timestamp set by the originating management machine */ - long getLocalTimestamp(); - - /** timestamp set by shared persistent store, if available - * <p> - * this will not be set on records originating at this machine, nor will it be persisted, - * but it will be populated for records being read */ - Long getRemoteTimestamp(); - - String toVerboseString(); - -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/f7f03580/api/src/main/java/brooklyn/management/ha/ManagementPlaneSyncRecord.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/brooklyn/management/ha/ManagementPlaneSyncRecord.java b/api/src/main/java/brooklyn/management/ha/ManagementPlaneSyncRecord.java deleted file mode 100644 index f70e3e3..0000000 --- a/api/src/main/java/brooklyn/management/ha/ManagementPlaneSyncRecord.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package brooklyn.management.ha; - -import java.util.Map; - -import org.apache.brooklyn.mementos.BrooklynMemento; -import org.apache.brooklyn.mementos.BrooklynMementoPersister; - -import com.google.common.annotations.Beta; - -/** - * Meta-data about the management plane - the management nodes and who is currently master. - * Does not contain any data about the entities under management. - * <p> - * This is very similar to how {@link BrooklynMemento} is used by {@link BrooklynMementoPersister}, - * but it is not a memento in the sense it does not reconstitute the entire management plane - * (so is not called Memento although it can be used by the same memento-serializers). - * - * @since 0.7.0 - * - * @author aled - */ -@Beta -public interface ManagementPlaneSyncRecord { - - // TODO Add getPlaneId(); but first need to set it sensibly on each management node - - String getMasterNodeId(); - - /** returns map of {@link ManagementNodeSyncRecord} instances keyed by the nodes' IDs */ - Map<String, ManagementNodeSyncRecord> getManagementNodes(); - - String toVerboseString(); -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/f7f03580/api/src/main/java/brooklyn/management/ha/ManagementPlaneSyncRecordPersister.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/brooklyn/management/ha/ManagementPlaneSyncRecordPersister.java b/api/src/main/java/brooklyn/management/ha/ManagementPlaneSyncRecordPersister.java deleted file mode 100644 index 922deaa..0000000 --- a/api/src/main/java/brooklyn/management/ha/ManagementPlaneSyncRecordPersister.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package brooklyn.management.ha; - -import java.io.IOException; -import java.util.Collection; -import java.util.concurrent.TimeoutException; - -import org.apache.brooklyn.mementos.BrooklynMementoPersister; - -import brooklyn.util.time.Duration; - -import com.google.common.annotations.Beta; -import com.google.common.annotations.VisibleForTesting; - -/** - * Controls the persisting and reading back of mementos relating to the management plane. - * This state does not relate to the entities being managed. - * - * @see {@link HighAvailabilityManager#setPersister(ManagementPlaneSyncRecordPersister)} for its use in management-node failover - * - * @since 0.7.0 - */ -@Beta -public interface ManagementPlaneSyncRecordPersister { - - /** - * Analogue to {@link BrooklynMementoPersister#loadMemento(org.apache.brooklyn.mementos.BrooklynMementoPersister.LookupContext)} - * <p> - * Note that this method is *not* thread safe. - */ - ManagementPlaneSyncRecord loadSyncRecord() throws IOException; - - void delta(Delta delta); - - void stop(); - - @VisibleForTesting - void waitForWritesCompleted(Duration timeout) throws InterruptedException, TimeoutException; - - public interface Delta { - public enum MasterChange { - NO_CHANGE, - SET_MASTER, - CLEAR_MASTER - } - Collection<ManagementNodeSyncRecord> getNodes(); - Collection<String> getRemovedNodeIds(); - MasterChange getMasterChange(); - String getNewMasterOrNull(); - String getExpectedMasterToClear(); - } -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/f7f03580/api/src/main/java/brooklyn/management/ha/MementoCopyMode.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/brooklyn/management/ha/MementoCopyMode.java b/api/src/main/java/brooklyn/management/ha/MementoCopyMode.java deleted file mode 100644 index 83c37f5..0000000 --- a/api/src/main/java/brooklyn/management/ha/MementoCopyMode.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package brooklyn.management.ha; - -public enum MementoCopyMode { - /** Use items currently managed at this node */ - LOCAL, - /** Use items as stored in the remote persistence store */ - REMOTE, - /** Auto-detect whether to use {@link #LOCAL} or {@link #REMOTE} depending on the - * HA mode of this management node (usually {@link #LOCAL} for master and {@link #REMOTE} otherwise)*/ - AUTO -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/f7f03580/api/src/main/java/org/apache/brooklyn/management/AccessController.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/org/apache/brooklyn/management/AccessController.java b/api/src/main/java/org/apache/brooklyn/management/AccessController.java new file mode 100644 index 0000000..a7d8a4f --- /dev/null +++ b/api/src/main/java/org/apache/brooklyn/management/AccessController.java @@ -0,0 +1,65 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.brooklyn.management; + +import brooklyn.entity.Entity; +import brooklyn.location.Location; + +import com.google.common.annotations.Beta; + +@Beta +public interface AccessController { + + // TODO Expect this class' methods to change, e.g. including the user doing the + // provisioning or the provisioning parameters such as jurisdiction + + public static class Response { + private static final Response ALLOWED = new Response(true, ""); + + public static Response allowed() { + return ALLOWED; + } + + public static Response disallowed(String msg) { + return new Response(false, msg); + } + + private final boolean allowed; + private final String msg; + + private Response(boolean allowed, String msg) { + this.allowed = allowed; + this.msg = msg; + } + + public boolean isAllowed() { + return allowed; + } + + public String getMsg() { + return msg; + } + } + + public Response canProvisionLocation(Location provisioner); + + public Response canManageLocation(Location loc); + + public Response canManageEntity(Entity entity); +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/f7f03580/api/src/main/java/org/apache/brooklyn/management/EntityManager.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/org/apache/brooklyn/management/EntityManager.java b/api/src/main/java/org/apache/brooklyn/management/EntityManager.java new file mode 100644 index 0000000..0c784d9 --- /dev/null +++ b/api/src/main/java/org/apache/brooklyn/management/EntityManager.java @@ -0,0 +1,127 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.brooklyn.management; + +import java.util.Collection; +import java.util.Map; + +import javax.annotation.Nullable; + +import org.apache.brooklyn.policy.Enricher; +import org.apache.brooklyn.policy.EnricherSpec; +import org.apache.brooklyn.policy.Policy; +import org.apache.brooklyn.policy.PolicySpec; + +import brooklyn.entity.Application; +import brooklyn.entity.Entity; +import brooklyn.entity.proxying.EntitySpec; +import brooklyn.entity.proxying.EntityTypeRegistry; + +import com.google.common.base.Predicate; + +/** + * For managing and querying entities. + */ +public interface EntityManager { + + /** + * Returns the type registry, used to identify the entity implementation when instantiating an + * entity of a given type. + * + * @see EntityManager.createEntity(EntitySpec) + */ + EntityTypeRegistry getEntityTypeRegistry(); + + /** + * Creates a new (unmanaged) entity. + * + * @param spec + * @return A proxy to the created entity (rather than the actual entity itself). + */ + <T extends Entity> T createEntity(EntitySpec<T> spec); + + /** + * Convenience (particularly for groovy code) to create an entity. + * Equivalent to {@code createEntity(EntitySpec.create(type).configure(config))} + * + * @see createEntity(EntitySpec) + */ + <T extends Entity> T createEntity(Map<?,?> config, Class<T> type); + + /** + * Creates a new policy (not managed; not associated with any entity). + * + * @param spec + */ + <T extends Policy> T createPolicy(PolicySpec<T> spec); + + /** + * Creates a new enricher (not managed; not associated with any entity). + * + * @param spec + */ + <T extends Enricher> T createEnricher(EnricherSpec<T> spec); + + /** + * All entities under control of this management plane + */ + Collection<Entity> getEntities(); + + /** + * All entities managed as part of the given application + */ + Collection<Entity> getEntitiesInApplication(Application application); + + /** + * All entities under control of this management plane that match the given filter + */ + Collection<Entity> findEntities(Predicate<? super Entity> filter); + + /** + * All entities managed as part of the given application that match the given filter + */ + Collection<Entity> findEntitiesInApplication(Application application, Predicate<? super Entity> filter); + + /** + * Returns the entity with the given identifier (may be a full instance, or a proxy to one which is remote), + * or null. + */ + @Nullable Entity getEntity(String id); + + /** whether the entity is under management by this management context */ + boolean isManaged(Entity entity); + + /** + * Begins management for the given entity and its children, recursively. + * + * depending on the implementation of the management context, + * this might push it out to one or more remote management nodes. + * Manage an entity. + */ + // TODO manage and unmanage without arguments should be changed to take an explicit ManagementTransitionMode + // (but that class is not currently in the API project) + void manage(Entity e); + + /** + * Causes the given entity and its children, recursively, to be removed from the management plane + * (for instance because the entity is no longer relevant) + */ + void unmanage(Entity e); + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/f7f03580/api/src/main/java/org/apache/brooklyn/management/ExecutionContext.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/org/apache/brooklyn/management/ExecutionContext.java b/api/src/main/java/org/apache/brooklyn/management/ExecutionContext.java new file mode 100644 index 0000000..f783619 --- /dev/null +++ b/api/src/main/java/org/apache/brooklyn/management/ExecutionContext.java @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.brooklyn.management; + +import java.util.Map; +import java.util.Set; +import java.util.concurrent.Callable; +import java.util.concurrent.Executor; + +import brooklyn.entity.Entity; + +/** + * This is a Brooklyn extension to the Java {@link Executor}. + * + * The "context" could, for example, be an {@link Entity} so that tasks executed + * can be annotated as executing in that context. + */ +public interface ExecutionContext extends Executor { + + /** + * Get the tasks executed through this context (returning an immutable set). + */ + Set<Task<?>> getTasks(); + + /** + * See {@link ExecutionManager#submit(Map, TaskAdaptable)} for properties that can be passed in. + */ + Task<?> submit(Map<?,?> properties, Runnable runnable); + + /** + * See {@link ExecutionManager#submit(Map, TaskAdaptable)} for properties that can be passed in. + */ + <T> Task<T> submit(Map<?,?> properties, Callable<T> callable); + + /** {@link ExecutionManager#submit(Runnable) */ + Task<?> submit(Runnable runnable); + + /** {@link ExecutionManager#submit(Callable) */ + <T> Task<T> submit(Callable<T> callable); + + /** See {@link ExecutionManager#submit(Map, TaskAdaptable)}. */ + <T> Task<T> submit(TaskAdaptable<T> task); + + /** + * See {@link ExecutionManager#submit(Map, TaskAdaptable)} for properties that can be passed in. + */ + <T> Task<T> submit(Map<?,?> properties, TaskAdaptable<T> task); + + boolean isShutdown(); + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/f7f03580/api/src/main/java/org/apache/brooklyn/management/ExecutionManager.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/org/apache/brooklyn/management/ExecutionManager.java b/api/src/main/java/org/apache/brooklyn/management/ExecutionManager.java new file mode 100644 index 0000000..431faa5 --- /dev/null +++ b/api/src/main/java/org/apache/brooklyn/management/ExecutionManager.java @@ -0,0 +1,117 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.brooklyn.management; + +import java.util.Collection; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; + +import brooklyn.entity.Entity; + +/** + * This class manages the execution of a number of jobs with tags. + * + * It is like an executor service (and it ends up delegating to one) but adds additional support + * where jobs can be: + * <ul> + * <li>Tracked with tags/buckets + * <li>Be {@link Runnable}s, {@link Callable}s, or {@link groovy.lang.Closure}s + * <li>Remembered after completion + * <li>Treated as {@link Task} instances (see below) + * <li>Given powerful synchronization capabilities + * </ul> + * <p> + * The advantage of treating them as {@link Task} instances include: + * <ul> + * <li>Richer status information + * <li>Started-by, contained-by relationships automatically remembered + * <li>Runtime metadata (start, stop, etc) + * <li>Grid and multi-machine support) + * </ul> + * <p> + * For usage instructions see {@link #submit(Map, TaskAdaptable)}, and for examples see the various + * {@code ExecutionTest} and {@code TaskTest} instances. + * <p> + * It has been developed for multi-location provisioning and management to track work being + * done by each {@link Entity}. + * <p> + * Note the use of the environment variable {@code THREAD_POOL_SIZE} which is used to size + * the {@link ExecutorService} thread pool. The default is calculated as twice the number + * of CPUs in the system plus two, giving 10 for a four core system, 18 for an eight CPU + * server and so on. + */ +public interface ExecutionManager { + public boolean isShutdown(); + + /** returns the task with the given ID, or null if none */ + public Task<?> getTask(String id); + + /** returns all tasks with the given tag (immutable) */ + public Set<Task<?>> getTasksWithTag(Object tag); + + /** returns all tasks that have any of the given tags (immutable) */ + public Set<Task<?>> getTasksWithAnyTag(Iterable<?> tags); + + /** returns all tasks that have all of the given tags (immutable) */ + public Set<Task<?>> getTasksWithAllTags(Iterable<?> tags); + + /** returns all tags known to this manager (immutable) */ + public Set<Object> getTaskTags(); + +// /** returns all tasks known to this manager (immutable) */ +// public Set<Task<?>> getAllTasks(); + + /** see {@link #submit(Map, TaskAdaptable)} */ + public Task<?> submit(Runnable r); + + /** see {@link #submit(Map, TaskAdaptable)} */ + public <T> Task<T> submit(Callable<T> c); + + /** see {@link #submit(Map, TaskAdaptable)} */ + public <T> Task<T> submit(TaskAdaptable<T> task); + + /** see {@link #submit(Map, TaskAdaptable)} */ + public Task<?> submit(Map<?, ?> flags, Runnable r); + + /** see {@link #submit(Map, TaskAdaptable)} */ + public <T> Task<T> submit(Map<?, ?> flags, Callable<T> c); + + /** + * Submits the given {@link Task} for execution in the context associated with this manager. + * + * The following optional flags supported (in the optional map first arg): + * <ul> + * <li><em>tag</em> - A single object to be used as a tag for looking up the task + * <li><em>tags</em> - A {@link Collection} of object tags each of which the task should be associated, + * used for associating with contexts, mutex execution, and other purposes + * <li><em>synchId</em> - A string, or {@link Collection} of strings, representing a category on which an object should own a synch lock + * <li><em>synchObj</em> - A string, or {@link Collection} of strings, representing a category on which an object should own a synch lock + * <li><em>newTaskStartCallback</em> - A {@link groovy.lang.Closure} that will be invoked just before the task starts if it starts as a result of this call + * <li><em>newTaskEndCallback</em> - A {@link groovy.lang.Closure} that will be invoked when the task completes if it starts as a result of this call + * </ul> + * Callbacks run in the task's thread, and if the callback is a {@link groovy.lang.Closure} it is passed the task for convenience. The closure can be any of the + * following types; either a {@link groovy.lang.Closure}, {@link Runnable} or {@link Callable}. + * <p> + * If a Map is supplied it must be modifiable (currently; may allow immutable maps in future). + */ + public <T> Task<T> submit(Map<?, ?> flags, TaskAdaptable<T> task); + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/f7f03580/api/src/main/java/org/apache/brooklyn/management/HasTaskChildren.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/org/apache/brooklyn/management/HasTaskChildren.java b/api/src/main/java/org/apache/brooklyn/management/HasTaskChildren.java new file mode 100644 index 0000000..d8d5c57 --- /dev/null +++ b/api/src/main/java/org/apache/brooklyn/management/HasTaskChildren.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.brooklyn.management; + +import com.google.common.annotations.Beta; + +/** + * Interface marks tasks which have explicit children, + * typically where the task defines the ordering of running those children tasks + * <p> + * The {@link Task#getSubmittedByTask()} on the child will typically return the parent, + * but note there are other means of submitting tasks (e.g. background, in the same {@link ExecutionContext}), + * where the submitter has no API reference to the submitted tasks. + * <p> + * In general the children mechanism is preferred as it is easier to navigate + * (otherwise you have to scan the {@link ExecutionContext} to find tasks submitted by a task). + */ +@Beta // in 0.6.0 +public interface HasTaskChildren { + + public Iterable<Task<?>> getChildren(); + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/f7f03580/api/src/main/java/org/apache/brooklyn/management/LocationManager.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/org/apache/brooklyn/management/LocationManager.java b/api/src/main/java/org/apache/brooklyn/management/LocationManager.java new file mode 100644 index 0000000..c10249c --- /dev/null +++ b/api/src/main/java/org/apache/brooklyn/management/LocationManager.java @@ -0,0 +1,87 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.brooklyn.management; + +import java.util.Collection; +import java.util.Map; + +import brooklyn.location.Location; +import brooklyn.location.LocationSpec; + +/** + * For managing and querying entities. + */ +public interface LocationManager { + + /** + * Creates a new location, which is tracked by the management context. + * + * @param spec + */ + <T extends Location> T createLocation(LocationSpec<T> spec); + + /** + * Convenience (particularly for groovy code) to create a location. + * Equivalent to {@code createLocation(LocationSpec.create(type).configure(config))} + * + * @see #createLocation(LocationSpec) + */ + <T extends Location> T createLocation(Map<?,?> config, Class<T> type); + + /** + * All locations under control of this management plane. + * + * This returns a snapshot of the current locations; it will not reflect future changes in the locations. + * If no locations are found, the collection will be empty (i.e. null is never returned). + */ + Collection<Location> getLocations(); + + /** + * Returns the location under management (e.g. in use) with the given identifier + * (e.g. random string; and different to the LocationDefinition id). + * May return a full instance, or a proxy to one which is remote. + * If no location found with that id, returns null. + */ + Location getLocation(String id); + + /** whether the location is under management by this management context */ + boolean isManaged(Location loc); + + /** + * Begins management for the given location and its children, recursively. + * + * depending on the implementation of the management context, + * this might push it out to one or more remote management nodes. + * Manage a location. + * + * @since 0.6.0 (added only for backwards compatibility, where locations are being created directly). + * @deprecated in 0.6.0; use {@link #createLocation(LocationSpec)} instead. + */ + Location manage(Location loc); + + /** + * Causes the given location and its children, recursively, to be removed from the management plane + * (for instance because the location is no longer relevant). + * + * If the given location is not managed (e.g. it has already been unmanaged) then this is a no-op + * (though it may be logged so duplicate calls are best avoided). + */ + void unmanage(Location loc); + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/f7f03580/api/src/main/java/org/apache/brooklyn/management/ManagementContext.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/org/apache/brooklyn/management/ManagementContext.java b/api/src/main/java/org/apache/brooklyn/management/ManagementContext.java new file mode 100644 index 0000000..2c8d9d1 --- /dev/null +++ b/api/src/main/java/org/apache/brooklyn/management/ManagementContext.java @@ -0,0 +1,256 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.brooklyn.management; + +import java.io.Serializable; +import java.net.URI; +import java.util.Collection; + +import brooklyn.basic.BrooklynObject; + +import org.apache.brooklyn.catalog.BrooklynCatalog; +import org.apache.brooklyn.management.entitlement.EntitlementManager; +import org.apache.brooklyn.management.ha.HighAvailabilityManager; + +import brooklyn.config.StringConfigMap; +import brooklyn.entity.Application; +import brooklyn.entity.Entity; +import brooklyn.entity.drivers.DriverDependentEntity; +import brooklyn.entity.drivers.EntityDriverManager; +import brooklyn.entity.drivers.downloads.DownloadResolverManager; +import brooklyn.entity.rebind.RebindManager; +import brooklyn.location.LocationRegistry; +import brooklyn.util.guava.Maybe; + +import com.google.common.annotations.Beta; + +/** + * This is the entry point for accessing and interacting with a realm of applications and their entities in Brooklyn. + * + * For example, policies and the management console(s) (web-app, etc) can use this to interact with entities; + * policies, web-app, and entities share the realm for subscribing to events, executing tasks, and generally co-existing. + * <p> + * It may refer to several applications, and it refers to all the entities descended from those applications. + */ +public interface ManagementContext { + + // TODO Consider separating out into a ConfigManager for methods like: + // - getConfig() + // - reloadBrooklynProperties(); + // - addPropertiesReloadListener + // - removePropertiesReloadListener + // - interface PropertiesReloadListener + + /** + * UID for the Brooklyn management plane which this {@link ManagementContext} node is a part of. + * <p> + * Each Brooklyn entity is actively managed by a unique management plane + * whose ID which should not normally change for the duration of that entity, + * even though the nodes in that plane might, and the plane may go down and come back up. + * In other words the value of {@link Application#getManagementContext()#getManagementPlaneId()} + * will generally be constant (in contrast to {@link #getManagementNodeId()}). + * <p> + * This value should not be null unless the management context is a non-functional + * (non-deployment) instance. */ + String getManagementPlaneId(); + + /** + * UID for this {@link ManagementContext} node (as part of a single management plane). + * <p> + * No two instances of {@link ManagementContext} should ever have the same node UID. + * The value of {@link Application#getManagementContext()#getManagementNodeId()} may + * change many times (in contrast to {@link #getManagementPlaneId()}). + * <p> + * This value should not be null unless the management context is a non-functional + * (non-deployment) instance. */ + String getManagementNodeId(); + + /** + * The URI that this management node's REST API is available at, or absent if the node's + * API is unavailable. + */ + Maybe<URI> getManagementNodeUri(); + + /** + * All applications under control of this management plane + */ + Collection<Application> getApplications(); + + /** + * Returns the {@link EntityManager} instance for managing/querying entities. + */ + EntityManager getEntityManager(); + + /** + * Returns the {@link ExecutionManager} instance for entities and users in this management realm + * to submit tasks and to observe what tasks are occurring + */ + ExecutionManager getExecutionManager(); + + /** + * Returns an {@link ExecutionContext} within the {@link ExecutionManager} for tasks + * associated to the Brooklyn node's operation (not any entities). + **/ + ExecutionContext getServerExecutionContext(); + + /** + * Returns the {@link EntityDriverManager} entities can use to create drivers. This + * manager can also be used to programmatically customize which driver type to use + * for entities in different locations. + * + * The default strategy for choosing a driver is to use a naming convention: + * {@link DriverDependentEntity#getDriverInterface()} returns the interface that the + * driver must implement; its name should end in "Driver". For example, this suffix is + * replaced with "SshDriver" for SshMachineLocation, for example. + */ + EntityDriverManager getEntityDriverManager(); + + /** + * Returns the {@link DownloadResolverManager} for resolving things like which URL to download an installer from. + * + * The default {@link DownloadResolverManager} will retrieve {@code entity.getAttribute(Attributes.DOWNLOAD_URL)}, + * and substitute things like "${version}" etc. + * + * However, additional resolvers can be registered to customize this behaviour (e.g. to always go to an + * enterprise's repository). + */ + DownloadResolverManager getEntityDownloadsManager(); + + /** + * Returns the {@link SubscriptionManager} instance for entities and users of this management realm + * to subscribe to sensor events (and, in the case of entities, to emit sensor events) + */ + SubscriptionManager getSubscriptionManager(); + + //TODO (Alex) I'm not sure the following two getXxxContext methods are needed on the interface; + //I expect they will only be called once, in AbstractEntity, and fully capable + //there of generating the respective contexts from the managers + //(Litmus test will be whether there is anything in FederatedManagementContext + //which requires a custom FederatedExecutionContext -- or whether BasicEC + //works with FederatedExecutionManager) + /** + * Returns an {@link ExecutionContext} instance representing tasks + * (from the {@link ExecutionManager}) associated with this entity, and capable + * of conveniently running such tasks which will be associated with that entity + */ + ExecutionContext getExecutionContext(Entity entity); + + /** + * Returns a {@link SubscriptionContext} instance representing subscriptions + * (from the {@link SubscriptionManager}) associated with this entity, and capable + * of conveniently subscribing on behalf of that entity + */ + SubscriptionContext getSubscriptionContext(Entity entity); + + @Beta // method may move to an internal interface; brooklyn users should not need to call this directly + RebindManager getRebindManager(); + + /** + * @since 0.7.0 + */ + @Beta // method may move to an internal interface; brooklyn users should not need to call this directly + HighAvailabilityManager getHighAvailabilityManager(); + + /** + * Returns the ConfigMap (e.g. BrooklynProperties) applicable to this management context. + * Defaults to reading ~/.brooklyn/brooklyn.properties but configurable in the management context. + */ + StringConfigMap getConfig(); + + /** + * Whether the management context has been initialized and not yet terminated. + * This does not mean startup is entirely completed. See also {@link #isStartupComplete()}. + */ + // TODO should we replace this with isNotYetTerminated() ?? + // and perhaps introduce isFullyRunning() which does (isStartupComplete() && isRunning()), + // and/or move to a MgmtContextStatus subclass where errors can also be stored? + public boolean isRunning(); + + /** + * Whether all startup tasks have completed. Previous to this point the management context is still usable + * (and hence {@link #isRunning()} returns true immediately after construction) + * but some subsystems (e.g. persistence, OSGi, webapps, entities started at startup) + * may not be available until this returns true. + * <p> + * Also see {@link #isStartupComplete()}. + */ + @Beta // see comment on isRunning() as items might move to a status handler + public boolean isStartupComplete(); + + /** Record of configured locations and location resolvers */ + LocationRegistry getLocationRegistry(); + + /** Record of configured Brooklyn entities (and templates and policies) which can be loaded */ + BrooklynCatalog getCatalog(); + + /** Returns the class loader to be used to load items. + * Temporary routine while catalog supports classloader-based and OSGi-based classloading. */ + @Beta + ClassLoader getCatalogClassLoader(); + + LocationManager getLocationManager(); + + /** + * For controlling access to operations - can be queried to find if an operation is allowed. + * Callers should *not* cache the result of this method, but should instead always call + * again to get the {@link AccessController}. + */ + AccessController getAccessController(); + + /** + * Reloads locations from {@code brooklyn.properties}. Any changes will apply only to newly created applications + */ + void reloadBrooklynProperties(); + + /** + * Listener for {@code brooklyn.properties} reload events. + * + * @see {@link #raddPropertiesReloadListenerPropertiesReloadListener)} + * @see {@link #removePropertiesReloadListener(PropertiesReloadListener)} + */ + interface PropertiesReloadListener extends Serializable { + + /** Called when {@code brooklyn.properties} is reloaded. */ + void reloaded(); + + } + + /** + * Registers a listener to be notified when brooklyn.properties is reloaded + */ + void addPropertiesReloadListener(PropertiesReloadListener listener); + + /** + * Deregisters a listener from brooklyn.properties reload notifications + */ + void removePropertiesReloadListener(PropertiesReloadListener listener); + + /** + * Active entitlements checker instance. + */ + EntitlementManager getEntitlementManager(); + + /** As {@link #lookup(String, Class)} but not constraining the return type */ + public BrooklynObject lookup(String id); + + /** Finds an entity with the given ID known at this management context */ + // TODO in future support policies etc + public <T extends BrooklynObject> T lookup(String id, Class<T> type); + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/f7f03580/api/src/main/java/org/apache/brooklyn/management/SubscriptionContext.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/org/apache/brooklyn/management/SubscriptionContext.java b/api/src/main/java/org/apache/brooklyn/management/SubscriptionContext.java new file mode 100644 index 0000000..76715ae --- /dev/null +++ b/api/src/main/java/org/apache/brooklyn/management/SubscriptionContext.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.brooklyn.management; + +import java.util.Map; +import java.util.Set; + +import brooklyn.entity.Entity; +import brooklyn.entity.Group; +import brooklyn.event.Sensor; +import brooklyn.event.SensorEvent; +import brooklyn.event.SensorEventListener; + +/** + * This is the context through which an {@link Entity} can manage its subscriptions. + */ +public interface SubscriptionContext { + /** + * As {@link SubscriptionManager#subscribe(Map, Entity, Sensor, SensorEventListener)} with default subscription parameters for this context + */ + <T> SubscriptionHandle subscribe(Map<String, Object> flags, Entity producer, Sensor<T> sensor, SensorEventListener<? super T> listener); + + /** @see #subscribe(Map, Entity, Sensor, SensorEventListener) */ + <T> SubscriptionHandle subscribe(Entity producer, Sensor<T> sensor, SensorEventListener<? super T> listener); + + /** @see #subscribe(Map, Entity, Sensor, SensorEventListener) */ + <T> SubscriptionHandle subscribeToChildren(Map<String, Object> flags, Entity parent, Sensor<T> sensor, SensorEventListener<? super T> listener); + + /** @see #subscribe(Map, Entity, Sensor, SensorEventListener) */ + <T> SubscriptionHandle subscribeToChildren(Entity parent, Sensor<T> sensor, SensorEventListener<? super T> listener); + + /** @see #subscribe(Map, Entity, Sensor, SensorEventListener) */ + <T> SubscriptionHandle subscribeToMembers(Map<String, Object> flags, Group parent, Sensor<T> sensor, SensorEventListener<? super T> listener); + + /** @see #subscribe(Map, Entity, Sensor, SensorEventListener) */ + <T> SubscriptionHandle subscribeToMembers(Group parent, Sensor<T> sensor, SensorEventListener<? super T> listener); + + /** @see SubscriptionManager#unsubscribe(SubscriptionHandle) */ + boolean unsubscribe(SubscriptionHandle subscriptionId); + + /** causes all subscriptions to be deregistered + * @return number of subscriptions removed */ + int unsubscribeAll(); + + /** @see SubscriptionManager#publish(SensorEvent) */ + <T> void publish(SensorEvent<T> event); + + /** Return the subscriptions associated with this context */ + Set<SubscriptionHandle> getSubscriptions(); +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/f7f03580/api/src/main/java/org/apache/brooklyn/management/SubscriptionHandle.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/org/apache/brooklyn/management/SubscriptionHandle.java b/api/src/main/java/org/apache/brooklyn/management/SubscriptionHandle.java new file mode 100644 index 0000000..e601579 --- /dev/null +++ b/api/src/main/java/org/apache/brooklyn/management/SubscriptionHandle.java @@ -0,0 +1,27 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.brooklyn.management; + +/** + * A "receipt" returned by {@link SubscriptionContext} and {@link SubscriptionManager}'s {@code subscribe()} + * methods. It can be used to unsubscribe - see {@link SubscriptionContext#unsubscribe(SubscriptionHandle)} + * and {@link SubscriptionManager#unsubscribe(SubscriptionHandle)}. + */ +public interface SubscriptionHandle { +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/f7f03580/api/src/main/java/org/apache/brooklyn/management/SubscriptionManager.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/org/apache/brooklyn/management/SubscriptionManager.java b/api/src/main/java/org/apache/brooklyn/management/SubscriptionManager.java new file mode 100644 index 0000000..3e5802b --- /dev/null +++ b/api/src/main/java/org/apache/brooklyn/management/SubscriptionManager.java @@ -0,0 +1,112 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.brooklyn.management; + +import java.util.Map; +import java.util.Set; + +import brooklyn.entity.Entity; +import brooklyn.entity.Group; +import brooklyn.event.Sensor; +import brooklyn.event.SensorEvent; +import brooklyn.event.SensorEventListener; + +/** + * The management interface for subscriptions. + * + * Different implementations will handle entities subscribing and unsubscribing to {@link SensorEvent}s + * and their delivery. + * + * @see SubscriptionContext + */ +public interface SubscriptionManager { + /** + * Subscribe to {@link Sensor} data changes and events on a given {@link Entity}, supplying the {@link SensorEventListener} + * to invoke when they occur. + * + * The method returns an id which can be used to {@link #unsubscribe(SubscriptionHandle)} later. + * <p> + * The listener callback is in-order single-threaded and synchronized on this object. The flags + * parameters can include the following: + * <ul> + * <li>subscriber - object to identify the subscriber (e.g. entity, or console session uid) + * <li><i>in future</i> - control parameters for the subscription (period, minimum delta for updates, etc) + * </ul> + * + * @param flags optional parameters to be associated with the subscription + * @param producer entity to listen to, or null to listen to all entities + * @param sensor sensor channel of events to listen to, or null for all sensors from the given producer(s) + * @param listener callback to invoke when an event occurs + * @return an id for this subscription + * + * @see #unsubscribe(SubscriptionHandle) + */ + <T> SubscriptionHandle subscribe(Map<String, Object> flags, Entity producer, Sensor<T> sensor, SensorEventListener<? super T> listener); + + /** @see #subscribe(Map, Entity, Sensor, SensorEventListener) */ + <T> SubscriptionHandle subscribe(Entity producer, Sensor<T> sensor, SensorEventListener<? super T> listener); + + /** + * Subscribe to {@link Sensor} data changes and events on all children of a given {@link Entity}, supplying the + * {@link SensorEventListener} to invoke when they occur. + * + * The subscriptions will be created recursively for all children, and the same listener callback will be invoked for each + * sensor datum. The semantics are otherwise identical to the {@link #subscribe(Map, Entity, Sensor, SensorEventListener)} method. + * + * @see #subscribe(Map, Entity, Sensor, SensorEventListener) + */ + <T> SubscriptionHandle subscribeToChildren(Map<String, Object> flags, Entity parent, Sensor<T> sensor, SensorEventListener<? super T> listener); + + /** @see #subscribeToChildren(Map, Entity, Sensor, SensorEventListener) */ + <T> SubscriptionHandle subscribeToChildren(Entity parent, Sensor<T> sensor, SensorEventListener<? super T> listener); + + /** + * Subscribe to {@link Sensor} data changes and events on all members of a given {@link Group}, supplying the + * {@link SensorEventListener} to invoke when they occur. + * + * The subscriptions will be created recursively for all children, and the same listener callback will be invoked for each + * sensor datum. The semantics are otherwise identical to the {@link #subscribe(Map, Entity, Sensor, SensorEventListener)} method. + * + * @see #subscribe(Map, Entity, Sensor, SensorEventListener) + */ + <T> SubscriptionHandle subscribeToMembers(Map<String, Object> flags, Group parent, Sensor<T> sensor, SensorEventListener<? super T> listener); + + /** @see #subscribeToMembers(Map, Group, Sensor, SensorEventListener) */ + <T> SubscriptionHandle subscribeToMembers(Group parent, Sensor<T> sensor, SensorEventListener<? super T> listener); + + /** + * Unsubscribe the given subscription id. + * + * @return true if such a subscription was present (and it will now be removed) + * + * @see #subscribe(Map, Entity, Sensor, SensorEventListener) + */ + boolean unsubscribe(SubscriptionHandle subscriptionId); + + /** + * Deliver a {@link SensorEvent} to all subscribed listeners. + */ + <T> void publish(SensorEvent<T> event); + + /** Return the subscriptions requested by a given subscriber */ + Set<SubscriptionHandle> getSubscriptionsForSubscriber(Object subscriber); + + /** Return the subscriptions on a given source-sensor pair */ + Set<SubscriptionHandle> getSubscriptionsForEntitySensor(Entity source, Sensor<?> sensor); +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/f7f03580/api/src/main/java/org/apache/brooklyn/management/Task.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/org/apache/brooklyn/management/Task.java b/api/src/main/java/org/apache/brooklyn/management/Task.java new file mode 100644 index 0000000..3a4fdef --- /dev/null +++ b/api/src/main/java/org/apache/brooklyn/management/Task.java @@ -0,0 +1,128 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.brooklyn.management; + +import java.util.Set; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; +import java.util.concurrent.FutureTask; +import java.util.concurrent.TimeoutException; + +import com.google.common.util.concurrent.ListenableFuture; + +import brooklyn.util.time.Duration; + +/** + * Represents a unit of work for execution. + * + * When used with an {@link ExecutionManager} or {@link ExecutionContext} it will record submission time, + * execution start time, end time, and any result. A task can be submitted to the ExecutionManager or + * ExecutionContext, in which case it will be returned, or it may be created by submission + * of a {@link Runnable} or {@link Callable} and thereafter it can be treated just like a {@link Future}. + */ +public interface Task<T> extends ListenableFuture<T>, TaskAdaptable<T> { + + public String getId(); + + public Set<Object> getTags(); + /** if {@link #isSubmitted()} returns the time when the task was submitted; or -1 otherwise */ + public long getSubmitTimeUtc(); + /** if {@link #isBegun()} returns the time when the task was starts; + * guaranteed to be >= {@link #getSubmitTimeUtc()} > 0 if started, or -1 otherwise */ + public long getStartTimeUtc(); + /** if {@link #isDone()} (for any reason) returns the time when the task ended; + * guaranteed to be >= {@link #getStartTimeUtc()} > 0 if ended, or -1 otherwise */ + public long getEndTimeUtc(); + public String getDisplayName(); + public String getDescription(); + + /** task which submitted this task, if was submitted by a task */ + public Task<?> getSubmittedByTask(); + + /** The thread where the task is running, if it is running. */ + public Thread getThread(); + + /** + * Whether task has been submitted + * + * Submitted tasks are normally expected to start running then complete, + * but unsubmitted tasks are sometimes passed around for someone else to submit them. + */ + public boolean isSubmitted(); + + /** + * Whether task has started running. + * + * Will remain true after normal completion or non-cancellation error. + * will be true on cancel iff the thread did actually start. + */ + public boolean isBegun(); + + /** + * Whether the task threw an error, including cancellation (implies {@link #isDone()}) + */ + public boolean isError(); + + /** + * Causes calling thread to block until the task is started. + */ + public void blockUntilStarted(); + + /** + * Causes calling thread to block until the task is ended. + * <p> + * Either normally or by cancellation or error, but without throwing error on cancellation or error. + * (Errors are logged at debug.) + */ + public void blockUntilEnded(); + + /** + * As {@link #blockUntilEnded()}, but returning after the given timeout; + * true if the task has ended and false otherwise + */ + public boolean blockUntilEnded(Duration timeout); + + public String getStatusSummary(); + + /** + * Returns detailed status, suitable for a hover. + * + * Plain-text format, with new-lines (and sometimes extra info) if multiline enabled. + */ + public String getStatusDetail(boolean multiline); + + /** As {@link #get(long, java.util.concurrent.TimeUnit)} */ + public T get(Duration duration) throws InterruptedException, ExecutionException, TimeoutException; + + /** As {@link #get()}, but propagating checked exceptions as unchecked for convenience. */ + public T getUnchecked(); + + /** As {@link #get()}, but propagating checked exceptions as unchecked for convenience + * (including a {@link TimeoutException} if the duration expires) */ + public T getUnchecked(Duration duration); + + /** As {@link Future#cancel(boolean)}. Note that {@link #isDone()} and {@link #blockUntilEnded(Duration)} return immediately + * once a task is cancelled, consistent with the underlying {@link FutureTask} behaviour. + * TODO Fine-grained control over underlying jobs, e.g. to ensure anything represented by this task is actually completed, + * is not (yet) publicly exposed. See the convenience method blockUntilInternalTasksEnded in the Tasks set of helpers + * for more discussion. */ + public boolean cancel(boolean mayInterruptIfRunning); + +}
