zhangmeng916 commented on a change in pull request #1935: URL: https://github.com/apache/helix/pull/1935#discussion_r791024954
########## File path: helix-core/src/main/java/org/apache/helix/cloud/topology/VirtualTopologyGroupStrategy.java ########## @@ -0,0 +1,80 @@ +package org.apache.helix.cloud.topology; + +/* + * 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 com.google.common.collect.ImmutableMap; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import org.apache.helix.util.HelixUtil; + +public enum VirtualTopologyGroupStrategy { + + /** + * A default assignment strategy that is deterministic and stable where: + * 1. assignment is guaranteed consistent for same inputs. + * 2. number of instance shuffles is reduced. Review comment: What do we compare and conclude the "instance shuffles is reduced"? Could you please illustrate? Is this the best strategy for deterministic choices? ########## File path: helix-rest/src/main/java/org/apache/helix/rest/server/service/VirtualTopologyGroupService.java ########## @@ -0,0 +1,163 @@ +package org.apache.helix.rest.server.service; + +/* + * 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 com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Preconditions; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import org.apache.commons.lang3.StringUtils; +import org.apache.helix.AccessOption; +import org.apache.helix.ConfigAccessor; +import org.apache.helix.HelixAdmin; +import org.apache.helix.HelixDataAccessor; +import org.apache.helix.HelixException; +import org.apache.helix.common.VirtualTopologyGroupConstants; +import org.apache.helix.cloud.topology.VirtualTopologyGroupStrategy; +import org.apache.helix.model.CloudConfig; +import org.apache.helix.model.ClusterConfig; +import org.apache.helix.model.HelixConfigScope; +import org.apache.helix.model.InstanceConfig; +import org.apache.helix.model.builder.HelixConfigScopeBuilder; +import org.apache.helix.rest.server.json.cluster.ClusterTopology; +import org.apache.helix.zookeeper.datamodel.ZNRecord; +import org.apache.helix.zookeeper.zkclient.DataUpdater; + + +/** + * Service for virtual topology group. + */ +public class VirtualTopologyGroupService { + private final HelixAdmin _helixAdmin; + private final ClusterService _clusterService; + private final ConfigAccessor _configAccessor; + private final HelixDataAccessor _dataAccessor; + private final VirtualTopologyGroupStrategy _strategy; + + public VirtualTopologyGroupService(HelixAdmin helixAdmin, ClusterService clusterService, + ConfigAccessor configAccessor, HelixDataAccessor dataAccessor) { + _helixAdmin = helixAdmin; + _clusterService = clusterService; + _configAccessor = configAccessor; + _dataAccessor = dataAccessor; + _strategy = VirtualTopologyGroupStrategy.DEFAULT; + } + + /** + * Add virtual topology group for a cluster. + * This includes calculating the virtual group assignment for all instances in the cluster then update instance config + * and cluster config. Cluster is expected to enter maintenanceMode during config update, this is either enabled/disabled + * in place this method or handled by client side code. + * @param clusterName the cluster name. + * @param customFields custom fields, {@link VirtualTopologyGroupConstants#GROUP_NAME} + * and {@link VirtualTopologyGroupConstants#GROUP_NUMBER} are required + * @param enterMaintenanceMode if enabled, the cluster will enter maintenance mode during the setup and exit once it + * completes. Otherwise, it's expected the maintenanceMode is controlled by client side. + */ + public void addVirtualTopologyGroup(String clusterName, Map<String, String> customFields, boolean enterMaintenanceMode) { + // only support if CLOUD_ENABLED AND VIRTUAL_GROUP_ENABLED + CloudConfig cloudConfig = _configAccessor.getCloudConfig(clusterName); + if (cloudConfig == null || !cloudConfig.isCloudEnabled()) { + throw new HelixException( + "Cloud is not enabled, addVirtualTopologyGroup is not allowed to run in non-cloud environment."); + } + ClusterConfig clusterConfig = _configAccessor.getClusterConfig(clusterName); + if (!clusterConfig.isVirtualGroupEnabled()) { Review comment: Actually I don't see how adding this config could improve safety. As long as cloud config is on, we allow virtual grouping, at least this is the agreement for now. Whether the virtual grouping is used correctly is purely decided by users' knowledge. Asking users to turn on another config does not really help ensure the correctness. ########## File path: helix-rest/src/main/java/org/apache/helix/rest/server/service/VirtualTopologyGroupService.java ########## @@ -0,0 +1,163 @@ +package org.apache.helix.rest.server.service; + +/* + * 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 com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Preconditions; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import org.apache.commons.lang3.StringUtils; +import org.apache.helix.AccessOption; +import org.apache.helix.ConfigAccessor; +import org.apache.helix.HelixAdmin; +import org.apache.helix.HelixDataAccessor; +import org.apache.helix.HelixException; +import org.apache.helix.common.VirtualTopologyGroupConstants; +import org.apache.helix.cloud.topology.VirtualTopologyGroupStrategy; +import org.apache.helix.model.CloudConfig; +import org.apache.helix.model.ClusterConfig; +import org.apache.helix.model.HelixConfigScope; +import org.apache.helix.model.InstanceConfig; +import org.apache.helix.model.builder.HelixConfigScopeBuilder; +import org.apache.helix.rest.server.json.cluster.ClusterTopology; +import org.apache.helix.zookeeper.datamodel.ZNRecord; +import org.apache.helix.zookeeper.zkclient.DataUpdater; + + +/** + * Service for virtual topology group. + */ +public class VirtualTopologyGroupService { + private final HelixAdmin _helixAdmin; + private final ClusterService _clusterService; + private final ConfigAccessor _configAccessor; + private final HelixDataAccessor _dataAccessor; + private final VirtualTopologyGroupStrategy _strategy; + + public VirtualTopologyGroupService(HelixAdmin helixAdmin, ClusterService clusterService, + ConfigAccessor configAccessor, HelixDataAccessor dataAccessor) { + _helixAdmin = helixAdmin; + _clusterService = clusterService; + _configAccessor = configAccessor; + _dataAccessor = dataAccessor; + _strategy = VirtualTopologyGroupStrategy.DEFAULT; + } + + /** + * Add virtual topology group for a cluster. + * This includes calculating the virtual group assignment for all instances in the cluster then update instance config + * and cluster config. Cluster is expected to enter maintenanceMode during config update, this is either enabled/disabled + * in place this method or handled by client side code. + * @param clusterName the cluster name. + * @param customFields custom fields, {@link VirtualTopologyGroupConstants#GROUP_NAME} + * and {@link VirtualTopologyGroupConstants#GROUP_NUMBER} are required + * @param enterMaintenanceMode if enabled, the cluster will enter maintenance mode during the setup and exit once it + * completes. Otherwise, it's expected the maintenanceMode is controlled by client side. + */ + public void addVirtualTopologyGroup(String clusterName, Map<String, String> customFields, boolean enterMaintenanceMode) { + // only support if CLOUD_ENABLED AND VIRTUAL_GROUP_ENABLED + CloudConfig cloudConfig = _configAccessor.getCloudConfig(clusterName); + if (cloudConfig == null || !cloudConfig.isCloudEnabled()) { + throw new HelixException( + "Cloud is not enabled, addVirtualTopologyGroup is not allowed to run in non-cloud environment."); + } + ClusterConfig clusterConfig = _configAccessor.getClusterConfig(clusterName); + if (!clusterConfig.isVirtualGroupEnabled()) { + throw new HelixException("Virtual Group is disabled in cluster " + clusterName); + } + // validation + String groupName = customFields.get(VirtualTopologyGroupConstants.GROUP_NAME); + String groupNumberStr = Preconditions.checkNotNull( + customFields.get(VirtualTopologyGroupConstants.GROUP_NUMBER), + "virtualTopologyGroupNumber cannot be empty!"); + Preconditions.checkState(!StringUtils.isEmpty(groupName), "virtualTopologyGroupName cannot be empty!"); + int numGroups = Integer.parseInt(groupNumberStr); + + // compute group assignment + ClusterTopology clusterTopology = _clusterService.getClusterTopology(clusterName); + Map<String, Set<String>> assignment = + _strategy.computeAssignment(numGroups, groupName, clusterTopology.toZoneMapping()); + + if (enterMaintenanceMode) { + _helixAdmin.manuallyEnableMaintenanceMode(clusterName, true, + "Enable maintenanceMode for virtual topology group change.", customFields); + } + Preconditions.checkState(_helixAdmin.isInMaintenanceMode(clusterName), + "Cluster is not in maintenance mode. This is required for virtual topology group setting. " + + "Please enable enterMaintenanceMode or enter maintenance mode for the cluster prior to the API call."); + + updateConfigs(clusterName, clusterConfig, assignment); + if (enterMaintenanceMode) { + _helixAdmin.manuallyEnableMaintenanceMode(clusterName, false, + "Disable maintenanceMode after virtual topology group change.", customFields); + } + } + + private void updateConfigs(String clusterName, ClusterConfig clusterConfig, Map<String, Set<String>> assignment) { + List<String> zkPaths = new ArrayList<>(); + List<DataUpdater<ZNRecord>> updaters = new ArrayList<>(); + createInstanceConfigUpdater(clusterName, assignment).forEach((zkPath, updater) -> { + zkPaths.add(zkPath); + updaters.add(updater); + }); + boolean[] results = _dataAccessor.updateChildren(zkPaths, updaters, AccessOption.EPHEMERAL); + for (int i = 0; i < results.length; i++) { + if (!results[i]) { + throw new HelixException("Failed to update instance config for path " + zkPaths.get(i)); Review comment: So this fails, we have configs partially updated. Will this impact controller's pipeline logic before we update them again? Please provide some insights. ########## File path: helix-core/src/main/java/org/apache/helix/common/VirtualTopologyGroupConstants.java ########## @@ -0,0 +1,31 @@ +package org.apache.helix.common; + +/* + * 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.cloud.azure.AzureConstants; Review comment: +1. Why virtual topology should be bound with Azure? It should not have anything dependent on a specific type of cloud environment. Before using virtual grouping, you only need cloud config is on, but you don't need the topology to be the Azure one. ########## File path: helix-rest/src/test/java/org/apache/helix/rest/server/service/TestVirtualTopologyGroupService.java ########## @@ -0,0 +1,172 @@ +package org.apache.helix.rest.server.service; + +/* + * 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 com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import org.apache.helix.ConfigAccessor; +import org.apache.helix.HelixAdmin; +import org.apache.helix.HelixDataAccessor; +import org.apache.helix.HelixException; +import org.apache.helix.cloud.azure.AzureConstants; +import org.apache.helix.cloud.constants.CloudProvider; +import org.apache.helix.model.CloudConfig; +import org.apache.helix.model.ClusterConfig; +import org.apache.helix.model.HelixConfigScope; +import org.apache.helix.model.InstanceConfig; +import org.apache.helix.model.builder.HelixConfigScopeBuilder; +import org.apache.helix.rest.server.json.cluster.ClusterTopology; +import org.apache.helix.zookeeper.datamodel.ZNRecord; +import org.apache.helix.zookeeper.zkclient.DataUpdater; +import org.testng.Assert; +import org.testng.annotations.BeforeTest; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +import static org.apache.helix.common.VirtualTopologyGroupConstants.*; +import static org.mockito.Mockito.*; + + +public class TestVirtualTopologyGroupService { + private static final String TEST_CLUSTER = "Test_Cluster"; + private static final String TEST_CLUSTER0 = "TestCluster_0"; + private static final String TEST_CLUSTER1 = "TestCluster_1"; + private static final String TEST_CLUSTER2 = "TestCluster_2"; + + private final ConfigAccessor _configAccessor = mock(ConfigAccessor.class); + private final HelixDataAccessor _dataAccessor = mock(HelixDataAccessor.class); + private InstanceConfig _instanceConfig0; + private InstanceConfig _instanceConfig1; + private InstanceConfig _instanceConfig2; + private Map<String, DataUpdater<ZNRecord>> _updaterMap; + private VirtualTopologyGroupService _service; + + @BeforeTest + public void prepare() { + Map<String, Set<String>> assignment = new HashMap<>(); + _instanceConfig0 = new InstanceConfig("instance_0"); + _instanceConfig0.setDomain("helixZoneId=zone0"); + _instanceConfig1 = new InstanceConfig("instance_1"); + _instanceConfig1.setDomain("helixZoneId=zone0"); + _instanceConfig2 = new InstanceConfig("instance_2"); + _instanceConfig2.setDomain("helixZoneId=zone1"); + + assignment.put("virtual_group_0", ImmutableSet.of("instance_0", "instance_1")); + assignment.put("virtual_group_1", ImmutableSet.of("instance_2")); + _updaterMap = VirtualTopologyGroupService.createInstanceConfigUpdater(TEST_CLUSTER, assignment); + + ClusterConfig clusterConfig = new ClusterConfig(TEST_CLUSTER0); + clusterConfig.setFaultZoneType(AzureConstants.AZURE_FAULT_ZONE_TYPE); + clusterConfig.setTopology(AzureConstants.AZURE_TOPOLOGY); + clusterConfig.setVirtualGroupEnabled(true); + when(_configAccessor.getClusterConfig(TEST_CLUSTER0)).thenReturn(clusterConfig); + + CloudConfig.Builder cloudConfigBuilder = new CloudConfig.Builder(); + cloudConfigBuilder.setCloudEnabled(true); + cloudConfigBuilder.setCloudProvider(CloudProvider.AZURE); + cloudConfigBuilder.setCloudID("TestID"); + CloudConfig cloudConfig = cloudConfigBuilder.build(); + when(_configAccessor.getCloudConfig(TEST_CLUSTER0)).thenReturn(cloudConfig); + when(_configAccessor.getCloudConfig(TEST_CLUSTER2)).thenReturn(cloudConfig); + + HelixAdmin helixAdmin = mock(HelixAdmin.class); + when(helixAdmin.isInMaintenanceMode(anyString())).thenReturn(true); + + boolean[] results = new boolean[2]; + results[0] = results[1] = true; + when(_dataAccessor.updateChildren(anyList(), anyList(), anyInt())).thenReturn(results); + ClusterService clusterService = mock(ClusterService.class); + when(clusterService.getClusterTopology(anyString())).thenReturn(prepareClusterTopology()); + _service = new VirtualTopologyGroupService(helixAdmin, clusterService, _configAccessor, _dataAccessor); + } + + @Test(expectedExceptions = HelixException.class, expectedExceptionsMessageRegExp = "Cloud is not enabled.*") + public void testClusterCloudConfigSetup() { + ClusterConfig clusterConfig1 = new ClusterConfig(TEST_CLUSTER1); + clusterConfig1.setVirtualGroupEnabled(true); + when(_configAccessor.getClusterConfig(TEST_CLUSTER1)).thenReturn(clusterConfig1); + _service.addVirtualTopologyGroup( + TEST_CLUSTER1, ImmutableMap.of(GROUP_NAME, "test-group", GROUP_NUMBER, "2"), true); + } + + @Test(expectedExceptions = HelixException.class, expectedExceptionsMessageRegExp = "Virtual Group is disabled in cluster.*") + public void testClusterVirtualGroupConfigSetup() { + ClusterConfig clusterConfig2 = new ClusterConfig(TEST_CLUSTER2); + clusterConfig2.setVirtualGroupEnabled(false); + when(_configAccessor.getClusterConfig(TEST_CLUSTER2)).thenReturn(clusterConfig2); + _service.addVirtualTopologyGroup( + TEST_CLUSTER2, ImmutableMap.of(GROUP_NAME, "test-group", GROUP_NUMBER, "2"), true); + } + + @Test + public void testService() { Review comment: Please have more concrete naming for tests. This is difficult to tell what is being tested, especially if it's in the log. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
