private void checkOperationExpiration(long now, Iterator<Operation> iterator) { while (iterator.hasNext()) { Operation op = iterator.next(); if (op == null || op.getExpirationMicrosUtc() > now) { // not expired, and since we walk in ascending order, no other operations // are expired break; } iterator.remove(); this.host.run(() -> op.fail(new TimeoutException(op.toString()))); } }
private void failWithTimeout(Operation op, Object originalBody) { Throwable e = new TimeoutException(op.getUri() + ":" + op.getExpirationMicrosUtc()); op.setStatusCode(Operation.STATUS_CODE_TIMEOUT); fail(e, op, originalBody); }
private void prepareRequest(Operation op, boolean longRunningRequest) { op.forceRemote(); if (op.getExpirationMicrosUtc() == 0L) { long timeout; if (longRunningRequest) { timeout = TimeUnit.SECONDS .toMicros(ClosureProps.DOCKER_IMAGE_REQUEST_TIMEOUT_SECONDS); } else { timeout = ServiceHost.ServiceHostState.DEFAULT_OPERATION_TIMEOUT_MICROS; } op.setExpiration(ServiceUtils.getExpirationTimeFromNowInMicros(timeout)); } }
private void setExpiration(Operation op) { if (op.getExpirationMicrosUtc() != 0) { return; } long expMicros = this.host != null ? this.host.getOperationTimeoutMicros() : ServiceHostState.DEFAULT_OPERATION_TIMEOUT_MICROS; op.setExpiration(Utils.fromNowMicrosUtc(expMicros)); }
/** * Common settings on all outgoing requests to the docker server */ protected void prepareRequest(Operation op, boolean longRunningRequest) { op.setReferer(host.getPublicUri()) .addRequestHeader(Operation.REQUEST_AUTH_TOKEN_HEADER, "") .forceRemote(); if (op.getExpirationMicrosUtc() == 0) { long timeout = longRunningRequest ? TimeUnit.SECONDS.toMicros(DOCKER_IMAGE_REQUEST_TIMEOUT_SECONDS) : TimeUnit.SECONDS.toMicros(DOCKER_REQUEST_TIMEOUT_SECONDS); op.setExpiration(ServiceUtils.getExpirationTimeFromNowInMicros(timeout)); } }
if (op.getExpirationMicrosUtc() < Utils.getSystemNowMicrosUtc()) { op.getExpirationMicrosUtc(), Utils.getSystemNowMicrosUtc()))); return true;
private void prepareRequest(Operation op, KubernetesContext context) { String authorizationHeaderValue = AuthUtils.createAuthorizationHeader(context.credentials); if (authorizationHeaderValue != null) { op.addRequestHeader(Operation.AUTHORIZATION_HEADER, authorizationHeaderValue); } createOrUpdateTargetSsl(context); op.setReferer(host.getPublicUri()); op.forceRemote(); setConnectionTag(context.credentials, op); if (op.getExpirationMicrosUtc() == 0) { long timeout = TimeUnit.SECONDS.toMicros(REQUEST_TIMEOUT_SECONDS); op.setExpiration(ServiceUtils.getExpirationTimeFromNowInMicros(timeout)); } }
/** * Complete operation when target service for the given path becomes available in node group * * @see ServiceHost#registerForServiceAvailability(CompletionHandler, boolean, String...) * @see #checkServiceAvailability(CompletionHandler, ServiceHost, URI, String) */ public static void registerForReplicatedServiceAvailability(ServiceHost host, Operation op, String servicePath, String nodeSelectorPath) { CompletionHandler ch = (o, e) -> { if (e != null) { if (op.getExpirationMicrosUtc() < Utils.getSystemNowMicrosUtc()) { String msg = "Failed to check replicated service availability"; op.fail(new TimeoutException(msg)); return; } // service is not yet available, reschedule host.scheduleCore(() -> { registerForReplicatedServiceAvailability(host, op, servicePath, nodeSelectorPath); }, host.getMaintenanceIntervalMicros(), TimeUnit.MICROSECONDS); return; } op.complete(); }; host.checkReplicatedServiceAvailable(ch, servicePath, nodeSelectorPath); }
public static void registerForReplicatedServiceAvailability(ServiceHost host, Operation op, String servicePath, String nodeSelectorPath, AuthorizationContext authorizationContext) { CompletionHandler ch = (o, e) -> { if (e != null) { if (op.getExpirationMicrosUtc() < Utils.getSystemNowMicrosUtc()) { String msg = "Failed to check replicated service availability"; op.fail(new TimeoutException(msg)); return; } // service is not yet available, reschedule host.scheduleCore(() -> { registerForReplicatedServiceAvailability(host, op, servicePath, nodeSelectorPath, authorizationContext); }, host.getMaintenanceIntervalMicros(), TimeUnit.MICROSECONDS); return; } op.complete(); }; URI serviceUri = UriUtils.buildUri(host, servicePath); checkReplicatedServiceAvailability(ch, host, serviceUri, nodeSelectorPath, authorizationContext); }
if (op.getExpirationMicrosUtc() == 0) { long expirationMicros = Utils .fromNowMicrosUtc(this.state.operationTimeoutMicros);
if (op.getExpirationMicrosUtc() < Utils.getSystemNowMicrosUtc()) { op.setBodyNoCloning(fo.getBodyRaw()) .fail(new CancellationException("Expired at " + op.getExpirationMicrosUtc())); return;
.setRetryCount(0) .transferRefererFrom(put) .setExpiration(put.getExpirationMicrosUtc()) .setCompletion( (o, e) -> {
private void selectAndForwardRequestToOwner(Service s, String path, Operation op, Service parent, OperationProcessingContext context) { String nodeSelectorPath = parent != null ? parent.getPeerNodeSelectorPath() : s.getPeerNodeSelectorPath(); ServiceHost host = context.getHost(); CompletionHandler ch = (o, e) -> { if (e != null) { host.log(Level.SEVERE, "Owner selection failed for service %s, op %d. Error: %s", op .getUri().getPath(), op.getId(), e.toString()); context.resumeProcessingRequest(op, FilterReturnCode.FAILED_STOP_PROCESSING, e); op.setRetryCount(0).fail(e); return; } SelectOwnerResponse rsp = o.getBody(SelectOwnerResponse.class); if (rsp.isLocalHostOwner) { context.resumeProcessingRequest(op, FilterReturnCode.CONTINUE_PROCESSING, null); } else { forwardRequestToOwner(op, rsp, context); } }; Operation selectOwnerOp = Operation .createPost(null) .setExpiration(op.getExpirationMicrosUtc()) .setCompletion(ch); host.selectOwner(nodeSelectorPath, path, selectOwnerOp); }
private static Operation createReplicationRequest(Operation outboundOp, URI remoteUri) { Operation update = Operation.createPost(remoteUri) .setAction(outboundOp.getAction()) .setRetryCount(1) .forceRemote() .setExpiration(outboundOp.getExpirationMicrosUtc()) .transferRefererFrom(outboundOp); String pragmaHeader = outboundOp.getRequestHeaderAsIs(Operation.PRAGMA_HEADER); if (pragmaHeader != null && !Operation.PRAGMA_DIRECTIVE_FORWARDED.equals(pragmaHeader)) { update.addRequestHeader(Operation.PRAGMA_HEADER, pragmaHeader); update.addPragmaDirective(Operation.PRAGMA_DIRECTIVE_REPLICATED); } String commitHeader = outboundOp.getRequestHeaderAsIs(Operation.REPLICATION_PHASE_HEADER); if (commitHeader != null) { update.addRequestHeader(Operation.REPLICATION_PHASE_HEADER, commitHeader); } Utils.encodeAndTransferLinkedStateToBody(outboundOp, update, BINARY_SERIALIZATION == 1); update.setFromReplication(true); update.setConnectionTag(ServiceClient.CONNECTION_TAG_REPLICATION); if (NodeSelectorService.REPLICATION_OPERATION_OPTION != null) { update.toggleOption(NodeSelectorService.REPLICATION_OPERATION_OPTION, true); } if (update.getCookies() != null) { update.getCookies().clear(); } return update; }
private void sendPatchToBackupService(Operation op, String backupServiceLink, Object body) { Operation patch = Operation.createPatch(this, backupServiceLink) .transferRequestHeadersFrom(op) .transferRefererFrom(op) .setExpiration(op.getExpirationMicrosUtc()) .setBody(body) .setCompletion((o, e) -> { if (e != null) { op.fail(e); return; } op.transferResponseHeadersFrom(o); op.setBodyNoCloning(o.getBodyRaw()); op.complete(); }); sendRequest(patch); }
/** * Executes /stats request to the container state and copy its response to the GET operation. */ private void forwardStatsResponse(Operation op, ContainerState containerState) { sendRequest(Operation .createGet(UriUtils.buildStatsUri(getHost(), containerState.documentSelfLink)) .setExpiration(op.getExpirationMicrosUtc()) .setCompletion((o, e) -> { op.setBodyNoCloning(o.getBodyRaw()); op.setStatusCode(o.getStatusCode()); op.transferResponseHeadersFrom(o); if (e != null) { op.fail(e); } else { op.complete(); } })); }
private void verifyOp(Operation op, SerializedOperation sop) { assertEquals(op.getAction(), sop.action); assertEquals(op.getUri().getHost(), sop.host); assertEquals(op.getUri().getPort(), sop.port); assertEquals(op.getUri().getPath(), sop.path); assertEquals(op.getUri().getQuery(), sop.query); assertEquals(op.getId(), sop.id.longValue()); assertEquals(op.getReferer(), sop.referer); assertEquals(op.getBodyRaw(), sop.jsonBody); assertEquals(op.getStatusCode(), sop.statusCode); assertEquals(op.getOptions(), sop.options); assertEquals(op.getContextId(), sop.contextId); assertEquals(op.getTransactionId(), sop.transactionId); assertEquals(op.getUri().getUserInfo(), sop.userInfo); assertEquals(SerializedOperation.KIND, sop.documentKind); assertEquals(op.getExpirationMicrosUtc(), sop.documentExpirationTimeMicros); }
private void verifyOp(Operation op, SerializedOperation sop) { assertEquals(op.getAction(), sop.action); assertEquals(op.getUri().getHost(), sop.host); assertEquals(op.getUri().getPort(), sop.port); assertEquals(op.getUri().getPath(), sop.path); assertEquals(op.getUri().getQuery(), sop.query); assertEquals(op.getId(), sop.id.longValue()); assertEquals(op.getReferer(), sop.referer); assertEquals(op.getBodyRaw(), sop.jsonBody); assertEquals(op.getStatusCode(), sop.statusCode); assertEquals(op.getOptions(), sop.options); assertEquals(op.getContextId(), sop.contextId); assertEquals(op.getTransactionId(), sop.transactionId); assertEquals(op.getUri().getUserInfo(), sop.userInfo); assertEquals(SerializedOperation.KIND, sop.documentKind); assertEquals(op.getExpirationMicrosUtc(), sop.documentExpirationTimeMicros); }
.transferRefererFrom(parentGet) .addHeader(range[0].toRangeHeader(), false) .setExpiration(parentGet.getExpirationMicrosUtc()) .setCompletion( (ox, ex) -> {
private void synchWithPeersAndReProcess(Operation op) { ServiceDocument d = new ServiceDocument(); d.documentSelfLink = UriUtils.getLastPathSegment(getSelfLink()); Operation synchOwnerRequest = Operation.createPost(getUri()) .setBody(d) .linkState(op.getLinkedState()) .setReferer(getUri()) .setConnectionSharing(true) .setConnectionTag(ServiceClient.CONNECTION_TAG_SYNCHRONIZATION) .addPragmaDirective(Operation.PRAGMA_DIRECTIVE_SYNCH_OWNER) .setRetryCount(0) .setExpiration(op.getExpirationMicrosUtc()) .setCompletion((o, e) -> { if (e != null) { op.fail(e); return; } // now that we have the latest state, and are marked as owner, // we can re-process the request handleRequest(op); }); synchronizeWithPeers(synchOwnerRequest, null); }