You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@helix.apache.org by GitBox <gi...@apache.org> on 2022/03/13 01:13:15 UTC
[GitHub] [helix] qqu0127 commented on a change in pull request #1978: Enable HelixManager as an event listener
qqu0127 commented on a change in pull request #1978:
URL: https://github.com/apache/helix/pull/1978#discussion_r825366482
##########
File path: helix-core/src/main/java/org/apache/helix/HelixCloudProperty.java
##########
@@ -180,4 +185,20 @@ public void setCloudRequestTimeout(long cloudRequestTimeout) {
public void setCustomizedCloudProperties(Properties customizedCloudProperties) {
_customizedCloudProperties.putAll(customizedCloudProperties);
}
+
+ public boolean isCloudEventCallbackEnabled() {
+ return _isCloudEventCallbackEnabled;
+ }
+
+ public void enableCloudEventCallback(boolean enabled) {
+ _isCloudEventCallbackEnabled = enabled;
+ }
Review comment:
nit: rename to `setCloudEventCallbackEnabled`? since it can be either enabled or disabled.
##########
File path: helix-core/src/main/java/org/apache/helix/cloud/event/helix/CloudEventCallbackProperty.java
##########
@@ -0,0 +1,233 @@
+package org.apache.helix.cloud.event.helix;
+
+/*
+ * 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.Arrays;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.function.BiConsumer;
+
+import org.apache.helix.HelixManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * A property for users to customize the behavior of a Helix manager as a cloud event listener
+ * Use this
+ */
+public class CloudEventCallbackProperty {
+ private static final Logger LOG =
+ LoggerFactory.getLogger(CloudEventCallbackProperty.class.getName());
+
+ // The key for user to pass in callback impl class name in the constructor
+ public static final String CALLBACK_IMPL_CLASS_NAME = "callbackImplClassName";
+ private ConcurrentHashMap<OnPauseOperations, BiConsumer<HelixManager, Object>>
+ _onPauseOperationMap;
+ private ConcurrentHashMap<OnResumeOperations, BiConsumer<HelixManager, Object>>
+ _onResumeOperationMap;
+ private String _callbackImplClassName;
+
+ /**
+ * Constructor
+ * @param userArgs A map contains information that users pass in
+ */
+ public CloudEventCallbackProperty(Map<String, String> userArgs) {
+ _callbackImplClassName = userArgs.get(CALLBACK_IMPL_CLASS_NAME);
+ _onPauseOperationMap = new ConcurrentHashMap<>();
+ _onResumeOperationMap = new ConcurrentHashMap<>();
+ }
+
+ /**
+ * A collection of types of optional Helix-supported operations
+ */
+ public enum OptionalHelixOperation {
+ MAINTENANCE_MODE
+ }
+
+ /**
+ * A collection of types and positions for user to plugin customized callback
+ */
+ public enum UserDefinedCallbackType {
+ PRE_ON_PAUSE, POST_ON_PAUSE, PRE_ON_RESUME, POST_ON_RESUME,
+ }
+
+ private enum OnPauseOperations {
+ PRE_ON_PAUSE, MAINTENANCE_MODE, DEFAULT_HELIX_OPERATION, POST_ON_PAUSE
+ }
+
+ private enum OnResumeOperations {
+ PRE_ON_RESUME, DEFAULT_HELIX_OPERATION, MAINTENANCE_MODE, POST_ON_RESUME,
+ }
+
+ /**
+ * Trigger the registered onPause callbacks one by one
+ * Order:
+ * 1. PRE onPause
+ * 2. Helix optional: Maintenance Mode
+ * 3. Helix default
+ * 4. POST onPause
Review comment:
It seems ordering matters, and we are relying on the declaration ordering of enums.
Let's add some comments to enums to indicate this. Alternatively, we can have a fixed array to iterate.
##########
File path: helix-core/src/main/java/org/apache/helix/cloud/event/helix/CloudEventCallbackProperty.java
##########
@@ -0,0 +1,233 @@
+package org.apache.helix.cloud.event.helix;
+
+/*
+ * 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.Arrays;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.function.BiConsumer;
+
+import org.apache.helix.HelixManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * A property for users to customize the behavior of a Helix manager as a cloud event listener
+ * Use this
+ */
+public class CloudEventCallbackProperty {
+ private static final Logger LOG =
+ LoggerFactory.getLogger(CloudEventCallbackProperty.class.getName());
+
+ // The key for user to pass in callback impl class name in the constructor
+ public static final String CALLBACK_IMPL_CLASS_NAME = "callbackImplClassName";
+ private ConcurrentHashMap<OnPauseOperations, BiConsumer<HelixManager, Object>>
+ _onPauseOperationMap;
+ private ConcurrentHashMap<OnResumeOperations, BiConsumer<HelixManager, Object>>
+ _onResumeOperationMap;
+ private String _callbackImplClassName;
Review comment:
nit: These could be final?
Also can we use `Map` as upcast type?
##########
File path: helix-core/src/main/java/org/apache/helix/cloud/event/helix/CloudEventCallbackProperty.java
##########
@@ -0,0 +1,233 @@
+package org.apache.helix.cloud.event.helix;
+
+/*
+ * 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.Arrays;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.function.BiConsumer;
+
+import org.apache.helix.HelixManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * A property for users to customize the behavior of a Helix manager as a cloud event listener
+ * Use this
+ */
+public class CloudEventCallbackProperty {
+ private static final Logger LOG =
+ LoggerFactory.getLogger(CloudEventCallbackProperty.class.getName());
+
+ // The key for user to pass in callback impl class name in the constructor
+ public static final String CALLBACK_IMPL_CLASS_NAME = "callbackImplClassName";
+ private ConcurrentHashMap<OnPauseOperations, BiConsumer<HelixManager, Object>>
+ _onPauseOperationMap;
+ private ConcurrentHashMap<OnResumeOperations, BiConsumer<HelixManager, Object>>
+ _onResumeOperationMap;
+ private String _callbackImplClassName;
+
+ /**
+ * Constructor
+ * @param userArgs A map contains information that users pass in
+ */
+ public CloudEventCallbackProperty(Map<String, String> userArgs) {
+ _callbackImplClassName = userArgs.get(CALLBACK_IMPL_CLASS_NAME);
+ _onPauseOperationMap = new ConcurrentHashMap<>();
+ _onResumeOperationMap = new ConcurrentHashMap<>();
+ }
+
+ /**
+ * A collection of types of optional Helix-supported operations
+ */
+ public enum OptionalHelixOperation {
+ MAINTENANCE_MODE
+ }
+
+ /**
+ * A collection of types and positions for user to plugin customized callback
+ */
+ public enum UserDefinedCallbackType {
+ PRE_ON_PAUSE, POST_ON_PAUSE, PRE_ON_RESUME, POST_ON_RESUME,
+ }
+
+ private enum OnPauseOperations {
+ PRE_ON_PAUSE, MAINTENANCE_MODE, DEFAULT_HELIX_OPERATION, POST_ON_PAUSE
+ }
+
+ private enum OnResumeOperations {
+ PRE_ON_RESUME, DEFAULT_HELIX_OPERATION, MAINTENANCE_MODE, POST_ON_RESUME,
+ }
+
+ /**
+ * Trigger the registered onPause callbacks one by one
+ * Order:
+ * 1. PRE onPause
+ * 2. Helix optional: Maintenance Mode
+ * 3. Helix default
+ * 4. POST onPause
+ * @param helixManager Helix manager to perform operations
+ * @param eventInfo Information of the event provided by upsteam
+ * @param callbackImplClass Implementation class for Helix default and optional operations
+ */
+ public void onPause(HelixManager helixManager, Object eventInfo,
+ AbstractCloudEventCallbackImpl callbackImplClass) {
+ Iterator<OnPauseOperations> iterator = Arrays.stream(OnPauseOperations.values()).iterator();
+ while (iterator.hasNext()) {
+ OnPauseOperations operation = iterator.next();
+ switch (operation) {
+ case DEFAULT_HELIX_OPERATION:
+ callbackImplClass.onPauseDefaultHelixOperation(helixManager, eventInfo);
+ break;
+ case MAINTENANCE_MODE:
+ callbackImplClass.onPauseMaintenanceMode(helixManager, eventInfo);
+ break;
+ default:
+ _onPauseOperationMap.getOrDefault(operation, (manager, info) -> {
+ }).accept(helixManager, eventInfo);
+ }
+ }
+ }
+
+ /**
+ * Trigger the registered onResume callbacks one by one
+ * Order:
+ * 1. PRE onResume
+ * 2. Helix default
+ * 3. Helix optional: Maintenance Mode
+ * 4. POST onResume
Review comment:
Same as above
##########
File path: helix-core/src/main/java/org/apache/helix/cloud/event/helix/CloudEventCallbackProperty.java
##########
@@ -0,0 +1,233 @@
+package org.apache.helix.cloud.event.helix;
+
+/*
+ * 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.Arrays;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.function.BiConsumer;
+
+import org.apache.helix.HelixManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * A property for users to customize the behavior of a Helix manager as a cloud event listener
+ * Use this
+ */
+public class CloudEventCallbackProperty {
+ private static final Logger LOG =
+ LoggerFactory.getLogger(CloudEventCallbackProperty.class.getName());
+
+ // The key for user to pass in callback impl class name in the constructor
+ public static final String CALLBACK_IMPL_CLASS_NAME = "callbackImplClassName";
+ private ConcurrentHashMap<OnPauseOperations, BiConsumer<HelixManager, Object>>
+ _onPauseOperationMap;
+ private ConcurrentHashMap<OnResumeOperations, BiConsumer<HelixManager, Object>>
+ _onResumeOperationMap;
+ private String _callbackImplClassName;
+
+ /**
+ * Constructor
+ * @param userArgs A map contains information that users pass in
+ */
+ public CloudEventCallbackProperty(Map<String, String> userArgs) {
+ _callbackImplClassName = userArgs.get(CALLBACK_IMPL_CLASS_NAME);
+ _onPauseOperationMap = new ConcurrentHashMap<>();
+ _onResumeOperationMap = new ConcurrentHashMap<>();
+ }
+
+ /**
+ * A collection of types of optional Helix-supported operations
+ */
+ public enum OptionalHelixOperation {
+ MAINTENANCE_MODE
+ }
+
+ /**
+ * A collection of types and positions for user to plugin customized callback
+ */
+ public enum UserDefinedCallbackType {
+ PRE_ON_PAUSE, POST_ON_PAUSE, PRE_ON_RESUME, POST_ON_RESUME,
+ }
+
+ private enum OnPauseOperations {
+ PRE_ON_PAUSE, MAINTENANCE_MODE, DEFAULT_HELIX_OPERATION, POST_ON_PAUSE
+ }
+
+ private enum OnResumeOperations {
+ PRE_ON_RESUME, DEFAULT_HELIX_OPERATION, MAINTENANCE_MODE, POST_ON_RESUME,
+ }
+
+ /**
+ * Trigger the registered onPause callbacks one by one
+ * Order:
+ * 1. PRE onPause
+ * 2. Helix optional: Maintenance Mode
+ * 3. Helix default
+ * 4. POST onPause
+ * @param helixManager Helix manager to perform operations
+ * @param eventInfo Information of the event provided by upsteam
+ * @param callbackImplClass Implementation class for Helix default and optional operations
+ */
+ public void onPause(HelixManager helixManager, Object eventInfo,
+ AbstractCloudEventCallbackImpl callbackImplClass) {
+ Iterator<OnPauseOperations> iterator = Arrays.stream(OnPauseOperations.values()).iterator();
+ while (iterator.hasNext()) {
Review comment:
Why use iterator over for-loop with `OnPauseOperations.values()`?
##########
File path: helix-core/src/main/java/org/apache/helix/manager/zk/ZKHelixManager.java
##########
@@ -306,6 +317,26 @@ public ZKHelixManager(String clusterName, String instanceName, InstanceType inst
}
_monitorLevel = configuredMonitorLevel;
+ if (helixManagerProperty != null) {
+ HelixCloudProperty helixCloudProperty = _helixManagerProperty.getHelixCloudProperty();
+ if (helixCloudProperty != null && helixCloudProperty.isCloudEventCallbackEnabled()) {
+ // Load callback implementation class
+ if ((_cloudEventCallbackProperty = helixCloudProperty.getCloudEventCallbackProperty())
+ != null) {
+ _callbackImplClass = loadCloudEventCallbackImplClass(
+ _cloudEventCallbackProperty.getCallbackImplClassName());
+ LOG.info("Using {} as cloud event callback impl class.",
+ _callbackImplClass.getClass().getCanonicalName());
+ }
+ if (_callbackImplClass == null) {
+ throw new HelixException("No implementation class is loaded for cloud event callback.");
+ }
+
+ // Register this helix manager as a listener
+ CloudEventHandlerFactory.getInstance().registerCloudEventListener(this);
Review comment:
So we are leaking "this" in a constructor. I'm not sure what it will mean for VM freeze case, but generally, it's potentially an issue where external class with this reference could call in another thread before "this" is fully constructed.
##########
File path: helix-core/src/main/java/org/apache/helix/cloud/event/helix/AbstractCloudEventCallbackImpl.java
##########
@@ -0,0 +1,46 @@
+package org.apache.helix.cloud.event.helix;
+
+/*
+ * 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 org.apache.helix.HelixManager;
+import sun.reflect.generics.reflectiveObjects.NotImplementedException;
+
+public abstract class AbstractCloudEventCallbackImpl {
Review comment:
Can this be an interface?
--
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.
To unsubscribe, e-mail: reviews-unsubscribe@helix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@helix.apache.org
For additional commands, e-mail: reviews-help@helix.apache.org