use of org.apache.helix.HelixProperty in project helix by apache.
the class CriteriaEvaluator method evaluateCriteria.
/**
* Examine persisted data to match wildcards in {@link Criteria}
* @param recipientCriteria Criteria specifying the message destinations
* @param manager connection to the persisted data
* @return map of evaluated criteria
*/
public List<Map<String, String>> evaluateCriteria(Criteria recipientCriteria, HelixManager manager) {
// get the data
HelixDataAccessor accessor = manager.getHelixDataAccessor();
PropertyKey.Builder keyBuilder = accessor.keyBuilder();
List<HelixProperty> properties;
DataSource dataSource = recipientCriteria.getDataSource();
String resourceName = recipientCriteria.getResource();
String instanceName = recipientCriteria.getInstanceName();
switch(dataSource) {
case EXTERNALVIEW:
properties = getProperty(accessor, resourceName, keyBuilder.externalViews(), keyBuilder.externalView(resourceName), DataSource.EXTERNALVIEW.name());
break;
case IDEALSTATES:
properties = getProperty(accessor, resourceName, keyBuilder.idealStates(), keyBuilder.idealStates(resourceName), DataSource.IDEALSTATES.name());
break;
case LIVEINSTANCES:
properties = getProperty(accessor, instanceName, keyBuilder.liveInstances(), keyBuilder.liveInstance(instanceName), DataSource.LIVEINSTANCES.name());
break;
case INSTANCES:
properties = getProperty(accessor, instanceName, keyBuilder.instances(), keyBuilder.instance(instanceName), DataSource.INSTANCES.name());
break;
default:
return Lists.newArrayList();
}
// flatten the data
List<ZNRecordRow> allRows = ZNRecordRow.flatten(HelixProperty.convertToList(properties));
// save the matches
Set<String> liveParticipants = accessor.getChildValuesMap(keyBuilder.liveInstances()).keySet();
List<ZNRecordRow> result = Lists.newArrayList();
for (ZNRecordRow row : allRows) {
// The participant instance name is stored in the return value of either getRecordId() or getMapSubKey()
if (rowMatches(recipientCriteria, row) && (liveParticipants.contains(row.getRecordId()) || liveParticipants.contains(row.getMapSubKey()))) {
result.add(row);
}
}
Set<Map<String, String>> selected = Sets.newHashSet();
// deduplicate and convert the matches into the required format
for (ZNRecordRow row : result) {
Map<String, String> resultRow = new HashMap<String, String>();
resultRow.put("instanceName", !recipientCriteria.getInstanceName().equals("") ? (!Strings.isNullOrEmpty(row.getMapSubKey()) ? row.getMapSubKey() : row.getRecordId()) : "");
resultRow.put("resourceName", !recipientCriteria.getResource().equals("") ? row.getRecordId() : "");
resultRow.put("partitionName", !recipientCriteria.getPartition().equals("") ? row.getMapKey() : "");
resultRow.put("partitionState", !recipientCriteria.getPartitionState().equals("") ? row.getMapValue() : "");
selected.add(resultRow);
}
logger.info("Query returned " + selected.size() + " rows");
return Lists.newArrayList(selected);
}
use of org.apache.helix.HelixProperty in project helix by apache.
the class CallbackHandler method invoke.
public void invoke(NotificationContext changeContext) throws Exception {
Type type = changeContext.getType();
long start = System.currentTimeMillis();
// This allows the listener to work with one change at a time
synchronized (_manager) {
if (logger.isInfoEnabled()) {
logger.info(Thread.currentThread().getId() + " START:INVOKE " + _path + " listener:" + _listener + " type: " + type);
}
if (!_expectTypes.contains(type)) {
logger.warn("Callback handler received event in wrong order. Listener: " + _listener + ", path: " + _path + ", expected types: " + _expectTypes + " but was " + type);
return;
}
_expectTypes = nextNotificationType.get(type);
if (type == Type.INIT || type == Type.FINALIZE) {
subscribeForChanges(changeContext.getType(), _path, _watchChild);
} else {
// put SubscribeForChange run in async thread to reduce the latency of zk callback handling.
subscribeForChangesAsyn(changeContext.getType(), _path, _watchChild);
}
_expectTypes = nextNotificationType.get(type);
if (_changeType == IDEAL_STATE) {
IdealStateChangeListener idealStateChangeListener = (IdealStateChangeListener) _listener;
List<IdealState> idealStates = preFetch(_propertyKey);
idealStateChangeListener.onIdealStateChange(idealStates, changeContext);
} else if (_changeType == INSTANCE_CONFIG) {
if (_listener instanceof ConfigChangeListener) {
ConfigChangeListener configChangeListener = (ConfigChangeListener) _listener;
List<InstanceConfig> configs = preFetch(_propertyKey);
configChangeListener.onConfigChange(configs, changeContext);
} else if (_listener instanceof InstanceConfigChangeListener) {
InstanceConfigChangeListener listener = (InstanceConfigChangeListener) _listener;
List<InstanceConfig> configs = preFetch(_propertyKey);
listener.onInstanceConfigChange(configs, changeContext);
}
} else if (_changeType == RESOURCE_CONFIG) {
ResourceConfigChangeListener listener = (ResourceConfigChangeListener) _listener;
List<ResourceConfig> configs = preFetch(_propertyKey);
listener.onResourceConfigChange(configs, changeContext);
} else if (_changeType == CLUSTER_CONFIG) {
ClusterConfigChangeListener listener = (ClusterConfigChangeListener) _listener;
ClusterConfig config = null;
if (_preFetchEnabled) {
config = _accessor.getProperty(_propertyKey);
}
listener.onClusterConfigChange(config, changeContext);
} else if (_changeType == CONFIG) {
ScopedConfigChangeListener listener = (ScopedConfigChangeListener) _listener;
List<HelixProperty> configs = preFetch(_propertyKey);
listener.onConfigChange(configs, changeContext);
} else if (_changeType == LIVE_INSTANCE) {
LiveInstanceChangeListener liveInstanceChangeListener = (LiveInstanceChangeListener) _listener;
List<LiveInstance> liveInstances = preFetch(_propertyKey);
liveInstanceChangeListener.onLiveInstanceChange(liveInstances, changeContext);
} else if (_changeType == CURRENT_STATE) {
CurrentStateChangeListener currentStateChangeListener = (CurrentStateChangeListener) _listener;
String instanceName = PropertyPathConfig.getInstanceNameFromPath(_path);
List<CurrentState> currentStates = preFetch(_propertyKey);
currentStateChangeListener.onStateChange(instanceName, currentStates, changeContext);
} else if (_changeType == MESSAGE) {
MessageListener messageListener = (MessageListener) _listener;
String instanceName = PropertyPathConfig.getInstanceNameFromPath(_path);
List<Message> messages = preFetch(_propertyKey);
messageListener.onMessage(instanceName, messages, changeContext);
} else if (_changeType == MESSAGES_CONTROLLER) {
MessageListener messageListener = (MessageListener) _listener;
List<Message> messages = preFetch(_propertyKey);
messageListener.onMessage(_manager.getInstanceName(), messages, changeContext);
} else if (_changeType == EXTERNAL_VIEW || _changeType == TARGET_EXTERNAL_VIEW) {
ExternalViewChangeListener externalViewListener = (ExternalViewChangeListener) _listener;
List<ExternalView> externalViewList = preFetch(_propertyKey);
externalViewListener.onExternalViewChange(externalViewList, changeContext);
} else if (_changeType == CONTROLLER) {
ControllerChangeListener controllerChangelistener = (ControllerChangeListener) _listener;
controllerChangelistener.onControllerChange(changeContext);
} else {
logger.warn("Unknown change type: " + _changeType);
}
long end = System.currentTimeMillis();
if (logger.isInfoEnabled()) {
logger.info(Thread.currentThread().getId() + " END:INVOKE " + _path + " listener:" + _listener + " type: " + type + " Took: " + (end - start) + "ms");
}
if (_monitor != null) {
_monitor.increaseCallbackCounters(end - start);
}
}
}
use of org.apache.helix.HelixProperty in project helix by apache.
the class TestResourceValidationStage method createISSpec.
private void createISSpec(HelixDataAccessor accessor, String specId, String stateModelDefRef, RebalanceMode rebalanceMode) {
PropertyKey propertyKey = accessor.keyBuilder().clusterConfig();
HelixProperty property = accessor.getProperty(propertyKey);
if (property == null) {
property = new HelixProperty("sampleClusterConfig");
}
String key = "IdealStateRule!" + specId;
String value = IdealStateProperty.REBALANCE_MODE.toString() + "=" + rebalanceMode.toString() + "," + IdealStateProperty.STATE_MODEL_DEF_REF.toString() + "=" + stateModelDefRef;
property.getRecord().setSimpleField(key, value);
accessor.setProperty(propertyKey, property);
}
use of org.apache.helix.HelixProperty in project helix by apache.
the class TestAlertingRebalancerFailure method pollForError.
private HelixProperty pollForError(HelixDataAccessor accessor, PropertyKey key) {
final int POLL_TIMEOUT = 5000;
final int POLL_INTERVAL = 100;
HelixProperty property = accessor.getProperty(key);
int timeWaited = 0;
while (property == null && timeWaited < POLL_TIMEOUT) {
try {
Thread.sleep(POLL_INTERVAL);
} catch (InterruptedException e) {
return null;
}
timeWaited += POLL_INTERVAL;
property = accessor.getProperty(key);
}
return property;
}
use of org.apache.helix.HelixProperty in project ambry by linkedin.
the class HelixHealthReportAggregationTaskTest method initializeNodeReports.
/**
* Initialize the reports and create instances in helix if not exists.
* @param type The type of reports to create
* @param numNode The number of nodes to initiate.
* @param startingPort The starting port number, which will then be incremented to represent different nodes.
* @throws IOException
*/
private void initializeNodeReports(StatsReportType type, int numNode, int startingPort) throws IOException {
String healthReportName = type == StatsReportType.ACCOUNT_REPORT ? HEALTH_REPORT_NAME_ACCOUNT : HEALTH_REPORT_NAME_PARTITION;
String statsFieldName = type == StatsReportType.ACCOUNT_REPORT ? STATS_FIELD_NAME_ACCOUNT : STATS_FIELD_NAME_PARTITION;
List<StatsSnapshot> storeSnapshots = new ArrayList<>();
Random random = new Random();
for (int i = 3; i < 6; i++) {
storeSnapshots.add(TestUtils.generateStoreStats(i, 3, random, type));
}
StatsWrapper nodeStats = TestUtils.generateNodeStats(storeSnapshots, 1000, type);
String nodeStatsJSON = mapper.writeValueAsString(nodeStats);
HelixDataAccessor dataAccessor = mockHelixManager.getHelixDataAccessor();
for (int i = 0; i < numNode; i++) {
String instanceName = ClusterMapUtils.getInstanceName("localhost", startingPort);
InstanceConfig instanceConfig = new InstanceConfig(instanceName);
instanceConfig.setHostName("localhost");
instanceConfig.setPort(Integer.toString(startingPort));
mockHelixAdmin.addInstance(CLUSTER_NAME, instanceConfig);
PropertyKey key = dataAccessor.keyBuilder().healthReport(instanceName, healthReportName);
ZNRecord znRecord = new ZNRecord(instanceName);
// Set the same reports for all instances
znRecord.setSimpleField(statsFieldName, nodeStatsJSON);
HelixProperty helixProperty = new HelixProperty(znRecord);
dataAccessor.setProperty(key, helixProperty);
startingPort++;
}
}
Aggregations