/** * Flush all active profiles. * * <p>A profile will remain active as long as it continues to receive messages. If a profile * does not receive a message for an extended duration, it may be marked as expired. * * <p>Flushes all active {@link ProfileBuilder} objects that this distributor is responsible for. * * @return The {@link ProfileMeasurement} values; one for each (profile, entity) pair. */ @Override public List<ProfileMeasurement> flush() { LOG.debug("About to flush active profiles"); // cache maintenance needed here to ensure active profiles will expire cacheMaintenance(); List<ProfileMeasurement> measurements = flushCache(activeCache); return measurements; }
public DefaultMessageDistributor withPeriodDuration(int duration, TimeUnit units) { return withPeriodDurationMillis(units.toMillis(duration)); }
/** * Create a new Profiler. * * @param config The Profiler configuration. * @param periodDurationMillis The period duration in milliseconds. * @param profileTimeToLiveMillis The time-to-live of a profile in milliseconds. * @param maxNumberOfRoutes The max number of unique routes to maintain. After this is exceeded, lesser * used routes will be evicted from the internal cache. * @param context The Stellar execution context. */ public StandAloneProfiler(ProfilerConfig config, long periodDurationMillis, long profileTimeToLiveMillis, long maxNumberOfRoutes, Context context) { this.context = context; this.config = config; this.router = new DefaultMessageRouter(context); this.distributor = new DefaultMessageDistributor(periodDurationMillis, profileTimeToLiveMillis, maxNumberOfRoutes); this.clockFactory = new DefaultClockFactory(); this.messageCount = 0; this.routeCount = 0; }
/** * Distribute a message along a MessageRoute. * * @param route The message route. * @param context The Stellar execution context. */ @Override public void distribute(MessageRoute route, Context context) { try { ProfileBuilder builder = getBuilder(route, context); builder.apply(route.getMessage(), route.getTimestamp()); } catch(ExecutionException e) { LOG.error("Unexpected error", e); throw new RuntimeException(e); } }
/** * Retrieves the cached ProfileBuilder that is used to build and maintain the Profile. If none exists, * one will be created and returned. * * @param route The message route. * @param context The Stellar execution context. */ public ProfileBuilder getBuilder(MessageRoute route, Context context) throws ExecutionException { ProfileConfig profile = route.getProfileDefinition(); String entity = route.getEntity(); Function<Integer, ProfileBuilder> profileCreator = (k) -> new DefaultProfileBuilder.Builder() .withDefinition(profile) .withEntity(entity) .withPeriodDurationMillis(periodDurationMillis) .withContext(context) .build(); return activeCache.get(cacheKey(profile, entity), profileCreator); }
this.messageDistributor = new DefaultMessageDistributor(periodDurationMillis, profileTimeToLiveMillis, maxNumberOfRoutes); this.configurations = new ProfilerConfigurations(); this.activeFlushSignal = new FixedFrequencyFlushSignal(periodDurationMillis);
MessageDistributor distributor = new DefaultMessageDistributor(periodDurationMillis, profileTTLMillis, maxRoutes); Context context = TaskUtils.getContext(globals);
/** * Flush all expired profiles. * * <p>Flushes all expired {@link ProfileBuilder}s that this distributor is responsible for. * * <p>If a profile has not received messages for an extended period of time, it will be marked as * expired. When a profile is expired, it can no longer receive new messages. Expired profiles * remain only to give the client a chance to flush them. * * <p>If the client does not flush the expired profiles periodically, any state maintained in the * profile since the last flush may be lost. * * @return The {@link ProfileMeasurement} values; one for each (profile, entity) pair. */ @Override public List<ProfileMeasurement> flushExpired() { LOG.debug("About to flush expired profiles"); // cache maintenance needed here to ensure active profiles will expire cacheMaintenance(); // flush all expired profiles List<ProfileMeasurement> measurements = flushCache(expiredCache); // once the expired profiles have been flushed, they are no longer needed expiredCache.invalidateAll(); return measurements; }