This is an automated email from the ASF dual-hosted git repository. jxue pushed a commit to branch helix-vm-freeze in repository https://gitbox.apache.org/repos/asf/helix.git
commit 0e658ae20a1684f687e2db5acaeb7303c5a4ebd3 Author: Molly Gao <[email protected]> AuthorDate: Thu Mar 10 14:05:33 2022 -0800 Add event handler and event listener interface (#1976) This commit creates a skeleton for event handling framework and adds the following classes: CloudEventListener interface CloudEventHandler class CloudEventHandlerFactory class --- .../helix/cloud/event/CloudEventHandler.java | 97 ++++++++++++++++++++++ .../cloud/event/CloudEventHandlerFactory.java | 45 ++++++++++ .../helix/cloud/event/CloudEventListener.java | 48 +++++++++++ 3 files changed, 190 insertions(+) diff --git a/helix-core/src/main/java/org/apache/helix/cloud/event/CloudEventHandler.java b/helix-core/src/main/java/org/apache/helix/cloud/event/CloudEventHandler.java new file mode 100644 index 000000000..295194b97 --- /dev/null +++ b/helix-core/src/main/java/org/apache/helix/cloud/event/CloudEventHandler.java @@ -0,0 +1,97 @@ +package org.apache.helix.cloud.event; + +/* + * 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. + */ + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class implements a wrapper around multiple {@link CloudEventListener}, and arrange them by + * 1. PreEventHandlerCallback -> only one allowed + * 2. Unordered CloudEventListener list -> multiple allowed + * 3. PostEventHandlerCallback -> only one allowed + * to enable an easy management of event listeners and callbacks. + */ +public class CloudEventHandler { + private static final Logger LOG = LoggerFactory.getLogger(CloudEventHandler.class.getName()); + private List<CloudEventListener> _unorderedEventListenerList = new ArrayList<>(); + private Optional<CloudEventListener> _preEventHandlerCallback; + private Optional<CloudEventListener> _postEventHandlerCallback; + + /** + * Register an event listener to the event handler. + * If no listener type is specified, register as an unordered listener. + * @param listener + */ + public void registerCloudEventListener(CloudEventListener listener) { + if (listener != null) { + switch (listener.getListenerType()) { + case PRE_EVENT_HANDLER: + _preEventHandlerCallback = Optional.of(listener); + break; + case POST_EVENT_HANDLER: + _postEventHandlerCallback = Optional.of(listener); + break; + case UNORDERED: + default: + _unorderedEventListenerList.add(listener); + break; + } + } + } + + /** + * Unregister an event listener to the event handler. + * @param listener + */ + public void unregisterCloudEventListener(CloudEventListener listener) { + _unorderedEventListenerList.remove(listener); + } + + /** + * Trigger the callback / listeners in order of + * 1. PreEventHandlerCallback + * 2. Unordered CloudEventListener list + * 3. PostEventHandlerCallback + * @param eventInfo the object contains any information about the incoming event + */ + public void onPause(Object eventInfo) { + _preEventHandlerCallback.ifPresent(callback -> callback.onPause(eventInfo)); + _unorderedEventListenerList.parallelStream().forEach(listener -> listener.onPause(eventInfo)); + _postEventHandlerCallback.ifPresent(callback -> callback.onPause(eventInfo)); + } + + /** + * Trigger the callback / listeners in order of + * 1. PreEventHandlerCallback + * 2. Unordered CloudEventListener list + * 3. PostEventHandlerCallback + * @param eventInfo the object contains any information about the incoming event + */ + public void onResume(Object eventInfo) { + _preEventHandlerCallback.ifPresent(callback -> callback.onResume(eventInfo)); + _unorderedEventListenerList.parallelStream().forEach(listener -> listener.onResume(eventInfo)); + _postEventHandlerCallback.ifPresent(callback -> callback.onResume(eventInfo)); + } +} diff --git a/helix-core/src/main/java/org/apache/helix/cloud/event/CloudEventHandlerFactory.java b/helix-core/src/main/java/org/apache/helix/cloud/event/CloudEventHandlerFactory.java new file mode 100644 index 000000000..1b1a53c3e --- /dev/null +++ b/helix-core/src/main/java/org/apache/helix/cloud/event/CloudEventHandlerFactory.java @@ -0,0 +1,45 @@ +package org.apache.helix.cloud.event; + +/* + * 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. + */ + +/** + * This class is the factory for singleton class {@link CloudEventHandler} + */ +public class CloudEventHandlerFactory { + private static CloudEventHandler INSTANCE = null; + + private CloudEventHandlerFactory() { + } + + /** + * Get a CloudEventHandler instance + * @return + */ + public static CloudEventHandler getInstance() { + if (INSTANCE == null) { + synchronized (CloudEventHandlerFactory.class) { + if (INSTANCE == null) { + INSTANCE = new CloudEventHandler(); + } + } + } + return INSTANCE; + } +} diff --git a/helix-core/src/main/java/org/apache/helix/cloud/event/CloudEventListener.java b/helix-core/src/main/java/org/apache/helix/cloud/event/CloudEventListener.java new file mode 100644 index 000000000..b4adcc396 --- /dev/null +++ b/helix-core/src/main/java/org/apache/helix/cloud/event/CloudEventListener.java @@ -0,0 +1,48 @@ +package org.apache.helix.cloud.event; + +/* + * 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. + */ + +/** + * This class is a basic unit for callbacks to handle events. + * The listeners can be registered to {@link CloudEventHandler}. + */ +public interface CloudEventListener { + enum ListenerType { + PRE_EVENT_HANDLER, UNORDERED, POST_EVENT_HANDLER + } + + /** + * Callback for when a pause event is incoming + * @param eventInfo the info of the incoming event + */ + void onPause(Object eventInfo); + + /** + * Callback for when a pause event finishes + * @param eventInfo the info of the incoming event + */ + void onResume(Object eventInfo); + + /** + * Get the listener type of a listener + * @return the type of the listener + */ + ListenerType getListenerType(); +}
