sigram commented on a change in pull request #1962: URL: https://github.com/apache/lucene-solr/pull/1962#discussion_r512473892
########## File path: solr/core/src/java/org/apache/solr/core/ClusterEventProducerFactory.java ########## @@ -0,0 +1,178 @@ +package org.apache.solr.core; + +import org.apache.solr.api.CustomContainerPlugins; +import org.apache.solr.cluster.events.ClusterEvent; +import org.apache.solr.cluster.events.ClusterEventListener; +import org.apache.solr.cluster.events.ClusterEventProducer; +import org.apache.solr.cluster.events.impl.DefaultClusterEventProducer; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +/** + * This class helps in handling the initial registration of plugin-based listeners, + * when both the final {@link ClusterEventProducer} implementation and listeners + * are configured using plugins. + */ +public class ClusterEventProducerFactory implements ClusterEventProducer { + private Map<ClusterEvent.EventType, Set<ClusterEventListener>> initialListeners = new HashMap<>(); + private CustomContainerPlugins.PluginRegistryListener initialPluginListener; + private final CoreContainer cc; + private boolean created = false; + + public ClusterEventProducerFactory(CoreContainer cc) { + this.cc = cc; + initialPluginListener = new CustomContainerPlugins.PluginRegistryListener() { + @Override + public void added(CustomContainerPlugins.ApiInfo plugin) { + if (plugin == null || plugin.getInstance() == null) { + return; + } + Object instance = plugin.getInstance(); + if (instance instanceof ClusterEventListener) { + registerListener((ClusterEventListener) instance); + } + } + + @Override + public void deleted(CustomContainerPlugins.ApiInfo plugin) { + if (plugin == null || plugin.getInstance() == null) { + return; + } + Object instance = plugin.getInstance(); + if (instance instanceof ClusterEventListener) { + unregisterListener((ClusterEventListener) instance); + } + } + + @Override + public void modified(CustomContainerPlugins.ApiInfo old, CustomContainerPlugins.ApiInfo replacement) { + added(replacement); + deleted(old); + } + }; + } + + /** + * This method returns an initial plugin registry listener that helps to capture the + * freshly loaded listener plugins before the final cluster event producer is created. + * @return initial listener + */ + public CustomContainerPlugins.PluginRegistryListener getPluginRegistryListener() { + return initialPluginListener; + } + + /** + * Create a {@link ClusterEventProducer} based on the current plugin configurations. + * <p>NOTE: this method can only be called once because it has side-effects, such as + * transferring the initially collected listeners to the resulting producer's instance, and + * installing a {@link org.apache.solr.api.CustomContainerPlugins.PluginRegistryListener}. + * Calling this method more than once will result in an exception.</p> + * @param plugins current plugin configurations + * @return configured instance of cluster event producer (with side-effects, see above) + */ + public ClusterEventProducer create(CustomContainerPlugins plugins) { + if (created) { + throw new RuntimeException("this factory can be called only once!"); + } + final ClusterEventProducer clusterEventProducer; + CustomContainerPlugins.ApiInfo clusterEventProducerInfo = plugins.getPlugin(ClusterEventProducer.PLUGIN_NAME); + if (clusterEventProducerInfo != null) { + // the listener in ClusterSingletons already registered it + clusterEventProducer = (ClusterEventProducer) clusterEventProducerInfo.getInstance(); + } else { + // create the default impl + clusterEventProducer = new DefaultClusterEventProducer(cc); Review comment: I'm not sure. If we treat this as a part of core functionality (the ability to produce and listen to cluster-level events) then we need a rudimentary default implementation to be always present. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For additional commands, e-mail: issues-h...@lucene.apache.org