com.gemstone.gemfire.internal.statistics.ValueMonitor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gemfire-core Show documentation
Show all versions of gemfire-core Show documentation
SnappyData store based off Pivotal GemFireXD
The newest version!
/*
* Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You
* may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License. See accompanying
* LICENSE file.
*/
package com.gemstone.gemfire.internal.statistics;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.gemstone.gemfire.StatisticDescriptor;
import com.gemstone.gemfire.Statistics;
import com.gemstone.gemfire.internal.CopyOnWriteHashSet;
import com.gemstone.gemfire.internal.StatisticDescriptorImpl;
/**
* Adds coarser-grained monitoring of entire Statistics instances.
*
* The following code is an example of how to find an existing Statistics
* instance and define a ValueMonitor listener for notifications of any stat
* sample containing updates to any stats in the instance being monitored:
*
StatisticsFactory factory = InternalDistributedSystem.getAnyInstance();
Statistics[] statistics = f.findStatisticsByTextId("statSampler");
if (statistics.length == 1) {
ValueMonitor monitor = new ValueMonitor().addStatistics(statistics[0]);
monitor.addListener(new StatisticsListener() {
public void handleNotification(StatisticsNotification notify) {
System.out.println("One or more statSampler stats changed at "
+ notify.getTimeStamp());
for (StatisticId statId : notify) {
System.out.println("\t"
+ statId.getStatisticDescriptor().getName()
+ " = " + notify.getValue(statId));
}
}
};
}
*
* @author Kirk Lund
* @since 7.0
* @see com.gemstone.gemfire.Statistics
*/
public final class ValueMonitor extends StatisticsMonitor {
public enum Type {
CHANGE, MATCH, DIFFER
}
private final CopyOnWriteHashSet statistics = new CopyOnWriteHashSet();
public ValueMonitor() {
super();
}
@Override
public ValueMonitor addStatistic(StatisticId statId) {
super.addStatistic(statId);
return this;
}
@Override
public ValueMonitor removeStatistic(StatisticId statId) {
super.removeStatistic(statId);
return this;
}
public ValueMonitor addStatistics(Statistics statistics) {
if (statistics == null) {
throw new NullPointerException("Statistics is null");
}
this.statistics.add(statistics);
return this;
}
public ValueMonitor removeStatistics(Statistics statistics) {
if (statistics == null) {
throw new NullPointerException("Statistics is null");
}
this.statistics.remove(statistics);
return this;
}
protected void monitor(long millisTimeStamp, List resourceInstances) {
super.monitor(millisTimeStamp, resourceInstances);
monitorStatistics(millisTimeStamp, resourceInstances);
}
protected void monitorStatistics(long millisTimeStamp, List resourceInstances) {
if (!this.statistics.isEmpty()) {
Map stats = new HashMap();
for (ResourceInstance resource : resourceInstances) {
if (this.statistics.contains(resource.getStatistics())) {
ResourceType resourceType = resource.getResourceType();
StatisticDescriptor[] sds = resourceType.getStatisticDescriptors();
int[] updatedStats = resource.getUpdatedStats();
for (int i = 0; i < updatedStats.length; i++) {
int idx = updatedStats[i];
StatisticDescriptorImpl sdi = (StatisticDescriptorImpl)sds[idx];
SimpleStatisticId statId = new SimpleStatisticId(sdi, resource.getStatistics());
long rawbits = resource.getLatestStatValues()[idx];
stats.put(statId, sdi.getNumberForRawBits(rawbits));
}
}
}
if (!stats.isEmpty()) {
MapBasedStatisticsNotification notification = new MapBasedStatisticsNotification(
millisTimeStamp, StatisticsNotification.Type.VALUE_CHANGED, stats);
notifyListeners(notification);
}
}
}
@Override
protected StringBuilder appendToString() {
final StringBuilder sb = new StringBuilder();
sb.append("statistics=").append(this.statistics);
return sb;
}
}