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 2019/08/08 23:45:27 UTC

[GitHub] [helix] jiajunwang commented on a change in pull request #388: Add ChangeDetector interface and ResourceChangeDetector implementation

jiajunwang commented on a change in pull request #388: Add ChangeDetector interface and ResourceChangeDetector implementation
URL: https://github.com/apache/helix/pull/388#discussion_r312284218
 
 

 ##########
 File path: helix-core/src/main/java/org/apache/helix/controller/changedetector/ResourceChangeDetector.java
 ##########
 @@ -0,0 +1,132 @@
+package org.apache.helix.controller.changedetector;
+
+/*
+ * 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.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Map;
+import org.apache.helix.HelixConstants;
+import org.apache.helix.HelixException;
+import org.apache.helix.HelixProperty;
+import org.apache.helix.controller.dataproviders.ResourceControllerDataProvider;
+
+public class ResourceChangeDetector implements ChangeDetector {
+
+  private ResourceChangeCache _oldCache; // cache snapshot for previous pipeline run
+  private ResourceChangeCache _newCache; // cache snapshot for this pipeline run
+  private Map<String, ? extends HelixProperty> _oldPropertyMap;
+  private Map<String, ? extends HelixProperty> _newPropertyMap;
+
+  /**
+   * Compare the underlying HelixProperty objects and produce a collection of names of changed
+   * properties.
+   * @return
+   */
+  private Collection<String> getChangedItems() {
+    Collection<String> changedItems = new HashSet<>();
+    _oldPropertyMap.forEach((name, property) -> {
+      if (_newPropertyMap.containsKey(name) && !property.equals(_newPropertyMap.get(name))) {
+        changedItems.add(name);
+      }
+    });
+    return changedItems;
+  }
+
+  /**
+   * Return a collection of names that are newly added.
+   * @return
+   */
+  private Collection<String> getAddedItems() {
+    Collection<String> addedItems = new HashSet<>(_newPropertyMap.keySet());
+    addedItems.removeAll(_oldPropertyMap.keySet());
+    return addedItems;
+  }
+
+  /**
+   * Return a collection of names that were removed.
+   * @return
+   */
+  private Collection<String> getRemovedItems() {
+    Collection<String> removedItems = new HashSet<>(_oldPropertyMap.keySet());
+    removedItems.removeAll(_newPropertyMap.keySet());
+    return removedItems;
+  }
+
+  /**
+   * Based on the change type given, call the right getters for two property maps.
+   * @param changeType
+   */
+  private void assignPropertyMapByType(HelixConstants.ChangeType changeType) {
+    switch (changeType) {
+    case INSTANCE_CONFIG:
+      _oldPropertyMap = _oldCache.getInstanceConfigMap();
+      _newPropertyMap = _newCache.getInstanceConfigMap();
+      break;
+    case IDEAL_STATE:
+      _oldPropertyMap = _oldCache.getIdealStateMap();
+      _newPropertyMap = _newCache.getIdealStateMap();
+      break;
+    case RESOURCE_CONFIG:
+      _oldPropertyMap = _oldCache.getResourceConfigMap();
+      _newPropertyMap = _newCache.getResourceConfigMap();
+      break;
+    case LIVE_INSTANCE:
+      _oldPropertyMap = _oldCache.getLiveInstances();
+      _newPropertyMap = _newCache.getLiveInstances();
+      break;
+    default:
+      throw new HelixException(String
+          .format("ResourceChangeDetector does not support the given change type: %s", changeType));
+    }
+  }
+
+  /**
+   * Makes the current newCache the oldCache and reads in the up-to-date cache for change
+   * computation. To be called in the controller pipeline.
+   * @param dataProvider newly refreshed DataProvider (cache)
+   */
+  public void updateCache(ResourceControllerDataProvider dataProvider) {
+    _oldCache = new ResourceChangeCache(_newCache);
+    _newCache = new ResourceChangeCache(dataProvider);
+  }
+
+  @Override
+  public Collection<HelixConstants.ChangeType> getChangeTypes() {
+    return Collections.unmodifiableSet(_newCache.getChangedTypes());
+  }
+
+  @Override
+  public Collection<String> getChangesByType(HelixConstants.ChangeType changeType) {
+    assignPropertyMapByType(changeType);
+    return getChangedItems();
 
 Review comment:
   Is it possible we do the calculation once during the updateCahce()?

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@helix.apache.org
For additional commands, e-mail: reviews-help@helix.apache.org