/** Handle service periodic maintenance calls. */ @Override public void handleMaintenance(Operation post) { post.complete(); Operation.CompletionHandler handler = (Operation op, Throwable failure) -> { if (null != failure) { // query failed so abort and retry next time logFailure(failure); return; } NodeSelectorService.SelectOwnerResponse rsp = op.getBody(NodeSelectorService.SelectOwnerResponse.class); if (!getHost().getId().equals(rsp.ownerNodeId)) { ServiceUtils.logInfo( TaskTriggerService.this, "Host[%s]: Not owner of scheduler [%s] (Owner Info [%s])", getHost().getId(), getSelfLink(), Utils.toJson(rsp)); return; } State state = new State(); sendSelfPatch(state); }; Operation selectOwnerOp = Operation.createPost(null) .setExpiration(ServiceUtils.computeExpirationTime(OWNER_SELECTION_TIMEOUT_MILLIS)) .setCompletion(handler); getHost().selectOwner(null, getSelfLink(), selectOwnerOp); }
@Override public void handleStart(Operation startOperation) { ServiceUtils.logInfo(this, "Starting service %s", getSelfLink()); State startState = startOperation.getBody(State.class); InitializationUtils.initialize(startState); ValidationUtils.validateState(startState); startOperation.complete(); }
@Override public void handleStart(Operation start) { try { validateState(start); start.complete(); } catch (Exception e) { start.fail(e); } }
void handlePatchForDeposit(Operation patch) { BankAccountServiceState currentState = getState(patch); BankAccountServiceRequest body = patch.getBody(BankAccountServiceRequest.class); currentState.balance += body.amount; setState(patch, currentState); patch.setBody(currentState); patch.complete(); }
@Override public void handlePatch(Operation patchOperation) { ServiceUtils.logInfo(this, "Handling patch for service %s", getSelfLink()); State currentState = getState(patchOperation); State patchState = patchOperation.getBody(State.class); ValidationUtils.validatePatch(currentState, patchState); PatchUtils.patchState(currentState, patchState); ValidationUtils.validateState(currentState); patchOperation.complete(); }
@Override public void handlePatch(Operation patch) { State currentState = getState(patch); State patchState = patch.getBody(State.class); this.validatePatch(currentState, patchState); this.applyPatch(currentState, patchState); this.validateState(currentState); patch.complete(); this.processPatch(currentState); }
@Override public void handleStart(Operation start) { ServiceUtils.logInfo(this, "Starting service %s", getSelfLink()); State s = start.getBody(State.class); this.initializeState(s); this.validateState(s); // set the maintenance interval to match the value in the state. this.setMaintenanceIntervalMicros(TimeUnit.MILLISECONDS.toMicros(s.triggerIntervalMillis)); start.complete(); }
void handlePatchForWithdraw(Operation patch) { BankAccountServiceState currentState = getState(patch); BankAccountServiceRequest body = patch.getBody(BankAccountServiceRequest.class); if (body.amount > currentState.balance) { patch.fail(new IllegalArgumentException("Not enough funds to withdraw")); return; } currentState.balance -= body.amount; setState(patch, currentState); patch.setBody(currentState); patch.complete(); }
@Override public void handlePatch(Operation patchOperation) { ServiceUtils.logInfo(this, "Handling patch for service %s", getSelfLink()); State startState = getState(patchOperation); State patchState = patchOperation.getBody(State.class); validatePatchState(startState, patchState); State currentState = applyPatch(startState, patchState); validateState(currentState); patchOperation.complete(); try { if (ControlFlags.isOperationProcessingDisabled(currentState.controlFlags)) { ServiceUtils.logInfo(this, "Skipping patch operation processing (disabled)"); } else if (TaskState.TaskStage.STARTED == currentState.taskState.stage) { createTenant(currentState); } } catch (Throwable t) { failTask(t); } }