[120] | 1 | /** |
---|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
| 3 | * or more contributor license agreements. See the NOTICE file |
---|
| 4 | * distributed with this work for additional information |
---|
| 5 | * regarding copyright ownership. The ASF licenses this file |
---|
| 6 | * to you under the Apache License, Version 2.0 (the |
---|
| 7 | * "License"); you may not use this file except in compliance |
---|
| 8 | * with the License. You may obtain a copy of the License at |
---|
| 9 | * |
---|
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 11 | * |
---|
| 12 | * Unless required by applicable law or agreed to in writing, software |
---|
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 15 | * See the License for the specific language governing permissions and |
---|
| 16 | * limitations under the License. |
---|
| 17 | */ |
---|
| 18 | package org.apache.hadoop.ipc.metrics; |
---|
| 19 | |
---|
| 20 | import org.apache.commons.logging.Log; |
---|
| 21 | import org.apache.commons.logging.LogFactory; |
---|
| 22 | import org.apache.hadoop.ipc.Server; |
---|
| 23 | import org.apache.hadoop.metrics.MetricsContext; |
---|
| 24 | import org.apache.hadoop.metrics.MetricsRecord; |
---|
| 25 | import org.apache.hadoop.metrics.MetricsUtil; |
---|
| 26 | import org.apache.hadoop.metrics.Updater; |
---|
| 27 | import org.apache.hadoop.metrics.util.MetricsBase; |
---|
| 28 | import org.apache.hadoop.metrics.util.MetricsIntValue; |
---|
| 29 | import org.apache.hadoop.metrics.util.MetricsRegistry; |
---|
| 30 | import org.apache.hadoop.metrics.util.MetricsTimeVaryingRate; |
---|
| 31 | |
---|
| 32 | /** |
---|
| 33 | * |
---|
| 34 | * This class is for maintaining the various RPC statistics |
---|
| 35 | * and publishing them through the metrics interfaces. |
---|
| 36 | * This also registers the JMX MBean for RPC. |
---|
| 37 | * <p> |
---|
| 38 | * This class has a number of metrics variables that are publicly accessible; |
---|
| 39 | * these variables (objects) have methods to update their values; |
---|
| 40 | * for example: |
---|
| 41 | * <p> {@link #rpcQueueTime}.inc(time) |
---|
| 42 | * |
---|
| 43 | */ |
---|
| 44 | public class RpcMetrics implements Updater { |
---|
| 45 | public MetricsRegistry registry = new MetricsRegistry(); |
---|
| 46 | private MetricsRecord metricsRecord; |
---|
| 47 | private Server myServer; |
---|
| 48 | private static Log LOG = LogFactory.getLog(RpcMetrics.class); |
---|
| 49 | RpcActivityMBean rpcMBean; |
---|
| 50 | |
---|
| 51 | public RpcMetrics(String hostName, String port, Server server) { |
---|
| 52 | myServer = server; |
---|
| 53 | MetricsContext context = MetricsUtil.getContext("rpc"); |
---|
| 54 | metricsRecord = MetricsUtil.createRecord(context, "metrics"); |
---|
| 55 | |
---|
| 56 | metricsRecord.setTag("port", port); |
---|
| 57 | |
---|
| 58 | LOG.info("Initializing RPC Metrics with hostName=" |
---|
| 59 | + hostName + ", port=" + port); |
---|
| 60 | |
---|
| 61 | context.registerUpdater(this); |
---|
| 62 | |
---|
| 63 | // Need to clean up the interface to RpcMgt - don't need both metrics and server params |
---|
| 64 | rpcMBean = new RpcActivityMBean(registry, hostName, port); |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | |
---|
| 68 | /** |
---|
| 69 | * The metrics variables are public: |
---|
| 70 | * - they can be set directly by calling their set/inc methods |
---|
| 71 | * -they can also be read directly - e.g. JMX does this. |
---|
| 72 | */ |
---|
| 73 | |
---|
| 74 | public MetricsTimeVaryingRate rpcQueueTime = |
---|
| 75 | new MetricsTimeVaryingRate("RpcQueueTime", registry); |
---|
| 76 | public MetricsTimeVaryingRate rpcProcessingTime = |
---|
| 77 | new MetricsTimeVaryingRate("RpcProcessingTime", registry); |
---|
| 78 | public MetricsIntValue numOpenConnections = |
---|
| 79 | new MetricsIntValue("NumOpenConnections", registry); |
---|
| 80 | public MetricsIntValue callQueueLen = |
---|
| 81 | new MetricsIntValue("callQueueLen", registry); |
---|
| 82 | |
---|
| 83 | /** |
---|
| 84 | * Push the metrics to the monitoring subsystem on doUpdate() call. |
---|
| 85 | */ |
---|
| 86 | public void doUpdates(MetricsContext context) { |
---|
| 87 | |
---|
| 88 | synchronized (this) { |
---|
| 89 | // ToFix - fix server to use the following two metrics directly so |
---|
| 90 | // the metrics do not have be copied here. |
---|
| 91 | numOpenConnections.set(myServer.getNumOpenConnections()); |
---|
| 92 | callQueueLen.set(myServer.getCallQueueLen()); |
---|
| 93 | for (MetricsBase m : registry.getMetricsList()) { |
---|
| 94 | m.pushMetric(metricsRecord); |
---|
| 95 | } |
---|
| 96 | } |
---|
| 97 | metricsRecord.update(); |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | public void shutdown() { |
---|
| 101 | if (rpcMBean != null) |
---|
| 102 | rpcMBean.shutdown(); |
---|
| 103 | } |
---|
| 104 | } |
---|