mmpataki commented on a change in pull request #1814: URL: https://github.com/apache/hbase/pull/1814#discussion_r444276936
########## File path: hbase-http/src/main/java/org/apache/hadoop/hbase/http/prometheus/PrometheusHadoop2Servlet.java ########## @@ -0,0 +1,115 @@ +/* + * 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. + */ + +package org.apache.hadoop.hbase.http.prometheus; + +import java.io.IOException; +import java.io.Writer; +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReference; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.apache.hadoop.metrics2.AbstractMetric; +import org.apache.hadoop.metrics2.MetricType; +import org.apache.hadoop.metrics2.MetricsRecord; +import org.apache.hadoop.metrics2.MetricsTag; +import org.apache.hadoop.metrics2.impl.MetricsExportHelper; +import org.apache.yetus.audience.InterfaceAudience; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting; + +@InterfaceAudience.Private +public class PrometheusHadoop2Servlet extends HttpServlet { + + private Logger LOG = LoggerFactory.getLogger(PrometheusHadoop2Servlet.class); + + /** + * These 2 structures will make sure that there is only one thread which + * reads the metricRecords and produces a string which can be directly consumed + * to write on socket / any writer. + */ + private final AtomicInteger amIProducer = new AtomicInteger(0); + private final Object waitingRoom = new Object(); + + /* reference to generated metrics buffer */ + private final AtomicReference<String> pMetricsBuffer = new AtomicReference<>(); + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) + throws IOException { + writeMetrics(resp.getWriter()); + } + + @VisibleForTesting + void writeMetrics(Writer writer) throws IOException { + if (amIProducer.compareAndSet(0, 1)) { + try { + renderMetrics(MetricsExportHelper.export()); + } finally { + amIProducer.set(0); + synchronized (waitingRoom) { + waitingRoom.notifyAll(); + } + } + } else { + synchronized (waitingRoom) { + try { + waitingRoom.wait(5000L); Review comment: It's not worth waiting for more than X seconds to get the metrics. (X should be 1s) if we can't produce in 1s we should/can return the metrics which were produced by earlier producer. (1s old metrics are acceptable?) It's guaranteed that the old metrics will be there when we come out of this wait. Once the thread comes out of wait, it tries to get back the lock which is unnecessary is there way to avoid it? ---------------------------------------------------------------- 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