2323import java .util .List ;
2424import java .util .Map ;
2525import java .util .Set ;
26+ import java .util .concurrent .ConcurrentHashMap ;
2627import javax .management .JMException ;
2728import javax .management .ObjectName ;
2829
@@ -41,17 +42,17 @@ public class InstanceMonitor extends DynamicMBeanProvider {
4142 /**
4243 * Metric names for instance capacity.
4344 */
44- public enum InstanceMonitorMetrics {
45+ public enum InstanceMonitorMetric {
4546 // TODO: change the metric names with Counter and Gauge suffix and deprecate old names.
4647 TOTAL_MESSAGE_RECEIVED_COUNTER ("TotalMessageReceived" ),
4748 ENABLED_STATUS_GAUGE ("Enabled" ),
4849 ONLINE_STATUS_GAUGE ("Online" ),
4950 DISABLED_PARTITIONS_GAUGE ("DisabledPartitions" ),
5051 MAX_CAPACITY_USAGE_GAUGE ("MaxCapacityUsageGauge" );
5152
52- private String metricName ;
53+ private final String metricName ;
5354
54- InstanceMonitorMetrics (String name ) {
55+ InstanceMonitorMetric (String name ) {
5556 metricName = name ;
5657 }
5758
@@ -75,6 +76,9 @@ public String metricName() {
7576 private SimpleDynamicMetric <Long > _onlineStatusGauge ;
7677 private SimpleDynamicMetric <Double > _maxCapacityUsageGauge ;
7778
79+ // A map of dynamic capacity Gauges. The map's keys could change.
80+ private final Map <String , SimpleDynamicMetric <Long >> _dynamicCapacityMetricsMap ;
81+
7882 /**
7983 * Initialize the bean
8084 * @param clusterName the cluster to monitor
@@ -85,26 +89,41 @@ public InstanceMonitor(String clusterName, String participantName, ObjectName ob
8589 _participantName = participantName ;
8690 _tags = ImmutableList .of (ClusterStatusMonitor .DEFAULT_TAG );
8791 _initObjectName = objectName ;
92+ _dynamicCapacityMetricsMap = new ConcurrentHashMap <>();
8893
8994 createMetrics ();
9095 }
9196
9297 private void createMetrics () {
9398 _totalMessagedReceivedCounter = new SimpleDynamicMetric <>(
94- InstanceMonitorMetrics .TOTAL_MESSAGE_RECEIVED_COUNTER .metricName (), 0L );
99+ InstanceMonitorMetric .TOTAL_MESSAGE_RECEIVED_COUNTER .metricName (), 0L );
95100
96101 _disabledPartitionsGauge =
97- new SimpleDynamicMetric <>(InstanceMonitorMetrics .DISABLED_PARTITIONS_GAUGE .metricName (),
102+ new SimpleDynamicMetric <>(InstanceMonitorMetric .DISABLED_PARTITIONS_GAUGE .metricName (),
98103 0L );
99104 _enabledStatusGauge =
100- new SimpleDynamicMetric <>(InstanceMonitorMetrics .ENABLED_STATUS_GAUGE .metricName (), 0L );
105+ new SimpleDynamicMetric <>(InstanceMonitorMetric .ENABLED_STATUS_GAUGE .metricName (), 0L );
101106 _onlineStatusGauge =
102- new SimpleDynamicMetric <>(InstanceMonitorMetrics .ONLINE_STATUS_GAUGE .metricName (), 0L );
107+ new SimpleDynamicMetric <>(InstanceMonitorMetric .ONLINE_STATUS_GAUGE .metricName (), 0L );
103108 _maxCapacityUsageGauge =
104- new SimpleDynamicMetric <>(InstanceMonitorMetrics .MAX_CAPACITY_USAGE_GAUGE .metricName (),
109+ new SimpleDynamicMetric <>(InstanceMonitorMetric .MAX_CAPACITY_USAGE_GAUGE .metricName (),
105110 0.0d );
106111 }
107112
113+ private List <DynamicMetric <?, ?>> buildAttributeList () {
114+ List <DynamicMetric <?, ?>> attributeList = Lists .newArrayList (
115+ _totalMessagedReceivedCounter ,
116+ _disabledPartitionsGauge ,
117+ _enabledStatusGauge ,
118+ _onlineStatusGauge ,
119+ _maxCapacityUsageGauge
120+ );
121+
122+ attributeList .addAll (_dynamicCapacityMetricsMap .values ());
123+
124+ return attributeList ;
125+ }
126+
108127 @ Override
109128 public String getSensorName () {
110129 return String .format ("%s.%s.%s.%s" , ClusterStatusMonitor .PARTICIPANT_STATUS_KEY , _clusterName ,
@@ -183,33 +202,58 @@ public synchronized void increaseMessageCount(long messageReceived) {
183202 }
184203
185204 /**
186- * Update max capacity usage for this instance.
205+ * Updates max capacity usage for this instance.
187206 * @param maxUsage max capacity usage of this instance
188207 */
189208 public synchronized void updateMaxCapacityUsage (double maxUsage ) {
190209 _maxCapacityUsageGauge .updateValue (maxUsage );
191210 }
192211
193212 /**
194- * Get max capacity usage of this instance.
213+ * Gets max capacity usage of this instance.
195214 * @return Max capacity usage of this instance.
196215 */
197216 protected synchronized double getMaxCapacityUsageGauge () {
198217 return _maxCapacityUsageGauge .getValue ();
199218 }
200219
201- @ Override
202- public DynamicMBeanProvider register ()
203- throws JMException {
204- List <DynamicMetric <?, ?>> attributeList = ImmutableList .of (
205- _totalMessagedReceivedCounter ,
206- _disabledPartitionsGauge ,
207- _enabledStatusGauge ,
208- _onlineStatusGauge ,
209- _maxCapacityUsageGauge
210- );
220+ /**
221+ * Updates instance capacity metrics.
222+ * @param capacity A map of instance capacity.
223+ */
224+ public void updateCapacity (Map <String , Integer > capacity ) {
225+ synchronized (_dynamicCapacityMetricsMap ) {
226+ // If capacity keys don't have any change, we just update the metric values.
227+ if (_dynamicCapacityMetricsMap .keySet ().equals (capacity .keySet ())) {
228+ for (Map .Entry <String , Integer > entry : capacity .entrySet ()) {
229+ _dynamicCapacityMetricsMap .get (entry .getKey ()).updateValue ((long ) entry .getValue ());
230+ }
231+ return ;
232+ }
211233
212- doRegister (attributeList , _initObjectName );
234+ // If capacity keys have any changes, we need to retain the capacity metrics.
235+ // Make sure capacity metrics map has the same capacity keys.
236+ // And update metrics values.
237+ _dynamicCapacityMetricsMap .keySet ().retainAll (capacity .keySet ());
238+ for (Map .Entry <String , Integer > entry : capacity .entrySet ()) {
239+ String capacityName = entry .getKey ();
240+ if (_dynamicCapacityMetricsMap .containsKey (capacityName )) {
241+ _dynamicCapacityMetricsMap .get (capacityName ).updateValue ((long ) entry .getValue ());
242+ } else {
243+ _dynamicCapacityMetricsMap .put (capacityName ,
244+ new SimpleDynamicMetric <>(capacityName + "Gauge" , (long ) entry .getValue ()));
245+ }
246+ }
247+ }
248+
249+ // Update MBean's all attributes.
250+ updateAttributesInfo (buildAttributeList (),
251+ "Instance monitor for instance: " + getInstanceName ());
252+ }
253+
254+ @ Override
255+ public DynamicMBeanProvider register () throws JMException {
256+ doRegister (buildAttributeList (), _initObjectName );
213257
214258 return this ;
215259 }
0 commit comments