jiajunwang commented on a change in pull request #490: Add latency metric components for WAGED rebalancer URL: https://github.com/apache/helix/pull/490#discussion_r329780626
########## File path: helix-core/src/main/java/org/apache/helix/controller/rebalancer/waged/metrics/MetricCollector.java ########## @@ -0,0 +1,97 @@ +package org.apache.helix.controller.rebalancer.waged.metrics; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; +import javax.management.JMException; +import javax.management.ObjectName; +import org.apache.helix.HelixException; +import org.apache.helix.controller.rebalancer.waged.metrics.model.Metric; +import org.apache.helix.monitoring.mbeans.MonitorDomainNames; +import org.apache.helix.monitoring.mbeans.dynamicMBeans.DynamicMBeanProvider; +import org.apache.helix.monitoring.mbeans.dynamicMBeans.DynamicMetric; + +/** + * Collects and manages all metrics that implement the {@link Metric} interface. + */ +public class MetricCollector extends DynamicMBeanProvider { + private boolean _registered; + private String _clusterName; + private String _entityName; + private Map<String, Metric> _metricMap; + + public MetricCollector(String clusterName, String entityName) { + _clusterName = clusterName; + _entityName = entityName; + _metricMap = new HashMap<>(); + } + + @Override + public DynamicMBeanProvider register() throws JMException { + // First cast all Metric objects to DynamicMetrics + Collection<DynamicMetric<?, ?>> dynamicMetrics = new HashSet<>(); + _metricMap.values().forEach(metric -> dynamicMetrics.add(metric.getDynamicMetric())); + + // TODO: Create helper methods for mbeanName and ObjectName + String mbeanName = String.format("%s=%s, %s=%s", "MetricCollector", _clusterName, + "MetricCollector", _entityName); + doRegister(dynamicMetrics, new ObjectName( + String.format("%s:%s", MonitorDomainNames.WagedRebalancer.name(), mbeanName))); + _registered = true; + return this; + } + + @Override + public String getSensorName() { + return String.format("%s.%s.%s", "MetricCollector", _clusterName, _entityName); + } + + public boolean isRegistered() { + return _registered; + } + + public void addMetric(Metric metric) { Review comment: After register, addMetric() call will not impact the real metric list, right? Do we need dynamically adding metrics? If so, you need to call register every single time. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@helix.apache.org For additional commands, e-mail: reviews-h...@helix.apache.org