@Test public void statRESTActions() throws Throwable { String name = UUID.randomUUID().toString(); ExampleServiceState s = new ExampleServiceState(); s.name = name; Consumer<Operation> bodySetter = (o) -> { o.setBody(s); }; URI factoryURI = UriUtils.buildFactoryUri(this.host, ExampleService.class); long c = 2; Map<URI, ExampleServiceState> states = this.host.doFactoryChildServiceStart(null, c, ExampleServiceState.class, bodySetter, factoryURI); ExampleServiceState exampleServiceState = states.values().iterator().next(); ServiceStats.ServiceStat stat = new ServiceStat(); stat.name = "key1"; stat.latestValue = 100; stat.unit = "unit"; this.host.sendAndWaitExpectSuccess(Operation.createPost(UriUtils.buildStatsUri( this.host, exampleServiceState.documentSelfLink)).setBody(stat)); ServiceStats allStats = this.host.getServiceState(null, ServiceStats.class, UriUtils.buildStatsUri( this.host, exampleServiceState.documentSelfLink)); ServiceStat retStatEntry = allStats.entries.get("key1"); assertTrue(retStatEntry.accumulatedValue == 100); assertTrue(retStatEntry.latestValue == 100); assertTrue(retStatEntry.version == 1); assertTrue(retStatEntry.unit.equals("unit")); assertTrue(retStatEntry.sourceTimeMicrosUtc == null); stat.latestValue = 50; stat.unit = "unit1"; Long updatedMicrosUtc1 = Utils.getNowMicrosUtc(); stat.sourceTimeMicrosUtc = updatedMicrosUtc1; this.host.sendAndWaitExpectSuccess(Operation.createPost(UriUtils.buildStatsUri( this.host, exampleServiceState.documentSelfLink)).setBody(stat)); allStats = getStats(exampleServiceState); retStatEntry = allStats.entries.get("key1"); assertTrue(retStatEntry.accumulatedValue == 150); assertTrue(retStatEntry.latestValue == 50); assertTrue(retStatEntry.version == 2); assertTrue(retStatEntry.unit.equals("unit1")); assertTrue(retStatEntry.sourceTimeMicrosUtc == updatedMicrosUtc1); stat.name = "key2"; stat.latestValue = 50; stat.unit = "unit2"; Long updatedMicrosUtc2 = Utils.getNowMicrosUtc(); stat.sourceTimeMicrosUtc = updatedMicrosUtc2; this.host.sendAndWaitExpectSuccess(Operation.createPost(UriUtils.buildStatsUri( this.host, exampleServiceState.documentSelfLink)).setBody(stat)); allStats = getStats(exampleServiceState); retStatEntry = allStats.entries.get("key1"); assertTrue(retStatEntry.accumulatedValue == 150); assertTrue(retStatEntry.latestValue == 50); assertTrue(retStatEntry.version == 2); assertTrue(retStatEntry.unit.equals("unit1")); assertTrue(retStatEntry.sourceTimeMicrosUtc == updatedMicrosUtc1); retStatEntry = allStats.entries.get("key2"); assertTrue(retStatEntry.accumulatedValue == 50); assertTrue(retStatEntry.latestValue == 50); assertTrue(retStatEntry.version == 1); assertTrue(retStatEntry.unit.equals("unit2")); assertTrue(retStatEntry.sourceTimeMicrosUtc == updatedMicrosUtc2); stat.name = "key1"; stat.latestValue = 75; stat.unit = "replaceUnit"; stat.sourceTimeMicrosUtc = null; this.host.sendAndWaitExpectSuccess(Operation.createPut(UriUtils.buildStatsUri( this.host, exampleServiceState.documentSelfLink)).setBody(stat)); allStats = getStats(exampleServiceState); retStatEntry = allStats.entries.get("key1"); assertTrue(retStatEntry.accumulatedValue == 75); assertTrue(retStatEntry.latestValue == 75); assertTrue(retStatEntry.version == 1); assertTrue(retStatEntry.unit.equals("replaceUnit")); assertTrue(retStatEntry.sourceTimeMicrosUtc == null); ServiceStats stats = new ServiceStats(); stat.name = "key3"; stat.latestValue = 200; stat.unit = "unit3"; stats.entries.put("key3", stat); this.host.sendAndWaitExpectSuccess(Operation.createPut(UriUtils.buildStatsUri( this.host, exampleServiceState.documentSelfLink)).setBody(stats)); allStats = getStats(exampleServiceState); if (allStats.entries.size() != 1) { ServiceStat nodeGroupStat = allStats.entries.get( Service.STAT_NAME_NODE_GROUP_CHANGE_MAINTENANCE_COUNT); if (nodeGroupStat == null) { throw new IllegalStateException( "Expected single stat, got: " + Utils.toJsonHtml(allStats)); } } retStatEntry = allStats.entries.get("key3"); assertTrue(retStatEntry.accumulatedValue == 200); assertTrue(retStatEntry.latestValue == 200); assertTrue(retStatEntry.version == 1); assertTrue(retStatEntry.unit.equals("unit3")); stat.latestValue = 25; this.host.sendAndWaitExpectSuccess(Operation.createPatch(UriUtils.buildStatsUri( this.host, exampleServiceState.documentSelfLink)).setBody(stat)); allStats = getStats(exampleServiceState); retStatEntry = allStats.entries.get("key3"); assertTrue(retStatEntry.latestValue == 225); assertTrue(retStatEntry.version == 2); verifyGetWithODataOnStats(exampleServiceState); verifyStatCreationAttemptAfterGet(); }
@Override public ServiceStats getStats() { if (this.stats == null) { return null; } synchronized (this.stats) { return Utils.clone(this.stats); } }
UtilityService implements Service { @Override public ServiceStats getStats() { if (this.stats == null) { return null; } synchronized (this.stats) { return Utils.clone(this.stats); } } }
UtilityService implements Service { @Override public ServiceStats getStats() { if (this.stats == null) { return null; } synchronized (this.stats) { return Utils.clone(this.stats); } } UtilityService(); }
UtilityService implements Service { @Override public ServiceStats getStats() { if (this.stats == null) { return null; } synchronized (this.stats) { return Utils.clone(this.stats); } } UtilityService(); UtilityService setParent(Service parent); @Override void authorizeRequest(Operation op); @Override void handleRequest(Operation op); @Override void handleCreate(Operation post); @Override void handleStart(Operation startPost); @Override void handleStop(Operation op); @Override void handleRequest(Operation op, OperationProcessingStage opProcessingStage); boolean hasSubscribers(); boolean hasStats(); void notifySubscribers(Operation op); void redirectGetToHtmlUiResource(Operation op, String htmlResourcePath); @Override void handleConfigurationRequest(Operation op); void handlePatchConfiguration(Operation op, ServiceConfigUpdateRequest updateBody); @Override void setStat(ServiceStat stat, double newValue); @Override void adjustStat(ServiceStat stat, double delta); @Override ServiceStat getStat(String name); @Override ServiceStats getStats(); ServiceStat getStat(String name, boolean create); @Override ServiceHost getHost(); @Override String getSelfLink(); @Override URI getUri(); @Override OperationProcessingChain getOperationProcessingChain(); @Override ProcessingStage getProcessingStage(); @Override EnumSet<ServiceOption> getOptions(); @Override boolean hasOption(ServiceOption cap); @Override void toggleOption(ServiceOption cap, boolean enable); @Override void adjustStat(String name, double delta); @Override void setStat(String name, double newValue); @Override void handleMaintenance(Operation post); @Override void setHost(ServiceHost serviceHost); @Override void setSelfLink(String path); @Override void setOperationProcessingChain(OperationProcessingChain opProcessingChain); @Override void setProcessingStage(ProcessingStage initialized); @Override ServiceDocument setInitialState(Object state, Long initialVersion); @Override Service getUtilityService(String uriPath, boolean allocate); @Override boolean queueRequest(Operation op); @Override void sendRequest(Operation op); @Override ServiceDocument getDocumentTemplate(); @Override void setPeerNodeSelectorPath(String uriPath); @Override String getPeerNodeSelectorPath(); @Override void setDocumentIndexPath(String uriPath); @Override String getDocumentIndexPath(); @Override void setState(Operation op, ServiceDocument newState); @SuppressWarnings("unchecked") @Override T getState(Operation op); @Override void setMaintenanceIntervalMicros(long micros); @Override void setCacheClearDelayMicros(long micros); @Override long getMaintenanceIntervalMicros(); @Override long getCacheClearDelayMicros(); @Override Operation dequeueRequest(); @Override Class<? extends ServiceDocument> getStateType(); @Override final void setAuthorizationContext(Operation op, AuthorizationContext ctx); @Override final AuthorizationContext getSystemAuthorizationContext(); }
UtilityService implements Service { @Override public ServiceStats getStats() { if (this.stats == null) { return null; } synchronized (this.stats) { return Utils.clone(this.stats); } } UtilityService(); UtilityService setParent(Service parent); @Override void authorizeRequest(Operation op); @Override void handleRequest(Operation op); @Override void handleCreate(Operation post); @Override void handleStart(Operation startPost); @Override void handleStop(Operation op); @Override void handleRequest(Operation op, OperationProcessingStage opProcessingStage); boolean hasSubscribers(); boolean hasStats(); void notifySubscribers(Operation op); void redirectGetToHtmlUiResource(Operation op, String htmlResourcePath); @Override void handleConfigurationRequest(Operation op); void handlePatchConfiguration(Operation op, ServiceConfigUpdateRequest updateBody); @Override void setStat(ServiceStat stat, double newValue); @Override void adjustStat(ServiceStat stat, double delta); @Override ServiceStat getStat(String name); @Override ServiceStats getStats(); ServiceStat getStat(String name, boolean create); @Override ServiceHost getHost(); @Override String getSelfLink(); @Override URI getUri(); @Override OperationProcessingChain getOperationProcessingChain(); @Override ProcessingStage getProcessingStage(); @Override EnumSet<ServiceOption> getOptions(); @Override boolean hasOption(ServiceOption cap); @Override void toggleOption(ServiceOption cap, boolean enable); @Override void adjustStat(String name, double delta); @Override void setStat(String name, double newValue); @Override void handleMaintenance(Operation post); @Override void setHost(ServiceHost serviceHost); @Override void setSelfLink(String path); @Override void setOperationProcessingChain(OperationProcessingChain opProcessingChain); @Override void setProcessingStage(ProcessingStage initialized); @Override ServiceDocument setInitialState(Object state, Long initialVersion); @Override Service getUtilityService(String uriPath, boolean allocate); @Override boolean queueRequest(Operation op); @Override void sendRequest(Operation op); @Override ServiceDocument getDocumentTemplate(); @Override void setPeerNodeSelectorPath(String uriPath); @Override String getPeerNodeSelectorPath(); @Override void setDocumentIndexPath(String uriPath); @Override String getDocumentIndexPath(); @Override void setState(Operation op, ServiceDocument newState); @SuppressWarnings("unchecked") @Override T getState(Operation op); @Override void setMaintenanceIntervalMicros(long micros); @Override void setCacheClearDelayMicros(long micros); @Override long getMaintenanceIntervalMicros(); @Override long getCacheClearDelayMicros(); @Override Operation dequeueRequest(); @Override Class<? extends ServiceDocument> getStateType(); @Override final void setAuthorizationContext(Operation op, AuthorizationContext ctx); @Override final AuthorizationContext getSystemAuthorizationContext();  }