Skip to content

Commit

Permalink
[VBV-1301] Remove fetching container stats when inspecting container
Browse files Browse the repository at this point in the history
It appeared that after each inspect the adapter triggers calls explicitly
getting container stats. Removing that call.
Use setBodyNoCloning where possible, some other small refactorings.

Change-Id: Id80c368f26c337cfc798301c9021e30996d4a2b7
Reviewed-on: http://bellevue-ci.eng.vmware.com:8080/10989
Compute-Verified: jenkins <[email protected]>
Closures-Verified: jenkins <[email protected]>
Upgrade-Verified: jenkins <[email protected]>
CS-Verified: jenkins <[email protected]>
Bellevue-Verified: jenkins <[email protected]>
Reviewed-by: Georgi Muleshkov <[email protected]>
  • Loading branch information
lazarin committed Jun 2, 2017
1 parent 459887e commit d982957
Show file tree
Hide file tree
Showing 10 changed files with 581 additions and 726 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;
import java.util.logging.Level;

import com.vmware.admiral.adapter.common.AdapterRequest;
import com.vmware.admiral.adapter.docker.util.DockerImage;
Expand Down Expand Up @@ -109,8 +108,7 @@ public void handlePeriodicMaintenance(Operation post) {
protected DockerAdapterCommandExecutor getCommandExecutor() {
synchronized (AbstractDockerAdapterService.class) {
ServerX509TrustManager trustManager = ServerX509TrustManager.create(getHost());
return RemoteApiDockerAdapterCommandExecutorImpl.create(getHost(),
trustManager);
return RemoteApiDockerAdapterCommandExecutorImpl.create(getHost(), trustManager);
}
}

Expand All @@ -129,7 +127,8 @@ protected void getContainerHost(AdapterRequest request, Operation op,
return;
}

sendRequest(Operation.createGet(containerHostReference)
sendRequest(Operation
.createGet(containerHostReference)
.setContextId(request.getRequestId())
.setCompletion((o, ex) -> {
if (ex != null) {
Expand All @@ -138,22 +137,21 @@ protected void getContainerHost(AdapterRequest request, Operation op,
}
fail(request, ex);
} else {
ComputeState hostComputeState = o.getBody(ComputeState.class);
if (!hostComputeState.documentSelfLink
.startsWith(ComputeService.FACTORY_LINK)) {
getHost().log(Level.SEVERE,
"***** Wrong ComputeState returned: request: %s, response link: %s, json: %s",
containerHostReference, hostComputeState.documentSelfLink,
Utils.toJson(hostComputeState));
ComputeState hostState = o.getBody(ComputeState.class);
if (!hostState.documentSelfLink.startsWith(ComputeService.FACTORY_LINK)) {
logSevere("***** Wrong ComputeState returned: request: %s, response"
+ " link: %s, json: %s",
containerHostReference, hostState.documentSelfLink,
Utils.toJson(hostState));
}
handleExceptions(request, op, () -> {
createHostConnection(request, op, hostComputeState, callbackFunction);
createHostConnection(request, op, hostState, callbackFunction);
});
}
}));

// TODO: Change back to fine
getHost().log(Level.INFO, "Fetching ComputeState: %s %s", containerHostReference,
logInfo("Fetching ComputeState: %s %s", containerHostReference,
request.getRequestTrackingLog());
}

Expand Down Expand Up @@ -196,8 +194,7 @@ protected void processAuthentication(BaseRequestContext context, Runnable callba
registryLinks.add(r.getDocumentSelfLink());
} else {
if (registryLinks.isEmpty()) {
getHost().log(Level.WARNING,
"Failed to find registry state with address '%s'.",
logWarning("Failed to find registry state with address '%s'.",
image.getHost());
callback.run();
return;
Expand All @@ -213,7 +210,8 @@ private void fetchRegistryAuthState(String registryStateLink, BaseRequestContext
URI registryStateUri = UriUtils.buildUri(getHost(), registryStateLink,
UriUtils.URI_PARAM_ODATA_EXPAND);

Operation getRegistry = Operation.createGet(registryStateUri)
Operation getRegistry = Operation
.createGet(registryStateUri)
.setCompletion((o, ex) -> {
if (ex != null) {
if (context.operation != null) {
Expand All @@ -230,9 +228,10 @@ private void fetchRegistryAuthState(String registryStateLink, BaseRequestContext
if (registryState.authCredentials != null) {
AuthCredentialsServiceState authState = registryState.authCredentials;
AuthCredentialsType authType = AuthCredentialsType.valueOf(authState.type);
if (AuthCredentialsType.Password.equals(authType)) {
if (AuthCredentialsType.Password == authType) {
// create and encode AuthConfig
DockerAdapterService.AuthConfig authConfig = new DockerAdapterService.AuthConfig();
DockerAdapterService.AuthConfig authConfig =
new DockerAdapterService.AuthConfig();
authConfig.username = authState.userEmail;
authConfig.password = EncryptionUtils.decrypt(authState.privateKey);
authConfig.email = "";
Expand All @@ -246,8 +245,7 @@ private void fetchRegistryAuthState(String registryStateLink, BaseRequestContext
context.commandInput.getProperties().put(DOCKER_IMAGE_REGISTRY_AUTH,
authConfigEncoded);

getHost().log(Level.INFO,
"Detected registry requiring basic authn, %s header created.",
logInfo("Detected registry requiring basic authn, %s header created.",
DOCKER_IMAGE_REGISTRY_AUTH);
}
}
Expand Down Expand Up @@ -337,7 +335,7 @@ private void getCredentials(AdapterRequest request, Operation op, ComputeState h
callbackFunction.accept(hostComputeState, commandInput);
}));

getHost().log(Level.FINE, "Fetching AuthCredentials: %s %s", credentialsLink,
logFine("Fetching AuthCredentials: %s %s", credentialsLink,
request.getRequestTrackingLog());
}

Expand Down Expand Up @@ -366,16 +364,15 @@ protected void fail(AdapterRequest request, Throwable e) {
if (e.getMessage() != null && e.getMessage().contains(NOT_FOUND_EXCEPTION_MESSAGE)) {
logWarning(e.getMessage());
} else {
logWarning("%s", Utils.toString(e));
logWarning(Utils.toString(e));
}
patchTaskStage(request, TaskStage.FAILED, e);
}

protected void fail(AdapterRequest request, Operation o, Throwable e) {
if (o != null && o.getBodyRaw() != null) {
String reason = normalizeDockerError(o.getBody(String.class));
String errMsg = String.format("%s; Reason: %s", e.getMessage(),
Utils.toJson(reason));
String errMsg = String.format("%s; Reason: %s", e.getMessage(), Utils.toJson(reason));

e = new Exception(errMsg, e);
}
Expand Down Expand Up @@ -440,19 +437,20 @@ protected void patchTaskStage(AdapterRequest request, TaskStage taskStage, Throw
request.serviceTaskCallback.serviceSelfLink);
}

sendRequest(Operation.createPatch(callbackReference)
sendRequest(Operation
.createPatch(callbackReference)
.setBody(callbackResponse)
.setContextId(request.getRequestId())
.setCompletion((o, e) -> {
if (e != null) {
logWarning("Notifying parent task for resource: %s %s failed: %s",
request.resourceReference,
request.getRequestTrackingLog(), Utils.toString(e));
request.resourceReference, request.getRequestTrackingLog(),
Utils.toString(e));
}
}));
} catch (Throwable e) {
logWarning(
"System exception while calling back docker operation requester for resource: %s %s",
logWarning("System exception while calling back docker operation requester for"
+ " resource: %s %s",
request.resourceReference, request.getRequestTrackingLog());
logSevere(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@
import com.vmware.admiral.service.common.ConfigurationService.ConfigurationFactoryService;
import com.vmware.admiral.service.common.ConfigurationService.ConfigurationState;
import com.vmware.admiral.service.common.LogService;
import com.vmware.admiral.service.common.ServiceTaskCallback;
import com.vmware.photon.controller.model.resources.ComputeService.ComputeState;
import com.vmware.xenon.common.FileUtils;
import com.vmware.xenon.common.LocalizableValidationException;
Expand Down Expand Up @@ -998,8 +997,9 @@ private void inspectContainer(RequestContext context) {
context.request.getRequestTrackingLog());

if (context.containerState.id == null) {
if (!context.requestFailed && (context.containerState.powerState == null
|| context.containerState.powerState.isUnmanaged())) {
if (!context.requestFailed &&
(context.containerState.powerState == null
|| context.containerState.powerState.isUnmanaged())) {
patchTaskStage(context.request, TaskStage.FINISHED, null);
} else {
fail(context.request, new IllegalStateException("container id is required"
Expand All @@ -1008,25 +1008,19 @@ private void inspectContainer(RequestContext context) {
return;
}

context.executor.inspectContainer(
inspectCommandInput,
(o, ex) -> {
if (ex != null) {
logWarning("Exception while inspecting container [%s] of host [%s]",
context.containerState.documentSelfLink,
context.computeState.documentSelfLink);
fail(context.request, o, ex);
} else {
handleExceptions(
context.request,
context.operation,
() -> {
Map<String, Object> properties = o.getBody(Map.class);
patchContainerState(context.request, context.containerState,
properties, context);
});
}
context.executor.inspectContainer(inspectCommandInput, (o, ex) -> {
if (ex != null) {
logWarning("Exception while inspecting container [%s] of host [%s]",
context.containerState.documentSelfLink,
context.computeState.documentSelfLink);
fail(context.request, o, ex);
} else {
handleExceptions(context.request, context.operation, () -> {
Map<String, Object> props = o.getBody(Map.class);
patchContainerState(context.request, context.containerState, props, context);
});
}
});
}

private void execContainer(RequestContext context) {
Expand Down Expand Up @@ -1178,26 +1172,6 @@ private void patchContainerState(AdapterRequest request,
if (!context.requestFailed) {
patchTaskStage(request, TaskStage.FINISHED, ex);
}
if (newContainerState.powerState == PowerState.RUNNING) {
// request fetch stats

ContainerInstanceRequest containerRequest = new ContainerInstanceRequest();
containerRequest.operationTypeId = ContainerOperationType.STATS.id;
containerRequest.resourceReference = request.resourceReference;
containerRequest.serviceTaskCallback = ServiceTaskCallback.createEmpty();

RequestContext newContext = new RequestContext();
newContext.containerState = newContainerState;
newContext.computeState = context.computeState;
newContext.containerDescription = context.containerDescription;
newContext.request = containerRequest;
newContext.commandInput = context.commandInput;
newContext.executor = context.executor;
newContext.operation = context.operation;

processOperation(newContext);
return;
}
}));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;

import com.google.gson.JsonElement;

Expand Down Expand Up @@ -117,8 +116,7 @@ private void doCreateImage(BaseRequestContext ctx, ComputeService.ComputeState c
// use 'fromImage' - this will perform a docker pull
ctx.commandInput.withProperty(DOCKER_IMAGE_FROM_PROP_NAME, fullImageName);

getHost().log(Level.INFO, "Pulling image: %s %s", fullImageName, ctx.request
.getRequestTrackingLog());
logInfo("Pulling image: %s %s", fullImageName, ctx.request.getRequestTrackingLog());

Operation.CompletionHandler imageCompletionHandler = (o, ex) -> {
if (ex != null) {
Expand All @@ -127,14 +125,11 @@ private void doCreateImage(BaseRequestContext ctx, ComputeService.ComputeState c
computeState.documentSelfLink);
fail(ctx.request, o, ex);
} else {
handleExceptions(
ctx.request,
null,
() -> {
logInfo("Image pulled: %s on remote machine: %s", fullImageName,
computeState.documentSelfLink);
patchTaskStage(ctx.request, TaskState.TaskStage.FINISHED, null);
});
handleExceptions(ctx.request, null, () -> {
logInfo("Image pulled: %s on remote machine: %s", fullImageName,
computeState.documentSelfLink);
patchTaskStage(ctx.request, TaskState.TaskStage.FINISHED, null);
});
}
};

Expand All @@ -151,7 +146,7 @@ private void doTagImage(BaseRequestContext ctx, ComputeService.ComputeState comp
tagCommandInput.withProperty(DOCKER_IMAGE_REPOSITORY_PROP_NAME, imageRepo);
tagCommandInput.withProperty(DOCKER_IMAGE_TAG_PROP_NAME, imageTag);

getHost().log(Level.INFO, "Tagging image: %s %s with repo: %s, tag: %s", fullImageName,
logInfo("Tagging image: %s %s with repo: %s, tag: %s", fullImageName,
ctx.request.getRequestTrackingLog(), imageRepo, imageTag);

Operation.CompletionHandler imageCompletionHandler = (o, ex) -> {
Expand Down
Loading

0 comments on commit d982957

Please sign in to comment.