Return-Path: X-Original-To: apmail-sling-commits-archive@www.apache.org Delivered-To: apmail-sling-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 9159BF59B for ; Fri, 12 Apr 2013 08:42:28 +0000 (UTC) Received: (qmail 4041 invoked by uid 500); 12 Apr 2013 08:42:28 -0000 Delivered-To: apmail-sling-commits-archive@sling.apache.org Received: (qmail 3852 invoked by uid 500); 12 Apr 2013 08:42:23 -0000 Mailing-List: contact commits-help@sling.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@sling.apache.org Delivered-To: mailing list commits@sling.apache.org Received: (qmail 3756 invoked by uid 99); 12 Apr 2013 08:42:22 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 12 Apr 2013 08:42:22 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 12 Apr 2013 08:42:14 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 6415D23888FD; Fri, 12 Apr 2013 08:41:52 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1467207 - in /sling/trunk/contrib/extensions/discovery: ./ src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/ src/main/java/org/apache/sling/ src/main/java/org/apache/sling/discovery/ src/main/java/org/apache/sling/... Date: Fri, 12 Apr 2013 08:41:51 -0000 To: commits@sling.apache.org From: cziegeler@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20130412084152.6415D23888FD@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: cziegeler Date: Fri Apr 12 08:41:51 2013 New Revision: 1467207 URL: http://svn.apache.org/r1467207 Log: Add new discovery module Added: sling/trunk/contrib/extensions/discovery/ sling/trunk/contrib/extensions/discovery/pom.xml (with props) sling/trunk/contrib/extensions/discovery/src/ sling/trunk/contrib/extensions/discovery/src/main/ sling/trunk/contrib/extensions/discovery/src/main/java/ sling/trunk/contrib/extensions/discovery/src/main/java/org/ sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/ sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/ sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/ sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/ClusterView.java (with props) sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/DiscoveryAware.java (with props) sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/DiscoveryService.java (with props) sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/InstanceDescription.java (with props) sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/InstanceFilter.java (with props) sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/PropertyProvider.java (with props) sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/TopologyEvent.java (with props) sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/TopologyView.java (with props) sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/impl/ sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/impl/NoClusterDiscoveryService.java (with props) sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/package-info.java (with props) Added: sling/trunk/contrib/extensions/discovery/pom.xml URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/discovery/pom.xml?rev=1467207&view=auto ============================================================================== --- sling/trunk/contrib/extensions/discovery/pom.xml (added) +++ sling/trunk/contrib/extensions/discovery/pom.xml Fri Apr 12 08:41:51 2013 @@ -0,0 +1,82 @@ + + + + + 4.0.0 + + org.apache.sling + sling + 15 + ../../../parent/pom.xml + + + org.apache.sling.discovery.api + bundle + 0.1.0-SNAPSHOT + + Apache Sling Discovery API + + Support for topology discovery of instances. + + + + + + org.apache.felix + maven-scr-plugin + + + org.apache.felix + maven-bundle-plugin + true + + + + + + org.apache.felix + org.apache.felix.scr.annotations + + + biz.aQute + bndlib + 1.50.0 + provided + + + org.slf4j + slf4j-api + + + org.osgi + org.osgi.core + + + org.osgi + org.osgi.compendium + + + org.apache.sling + org.apache.sling.settings + 1.1.0 + provided + + + Propchange: sling/trunk/contrib/extensions/discovery/pom.xml ------------------------------------------------------------------------------ svn:eol-style = native Propchange: sling/trunk/contrib/extensions/discovery/pom.xml ------------------------------------------------------------------------------ svn:keywords = Id Added: sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/ClusterView.java URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/ClusterView.java?rev=1467207&view=auto ============================================================================== --- sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/ClusterView.java (added) +++ sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/ClusterView.java Fri Apr 12 08:41:51 2013 @@ -0,0 +1,50 @@ +/* + * 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.sling.discovery; + +import java.util.List; + +/** + * A ClusterView represents the instances of a cluster that are + * up and running and that all can see each other at a certain point in time. + *

