/** * Flush all of the profiles maintained in a cache. * * @param cache The cache to flush. * @return The measurements captured when flushing the profiles. */ private List<ProfileMeasurement> flushCache(Cache<Integer, ProfileBuilder> cache) { List<ProfileMeasurement> measurements = new ArrayList<>(); for(ProfileBuilder profileBuilder: cache.asMap().values()) { // only need to flush, if the profile has been initialized if(profileBuilder.isInitialized()) { // flush the profiler and save the measurement, if one exists Optional<ProfileMeasurement> measurement = profileBuilder.flush(); measurement.ifPresent(m -> measurements.add(m)); } } return measurements; }
@Override public void delete(@Nonnull Integer key, @Nullable ProfileBuilder value, @Nonnull RemovalCause cause) { if(cause.wasEvicted()) { // the expired profile was NOT flushed in time LOG.warn("Expired profile NOT flushed before removal, some state lost; profile={}, entity={}, cause={}", value.getDefinition().getProfile(), value.getEntity(), cause); } else { // the expired profile was flushed successfully LOG.debug("Expired profile successfully flushed; profile={}, entity={}, cause={}", value.getDefinition().getProfile(), value.getEntity(), cause); } } }
/** * 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); } }
@Override public void delete(@Nonnull Integer key, @Nullable ProfileBuilder value, @Nonnull RemovalCause cause) { if(cause.wasEvicted()) { // add the profile to the expired cache expiredCache.put(key, value); LOG.debug("Profile expired from active cache due to inactivity; profile={}, entity={}, cause={}", value.getDefinition().getProfile(), value.getEntity(), cause); } else { LOG.error("Profile removed from cache unexpectedly. File a bug report; profile={}, entity={}, cause={}", value.getDefinition().getProfile(), value.getEntity(), cause); } } }