use of com.vmware.xenon.services.common.QueryTask in project photon-model by vmware.
the class AWSEndpointAdapterService method checkIfAccountExistsAndGetExistingDocuments.
private void checkIfAccountExistsAndGetExistingDocuments(EndpointConfigRequest req, Operation op) {
if (req.isMockRequest) {
req.accountAlreadyExists = false;
op.setBody(req);
op.complete();
return;
}
String accountId = getAccountId(req.endpointProperties.get(ARN_KEY), req.endpointProperties.get(EndpointConfigRequest.PRIVATE_KEYID_KEY), req.endpointProperties.get(EndpointConfigRequest.PRIVATE_KEY_KEY));
if (accountId != null && !accountId.isEmpty()) {
QueryTask queryTask = QueryUtils.createAccountQuery(accountId, PhotonModelConstants.EndpointType.aws.name(), req.tenantLinks);
queryTask.tenantLinks = req.tenantLinks;
QueryUtils.startInventoryQueryTask(this, queryTask).whenComplete((qrt, e) -> {
if (e != null) {
logSevere(() -> String.format("Failure retrieving query results for compute host corresponding to" + "the account ID: %s", e.toString()));
op.fail(e);
return;
}
if (qrt.results.documentCount > 0) {
req.accountAlreadyExists = true;
Object state = qrt.results.documents.values().iterator().next();
ComputeState computeHost = Utils.fromJson(state, ComputeState.class);
req.existingComputeState = computeHost;
getComputeDescription(req, computeHost.descriptionLink, op);
} else {
req.accountAlreadyExists = false;
op.setBody(req);
op.complete();
return;
}
});
} else {
// If the account Id cannot be looked up with the given set of credentials then de duplication is not possible.
req.accountAlreadyExists = false;
op.setBody(req);
op.complete();
}
}
use of com.vmware.xenon.services.common.QueryTask in project photon-model by vmware.
the class AzureComputeEnumerationAdapterService method disassociateComputeStates.
/**
* Deletes undiscovered resources.
* <p>
* The logic works by recording a timestamp when enumeration starts. This timestamp is used to
* lookup resources which haven't been touched as part of current enumeration cycle. The other
* data point this method uses is the virtual machines discovered as part of list vm call.
* <p>
* Finally, delete on a resource is invoked only if it meets two criteria: - Timestamp older
* than current enumeration cycle. - VM not present on Azure.
* <p>
* The method paginates through list of resources for deletion.
*/
private void disassociateComputeStates(EnumerationContext ctx, ComputeEnumerationSubStages next) {
Query.Builder qBuilder = Builder.create().addKindFieldClause(ComputeState.class).addFieldClause(ComputeState.FIELD_NAME_PARENT_LINK, ctx.request.resourceLink()).addRangeClause(ComputeState.FIELD_NAME_UPDATE_TIME_MICROS, NumericRange.createLessThanRange(ctx.enumerationStartTimeInMicros)).addInClause(ComputeState.FIELD_NAME_LIFECYCLE_STATE, Arrays.asList(LifecycleState.PROVISIONING.toString(), LifecycleState.RETIRED.toString()), Occurance.MUST_NOT_OCCUR);
addScopeCriteria(qBuilder, ComputeState.class, ctx);
QueryTask q = QueryTask.Builder.createDirectTask().addOption(QueryOption.EXPAND_CONTENT).setQuery(qBuilder.build()).setResultLimit(getQueryResultLimit()).build();
q.tenantLinks = ctx.parentCompute.tenantLinks;
logFine(() -> "Querying compute resources for deletion");
QueryUtils.startInventoryQueryTask(this, q).whenComplete((queryTask, e) -> {
if (e != null) {
handleError(ctx, e);
return;
}
if (queryTask.results.nextPageLink == null) {
logFine(() -> "No compute states match for deletion");
ctx.subStage = next;
handleSubStage(ctx);
return;
}
ctx.deletionNextPageLink = queryTask.results.nextPageLink;
disassociateOrRetireHelper(ctx, next);
});
}
use of com.vmware.xenon.services.common.QueryTask in project photon-model by vmware.
the class AzureComputeEnumerationAdapterService method disassociateOrRetireHelper.
/**
* Helper method to paginate through resources to be deleted.
*/
private void disassociateOrRetireHelper(EnumerationContext ctx, ComputeEnumerationSubStages next) {
if (ctx.deletionNextPageLink == null) {
logFine(() -> String.format("Finished %s of compute states for Azure", ctx.request.preserveMissing ? "retiring" : "deletion"));
ctx.subStage = next;
handleSubStage(ctx);
return;
}
CompletionHandler completionHandler = (o, e) -> {
if (e != null) {
handleError(ctx, e);
return;
}
QueryTask queryTask = o.getBody(QueryTask.class);
ctx.deletionNextPageLink = queryTask.results.nextPageLink;
List<Operation> operations = new ArrayList<>();
for (Object s : queryTask.results.documents.values()) {
ComputeState computeState = Utils.fromJson(s, ComputeState.class);
String vmId = computeState.id;
// present in Azure but have older timestamp in local repository.
if (ctx.vmIds.contains(vmId) || ctx.regionIds.contains(vmId)) {
continue;
}
if (ctx.request.preserveMissing) {
logFine(() -> String.format("Retiring compute state %s", computeState.documentSelfLink));
ComputeState cs = new ComputeState();
cs.powerState = PowerState.OFF;
cs.lifecycleState = LifecycleState.RETIRED;
operations.add(Operation.createPatch(this, computeState.documentSelfLink).setBody(cs));
} else {
// Deleting the localResourceState is done by disassociating the endpointLink from the
// localResourceState. If the localResourceState isn't associated with any other
// endpointLink, we issue a delete then
Operation dOp = PhotonModelUtils.createRemoveEndpointLinksOperation(this, ctx.request.endpointLink, computeState);
if (dOp != null) {
dOp.sendWith(getHost());
logFine(() -> String.format("Deleting compute state %s", computeState.documentSelfLink));
}
if (computeState.diskLinks != null && !computeState.diskLinks.isEmpty()) {
computeState.diskLinks.forEach(dl -> {
sendRequest(Operation.createGet(this, dl).setCompletion((op, ex) -> {
if (ex != null) {
logWarning(() -> String.format("Error retrieving " + "diskState: %s", ex.getMessage()));
} else {
DiskState diskState = op.getBody(DiskState.class);
Operation diskOp = PhotonModelUtils.createRemoveEndpointLinksOperation(this, ctx.request.endpointLink, diskState);
if (diskOp != null) {
diskOp.sendWith(getHost());
logFine(() -> String.format("Deleting disk state %s of machine %s", dl, computeState.documentSelfLink));
}
}
}));
});
}
if (computeState.networkInterfaceLinks != null && !computeState.networkInterfaceLinks.isEmpty()) {
computeState.networkInterfaceLinks.forEach(nil -> {
sendRequest(Operation.createGet(this, nil).setCompletion((op, ex) -> {
if (ex != null) {
logWarning(() -> String.format("Error retrieving NetworkInterface state: %s", ex.getMessage()));
} else {
NetworkInterfaceState networkInterfaceState = op.getBody(NetworkInterfaceState.class);
Operation nicOp = PhotonModelUtils.createRemoveEndpointLinksOperation(this, ctx.request.endpointLink, networkInterfaceState);
if (nicOp != null) {
nicOp.sendWith(getHost());
logFine(() -> String.format("Deleting NetworkInterface state %s of machine %s", nil, computeState.documentSelfLink));
}
}
}));
});
}
}
}
if (operations.size() == 0) {
logFine(() -> String.format("No compute/disk states to %s", ctx.request.preserveMissing ? "retire" : "delete"));
disassociateOrRetireHelper(ctx, next);
return;
}
OperationJoin.create(operations).setCompletion((ops, exs) -> {
if (exs != null) {
// We don't want to fail the whole data collection if some of the
// operation fails.
exs.values().forEach(ex -> logWarning(() -> String.format("Error: %s", ex.getMessage())));
}
disassociateOrRetireHelper(ctx, next);
}).sendWith(this);
};
logFine(() -> String.format("Querying page [%s] for resources to be %s", ctx.deletionNextPageLink, ctx.request.preserveMissing ? "retire" : "delete"));
sendRequest(Operation.createGet(createInventoryUri(this.getHost(), ctx.deletionNextPageLink)).setCompletion(completionHandler));
}
use of com.vmware.xenon.services.common.QueryTask in project photon-model by vmware.
the class AzureComputeHostStatsGatherer method handleComputeQueryCompletion.
/**
* Get all the children computes and create a query task for each to query the metrics.
*/
private void handleComputeQueryCompletion(QueryTask queryTask, Throwable failure, AzureStatsDataHolder statsData) {
if (failure != null) {
logSevere(failure.getMessage());
statsData.error = failure;
statsData.stage = ComputeHostMetricsStages.ERROR;
handleMetricDiscovery(statsData);
return;
}
if (queryTask == null || queryTask.results == null) {
sendFailurePatch(statsData, new RuntimeException(String.format("Unexpected query result for '%s'", queryTask.documentSelfLink)));
return;
}
if (queryTask.results.documentLinks != null) {
statsData.childComputeLinks.addAll(queryTask.results.documentLinks);
}
if (queryTask.results.nextPageLink != null) {
Operation op = Operation.createGet(createInventoryUri(this.getHost(), queryTask.results.nextPageLink));
sendWithDeferredResult(op).whenComplete((o, e) -> handleComputeQueryCompletion(o.getBody(QueryTask.class), e, statsData));
return;
}
int computeCount = Math.toIntExact(statsData.childComputeLinks.size());
// No children found, proceed to finish
if (computeCount <= 0) {
statsData.stage = ComputeHostMetricsStages.FINISHED;
handleMetricDiscovery(statsData);
return;
}
// Create multiple operations, one each for a VM compute.
List<Operation> statOperations = new ArrayList<>(computeCount);
for (String computeLink : statsData.childComputeLinks) {
Operation statsOp = getStatsQueryTaskOperation(statsData, computeLink);
statOperations.add(statsOp);
}
OperationJoin.create(statOperations).setCompletion((ops, failures) -> handleQueryTaskResponseAndConsolidateStats(ops, failures, statsData)).sendWith(this, 50);
}
use of com.vmware.xenon.services.common.QueryTask in project photon-model by vmware.
the class AzureComputeHostStatsGatherer method aggregateComputeStatsResponses.
/**
* Aggregates stats from all the compute VMs to make up compute Host stats.
*/
private ComputeStats aggregateComputeStatsResponses(AzureStatsDataHolder statsData, List<QueryTask> items) {
int numberOfComputeResponse = items.size();
ComputeStats computeStats = new ComputeStats();
computeStats.computeLink = statsData.computeHost.documentSelfLink;
Map<String, ServiceStat> statMap = new HashMap<>();
// Gather all the stats in a single response.
for (QueryTask queryResult : items) {
if (queryResult.results.documents != null) {
for (String key : queryResult.results.documents.keySet()) {
ResourceMetrics metric = Utils.fromJson(queryResult.results.documents.get(key), ResourceMetrics.class);
for (Map.Entry<String, Double> entry : metric.entries.entrySet()) {
String metricName = entry.getKey();
if (statMap.containsKey(metricName)) {
statMap.get(metricName).latestValue += entry.getValue();
} else {
ServiceStat stat = new ServiceStat();
stat.latestValue = entry.getValue();
statMap.put(metricName, stat);
}
}
}
}
}
computeStats.statValues = new ConcurrentSkipListMap<>();
// Divide each metric value by the number of computes to get an average value.
for (String key : statMap.keySet()) {
ServiceStat serviceStatValue = statMap.get(key);
serviceStatValue.unit = PhotonModelConstants.getUnitForMetric(key);
serviceStatValue.sourceTimeMicrosUtc = Utils.getNowMicrosUtc();
serviceStatValue.latestValue = serviceStatValue.latestValue / numberOfComputeResponse;
computeStats.statValues.put(key, Collections.singletonList(serviceStatValue));
}
return computeStats;
}
Aggregations