/** * Gets the cached ServiceDocumentDescription instance for the service state. */ public ServiceDocumentDescription getStateDescription() { return getHost().buildDocumentDescription(this); }
@Override public long getCacheClearDelayMicros() { if (this.context.extras != null && this.context.extras.cacheClearDelayMicros != null) { return this.context.extras.cacheClearDelayMicros; } return getHost().getServiceCacheClearDelayMicros(); }
/** * Returns the authorization context associated with a given subject. */ public final AuthorizationContext getAuthorizationContextForSubject(String subject) { if (getHost().isPrivilegedService(this)) { return getHost().getAuthorizationContextForSubject(subject); } else { throw new RuntimeException( "Service not allowed to get authorization context for a subject"); } }
@Override public long getMaintenanceIntervalMicros() { if (this.context.extras == null) { return this.getHost().getMaintenanceIntervalMicros(); } return this.context.extras.maintenanceInterval; }
/** * Returns the system user's authorization context. */ @Override public final AuthorizationContext getSystemAuthorizationContext() { if (getHost().isPrivilegedService(this)) { return getHost().getSystemAuthorizationContext(); } else { throw new RuntimeException("Service not allowed to get system authorization context"); } }
/** * Set authorization context on operation. */ @Override public final void setAuthorizationContext(Operation op, AuthorizationContext ctx) { if (getHost().isPrivilegedService(this)) { op.setAuthorizationContext(ctx); } else { throw new RuntimeException("Service not allowed to set authorization context"); } }
@Override public void setMaintenanceIntervalMicros(long micros) { if (micros < 0) { throw new IllegalArgumentException("micros must be positive"); } if (micros > 0 && micros < Service.MIN_MAINTENANCE_INTERVAL_MICROS) { logWarning("Maintenance interval %d is less than the minimum interval %d" + ", reducing to min interval", micros, Service.MIN_MAINTENANCE_INTERVAL_MICROS); micros = Service.MIN_MAINTENANCE_INTERVAL_MICROS; } allocateExtraContext(); this.context.extras.maintenanceInterval = micros; if (getHost() != null && getProcessingStage() == ProcessingStage.AVAILABLE && micros < getHost().getMaintenanceCheckIntervalMicros()) { getHost().scheduleServiceMaintenance(this); } }
private void prepareRequest(Operation op) { // avoid URI allocation and parsing by using cached host URI string StringBuilder sb = Utils.getBuilder(); sb.append(getHost().getPublicUriAsString()).append(getSelfLink()); op.setReferer(sb.toString()); }
private void handleStopCompletion(Operation op) { if (checkServiceStopped(op, true)) { return; } getHost().stopService(this); op.complete(); }
public static URI getAdapterUri(StatefulService service, AdapterTypePath adapterTypePath, String endpointType) { return UriUtils.buildUri( ServiceHost.LOCAL_HOST, service.getHost().getPort(), adapterTypePath.adapterLink(endpointType), null); }
private boolean handleDeleteCompletion(Operation op) { if (op.isFromReplication() && hasOption(ServiceOption.OWNER_SELECTION)) { if (!op.isCommit()) { return false; } } if (checkServiceStopped(op, true)) { return true; } // DELETE completion runs when a DELETE was issued by a client, not local host shutdown. // It needs to stop the service now, since the handleDelete() and handleStop() handlers // have already run. getHost().markAsPendingDelete(this); getHost().stopService(this); return false; }
public TableDescription useStatefulService(StatefulService service) { if (service.getHost() == null) { service.setHost(this.host); } if (getFactoryLink() == null) { Class<? extends StatefulService> serviceType = service.getClass(); String factoryLink = getFactoryLinkFieldValue(serviceType); if (factoryLink == null) { throw new IllegalArgumentException( "Failed to add factory, missing FACTORY_LINK field"); } setFactoryLink(factoryLink); } setStateType(service.getStateType()); setServiceDocumentDescription(service.getDocumentTemplate().documentDescription); return this; }
@RouteDocumentation(description = "Read the service document") public void handleGet(Operation get) { if (!hasPendingTransactions()) { handleGetSimple(get); return; } handleGetWithinTransaction(this, get, this::handleGetSimple, this::failRequest, getHost().getSystemAuthorizationContext()); }
private void checkAndNestAuthupdateCompletionStage(Operation op) { if (!this.getHost().isAuthorizationEnabled() || !this.getHost().isPrivilegedService(this)) { processCompletionStageTransactionNotification(op, null); } else { op.nestCompletion((o, failure) -> { if (failure != null) { if (op.isWithinTransaction()) { processPending(op); } failRequest(op, failure); return; } processCompletionStageTransactionNotification(op, null); }); processCompletionStageUpdateAuthzArtifacts(op); } }
public static void findFactoryService(StatefulService service, Consumer<FactoryService> handler) { findService(service.getHost(), UriUtils.getParentPath(service.getSelfLink()), s -> { handler.accept((FactoryService) s); }); }
/** * Notify the transaction coordinator */ static Operation notifyTransactionCoordinatorOp(StatefulService s, Operation op, Throwable e) { URI txCoordinator = UriUtils.buildTransactionUri(s.getHost(), op.getTransactionId()); s.addPendingTransaction(txCoordinator.getPath()); Operation.TransactionContext operationsLogRecord = new Operation.TransactionContext(); operationsLogRecord.action = op.getAction(); operationsLogRecord.isSuccessful = e == null; Operation notifyCoordinatorOp = Operation.createPut(txCoordinator).setTransactionId(null); notifyCoordinatorOp.addRequestHeader(Operation.TRANSACTION_REFLINK_HEADER, s.getSelfLink()); return s.setPendingTransactionsAsBody(notifyCoordinatorOp, operationsLogRecord); }
public static Service findFactoryService(StatefulService service) { return findService(service.getHost(), UriUtils.getParentPath(service.getSelfLink())); }
public static void subscribeToNotifications(StatefulService service, Consumer<Operation> notificationConsumer, String taskLink) { ServiceSubscriber subscribeBody = new ServiceSubscriber(); subscribeBody.replayState = true; subscribeBody.usePublicUri = true; Operation subscribeOp = Operation .createPost(service, taskLink) .setReferer(service.getUri()) .setCompletion( (regOp, regEx) -> { if (regEx != null) { sendFailureSelfPatch(service, regEx); return; } }); ReliableSubscriptionService notificationTarget = ReliableSubscriptionService.create( subscribeOp, subscribeBody, notificationConsumer); service.getHost().startSubscriptionService(subscribeOp, notificationTarget, subscribeBody); }
private static Operation buildLatestInTransactionQueryTaskOp(StatefulService s, String txid, AuthorizationContext authContext) { Query.Builder queryBuilder = Query.Builder.create(); queryBuilder.addFieldClause(ServiceDocument.FIELD_NAME_SELF_LINK, s.getSelfLink()); queryBuilder.addFieldClause(ServiceDocument.FIELD_NAME_TRANSACTION_ID, txid); QueryTask.Builder queryTaskBuilder = QueryTask.Builder.createDirectTask() .setQuery(queryBuilder.build()); queryTaskBuilder.addOption(QueryOption.EXPAND_CONTENT); queryTaskBuilder.addOption(QueryOption.INCLUDE_ALL_VERSIONS); queryTaskBuilder.orderDescending(ServiceDocument.FIELD_NAME_VERSION, TypeName.LONG); QueryTask task = queryTaskBuilder.build(); Operation returnOp = Operation .createPost(s.getHost(), ServiceUriPaths.CORE_QUERY_TASKS) .setBody(task); returnOp.setAuthorizationContext(authContext); return returnOp; }