private Consumer<ServiceDocumentQuery.ServiceDocumentQueryElementResult<ContainerState>> processContainerStatesQueryResults(DeferredResult<?> df) { List<String> existingContainerStateLinks = new ArrayList<>(); return (ServiceDocumentQuery.ServiceDocumentQueryElementResult<ContainerState> r) -> { if (r.hasException()) { logger.warning( String.format("Failed to query resource container state [%s]", r.getDocumentSelfLink())); df.fail(r.getException()); } else if (r.hasResult()) { existingContainerStateLinks.add(r.getDocumentSelfLink()); } else { List<DeferredResult<Operation>> dfs = new ArrayList<>(); for (String csSelfLink : existingContainerStateLinks) { ContainerState cs = new ContainerState(); cs.documentSelfLink = csSelfLink; cs.powerState = ContainerState.PowerState.UNKNOWN; dfs.add(patchContainerState(cs)); } DeferredResult.allOf(dfs).whenComplete((o, e) -> { df.complete(null); }); } }; }
private void handleQueryServices(ServiceDocumentDeleteTaskState task) { QueryTask query = QueryUtil.buildQuery(task.deleteDocumentKind, false); query.querySpec.options = EnumSet.of(QueryOption.TOP_RESULTS); query.querySpec.resultLimit = 50; query.documentExpirationTimeMicros = Utils.fromNowMicrosUtc( TimeUnit.HOURS.toMicros(5)); if (task.tenantLinks != null) { // Delete documents only in current tenant and group query.querySpec.query.addBooleanClause(QueryUtil.addTenantAndGroupClause(task.tenantLinks)); } List<String> documents = new ArrayList<String>(); new ServiceDocumentQuery<ServiceDocument>(getHost(), ServiceDocument.class) .query(query, (r) -> { if (r.hasException()) { logWarning("Query failed, task will not finish: %s", r.getException().getMessage()); failTask(String.format("Could not get %s, task failed", task.deleteDocumentKind), r.getException()); return; } else if (r.hasResult() && documents.size() < r.getCount()) { documents.add(r.getDocumentSelfLink()); } else { handleDeleteServices(task, documents); } }); }
private void queryResourceNamePrefixes(ResourceNamePrefixTaskState state, boolean globalSearch) { // match on group property: QueryTask q = QueryUtil.buildQuery(ResourceNamePrefixState.class, true); q.tenantLinks = state.tenantLinks; List<String> documentLinks = new ArrayList<>(); new ServiceDocumentQuery<>(getHost(), ResourceNamePrefixState.class) .query(q, (r) -> { if (r.hasException()) { failTask("Cannot retrieve prefixes", r.getException()); } else if (r.hasResult()) { documentLinks.add(r.getDocumentSelfLink()); } else { selectResourceNamePrefix(state, documentLinks.iterator(), globalSearch); } }); }
failTask("Failure retrieving query results", r.getException()); } else if (r.hasResult()) { containerHosts.add(r.getDocumentSelfLink()); } else { if (containerHosts.isEmpty()) {
private void queryPortProfiles(ContainerHostRemovalTaskState state) { QueryTask q = QueryUtil .buildQuery(HostPortProfileService.HostPortProfileState.class, false); QueryUtil.addListValueClause(q, HostPortProfileService.HostPortProfileState.FIELD_HOST_LINK, state.resourceLinks); ServiceDocumentQuery<HostPortProfileService.HostPortProfileState> query = new ServiceDocumentQuery<>( getHost(), HostPortProfileService.HostPortProfileState.class); QueryUtil.addBroadcastOption(q); ArrayList<String> hostPortProfileLinks = new ArrayList<>(); query.query(q, (r) -> { if (r.hasException()) { failTask("Failure retrieving query results", r.getException()); return; } else if (r.hasResult()) { hostPortProfileLinks.add(r.getDocumentSelfLink()); } else { // if there are no host port profiles, go to the next stage if (hostPortProfileLinks.isEmpty()) { proceedTo(SubStage.REMOVED_PORT_PROFILES); return; } removePortProfiles(state, hostPortProfileLinks, null); proceedTo(SubStage.REMOVING_PORT_PROFILES); } }); }
private void patchContainerToHostPowerState(MockAdapterRequest state, ContainerState containerState) { String containerName = Service.getId(state.resourceReference.getPath()); QueryTask q = QueryUtil.buildPropertyQuery(MockDockerContainerToHostState.class, MockDockerContainerToHostState.FIELD_NAME_NAME, containerName); MockDockerContainerToHostState cthState = new MockDockerContainerToHostState(); cthState.powerState = containerState.powerState; new ServiceDocumentQuery<>(getHost(), MockDockerContainerToHostState.class).query(q, (r) -> { if (r.hasException()) { patchTaskStage(state, r.getException()); } else if (r.hasResult()) { sendRequest(Operation.createPatch(this, r.getDocumentSelfLink()) .addPragmaDirective(Operation.PRAGMA_DIRECTIVE_QUEUE_FOR_SERVICE_AVAILABILITY) .setBody(cthState) .setCompletion((o, e) -> { Throwable patchException = null; if (e != null) { logSevere(e); patchException = e; } patchTaskStage(state, patchException); })); } }); }
private void verifyContainerStatesRemoved(CompositeDescription compositeDesc, CountDownLatch latch) throws Throwable { ServiceDocumentQuery<ContainerState> query = new ServiceDocumentQuery<>(host, ContainerState.class); QueryTask q = QueryUtil.buildQuery(ContainerState.class, true); QueryUtil.addListValueClause(q, ContainerState.FIELD_NAME_DESCRIPTION_LINK, compositeDesc.descriptionLinks); List<String> containerLinks = new ArrayList<>(); waitFor("ContainerStates must be removed.", () -> { host.testStart(1); query.query(q, (r) -> { if (r.hasException()) { host.failIteration(r.getException()); } else if (r.hasResult()) { containerLinks.add(r.getDocumentSelfLink()); } else { host.completeIteration(); } }); host.testWait(); latch.countDown(); host.log("Container count: %s", containerLinks.size()); return containerLinks.isEmpty(); }); }
private void queryContainerVolumeResources(ContainerVolumeRemovalTaskState state) { QueryTask volumeQuery = createResourcesQuery(ContainerVolumeState.class, state.resourceLinks); ServiceDocumentQuery<ContainerVolumeState> query = new ServiceDocumentQuery<>(getHost(), ContainerVolumeState.class); List<String> volumeLinks = new ArrayList<>(); QueryUtil.addBroadcastOption(volumeQuery); QueryUtil.addExpandOption(volumeQuery); query.query(volumeQuery, (r) -> { if (r.hasException()) { failTask("Failure retrieving query results", r.getException()); } else if (r.hasResult()) { volumeLinks.add(r.getDocumentSelfLink()); } else { if (volumeLinks.isEmpty()) { logWarning("No available resources found to be removed with links: %s", state.resourceLinks); proceedTo(SubStage.COMPLETED); } else { deleteResourceInstances(state, volumeLinks, null); } } }); }
private void removeAllPlacements() throws Throwable { QueryTask q = QueryUtil.buildQuery(GroupResourcePlacementState.class, false); ServiceDocumentQuery<?> query = new ServiceDocumentQuery<>(host, GroupResourcePlacementState.class); List<String> placementLinks = new ArrayList<>(); TestContext ctx = testCreate(1); query.query(q, (r) -> { if (r.hasException()) { host.log(Level.SEVERE, "Exception during search for GroupResourcePlacementStates", r.getException().getMessage()); ctx.failIteration(r.getException()); } else if (r.hasResult()) { placementLinks.add(r.getDocumentSelfLink()); } else { ctx.completeIteration(); } }); ctx.await(); for (String selfLink : placementLinks) { delete(selfLink); } }
public List<String> findResourceLinks(Class<? extends ServiceDocument> type, Collection<String> resourceLinks) throws Throwable { TestContext ctx = testCreate(1); QueryTask query = QueryUtil.buildQuery(type, true); QueryUtil.addListValueClause(query, ServiceDocument.FIELD_NAME_SELF_LINK, resourceLinks); List<String> result = new LinkedList<>(); new ServiceDocumentQuery<>( host, type).query(query, (r) -> { if (r.hasException()) { ctx.failIteration(r.getException()); return; } if (r.hasResult()) { result.add(r.getDocumentSelfLink()); return; } ctx.completeIteration(); }); ctx.await(); return result; }
private void queryContainers(ContainerHostRemovalTaskState state) { QueryTask containerQuery = QueryUtil.buildQuery(ContainerState.class, true); QueryUtil.addListValueClause(containerQuery, ContainerState.FIELD_NAME_PARENT_LINK, state.resourceLinks); Set<String> containerLinks = new HashSet<>(); new ServiceDocumentQuery<ContainerState>(getHost(), ContainerState.class).query( containerQuery, (r) -> { if (r.hasException()) { failTask("Failure retrieving query results", r.getException()); return; } else if (r.hasResult()) { containerLinks.add(r.getDocumentSelfLink()); } else { if (containerLinks.isEmpty()) { queryNetworks(state); return; } removeContainers(state, containerLinks); } }); }
public List<String> getDocumentLinksOfType(Class<? extends ServiceDocument> type) throws Throwable { TestContext ctx = testCreate(1); QueryTask query = QueryUtil.buildQuery(type, true); List<String> result = new LinkedList<>(); new ServiceDocumentQuery<>( host, type).query(query, (r) -> { if (r.hasException()) { ctx.failIteration(r.getException()); return; } else if (r.hasResult()) { result.add(r.getDocumentSelfLink()); } else { ctx.completeIteration(); } }); ctx.await(); return result; }
private void queryContainerNetworkResources(ContainerNetworkRemovalTaskState state) { QueryTask networkQuery = createResourcesQuery(ContainerNetworkState.class, state.resourceLinks); ServiceDocumentQuery<ContainerNetworkState> query = new ServiceDocumentQuery<>(getHost(), ContainerNetworkState.class); List<String> networkLinks = new ArrayList<>(); QueryUtil.addBroadcastOption(networkQuery); QueryUtil.addExpandOption(networkQuery); query.query(networkQuery, (r) -> { if (r.hasException()) { failTask("Failure retrieving query results", r.getException()); } else if (r.hasResult()) { networkLinks.add(r.getDocumentSelfLink()); } else { if (networkLinks.isEmpty()) { logWarning("No available resources found to be removed with links: %s", state.resourceLinks); proceedTo(SubStage.COMPLETED); } else { deleteResourceInstances(state, networkLinks, null); } } }); }
private void selectComputeStates(AdmiralAdapterTaskState state, ContainerDescription contDesc) { Consumer<ServiceDocumentQuery.ServiceDocumentQueryElementResult<ServiceDocument>> completionHandler = (computeResult) -> { if (computeResult.hasException()) { failTask("Unable to fetch compute states: ", computeResult.getException()); return; } proceedTo(AdmiralAdapterTaskState.SubStage.COMPUTE_STATE_SELECTED, (s) -> s.selectedComputeLink = computeResult.getDocumentSelfLink()); }; if (state.hostSelections != null) { proceedWithSelection(contDesc, state, completionHandler); } else { proceedWithComputeStates(contDesc, state, ComputeState.class, ComputeState.FIELD_NAME_RESOURCE_POOL_LINK, state.placementZoneLink, completionHandler); } }
private void updateHostInfoDataCollection(Operation maintOp) { List<String> descriptionLinks = new ArrayList<>(); QueryTask q = createDockerComputeDescriptionQuery(); ServiceDocumentQuery<ComputeDescription> query = new ServiceDocumentQuery<>(getHost(), ComputeDescription.class); query.query(q, (r) -> { if (getHost().isStopping()) { maintOp.complete(); return; } if (r.hasException()) { logWarning("Exception while retrieving docker host descriptions. Error: %s", r.getException() instanceof CancellationException ? r.getException().getClass().getName() : Utils.toString(r.getException())); maintOp.fail(r.getException()); } else if (r.hasResult()) { descriptionLinks.add(r.getDocumentSelfLink()); maintOp.complete(); } else { findAllContainerHosts(descriptionLinks, maintOp); } }); }
private void disableContainersForHost(String computeStateSelfLink) { QueryTask containerQuery = QueryUtil.buildPropertyQuery(ContainerState.class, ContainerState.FIELD_NAME_PARENT_LINK, computeStateSelfLink); ContainerState errorState = new ContainerState(); errorState.powerState = ContainerState.PowerState.ERROR; new ServiceDocumentQuery<>(getHost(), ContainerState.class).query( containerQuery, (r) -> { if (r.hasException()) { logWarning("Failed to retrieve containers for ComputeState: %s", computeStateSelfLink); return; } else if (r.hasResult()) { logWarning("Disable container %s, because host %s is unavailable", r.getDocumentSelfLink(), computeStateSelfLink); sendRequest(Operation .createPatch(this, r.getDocumentSelfLink()) .setAuthorizationContext(getSystemAuthorizationContext()) .setBodyNoCloning(errorState)); } }); }
private void removeContainerByReference(MockAdapterRequest state) { String containerName = Service.getId(state.resourceReference.getPath()); QueryTask q = QueryUtil.buildPropertyQuery(MockDockerContainerToHostState.class, MockDockerContainerToHostState.FIELD_NAME_NAME, containerName); List<String> mockContainerToHostLinks = new ArrayList<>(); new ServiceDocumentQuery<>(getHost(), MockDockerContainerToHostState.class).query(q, (r) -> { if (r.hasException()) { patchTaskStage(state, r.getException()); } else if (r.hasResult()) { mockContainerToHostLinks.add(r.getDocumentSelfLink()); } else { removeMockContainerFromHost(state, mockContainerToHostLinks); } }); }
private void removeNetworkByReference(MockAdapterRequest state) { String networkName = Service.getId(state.resourceReference.getPath()); QueryTask q = QueryUtil.buildPropertyQuery(MockDockerNetworkToHostState.class, MockDockerNetworkToHostState.FIELD_NAME_NAME, networkName); List<String> mockNetworkToHostLinks = new ArrayList<>(); new ServiceDocumentQuery<>(getHost(), MockDockerNetworkToHostState.class).query(q, (r) -> { if (r.hasException()) { patchTaskStage(state, r.getException()); } else if (r.hasResult()) { mockNetworkToHostLinks.add(r.getDocumentSelfLink()); } else { removeMockNetworkFromHost(state, mockNetworkToHostLinks); } }); }
private void queryVolumes(ContainerHostRemovalTaskState state) { QueryTask volumeQuery = QueryUtil.buildQuery(ContainerVolumeState.class, false); QueryUtil.addListValueClause(volumeQuery, ContainerVolumeState.FIELD_NAME_ORIGINATING_HOST_LINK, state.resourceLinks); Set<String> volumeLinks = new HashSet<>(); new ServiceDocumentQuery<ContainerVolumeState>(getHost(), ContainerVolumeState.class).query( volumeQuery, (r) -> { if (r.hasException()) { failTask("Failure retrieving query results", r.getException()); return; } else if (r.hasResult()) { volumeLinks.add(r.getDocumentSelfLink()); } else { if (volumeLinks.isEmpty()) { filterKubernetesHosts(state); return; } removeVolumes(state, volumeLinks); } }); }
private void removeVolumeByReference(MockAdapterRequest state) { String volumeName = Service.getId(state.resourceReference.getPath()); QueryTask q = QueryUtil.buildPropertyQuery(MockDockerVolumeToHostState.class, MockDockerVolumeToHostState.FIELD_NAME_NAME, volumeName); List<String> mockVolumeToHostLinks = new ArrayList<>(); new ServiceDocumentQuery<>(getHost(), MockDockerVolumeToHostState.class).query(q, (r) -> { if (r.hasException()) { patchTaskStage(state, r.getException()); } else if (r.hasResult()) { mockVolumeToHostLinks.add(r.getDocumentSelfLink()); } else { removeMockVolumeFromHost(state, mockVolumeToHostLinks); } }); }