use of org.apache.helix.model.InstanceConfig in project pinot by linkedin.
the class HelixBrokerStarter method addInstanceTagIfNeeded.
private void addInstanceTagIfNeeded(String clusterName, String instanceName) {
InstanceConfig instanceConfig = _helixAdmin.getInstanceConfig(clusterName, instanceName);
List<String> instanceTags = instanceConfig.getTags();
if (instanceTags == null || instanceTags.isEmpty()) {
if (ZKMetadataProvider.getClusterTenantIsolationEnabled(_propertyStore)) {
_helixAdmin.addInstanceTag(clusterName, instanceName, ControllerTenantNameBuilder.getBrokerTenantNameForTenant(ControllerTenantNameBuilder.DEFAULT_TENANT_NAME));
} else {
_helixAdmin.addInstanceTag(clusterName, instanceName, CommonConstants.Helix.UNTAGGED_BROKER_INSTANCE);
}
}
}
use of org.apache.helix.model.InstanceConfig in project pinot by linkedin.
the class PqlQueryResource method get.
@Override
public Representation get() {
try {
Form query = getQuery();
LOGGER.debug("Running query: " + query);
String pqlQuery = query.getValues("pql");
String traceEnabled = query.getValues("trace");
// Get resource table name.
String tableName;
try {
tableName = REQUEST_COMPILER.compileToBrokerRequest(pqlQuery).getQuerySource().getTableName();
} catch (Exception e) {
LOGGER.error("Caught exception while compiling PQL query: " + pqlQuery, e);
return new StringRepresentation(QueryException.getException(QueryException.PQL_PARSING_ERROR, e).toString());
}
// Get brokers for the resource table.
List<String> instanceIds = _pinotHelixResourceManager.getBrokerInstancesFor(tableName);
if (instanceIds.isEmpty()) {
return new StringRepresentation(QueryException.BROKER_RESOURCE_MISSING_ERROR.toString());
}
// Retain only online brokers.
instanceIds.retainAll(_pinotHelixResourceManager.getOnlineInstanceList());
if (instanceIds.isEmpty()) {
return new StringRepresentation(QueryException.BROKER_INSTANCE_MISSING_ERROR.toString());
}
// Send query to a random broker.
String instanceId = instanceIds.get(RANDOM.nextInt(instanceIds.size()));
InstanceConfig instanceConfig = _pinotHelixResourceManager.getHelixInstanceConfig(instanceId);
String url = "http://" + instanceConfig.getHostName().split("_")[1] + ":" + instanceConfig.getPort() + "/query";
return new StringRepresentation(sendPQLRaw(url, pqlQuery, traceEnabled));
} catch (Exception e) {
LOGGER.error("Caught exception while processing get request", e);
return new StringRepresentation(QueryException.getException(QueryException.INTERNAL_ERROR, e).toString());
}
}
use of org.apache.helix.model.InstanceConfig in project pinot by linkedin.
the class PinotHelixResourceManager method instanceExists.
/**
* Check if an Instance exists in the Helix cluster.
*
* @param instanceName: Name of instance to check.
* @return True if instance exists in the Helix cluster, False otherwise.
*/
public boolean instanceExists(String instanceName) {
HelixDataAccessor helixDataAccessor = _helixZkManager.getHelixDataAccessor();
InstanceConfig config = helixDataAccessor.getProperty(_keyBuilder.instanceConfig(instanceName));
return (config != null);
}
use of org.apache.helix.model.InstanceConfig in project pinot by linkedin.
the class PinotHelixResourceManager method getAllBrokerTenantNames.
public Set<String> getAllBrokerTenantNames() {
Set<String> tenantSet = new HashSet<String>();
List<String> instancesInCluster = _helixAdmin.getInstancesInCluster(_helixClusterName);
for (String instanceName : instancesInCluster) {
InstanceConfig config = _helixDataAccessor.getProperty(_keyBuilder.instanceConfig(instanceName));
for (String tag : config.getTags()) {
if (tag.equals(CommonConstants.Helix.UNTAGGED_BROKER_INSTANCE) || tag.equals(CommonConstants.Helix.UNTAGGED_SERVER_INSTANCE)) {
continue;
}
if (ControllerTenantNameBuilder.getTenantRoleFromTenantName(tag) == TenantRole.BROKER) {
tenantSet.add(ControllerTenantNameBuilder.getExternalTenantName(tag));
}
}
}
return tenantSet;
}
use of org.apache.helix.model.InstanceConfig in project pinot by linkedin.
the class HelixExternalViewBasedRouting method isRoutingTableRebuildRequired.
private boolean isRoutingTableRebuildRequired(String tableName, ExternalView externalView, List<InstanceConfig> instanceConfigs) {
// In unit tests, always rebuild the routing table
if (_helixManager == null) {
return true;
}
// Do we know about this table?
if (!_lastKnownExternalViewVersionMap.containsKey(tableName)) {
LOGGER.info("Routing table for table {} requires rebuild due to it being newly added", tableName);
return true;
}
// Check if the znode version changed
int externalViewRecordVersion = externalView.getRecord().getVersion();
int lastKnownExternalViewVersion = _lastKnownExternalViewVersionMap.get(tableName);
if (externalViewRecordVersion != lastKnownExternalViewVersion || lastKnownExternalViewVersion == INVALID_EXTERNAL_VIEW_VERSION) {
LOGGER.info("Routing table for table {} requires rebuild due to external view change (current version {}, last known version {})", tableName, externalViewRecordVersion, lastKnownExternalViewVersion);
return true;
}
// Check if there are relevant instance config changes
Map<String, InstanceConfig> lastKnownInstanceConfigs = _lastKnownInstanceConfigsForTable.get(tableName);
if (lastKnownInstanceConfigs == null || lastKnownInstanceConfigs.isEmpty()) {
LOGGER.info("Routing table for table {} requires rebuild due to empty/null previous instance configs", tableName);
return true;
}
// Gather relevant incoming instance configs
Map<String, InstanceConfig> currentRelevantInstanceConfigs = new HashMap<>();
for (InstanceConfig incomingInstanceConfig : instanceConfigs) {
String instanceName = incomingInstanceConfig.getInstanceName();
if (lastKnownInstanceConfigs.containsKey(instanceName)) {
currentRelevantInstanceConfigs.put(instanceName, incomingInstanceConfig);
}
}
// Did some instances lose their configuration?
if (lastKnownInstanceConfigs.size() != currentRelevantInstanceConfigs.size()) {
LOGGER.info("Routing table for table {} requires rebuild due to having a different number of instance configs (known instance config count {}, current instance config count {})", tableName, lastKnownInstanceConfigs.size(), currentRelevantInstanceConfigs.size());
return true;
}
// Did some instance change state?
for (String instanceName : lastKnownInstanceConfigs.keySet()) {
InstanceConfig previousInstanceConfig = lastKnownInstanceConfigs.get(instanceName);
InstanceConfig currentInstanceConfig = currentRelevantInstanceConfigs.get(instanceName);
// If it's the same znode, don't bother comparing the contents of the instance configs
if (previousInstanceConfig.getRecord().getVersion() == currentInstanceConfig.getRecord().getVersion()) {
continue;
}
// Check if the instance got enabled/disabled or started/stopped shutting down since the last update
boolean wasEnabled = previousInstanceConfig.getInstanceEnabled();
boolean isEnabled = currentInstanceConfig.getInstanceEnabled();
String wasShuttingDown = previousInstanceConfig.getRecord().getSimpleField(CommonConstants.Helix.IS_SHUTDOWN_IN_PROGRESS);
String isShuttingDown = currentInstanceConfig.getRecord().getSimpleField(CommonConstants.Helix.IS_SHUTDOWN_IN_PROGRESS);
boolean instancesChanged = !EqualityUtils.isEqual(wasEnabled, isEnabled) || !EqualityUtils.isEqual(wasShuttingDown, isShuttingDown);
if (instancesChanged) {
LOGGER.info("Routing table for table {} requires rebuild due to at least one instance changing state (instance {} enabled: {} -> {}; shutting down {} -> {})", tableName, instanceName, wasEnabled, isEnabled, wasShuttingDown, isShuttingDown);
return true;
} else {
// Update the instance config in our last known instance config, since it hasn't changed
_lastKnownInstanceConfigs.put(instanceName, currentInstanceConfig);
for (String tableForInstance : _tablesForInstance.get(instanceName)) {
_lastKnownInstanceConfigsForTable.get(tableForInstance).put(instanceName, currentInstanceConfig);
}
}
}
// No relevant changes, no need to update the routing table
LOGGER.info("Routing table for table {} does not require a rebuild", tableName);
return false;
}
Aggregations