/** * Create a default factory service that starts instances of this task service on POST. */ public static Service createFactory() { Service fs = FactoryService.create(MigrationTaskService.class, State.class); // Set additional factory service option. This can be set in service constructor as well // but its really relevant on the factory of a service. fs.toggleOption(ServiceOption.IDEMPOTENT_POST, true); fs.toggleOption(ServiceOption.INSTRUMENTATION, true); return fs; }
private void discoverJarResources(Path path, Service s, Map<Path, String> pathToURIPath, Path baseUriPath, String prefix) throws URISyntaxException, IOException { for (ResourceEntry entry : FileUtils.findResources(s.getClass(), prefix)) { Path resourcePath = path.resolve(entry.suffix); Path uriPath = baseUriPath.resolve(entry.suffix); Path outputPath = getHost().copyResourceToSandbox(entry.url, resourcePath); if (outputPath == null) { // Failed to copy one resource, disable user interface for this service. s.toggleOption(ServiceOption.HTML_USER_INTERFACE, false); } else { pathToURIPath.put(outputPath, uriPath.toString().replace('\\', '/')); } } }
/** * Infrastructure use. Discover all jar resources for the specified service. */ public Path discoverJarResources(Path path, Service s, Map<Path, String> pathToURIPath, Path baseUriPath, String prefix) throws URISyntaxException, IOException { Path res = null; for (ResourceEntry entry : FileUtils.findResources(s.getClass(), prefix)) { Path resourcePath = path.resolve(entry.suffix); Path uriPath = baseUriPath.resolve(entry.suffix); Path outputPath = this.copyResourceToSandbox(entry.url, resourcePath); if (res == null) { String os = outputPath.toString(); res = Paths.get(os.substring(0, os.length() - entry.suffix.toString().length())); } if (outputPath == null) { // Failed to copy one resource, disable user interface for this service. s.toggleOption(ServiceOption.HTML_USER_INTERFACE, false); } else { pathToURIPath.put(outputPath, uriPath.toString().replace('\\', '/')); } } return res; }
@Override public Service createServiceInstance() throws Throwable { Service s = new MinimalTestService(); if (this.childServiceCaps != null) { for (ServiceOption c : this.childServiceCaps) { s.toggleOption(c, true); } } return s; }
@Override public Service createServiceInstance() throws Throwable { Service s = new OnDemandLoadService(); if (this.childServiceCaps != null) { for (ServiceOption c : this.childServiceCaps) { s.toggleOption(c, true); } } return s; } }
@Override public Service createServiceInstance() throws Throwable { Service s = new MinimalTestService(); if (this.childServiceCaps != null) { for (ServiceOption c : this.childServiceCaps) { s.toggleOption(c, true); } } return s; }
@Override public Service createServiceInstance() throws Throwable { Service s = new OnDemandLoadService(); if (this.childServiceCaps != null) { for (ServiceOption c : this.childServiceCaps) { s.toggleOption(c, true); } } return s; } }
private ServiceDocument getChildTemplate(String childLink) { if (this.childTemplate == null) { try { Service s = createServiceInstance(); s.setHost(getHost()); s.setSelfLink(childLink); s.toggleOption(ServiceOption.FACTORY_ITEM, true); this.childTemplate = s.getDocumentTemplate(); } catch (Throwable e) { logSevere(e); return null; } } return this.childTemplate; }
childService.toggleOption(ServiceOption.IDEMPOTENT_POST, true); childService.toggleOption(ServiceOption.FACTORY_ITEM, true);
private static void doHandlePeriodicMaintenanceImpl(Service s, Operation post) { ServiceMaintenanceRequest request = post.getBody(ServiceMaintenanceRequest.class); if (!request.reasons.contains( ServiceMaintenanceRequest.MaintenanceReason.PERIODIC_SCHEDULE)) { post.fail(new IllegalArgumentException("expected PERIODIC_SCHEDULE reason")); return; } post.complete(); ServiceStat stat = s.getStat(STAT_NAME_HANDLE_PERIODIC_MAINTENANCE); s.adjustStat(stat, 1); if (stat.latestValue >= PERIODIC_MAINTENANCE_MAX) { s.toggleOption(ServiceOption.PERIODIC_MAINTENANCE, false); } }
private static void doHandlePeriodicMaintenanceImpl(Service s, Operation post) { ServiceMaintenanceRequest request = post.getBody(ServiceMaintenanceRequest.class); if (!request.reasons.contains( ServiceMaintenanceRequest.MaintenanceReason.PERIODIC_SCHEDULE)) { post.fail(new IllegalArgumentException("expected PERIODIC_SCHEDULE reason")); return; } post.complete(); ServiceStat stat = s.getStat(STAT_NAME_HANDLE_PERIODIC_MAINTENANCE); s.adjustStat(stat, 1); if (stat.latestValue >= PERIODIC_MAINTENANCE_MAX) { s.toggleOption(ServiceOption.PERIODIC_MAINTENANCE, false); } }
@Test public void operationQueueLimit() throws Throwable { Service lifoService = new MinimalTestService(); lifoService.toggleOption(ServiceOption.LIFO_QUEUE, true); lifoService.toggleOption(ServiceOption.INSTRUMENTATION, true); lifoService = this.host.startServiceAndWait(lifoService, UUID.randomUUID().toString(), null); Service fifoService = new MinimalTestService(); fifoService.toggleOption(ServiceOption.LIFO_QUEUE, false); fifoService.toggleOption(ServiceOption.INSTRUMENTATION, true); fifoService = this.host.startServiceAndWait(fifoService, UUID.randomUUID().toString(), null); int limit = 2; this.host.log("Verifying LIFO service"); this.host.setOperationQueueLimit(lifoService.getUri(), limit); verifyOperationQueueLimit(lifoService.getUri(), limit); this.host.log("Verifying FIFO service"); this.host.setOperationQueueLimit(fifoService.getUri(), limit); verifyOperationQueueLimit(fifoService.getUri(), limit); }
@Test public void operationQueueLimit() throws Throwable { Service lifoService = new MinimalTestService(); lifoService.toggleOption(ServiceOption.LIFO_QUEUE, true); lifoService.toggleOption(ServiceOption.INSTRUMENTATION, true); lifoService = this.host.startServiceAndWait(lifoService, UUID.randomUUID().toString(), null); Service fifoService = new MinimalTestService(); fifoService.toggleOption(ServiceOption.LIFO_QUEUE, false); fifoService.toggleOption(ServiceOption.INSTRUMENTATION, true); fifoService = this.host.startServiceAndWait(fifoService, UUID.randomUUID().toString(), null); int limit = 2; this.host.log("Verifying LIFO service"); this.host.setOperationQueueLimit(lifoService.getUri(), limit); verifyOperationQueueLimit(lifoService.getUri(), limit); this.host.log("Verifying FIFO service"); this.host.setOperationQueueLimit(fifoService.getUri(), limit); verifyOperationQueueLimit(fifoService.getUri(), limit); }
if (options != null) { for (Service.ServiceOption cap : options) { e.toggleOption(cap, true); e.toggleOption(opt, false);
static void checkAndUpdateDocumentOwnership(ServiceHost host, Service service, long expirationTimeMicrosUtc, CompletionHandler ch) { Operation selectOwnerOp = Operation.createPost(null) .setExpiration(expirationTimeMicrosUtc); CompletionHandler c = (o, e) -> { if (e != null) { ch.handle(selectOwnerOp, e); return; } SelectOwnerResponse rsp = o.getBody(SelectOwnerResponse.class); service.toggleOption(ServiceOption.DOCUMENT_OWNER, rsp.isLocalHostOwner); ch.handle(selectOwnerOp, null); }; selectOwnerOp.setCompletion(c); host.selectOwner(service.getPeerNodeSelectorPath(), service.getSelfLink(), selectOwnerOp); }
if (options != null) { for (Service.ServiceOption cap : options) { e.toggleOption(cap, true); e.toggleOption(opt, false);
@Test public void periodicMaintenance() throws Throwable { try { this.host.setMaintenanceIntervalMicros(TimeUnit.MILLISECONDS.toMicros(50)); // Check StatelessService doCheckPeriodicMaintenance(new PeriodicMaintenanceTestStatelessService()); // Check StatefulService doCheckPeriodicMaintenance(new PeriodicMaintenanceTestStatefulService()); // Check StatelessService with dynamic toggle Service s = new PeriodicMaintenanceTestStatelessService(); s.toggleOption(ServiceOption.PERIODIC_MAINTENANCE, false); doCheckPeriodicMaintenance(s); // Check StatefulService with dynamic toggle s = new PeriodicMaintenanceTestStatefulService(); s.toggleOption(ServiceOption.PERIODIC_MAINTENANCE, false); doCheckPeriodicMaintenance(s); } finally { this.host.setMaintenanceIntervalMicros(TimeUnit.MILLISECONDS.toMicros( VerificationHost.FAST_MAINT_INTERVAL_MILLIS)); } }
@Test public void periodicMaintenance() throws Throwable { try { this.host.setMaintenanceIntervalMicros(TimeUnit.MILLISECONDS.toMicros(50)); // Check StatelessService doCheckPeriodicMaintenance(new PeriodicMaintenanceTestStatelessService()); // Check StatefulService doCheckPeriodicMaintenance(new PeriodicMaintenanceTestStatefulService()); // Check StatelessService with dynamic toggle Service s = new PeriodicMaintenanceTestStatelessService(); s.toggleOption(ServiceOption.PERIODIC_MAINTENANCE, false); doCheckPeriodicMaintenance(s); // Check StatefulService with dynamic toggle s = new PeriodicMaintenanceTestStatefulService(); s.toggleOption(ServiceOption.PERIODIC_MAINTENANCE, false); doCheckPeriodicMaintenance(s); } finally { this.host.setMaintenanceIntervalMicros(TimeUnit.MILLISECONDS.toMicros( VerificationHost.FAST_MAINT_INTERVAL_MILLIS)); } }
private void verifyFactoryStartedAndSynchronizedAfterNodeSynch(ExampleServiceHost h, String statName) throws Throwable { // start another instance of the example factory, verify that node synchronization maintenance // happened, even if it was started after the initial synch occurred Service factory = ExampleService.createFactory(); factory.toggleOption(ServiceOption.INSTRUMENTATION, true); Operation post = Operation.createPost( UriUtils.buildUri(h, UUID.randomUUID().toString())) .setCompletion(this.host.getCompletion()); this.host.testStart(1); h.startService(post, factory); this.host.testWait(); URI newExampleFactoryStatsUri = UriUtils.buildStatsUri(factory.getUri()); this.host.waitFor("node group change stat missing", () -> { ServiceStats stats = this.host.getServiceState(null, ServiceStats.class, newExampleFactoryStatsUri); ServiceStat st = stats.entries.get(statName); if (st != null && st.latestValue >= 1) { return true; } return false; }); this.host.waitForServiceAvailable(factory.getUri()); }
private void verifyFactoryStartedAndSynchronizedAfterNodeSynch(ExampleServiceHost h, String statName) throws Throwable { // start another instance of the example factory, verify that node synchronization maintenance // happened, even if it was started after the initial synch occurred Service factory = ExampleService.createFactory(); factory.toggleOption(ServiceOption.INSTRUMENTATION, true); Operation post = Operation.createPost( UriUtils.buildUri(h, UUID.randomUUID().toString())) .setCompletion(this.host.getCompletion()); this.host.testStart(1); h.startService(post, factory); this.host.testWait(); URI newExampleFactoryStatsUri = UriUtils.buildStatsUri(factory.getUri()); this.host.waitFor("node group change stat missing", () -> { ServiceStats stats = this.host.getServiceState(null, ServiceStats.class, newExampleFactoryStatsUri); ServiceStat st = stats.entries.get(statName); if (st != null && st.latestValue >= 1) { return true; } return false; }); this.host.waitForServiceAvailable(factory.getUri()); }