protected <T> void metricChanged(BaseMetricImpl<T> metric) { long lastTime = metric.changeTime; // NOTE: changeTime is only accurate for metrics that are enabled, and // to guarantee that it always corresponds to the value, updates to // enabled metrics are synchronized. metric.changeTime = timebase + System.nanoTime(); metric.valueChanged = true; metricsDataChanged = true; int level; if ((lastTime == 0) || (metric.changeTime <= lastTime)) { level = 0; } else { // Longer duration -> higher level. double r = random.nextDouble(); if (r == 0) { level = NLEVELS - 1; } else { level = Math.min( NLEVELS - 1, (int) (Math.log((metric.changeTime - lastTime) / r) / METRIC_LEVEL_DIVISOR)); } } MetricLevel<T> metricLevel = metric.levels.get(level); MetricLevelValues values = metricLevel.values.peekLast(); if ((values == null) || values.isFull()) { if (logger.isTraceEnabled()) { logger.trace( "New level {} entry at {} for {}", new Object[] {level, metric.changeTime, metric}); } values = new MetricLevelValues(metric.changeTime, metric.encodeValue()); metricLevel.values.addLast(values); } else { if (logger.isTraceEnabled()) { logger.trace( "Adding to level {} entry at {} for {}", new Object[] {level, metric.changeTime, metric}); } values.append(metric.encodeValue(metricLevel)); } metricLevel.lastValue = metric.getObject(); metricLevel.lastTime = metric.changeTime; }
protected void writeMetrics() { boolean anyConfChanges = false; for (BaseMetricImpl<?> metric : metrics.values()) { if (!metric.confChanged && !metric.valueChanged) continue; synchronized (metric) { if (metric.confChanged) { anyConfChanges = true; pendingWrites.add( new KeyValue( confSubspace.pack( tupleFrom( metric.getType(), metric.getName(), address, DEFAULT_ID, ENABLED_OPTION)), metric.enabled ? ENABLED_TRUE : ENABLED_FALSE)); metric.confChanged = false; } if (metric.valueChanged) { pendingWrites.add( new KeyValue( dataSubspace.pack( tupleFrom(metric.getType(), metric.getName(), address, DEFAULT_ID)), metric.encodeValue())); metric.valueChanged = false; } for (int level = 0; level < NLEVELS; level++) { MetricLevel<?> metricLevel = metric.levels.get(level); while (true) { MetricLevelValues values = metricLevel.values.pollFirst(); if (values == null) break; pendingWrites.add( new KeyValue( dataSubspace.pack( tupleFrom( metric.getType(), metric.getName(), address, DEFAULT_ID, level, values.start)), values.bytes)); // Continue to fill (will overwrite key with longer value). if (metricLevel.values.isEmpty() && !values.isFull()) { metricLevel.values.addLast(values); break; } } } } } if (anyConfChanges) { // Signal a change in configuration. We will respond to this change, // too, but that seemd harmless and difficult to avoid in a general // way. byte[] bytes = new byte[16]; random.nextBytes(bytes); if (logger.isDebugEnabled()) { logger.debug("Writing {}: {}", METRIC_CONF_CHANGES_KEY, ByteArrayUtil.printable(bytes)); } pendingWrites.add(new KeyValue(confChangesSubspace.getKey(), bytes)); } if (!pendingWrites.isEmpty()) { getDatabase() .run( new Function<Transaction, Void>() { @Override public Void apply(Transaction tr) { tr.options().setAccessSystemKeys(); for (KeyValue kv : pendingWrites) { tr.set(kv.getKey(), kv.getValue()); } return null; } }); pendingWrites.clear(); } }