/** * Adds the supplied argument to the value from {@link #getSystemNowMicrosUtc()} and returns * an absolute expiration time in the future */ public static long fromNowMicrosUtc(long deltaMicros) { return getSystemNowMicrosUtc() + deltaMicros; }
/** * Expects an absolute time, in microseconds since Epoch and returns true if the value represents * a time before the current system time */ public static boolean beforeNow(long microsUtc) { return getSystemNowMicrosUtc() >= microsUtc; }
public static boolean isExpired(ServiceDocument serviceDocument) { return serviceDocument != null && serviceDocument.documentExpirationTimeMicros != 0 && serviceDocument.documentExpirationTimeMicros < Utils.getSystemNowMicrosUtc(); } }
public void updateLastUseTime() { this.lastUseTimeMicros = Utils.getSystemNowMicrosUtc(); }
/** * Compares a time value with current time. Both time values are in micros since epoch. * Since we can not assume the time came from the same node, we use the concept of a * time epsilon: any two time values within epsilon are considered too close to * globally order in respect to each other and this method will return true. */ public static boolean isWithinTimeComparisonEpsilon(long timeMicros) { return Math.abs(timeMicros - Utils.getSystemNowMicrosUtc()) < timeComparisonEpsilon; }
private long calculatePKSTaskExpirationTime(RequestBrokerState state) { long t = (state.documentExpirationTimeMicros - Utils.getSystemNowMicrosUtc()) / 2; t = Utils.fromNowMicrosUtc(t) - TimeUnit.MINUTES.toMicros(5); return Math.max(t, Utils.fromNowMicrosUtc(TimeUnit.MINUTES.toMicros(5))); }
private String buildThreadTag() { if (this.host != null) { return UriUtils.extendUri(this.host.getUri(), "netty-client").toString(); } return getClass().getSimpleName() + ":" + Utils.getSystemNowMicrosUtc(); }
private void notifyCallbacks(String containerImageFilePath, byte[] imageData) { List<Consumer<byte[]>> pendingCallbacks; synchronized (RETRIEVE_LOCK) { cachedImages.put(containerImageFilePath, imageData); lastUsed = Utils.getSystemNowMicrosUtc(); pendingCallbacks = pendingCallbacksByImagePath.remove(containerImageFilePath); } host.log(Level.INFO, "Caching system agent image data for %s", containerImageFilePath); host.schedule(this::cleanCache, CACHED_DATA_MICROS, TimeUnit.MICROSECONDS); if (pendingCallbacks != null) { for (Consumer<byte[]> consumer : pendingCallbacks) { consumer.accept(imageData); } } }
private void cleanCache() { if (lastUsed + CACHED_DATA_MICROS < Utils.getSystemNowMicrosUtc()) { // expired, clean the reference cachedImages.clear(); lastUsed = 0; host.log(Level.INFO, "System image(s) removed from cache"); } else { // schedule next check host.schedule(this::cleanCache, CACHED_DATA_MICROS, TimeUnit.MICROSECONDS); } }
private void updateGossipPatchStat(long sendTimeMicros, NodeState remotePeer) { long patchCompletionTime = Utils.getSystemNowMicrosUtc(); String statName = remotePeer.id + STAT_NAME_PREFIX_GOSSIP_PATCH_DURATION; ServiceStat st = ServiceStatUtils.getOrCreateHourlyTimeSeriesHistogramStat(this, statName, EnumSet.of(AggregationType.AVG)); setStat(st, patchCompletionTime - sendTimeMicros); }
/** * Initiates periodic maintenance for a service. Called on service start or when maintenance is * dynamically toggled on */ protected void scheduleServiceMaintenance(Service s) { if (!s.hasOption(ServiceOption.PERIODIC_MAINTENANCE)) { return; } this.serviceMaintTracker.schedule(s, Utils.getSystemNowMicrosUtc()); }
public void handleMaintenance(Operation op) { long now = Utils.getSystemNowMicrosUtc(); if (this.isHttp2Only) { handleHttp2Maintenance(now); } else { handleHttp1Maintenance(now); } op.complete(); }
@Test public void getSystemNowMicrosUtc() { CommandLineArgumentParser.parseFromProperties(this); long s = System.nanoTime() / 1000; for (int i = 0; i < this.iterationCount; i++) { Utils.getSystemNowMicrosUtc(); } long e = System.nanoTime() / 1000; double thpt = this.iterationCount / ((e - s) / 1000000.0); Logger.getAnonymousLogger().info("Throughput: " + thpt); }
@Test public void getSystemNowMicrosUtc() { CommandLineArgumentParser.parseFromProperties(this); long s = System.nanoTime() / 1000; for (int i = 0; i < this.iterationCount; i++) { Utils.getSystemNowMicrosUtc(); } long e = System.nanoTime() / 1000; double thpt = this.iterationCount / ((e - s) / 1000000.0); Logger.getAnonymousLogger().info("Throughput: " + thpt); }
private void applyDocumentExpirationPolicy(long deadline) throws Exception { // TODO: need better solution to expire documents, this can be very slow to have // deletion in batches across tables int limit = expiredDocumentSearchThreshold; long now = Utils.getNowMicrosUtc(); for (TableDescription tableDescription : this.dao.getPostgresSchemaManager().getTableDescriptions()) { if (Utils.getSystemNowMicrosUtc() >= deadline || limit <= 0) { break; } int expired = applyDocumentExpirationPolicyForTable(tableDescription, now, deadline, limit); limit -= expired; } }
@Test public void isWithinTimeComparisonEpsilon() { Utils.setTimeComparisonEpsilonMicros(TimeUnit.SECONDS.toMicros(10)); // check a value within about a millisecond from now long l = Utils.getSystemNowMicrosUtc() + 1000; assertTrue(Utils.isWithinTimeComparisonEpsilon(l)); // check a value days from now l = Utils.getSystemNowMicrosUtc() + TimeUnit.DAYS.toMicros(2); assertFalse(Utils.isWithinTimeComparisonEpsilon(l)); }
@Test public void isWithinTimeComparisonEpsilon() { Utils.setTimeComparisonEpsilonMicros(TimeUnit.SECONDS.toMicros(10)); // check a value within about a millisecond from now long l = Utils.getSystemNowMicrosUtc() + 1000; assertTrue(Utils.isWithinTimeComparisonEpsilon(l)); // check a value days from now l = Utils.getSystemNowMicrosUtc() + TimeUnit.DAYS.toMicros(2); assertFalse(Utils.isWithinTimeComparisonEpsilon(l)); }
@Override public void handleStart(Operation post) { ServiceDocument initState = post.getBody(ServiceDocument.class); long interval = initState.documentExpirationTimeMicros - Utils.getSystemNowMicrosUtc(); if (interval < 0) { logWarning("Task expiration is in the past, extending it"); interval = getHost().getMaintenanceIntervalMicros() * 2; } super.toggleOption(ServiceOption.PERIODIC_MAINTENANCE, true); super.setMaintenanceIntervalMicros(interval); post.complete(); }
@Test public void testIsExpired() { assertFalse(ServiceUtils.isExpired(null)); ServiceDocument sd = new ServiceDocument(); sd.documentExpirationTimeMicros = 0; assertFalse(ServiceUtils.isExpired(new ServiceDocument())); sd.documentExpirationTimeMicros = Utils.getSystemNowMicrosUtc() - TimeUnit.MINUTES.toMicros(1); assertTrue(ServiceUtils.isExpired(sd)); sd.documentExpirationTimeMicros = Utils.getSystemNowMicrosUtc() + TimeUnit.MINUTES.toMicros(1); assertFalse(ServiceUtils.isExpired(sd)); } }
private State triggerMigrationTask(URI sourceHostUri, URI destHostUri) { State state = new State(); state.destinationFactoryLink = ExampleODLService.FACTORY_LINK; state.destinationNodeGroupReference = UriUtils.buildUri(destHostUri, ServiceUriPaths.DEFAULT_NODE_GROUP); state.sourceFactoryLink = ExampleODLService.FACTORY_LINK; state.sourceNodeGroupReference = UriUtils.buildUri(sourceHostUri, ServiceUriPaths.DEFAULT_NODE_GROUP); state.maintenanceIntervalMicros = TimeUnit.SECONDS.toMicros(1); state.querySpec = new QuerySpecification(); state.querySpec.resultLimit = this.resultLimit; // specify expiration time which transcends to query pages state.documentExpirationTimeMicros = Utils.getSystemNowMicrosUtc() + TimeUnit.SECONDS.toMicros(this.taskWaitSeconds); Operation op = Operation.createPost(UriUtils.buildUri(destHostUri, MigrationTaskService.FACTORY_LINK)).setBody(state); state = this.host.getTestRequestSender().sendAndWait(op, State.class); return state; }