@Override public Service createServiceInstance() throws Throwable { ExampleService s = new ExampleService(); s.toggleOption(ServiceOption.PERIODIC_MAINTENANCE, true); return s; } }
protected ExampleServiceState updateState(Operation update) { // A DCP service handler is state-less: Everything it needs is provided as part of the // of the operation. The body and latest state associated with the service are retrieved // below. ExampleServiceState body = getBody(update); ExampleServiceState currentState = getState(update); // use helper that will merge automatically current state, with state supplied in body. // Note the usage option PropertyUsageOption.AUTO_MERGE_IF_NOT_NULL has been set on the // "name" field. boolean hasStateChanged = Utils.mergeWithState(getStateDescription(), currentState, body); updateCounter(body, currentState, hasStateChanged); if (body.documentExpirationTimeMicros != currentState.documentExpirationTimeMicros) { currentState.documentExpirationTimeMicros = body.documentExpirationTimeMicros; } // response has latest, updated state update.setBody(currentState); return currentState; }
@Override public ServiceDocument getDocumentTemplate() { ServiceDocument template = super.getDocumentTemplate(); template.documentDescription.documentIndexingOptions = EnumSet.of( ServiceDocumentDescription.DocumentIndexingOption.INDEX_METADATA); return template; } }
@Override @RouteDocumentation(description = "@PUT") public void handlePut(Operation put) { ExampleServiceState newState = getBody(put); ExampleServiceState currentState = getState(put); // example of structural validation: check if the new state is acceptable if (currentState.name != null && newState.name == null) { put.fail(new IllegalArgumentException("name must be set")); return; } updateCounter(newState, currentState, false); // replace current state, with the body of the request, in one step setState(put, newState); put.complete(); }
public ExampleODLService() { super(); super.toggleOption(ServiceOption.INSTRUMENTATION, false); } }
@Override public Service createServiceInstance() throws Throwable { return new ExampleService(); } }
private void factorySynchronizationNoChildren() throws Throwable { int factoryCount = Math.max(this.serviceCount, 25); setUp(this.nodeCount); // start many factories, in each host, so when the nodes join there will be a storm // of synchronization requests between the nodes + factory instances TestContext testContext = this.host.testCreate(this.nodeCount * factoryCount); for (VerificationHost h : this.host.getInProcessHostMap().values()) { for (int i = 0; i < factoryCount; i++) { Operation startPost = Operation.createPost( UriUtils.buildUri(h, UriUtils.buildUriPath(ExampleService.FACTORY_LINK, UUID .randomUUID().toString()))) .setCompletion(testContext.getCompletion()); h.startService(startPost, ExampleService.createFactory()); } } testContext.await(); this.host.joinNodesAndVerifyConvergence(this.host.getPeerCount()); }
@Test public void childOptionsInConfigGetRequest() throws Throwable { EnumSet<ServiceOption> exampleOptions = new ExampleService().getOptions(); URI configUri = UriUtils.buildConfigUri(this.host, ExampleService.FACTORY_LINK); Operation get = Operation.createGet(configUri); FactoryServiceConfiguration config = this.host.getTestRequestSender().sendAndWait(get, FactoryServiceConfiguration.class); assertEquals(exampleOptions, config.childOptions); }
public InMemoryExampleService() { super(); super.setDocumentIndexPath(ServiceUriPaths.CORE_IN_MEMORY_DOCUMENT_INDEX); }
private void handlePatchForStrictUpdate(Operation patch) { ExampleServiceState currentState = getState(patch); StrictUpdateRequest body = patch.getBody(StrictUpdateRequest.class); if (body.kind == null || !body.kind.equals(Utils.buildKind(StrictUpdateRequest.class))) { patch.fail(new IllegalArgumentException("invalid kind: %s" + body.kind)); return; } if (body.name == null) { patch.fail(new IllegalArgumentException("name is required")); return; } if (body.documentVersion != currentState.documentVersion) { String errorString = String .format("Current version %d. Request version %d", currentState.documentVersion, body.documentVersion); patch.fail(new IllegalArgumentException(errorString)); return; } currentState.name = body.name; patch.setBody(currentState); patch.complete(); }
/** * A chain of filters, each of them is a {@link java.util.function.Predicate <Operation>}. When {@link #processRequest} is called * the filters are evaluated sequentially, where each filter's {@link java.util.function.Predicate <Operation>#test} can return * <code>true</code> to have the next filter in the chain continue process the request or * <code>false</code> to stop processing. */ @Override public OperationProcessingChain getOperationProcessingChain() { if (super.getOperationProcessingChain() != null) { return super.getOperationProcessingChain(); } RequestRouter myRouter = new RequestRouter(); myRouter.register( Action.PATCH, new RequestRouter.RequestBodyMatcher<>( StrictUpdateRequest.class, "kind", StrictUpdateRequest.KIND), this::handlePatchForStrictUpdate, "Strict update version check"); OperationProcessingChain opProcessingChain = OperationProcessingChain.create(myRouter); setOperationProcessingChain(opProcessingChain); return opProcessingChain; }
public ExampleService() { super(ExampleServiceState.class); toggleOption(ServiceOption.PERSISTENCE, true); toggleOption(ServiceOption.REPLICATION, true); toggleOption(ServiceOption.INSTRUMENTATION, true); toggleOption(ServiceOption.OWNER_SELECTION, true); }
@Override public Service createServiceInstance() throws Throwable { return new ExampleService(); } }
@Test public void registerForServiceAvailabilityBeforeAndAfterMultiple() throws Throwable { setUp(false); int serviceCount = 100; this.host.testStart(serviceCount * 3); String[] links = new String[serviceCount]; for (int i = 0; i < serviceCount; i++) { URI u = UriUtils.buildUri(this.host, UUID.randomUUID().toString()); links[i] = u.getPath(); this.host.registerForServiceAvailability(this.host.getCompletion(), u.getPath()); this.host.startService(Operation.createPost(u), ExampleService.createFactory()); this.host.registerForServiceAvailability(this.host.getCompletion(), u.getPath()); } this.host.registerForServiceAvailability(this.host.getCompletion(), links); this.host.testWait(); }
@Test public void childOptionsInConfigGetRequest() throws Throwable { EnumSet<ServiceOption> exampleOptions = new ExampleService().getOptions(); URI configUri = UriUtils.buildConfigUri(this.host, ExampleService.FACTORY_LINK); Operation get = Operation.createGet(configUri); FactoryServiceConfiguration config = this.host.getTestRequestSender().sendAndWait(get, FactoryServiceConfiguration.class); assertEquals(exampleOptions, config.childOptions); }
public InMemoryExampleService() { super(); super.setDocumentIndexPath(ServiceUriPaths.CORE_IN_MEMORY_DOCUMENT_INDEX); }
@Override public void handleDelete(Operation delete) { if (!delete.hasBody()) { delete.complete(); return; } // A DELETE can be used to both stop the service, mark it deleted in the index // so its excluded from queries, but it can also set its expiration so its state // history is permanently removed ExampleServiceState currentState = getState(delete); ExampleServiceState st = delete.getBody(ExampleServiceState.class); if (st.documentExpirationTimeMicros > 0) { currentState.documentExpirationTimeMicros = st.documentExpirationTimeMicros; } delete.complete(); }
@Override public Service createServiceInstance() throws Throwable { ExampleService s = new ExampleService(); s.toggleOption(ServiceOption.PERIODIC_MAINTENANCE, true); return s; } }
public ExampleImmutableService() { super(); super.toggleOption(ServiceOption.IMMUTABLE, true); super.toggleOption(ServiceOption.INSTRUMENTATION, false); } }
@Override public Service createServiceInstance() throws Throwable { return new ExampleService(); } }