Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ChangeDetector interface and ResourceChangeDetector implementation #388

Merged
merged 5 commits into from
Aug 15, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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 org.apache.helix.HelixConstants;

/**
* ChangeDetector interface that will be used to track deltas in the cluster from one pipeline run
* to another. The interface methods are designed to be flexible for both the resource pipeline and
* the task pipeline.
* TODO: Consider splitting this up into two different ChangeDetector interfaces:
* TODO: PropertyBasedChangeDetector and PathBasedChangeDetector.
*/
public interface ChangeDetector {
narendly marked this conversation as resolved.
Show resolved Hide resolved

/**
* Returns all types of changes detected.
* @return a collection of ChangeTypes
*/
Collection<HelixConstants.ChangeType> getChangeTypes();
narendly marked this conversation as resolved.
Show resolved Hide resolved

/**
* Returns the names of items that changed based on the change type given.
* @return a collection of names of items that changed
*/
Collection<String> getChangesByType(HelixConstants.ChangeType changeType);
narendly marked this conversation as resolved.
Show resolved Hide resolved

/**
* Returns the names of items that were added based on the change type given.
* @return a collection of names of items that were added
*/
Collection<String> getAdditionsByType(HelixConstants.ChangeType changeType);

/**
* Returns the names of items that were removed based on the change type given.
* @return a collection of names of items that were removed
*/
Collection<String> getRemovalsByType(HelixConstants.ChangeType changeType);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
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 com.google.common.collect.Sets;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
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;

/**
* ResourceChangeDetector implements ChangeDetector. It caches resource-related metadata from
* Helix's main resource pipeline cache (DataProvider) and the computation results of change
* detection.
* WARNING: the methods of this class are not thread-safe.
*/
public class ResourceChangeDetector implements ChangeDetector {

private ResourceChangeSnapshot _oldSnapshot; // snapshot for previous pipeline run
private ResourceChangeSnapshot _newSnapshot; // snapshot for this pipeline run

// The following caches the computation results
private Map<HelixConstants.ChangeType, Collection<String>> _changedItems = new HashMap<>();
private Map<HelixConstants.ChangeType, Collection<String>> _addedItems = new HashMap<>();
private Map<HelixConstants.ChangeType, Collection<String>> _removedItems = new HashMap<>();

/**
* Compare the underlying HelixProperty objects and produce a collection of names of changed
* properties.
* @return
*/
private Collection<String> getChangedItems(Map<String, ? extends HelixProperty> oldPropertyMap,
Map<String, ? extends HelixProperty> newPropertyMap) {
Collection<String> changedItems = new HashSet<>();
oldPropertyMap.forEach((name, property) -> {
if (newPropertyMap.containsKey(name)
&& !property.getRecord().equals(newPropertyMap.get(name).getRecord())) {
changedItems.add(name);
}
});
return changedItems;
}

/**
* Return a collection of names that are newly added.
* @return
*/
private Collection<String> getAddedItems(Map<String, ? extends HelixProperty> oldPropertyMap,
Map<String, ? extends HelixProperty> newPropertyMap) {
return Sets.difference(newPropertyMap.keySet(), oldPropertyMap.keySet());
}

/**
* Return a collection of names that were removed.
* @return
*/
private Collection<String> getRemovedItems(Map<String, ? extends HelixProperty> oldPropertyMap,
Map<String, ? extends HelixProperty> newPropertyMap) {
return Sets.difference(oldPropertyMap.keySet(), newPropertyMap.keySet());
}

private void clearCachedComputation() {
_changedItems.clear();
_addedItems.clear();
_removedItems.clear();
}

/**
* Initializes old and new snapshots when ResourceChangeDetector gets its first update.
*/
private void initializeSnapshots() {
_oldSnapshot = new ResourceChangeSnapshot();
_newSnapshot = new ResourceChangeSnapshot();
}

/**
* Based on the change type given and propertyMap type, call the right getters for propertyMap.
* @param changeType
* @param snapshot
* @return
*/
private Map<String, ? extends HelixProperty> determinePropertyMapByType(
HelixConstants.ChangeType changeType, ResourceChangeSnapshot snapshot) {
switch (changeType) {
case INSTANCE_CONFIG:
return snapshot.getInstanceConfigMap();
case IDEAL_STATE:
return snapshot.getIdealStateMap();
case RESOURCE_CONFIG:
return snapshot.getResourceConfigMap();
case LIVE_INSTANCE:
return snapshot.getLiveInstances();
default:
throw new HelixException(String.format(
"ResourceChangeDetector cannot compute the names of changes for the given ChangeType: %s",
changeType));
}
}

/**
* Makes the current newSnapshot the oldSnapshot and reads in the up-to-date snapshot for change
* computation. To be called in the controller pipeline.
* @param dataProvider newly refreshed DataProvider (cache)
*/
public synchronized void updateSnapshots(ResourceControllerDataProvider dataProvider) {
// If snapshots are null, initialize them
if (_oldSnapshot == null || _newSnapshot == null) {
initializeSnapshots();
narendly marked this conversation as resolved.
Show resolved Hide resolved
}

// If there are changes, update internal states
_oldSnapshot = new ResourceChangeSnapshot(_newSnapshot);
_newSnapshot = new ResourceChangeSnapshot(dataProvider);
dataProvider.clearRefreshedChangeTypes();
narendly marked this conversation as resolved.
Show resolved Hide resolved

// Invalidate cached computation
clearCachedComputation();
}

@Override
public Collection<HelixConstants.ChangeType> getChangeTypes() {
return Collections.unmodifiableSet(_newSnapshot.getChangedTypes());
}

@Override
public Collection<String> getChangesByType(HelixConstants.ChangeType changeType) {
return _changedItems.computeIfAbsent(changeType,
changedItems -> getChangedItems(determinePropertyMapByType(changeType, _oldSnapshot),
determinePropertyMapByType(changeType, _newSnapshot)));
}

@Override
public Collection<String> getAdditionsByType(HelixConstants.ChangeType changeType) {
return _addedItems.computeIfAbsent(changeType,
changedItems -> getAddedItems(determinePropertyMapByType(changeType, _oldSnapshot),
determinePropertyMapByType(changeType, _newSnapshot)));
}

@Override
public Collection<String> getRemovalsByType(HelixConstants.ChangeType changeType) {
return _removedItems.computeIfAbsent(changeType,
changedItems -> getRemovedItems(determinePropertyMapByType(changeType, _oldSnapshot),
determinePropertyMapByType(changeType, _newSnapshot)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
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.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.apache.helix.HelixConstants;
import org.apache.helix.controller.dataproviders.ResourceControllerDataProvider;
import org.apache.helix.model.IdealState;
import org.apache.helix.model.InstanceConfig;
import org.apache.helix.model.LiveInstance;
import org.apache.helix.model.ResourceConfig;

/**
* ResourceChangeSnapshot is a POJO that contains the following Helix metadata:
* 1. InstanceConfig
* 2. IdealState
* 3. ResourceConfig
* 4. LiveInstance
* 5. Changed property types
* It serves as a snapshot of the main controller cache to enable the difference (change)
* calculation between two rounds of the pipeline run.
*/
class ResourceChangeSnapshot {

private Set<HelixConstants.ChangeType> _changedTypes;
private Map<String, InstanceConfig> _instanceConfigMap;
private Map<String, IdealState> _idealStateMap;
private Map<String, ResourceConfig> _resourceConfigMap;
private Map<String, LiveInstance> _liveInstances;

/**
* Default constructor that constructs an empty snapshot.
*/
ResourceChangeSnapshot() {
_changedTypes = new HashSet<>();
_instanceConfigMap = new HashMap<>();
_idealStateMap = new HashMap<>();
_resourceConfigMap = new HashMap<>();
_liveInstances = new HashMap<>();
}

/**
* Constructor using controller cache (ResourceControllerDataProvider).
* @param dataProvider
*/
ResourceChangeSnapshot(ResourceControllerDataProvider dataProvider) {
// Consume all changed types from DataProvider. This is because it is possible that the
// DataProvider has gone through multiple rounds of rebalancing prior to the ChangeDetector
// consuming changed types.
_changedTypes = new HashSet<>(dataProvider.getRefreshedChangeTypes());
dataProvider.clearRefreshedChangeTypes();

_instanceConfigMap = new HashMap<>(dataProvider.getInstanceConfigMap());
_idealStateMap = new HashMap<>(dataProvider.getIdealStates());
_resourceConfigMap = new HashMap<>(dataProvider.getResourceConfigMap());
_liveInstances = new HashMap<>(dataProvider.getLiveInstances());
}

/**
* Copy constructor for ResourceChangeCache.
* @param cache
*/
ResourceChangeSnapshot(ResourceChangeSnapshot cache) {
_changedTypes = new HashSet<>(cache._changedTypes);
_instanceConfigMap = new HashMap<>(cache._instanceConfigMap);
_idealStateMap = new HashMap<>(cache._idealStateMap);
_resourceConfigMap = new HashMap<>(cache._resourceConfigMap);
_liveInstances = new HashMap<>(cache._liveInstances);
}

Set<HelixConstants.ChangeType> getChangedTypes() {
return _changedTypes;
}

Map<String, InstanceConfig> getInstanceConfigMap() {
return _instanceConfigMap;
}

Map<String, IdealState> getIdealStateMap() {
return _idealStateMap;
}

Map<String, ResourceConfig> getResourceConfigMap() {
return _resourceConfigMap;
}

Map<String, LiveInstance> getLiveInstances() {
return _liveInstances;
}
}
Loading