+ * A ClusterView can also consist of just one single instance. + */ +public interface ClusterView { + + /** + * Returns an id of this cluster view + * @return an id of this cluster view + */ + String getId(); + + /** + * Provides the list of InstanceDescriptions ordered by Sling Id. + * @return the list of InstanceDescriptions ordered by Sling Id + */ + List getInstances(); + + /** + * Provides the InstanceDescription belonging to the leader instance. + *

+ * Every ClusterView is guaranteed to have one and only one leader. + * @return the InstanceDescription belonging to the leader instance + */ + InstanceDescription getLeader(); +} Propchange: sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/ClusterView.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/ClusterView.java ------------------------------------------------------------------------------ svn:keywords = author date id revision rev url Added: sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/DiscoveryAware.java URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/DiscoveryAware.java?rev=1467207&view=auto ============================================================================== --- sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/DiscoveryAware.java (added) +++ sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/DiscoveryAware.java Fri Apr 12 08:41:51 2013 @@ -0,0 +1,60 @@ +/* + * 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.sling.discovery; + +/** + * The DiscoveryAware service interface may be implemented by + * components interested in being made aware of the cluster state or changes + * thereof. + *

+ * Upon registration and whenever changes in the topology occur, this + * service is informed. + */ +public interface DiscoveryAware { + + /** + * Inform the service about an event in the topology - or in the discovery + * of the topology. + *

+ * The TopologyEvent contains details about what changed. + * The supported event types are: + *

    + *
  • TOPOLOGY_INIT sent when the DiscoveryAware + * was first bound to the discovery service - represents the initial state + * of the topology at that time.
  • + *
  • TOPOLOGY_CHANGING sent when the discovery service + * discovered a change in the topology and has started to settle the change. + * This event is sent before TOPOLOGY_CHANGED but is optional
  • + *
  • TOPOLOGY_CHANGED sent when the discovery service + * discovered a change in the topology and has settled it.
  • + *
  • PROPERTIES_CHANGED sent when the one or many properties + * have changed in an instance in the current topology
  • + *
+ * A note on instance restarts: it is currently not a requirement on the + * discovery service to send a TopologyEvent should an instance restart + * occur rapidly (ie within the change detection timeout). A TopologyEvent + * is only sent if the number of instances or any property changes. + * Should there be a requirement to detect a restart in a guaranteed fashion, + * it is always possible to set a particular property (using the PropertyProvider) + * to the instance start time and have others detect a change in that property. + * @param event The topology event + */ + void handleTopologyEvent(TopologyEvent event); + +} Propchange: sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/DiscoveryAware.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/DiscoveryAware.java ------------------------------------------------------------------------------ svn:keywords = author date id revision rev url Added: sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/DiscoveryService.java URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/DiscoveryService.java?rev=1467207&view=auto ============================================================================== --- sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/DiscoveryService.java (added) +++ sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/DiscoveryService.java Fri Apr 12 08:41:51 2013 @@ -0,0 +1,50 @@ +/* + * 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.sling.discovery; + +/** + * The discovery service can be used to get the current topology view. + *

+ * The discovery service is in charge of managing live instances that + * have announced themselves as being part of a topology view. The exact + * details of how this announcement occurs is implementation dependent. + */ +public interface DiscoveryService { + + /** + * Returns the topology that was last discovered by this service. + *

+ * If for some reason the service is currently not able to do topology discovery + * it will return the last valid topology marked with false in the call + * to . This is also true if the service + * has noticed a potential change in the topology and is in the process of + * settling the change in the topology (eg with peers, ie voting). + *

+ * Note that this call is synchronized with DiscoveryAware.handleTopologyEvent() + * calls: ie if calls to DiscoveryAware.handleTopologyEvent() are currently + * ongoing, then the call to this method will block until all DiscoveryAwares + * have been called. Be careful not to cause deadlock situations. + *

+ * @return the topology that was last discovered by this service. This will never + * be null (ie even if a change in the topology is ongoing at the moment or the + * cluster consists only of the own instance). + */ + TopologyView getTopology(); + +} Propchange: sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/DiscoveryService.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/DiscoveryService.java ------------------------------------------------------------------------------ svn:keywords = author date id revision rev url Added: sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/InstanceDescription.java URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/InstanceDescription.java?rev=1467207&view=auto ============================================================================== --- sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/InstanceDescription.java (added) +++ sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/InstanceDescription.java Fri Apr 12 08:41:51 2013 @@ -0,0 +1,85 @@ +/* + * 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.sling.discovery; + +import java.util.Map; + +/** + * An InstanceDescription represents and contains information about an + * instance that is part of a TopologyView. + *

+ * Note that all methods are idempotent - they always return the same values + * on subsequent calls. Rather, on any change new InstanceDescriptions are created. + * @see TopologyView + */ +public interface InstanceDescription { + + /** + * Returns the ClusterView of which this instance is part of. + *

+ * Every instance is part of a ClusterView even if it is standalone. + * @return the ClusterView + */ + ClusterView getClusterView(); + + /** + * If an instance is part of a cluster, it can potentially be a leader of that cluster - + * this information is queried here. + *

+ * If an instance is not part of a cluster, this method returns true. + *

+ * Only one instance of a cluster is guaranteed to be the leader at any time. + * This guarantee is provided by this service. + * If the leader goes down, the service elects a new leader and announces it to + * DiscoveryAware listeners. + * @return true if this instance is the - only - leader in this cluster, + * false if it is one of the slaves, or true if it is not at all part of a cluster + */ + boolean isLeader(); + + /** + * Determines whether this InstanceDescription is representing myself, ie my own instance. + * @return whether this InstanceDescription is representing myself, ie my own instance + */ + boolean isOwn(); + + /** + * The identifier of the running Sling instance. + */ + String getSlingId(); + + /** + * Returns the value of a particular property. + *

+ * Note that there are no hard guarantees or requirements as to how quickly + * a property is available once it is set on a distant instance. + * @param name The property name + * @return The value of the property or null + * @see DiscoveryService#setProperty(String, String) + */ + String getProperty(final String name); + + /** + * Returns a Map containing all properties of this instance. + * This method always returns a map, it might be empty. The returned map + * is not modifiable. + * @return a Map containing all properties of this instance + */ + Map getProperties(); +} Propchange: sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/InstanceDescription.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/InstanceDescription.java ------------------------------------------------------------------------------ svn:keywords = author date id revision rev url Added: sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/InstanceFilter.java URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/InstanceFilter.java?rev=1467207&view=auto ============================================================================== --- sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/InstanceFilter.java (added) +++ sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/InstanceFilter.java Fri Apr 12 08:41:51 2013 @@ -0,0 +1,37 @@ +/* + * 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.sling.discovery; + +/** + * Used to filter InstanceDescriptions in a TopologyView. + *

+ * @see DiscoveryService#findInstances(InstanceFilter) + */ +public interface InstanceFilter { + + /** + * Returns true if this InstanceFilter selects the given InstanceDescription. + *

+ * @param instance the InstanceDescription for which to decide if this + * InstanceFilter accepts it or not. + * @return true if this InstanceFilter selects the given InstanceDescription + */ + boolean accept(InstanceDescription instance); + +} Propchange: sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/InstanceFilter.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/InstanceFilter.java ------------------------------------------------------------------------------ svn:keywords = author date id revision rev url Added: sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/PropertyProvider.java URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/PropertyProvider.java?rev=1467207&view=auto ============================================================================== --- sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/PropertyProvider.java (added) +++ sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/PropertyProvider.java Fri Apr 12 08:41:51 2013 @@ -0,0 +1,52 @@ +/* + * 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.sling.discovery; + + +/** + * The PropertyProvider service interface may be implemented by + * components that wish to define properties on the local instance which then + * are broadcast to the TopologyView instances. + *

+ * The provided properties are registered with the {@link #PROPERTY_PROPERTIES} + * service property. + * If the set of provided properties changes or one of the provided values + * change, the service registration of the provider should be updated. + * This avoids periodic polling for changes. + */ +public interface PropertyProvider { + + /** + * The name of the service registration property containing the names + * of the properties provided by this provider. + * The value is either a string or an array of strings. + * A property name must only contain alphanumeric characters plus ., + * _, -. + */ + String PROPERTY_PROPERTIES = "instance.properties"; + + /** + * Retrieves a property that is subsequently set on the local instance + * and broadcast to the TopologyView instances. + *

+ * These properties are non-persistent and disappear after my own instance goes down. + * @return The value of the property or null + */ + String getProperty(final String name); +} Propchange: sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/PropertyProvider.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/PropertyProvider.java ------------------------------------------------------------------------------ svn:keywords = author date id revision rev url Added: sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/TopologyEvent.java URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/TopologyEvent.java?rev=1467207&view=auto ============================================================================== --- sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/TopologyEvent.java (added) +++ sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/TopologyEvent.java Fri Apr 12 08:41:51 2013 @@ -0,0 +1,115 @@ +/* + * 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.sling.discovery; + +/** + * A topology event is sent whenever a change in the topology occurs. + * + * This event object might be extended in the future with new event types + * and methods. + * @see DiscoveryAware + */ +public class TopologyEvent { + + public static enum Type { + TOPOLOGY_INIT, // Inform the service about the initial topology state + TOPOLOGY_CHANGING, // Inform the service about the fact that a state change was detected + // in the cluster topology and that a voting amongst the members about + // a new, valid view has just started. + // Note that implementations might not support this event at all. + TOPOLOGY_CHANGED, // Inform the service about a state change in the cluster topology. + PROPERTIES_CHANGED // one or many properties have been changed on an instance which is part + // of the topology + } + + private final Type type; + private final TopologyView oldView; + private final TopologyView newView; + + public TopologyEvent(final Type type, final TopologyView oldView, final TopologyView newView) { + if (type==null) { + throw new IllegalArgumentException("type must not be null"); + } + + if (type==Type.TOPOLOGY_INIT) { + // then oldView is null + if (oldView!=null) { + throw new IllegalArgumentException("oldView must be null"); + } + // and newView must be not null + if (newView==null) { + throw new IllegalArgumentException("newView must not be null"); + } + } else if (type==Type.TOPOLOGY_CHANGING) { + // then newView is null + if (newView!=null) { + throw new IllegalArgumentException("newView must be null"); + } + // and oldView must not be null + if (oldView==null) { + throw new IllegalArgumentException("oldView must not be null"); + } + } else { + // in all other cases both oldView and newView must not be null + if (oldView==null) { + throw new IllegalArgumentException("oldView must not be null"); + } + if (newView==null) { + throw new IllegalArgumentException("newView must not be null"); + } + } + this.type = type; + this.oldView = oldView; + this.newView = newView; + } + + /** + * Returns the type of this event + * @return the type of this event + */ + public Type getType() { + return type; + } + + /** + * Returns the view which was valid up until now. + *

+ * This is null in case of TOPOLOGY_INIT + * @return the view which was valid up until now, or null in case of a fresh instance start + */ + public TopologyView getOldView() { + return oldView; + } + + /** + * Returns the view which is currently (i.e. newly) valid. + *

+ * This is null in case of TOPOLOGY_CHANGING + * @return the view which is currently valid, or null in case of TOPOLOGY_CHANGING + */ + public TopologyView getNewView() { + return newView; + } + + @Override + public String toString() { + return "TopologyEvent [type=" + type + ", oldView=" + oldView + + ", newView=" + newView + "]"; + } +} Propchange: sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/TopologyEvent.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/TopologyEvent.java ------------------------------------------------------------------------------ svn:keywords = author date id revision rev url Added: sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/TopologyView.java URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/TopologyView.java?rev=1467207&view=auto ============================================================================== --- sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/TopologyView.java (added) +++ sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/TopologyView.java Fri Apr 12 08:41:51 2013 @@ -0,0 +1,68 @@ +/* + * 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.sling.discovery; + +import java.util.Collection; +import java.util.List; + +/** + * A topology view is a cross-cluster list of instances and clusters + * that have announced themselves with the DiscoveryService. + * + */ +public interface TopologyView { + + /** + * Checks if this TopologyView is currently valid - or if the + * service knows of a topology change just going on (or another + * uncertainty about the topology such as IOException etc) + * @return true if this TopologyView is currently valid, false + * if the service knows of a topology change just going on (or + * another issue with discovery like IOException etc) + */ + boolean isCurrent(); + + /** + * Provides the InstanceDescription belonging to *this* instance. + * @return the InstanceDescription belonging to *this* instance + */ + InstanceDescription getOwnInstance(); + + /** + * Provides the list of InstanceDescriptions ordered by Sling Id. + * @return the list of InstanceDescriptions ordered by Sling Id or null if there are no instances + */ + List getInstances(); + + /** + * Search the current topology for instances which the provided InstancePicker has accepted. + * @param filter the filter to use + * @return the list of InstanceDescriptions which were accepted by the InstanceFilter + */ + List findInstances(InstanceFilter filter); + + /** + * Provides the collection of ClusterViews. + *

+ * Note that all InstanceDescriptions belong to a ClusterView, even if + * they are only a "cluster of 1" (ie not really a cluster). + * @return the collection of ClusterViews + */ + Collection getClusterViews(); +} Propchange: sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/TopologyView.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/TopologyView.java ------------------------------------------------------------------------------ svn:keywords = author date id revision rev url Added: sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/impl/NoClusterDiscoveryService.java URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/impl/NoClusterDiscoveryService.java?rev=1467207&view=auto ============================================================================== --- sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/impl/NoClusterDiscoveryService.java (added) +++ sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/impl/NoClusterDiscoveryService.java Fri Apr 12 08:41:51 2013 @@ -0,0 +1,389 @@ +/* + * 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.sling.discovery.impl; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +import org.apache.felix.scr.annotations.Activate; +import org.apache.felix.scr.annotations.Component; +import org.apache.felix.scr.annotations.ConfigurationPolicy; +import org.apache.felix.scr.annotations.Deactivate; +import org.apache.felix.scr.annotations.Reference; +import org.apache.felix.scr.annotations.ReferenceCardinality; +import org.apache.felix.scr.annotations.ReferencePolicy; +import org.apache.felix.scr.annotations.Service; +import org.apache.sling.discovery.ClusterView; +import org.apache.sling.discovery.DiscoveryAware; +import org.apache.sling.discovery.DiscoveryService; +import org.apache.sling.discovery.InstanceDescription; +import org.apache.sling.discovery.InstanceFilter; +import org.apache.sling.discovery.PropertyProvider; +import org.apache.sling.discovery.TopologyEvent; +import org.apache.sling.discovery.TopologyEvent.Type; +import org.apache.sling.discovery.TopologyView; +import org.apache.sling.settings.SlingSettingsService; +import org.osgi.framework.Constants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This is a simple implementation of the cluster service + * which can be used for a cluster less installation. + */ +@Component(policy = ConfigurationPolicy.REQUIRE, immediate=true) +@Service(value = {DiscoveryService.class}) +public class NoClusterDiscoveryService implements DiscoveryService { + + /** The logger. */ + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + /** + * Sling settings service to get the Sling ID and run modes. + */ + @Reference + private SlingSettingsService settingsService; + + /** + * All cluster aware instances. + */ + @Reference(cardinality = ReferenceCardinality.OPTIONAL_MULTIPLE, policy = ReferencePolicy.DYNAMIC) + private DiscoveryAware[] clusterAwares = new DiscoveryAware[0]; + + /** + * All property providers. + */ + @Reference(cardinality = ReferenceCardinality.OPTIONAL_MULTIPLE, policy = ReferencePolicy.DYNAMIC, + referenceInterface=PropertyProvider.class, updated="updatedPropertyProvider") + private List providerInfos = new ArrayList(); + + /** + * Special lock object to sync data structure access + */ + private final Object lock = new Object(); + + /** + * The current topology view. + */ + private TopologyView topologyView; + + private Map cachedProperties = new HashMap(); + + /** + * Activate this service + * Create a new description. + */ + @Activate + protected void activate() { + logger.debug("NoClusterDiscoveryService started."); + final InstanceDescription myDescription = new InstanceDescription() { + + public boolean isOwn() { + return true; + } + + public boolean isLeader() { + return true; + } + + public String getSlingId() { + return settingsService.getSlingId(); + } + + public String getProperty(final String name) { + synchronized(lock) { + return cachedProperties.get(name); + } + } + + public Map getProperties() { + synchronized(lock) { + return Collections.unmodifiableMap(cachedProperties); + } + } + + public ClusterView getClusterView() { + final Collection clusters = topologyView.getClusterViews(); + if (clusters==null || clusters.size()==0) { + return null; + } + return clusters.iterator().next(); + } + }; + final List instances = new ArrayList(); + instances.add(myDescription); + + final DiscoveryAware[] registeredServices; + synchronized ( lock ) { + registeredServices = this.clusterAwares; + final ClusterView clusterView = new ClusterView() { + + public InstanceDescription getLeader() { + return myDescription; + } + + public List getInstances() { + return instances; + } + + public String getId() { + return "0"; + } + }; + this.topologyView = new TopologyView() { + + public InstanceDescription getOwnInstance() { + return myDescription; + } + + public boolean isCurrent() { + return true; + } + + public List getInstances() { + return instances; + } + + public List findInstances(InstanceFilter picker) { + List result = new LinkedList(); + for (Iterator it = getTopology().getInstances().iterator(); it.hasNext();) { + InstanceDescription instance = it.next(); + if (picker.accept(instance)) { + result.add(instance); + } + } + return result; + } + + public List getClusterViews() { + LinkedList clusters = new LinkedList(); + clusters.add(clusterView); + return clusters; + } + + }; + } + for(final DiscoveryAware da: registeredServices) { + da.handleTopologyEvent(new TopologyEvent(Type.TOPOLOGY_INIT, null, topologyView)); + } + } + + /** + * Deactivate this service. + */ + @Deactivate + protected void deactivate() { + logger.debug("NoClusterDiscoveryService stopped."); + this.topologyView = null; + } + + /** + * Bind a new property provider. + */ + @SuppressWarnings("unused") + private void bindPropertyProvider(final PropertyProvider propertyProvider, final Map props) { + logger.debug("bindPropertyProvider: Binding PropertyProvider {}", propertyProvider); + + final DiscoveryAware[] awares; + synchronized (lock) { + final ProviderInfo info = new ProviderInfo(propertyProvider, props); + this.providerInfos.add(info); + Collections.sort(this.providerInfos); + this.updatePropertiesCache(); + if ( this.topologyView == null ) { + awares = null; + } else { + awares = this.clusterAwares; + } + } + if ( awares != null ) { + for(final DiscoveryAware da : awares) { + da.handleTopologyEvent(new TopologyEvent(Type.PROPERTIES_CHANGED, this.topologyView, this.topologyView)); + } + } + } + + /** + * Update a property provider. + */ + @SuppressWarnings("unused") + private void updatedPropertyProvider(final PropertyProvider propertyProvider, final Map props) { + logger.debug("bindPropertyProvider: Updating PropertyProvider {}", propertyProvider); + + this.unbindPropertyProvider(propertyProvider, props, false); + this.bindPropertyProvider(propertyProvider, props); + } + + /** + * Unbind a property provider + */ + @SuppressWarnings("unused") + private void unbindPropertyProvider(final PropertyProvider propertyProvider, final Map props) { + this.unbindPropertyProvider(propertyProvider, props, true); + } + + /** + * Unbind a property provider + */ + @SuppressWarnings("unused") + private void unbindPropertyProvider(final PropertyProvider propertyProvider, + final Map props, + final boolean inform) { + logger.debug("unbindPropertyProvider: Releasing PropertyProvider {}", propertyProvider); + + final DiscoveryAware[] awares; + synchronized (lock) { + final ProviderInfo info = new ProviderInfo(propertyProvider, props); + this.providerInfos.remove(info); + this.updatePropertiesCache(); + if ( this.topologyView == null ) { + awares = null; + } else { + awares = this.clusterAwares; + } + } + if ( inform && awares != null ) { + for(final DiscoveryAware da : awares) { + da.handleTopologyEvent(new TopologyEvent(Type.PROPERTIES_CHANGED, this.topologyView, this.topologyView)); + } + } + } + + private void updatePropertiesCache() { + final Map newProps = new HashMap(); + for(final ProviderInfo info : this.providerInfos) { + newProps.putAll(info.properties); + } + this.cachedProperties = newProps; + if ( this.logger.isDebugEnabled() ) { + this.logger.debug("New properties: {}", this.cachedProperties); + } + } + + @SuppressWarnings("unused") + private void bindDiscoveryAware(final DiscoveryAware clusterAware) { + + logger.debug("bindDiscoveryAware: Binding DiscoveryAware {}", clusterAware); + + boolean inform = true; + synchronized (lock) { + List currentList = new ArrayList( + Arrays.asList(clusterAwares)); + currentList.add(clusterAware); + this.clusterAwares = currentList.toArray(new DiscoveryAware[currentList.size()]); + if ( this.topologyView == null ) { + inform = false; + } + } + + if ( inform ) { + clusterAware.handleTopologyEvent(new TopologyEvent(Type.TOPOLOGY_INIT, null, topologyView)); + } + } + + @SuppressWarnings("unused") + private void unbindDiscoveryAware(final DiscoveryAware clusterAware) { + + logger.debug("unbindDiscoveryAware: Releasing DiscoveryAware {}", clusterAware); + + synchronized (lock) { + List currentList = new ArrayList( + Arrays.asList(clusterAwares)); + currentList.remove(clusterAware); + this.clusterAwares = currentList.toArray(new DiscoveryAware[currentList.size()]); + } + } + + /** + * @see DiscoveryService#getTopology() + */ + public TopologyView getTopology() { + return topologyView; + } + + /** + * Internal class caching some provider infos like service id and ranking. + */ + private final static class ProviderInfo implements Comparable { + + public final PropertyProvider provider; + public final int ranking; + public final long serviceId; + public final Map properties = new HashMap(); + + public ProviderInfo(final PropertyProvider provider, final Map serviceProps) { + this.provider = provider; + final Object sr = serviceProps.get(Constants.SERVICE_RANKING); + if ( sr == null || !(sr instanceof Integer)) { + this.ranking = 0; + } else { + this.ranking = (Integer)sr; + } + this.serviceId = (Long)serviceProps.get(Constants.SERVICE_ID); + final Object namesObj = serviceProps.get(PropertyProvider.PROPERTY_PROPERTIES); + if ( namesObj instanceof String ) { + final String val = provider.getProperty((String)namesObj); + if ( val != null ) { + this.properties.put((String)namesObj, val); + } + } else if ( namesObj instanceof String[] ) { + for(final String name : (String[])namesObj ) { + final String val = provider.getProperty(name); + if ( val != null ) { + this.properties.put(name, val); + } + } + } + } + + /** + * @see java.lang.Comparable#compareTo(java.lang.Object) + */ + public int compareTo(final ProviderInfo o) { + // Sort by rank in ascending order. + if ( this.ranking < o.ranking ) { + return -1; // lower rank + } else if (this.ranking > o.ranking ) { + return 1; // higher rank + } + // If ranks are equal, then sort by service id in descending order. + return (this.serviceId < o.serviceId) ? 1 : -1; + } + + @Override + public boolean equals(final Object obj) { + if ( obj instanceof ProviderInfo ) { + return ((ProviderInfo)obj).serviceId == this.serviceId; + } + return false; + } + + @Override + public int hashCode() { + return provider.hashCode(); + } + } +} Propchange: sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/impl/NoClusterDiscoveryService.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/impl/NoClusterDiscoveryService.java ------------------------------------------------------------------------------ svn:keywords = author date id revision rev url Added: sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/package-info.java URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/package-info.java?rev=1467207&view=auto ============================================================================== --- sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/package-info.java (added) +++ sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/package-info.java Fri Apr 12 08:41:51 2013 @@ -0,0 +1,30 @@ +/* + * 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. + */ + +/** + * Provides a service to interface which may be implemented by applications + * to get notified on cluster topology changes. + * + * @version 1.0 + */ +@Version("1.0") +package org.apache.sling.discovery; + +import aQute.bnd.annotation.Version; + Propchange: sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/package-info.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: sling/trunk/contrib/extensions/discovery/src/main/java/org/apache/sling/discovery/package-info.java ------------------------------------------------------------------------------ svn:keywords = author date id revision rev url