ndimiduk commented on a change in pull request #754: HBASE-22978 : Online slow response log URL: https://github.com/apache/hbase/pull/754#discussion_r377264208
########## File path: hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/slowlog/TestOnlineSlowLogProvider.java ########## @@ -0,0 +1,307 @@ +/* + * + * 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.regionserver.slowlog; + +import java.util.List; +import java.util.concurrent.TimeUnit; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.HBaseClassTestRule; +import org.apache.hadoop.hbase.HBaseTestingUtility; +import org.apache.hadoop.hbase.HConstants; +import org.apache.hadoop.hbase.testclassification.MasterTests; +import org.apache.hadoop.hbase.testclassification.MediumTests; +import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; +import org.junit.Assert; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.hbase.thirdparty.com.google.common.util.concurrent.Uninterruptibles; + +import org.apache.hadoop.hbase.shaded.protobuf.generated.TooSlowLog.SlowLogPayload; + +/** + * Tests for Online SlowLog Provider Service + */ +@Category({MasterTests.class, MediumTests.class}) +public class TestOnlineSlowLogProvider { + + @ClassRule + public static final HBaseClassTestRule CLASS_RULE = + HBaseClassTestRule.forClass(TestOnlineSlowLogProvider.class); + + private static final Logger LOG = LoggerFactory.getLogger(TestOnlineSlowLogProvider.class); + + private static final HBaseTestingUtility HBASE_TESTING_UTILITY = new HBaseTestingUtility(); + + private OnlineSlowLogProvider onlineSlowLogProvider; + + private Configuration getOnlineSlowLogConf(int eventSize) { + Configuration conf = HBASE_TESTING_UTILITY.getConfiguration(); + conf.setBoolean(HConstants.SLOW_LOG_BUFFER_ENABLED_KEY, true); + conf.setInt(HConstants.SLOW_LOG_RING_BUFFER_SIZE, eventSize); + return conf; + } + + private SlowLogPayload getSlowLogPayload(int i) { + SlowLogPayload slowLogPayload = SlowLogPayload.newBuilder() + .setCallDetails("call_" + i) + .setClientAddress("client_" + i) + .setMethodName("method_" + i) + .setUserName("userName_" + i) + .setMultiGets(i) + .setMultiMutations(i) + .setMultiServiceCalls(i) + .setProcessingTime(i + 500) + .setQueueTime(i + 400) + .setResponseSize(i + 200) + .setStartTime(EnvironmentEdgeManager.currentTime()) + .setServerClass("class_" + i) + .setParam("param_" + i) + .build(); + return slowLogPayload; + } + + /** + * confirm that for a ringbuffer of slow logs, payload on given index of buffer + * has expected elements + * + * @param i index of ringbuffer logs + * @param j data value that was put on index i + * @param slowLogPayloads list of payload retrieved from {@link OnlineSlowLogProvider} + */ + private void confirmPayloadParams(int i, int j, List<SlowLogPayload> slowLogPayloads) { + Assert.assertEquals(slowLogPayloads.get(i).getClientAddress(), "client_" + j); + Assert.assertEquals(slowLogPayloads.get(i).getCallDetails(), "call_" + j); + Assert.assertEquals(slowLogPayloads.get(i).getMethodName(), "method_" + j); + Assert.assertEquals(slowLogPayloads.get(i).getUserName(), "userName_" + j); + Assert.assertEquals(slowLogPayloads.get(i).getProcessingTime(), j + 500); + Assert.assertEquals(slowLogPayloads.get(i).getQueueTime(), j + 400); + Assert.assertEquals(slowLogPayloads.get(i).getResponseSize(), j + 200); + Assert.assertEquals(slowLogPayloads.get(i).getServerClass(), "class_" + j); + Assert.assertEquals(slowLogPayloads.get(i).getParam(), "param_" + j); + } + + @Test + public void testOnlieSlowLogConsumption() throws Exception { + Configuration conf = getOnlineSlowLogConf(8); + onlineSlowLogProvider = new OnlineSlowLogProvider(conf); + Assert.assertEquals(onlineSlowLogProvider.getSlowLogPayloads().size(), 0); + LOG.debug("Initially ringbuffer of Slow Log records is empty"); + + int i = 0; + + // add 5 records initially + for (; i < 5; i++) { + SlowLogPayload slowLogPayload = getSlowLogPayload(i + 1); + onlineSlowLogProvider.addSlowLogPayload(slowLogPayload); + } + + Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS); Review comment: Instead of a fixed sleep, how about using our `waitFor` test helper method, waiting on the condition of `slowLogPayloads.size() == 5`. This will make for a more robust test. ---------------------------------------------------------------- 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