ndimiduk commented on a change in pull request #754: HBASE-22978 : Online slow response log URL: https://github.com/apache/hbase/pull/754#discussion_r377268619
########## 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); + + List<SlowLogPayload> slowLogPayloads = onlineSlowLogProvider.getSlowLogPayloads(); + Assert.assertEquals(slowLogPayloads.size(), 5); + for (int j = 0; j < 5; j++) { + confirmPayloadParams(j, j + 1, slowLogPayloads); + } + + // add 2 more records + for (; i < 7; i++) { + SlowLogPayload slowLogPayload = getSlowLogPayload(i + 1); + onlineSlowLogProvider.addSlowLogPayload(slowLogPayload); + } + + Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS); + + slowLogPayloads = onlineSlowLogProvider.getSlowLogPayloads(); + + // confirm ringbuffer has only one payload left + Assert.assertEquals(slowLogPayloads.size(), 7); + confirmPayloadParams(5, 6, slowLogPayloads); + confirmPayloadParams(6, 7, slowLogPayloads); + + // add 3 more records + for (; i < 10; i++) { + SlowLogPayload slowLogPayload = getSlowLogPayload(i + 1); + onlineSlowLogProvider.addSlowLogPayload(slowLogPayload); + } + + Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS); + + slowLogPayloads = onlineSlowLogProvider.getSlowLogPayloads(); + // confirm ringbuffer is full + Assert.assertEquals(slowLogPayloads.size(), 8); + confirmPayloadParams(7, 8, slowLogPayloads); + confirmPayloadParams(0, 9, slowLogPayloads); + confirmPayloadParams(1, 10, slowLogPayloads); + + // add 4 more records + for (; i < 14; i++) { + SlowLogPayload slowLogPayload = getSlowLogPayload(i + 1); + onlineSlowLogProvider.addSlowLogPayload(slowLogPayload); + } + + Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS); + + slowLogPayloads = onlineSlowLogProvider.getSlowLogPayloads(); + // confirm ringbuffer is full + Assert.assertEquals(slowLogPayloads.size(), 8); + confirmPayloadParams(2, 11, slowLogPayloads); + confirmPayloadParams(3, 12, slowLogPayloads); + confirmPayloadParams(4, 13, slowLogPayloads); + + boolean isRingBufferCleaned = onlineSlowLogProvider.clearSlowLogPayloads(); + Assert.assertTrue(isRingBufferCleaned); + LOG.debug("cleared the ringbuffer of Online Slow Log records"); + slowLogPayloads = onlineSlowLogProvider.getSlowLogPayloads(); + // confirm ringbuffer is empty + Assert.assertEquals(slowLogPayloads.size(), 0); + } + + @Test + public void testOnlineSlowLogWithHighRecords() throws Exception { + Configuration conf = getOnlineSlowLogConf(14); + onlineSlowLogProvider = new OnlineSlowLogProvider(conf); + Assert.assertEquals(onlineSlowLogProvider.getSlowLogPayloads().size(), 0); + LOG.debug("Initially ringbuffer of Slow Log records is empty"); + + for (int i = 0; i < 4000; i++) { + SlowLogPayload slowLogPayload = getSlowLogPayload(i + 1); + onlineSlowLogProvider.addSlowLogPayload(slowLogPayload); + } + LOG.debug("Added 4000 records, ringbuffer should only provide latest 14 records"); + + Uninterruptibles.sleepUninterruptibly(2, TimeUnit.SECONDS); + + List<SlowLogPayload> slowLogPayloads = onlineSlowLogProvider.getSlowLogPayloads(); + Assert.assertEquals(slowLogPayloads.size(), 14); + + // confirm strict order of slow log payloads + confirmPayloadParams(0, 3991, slowLogPayloads); + confirmPayloadParams(1, 3992, slowLogPayloads); + confirmPayloadParams(2, 3993, slowLogPayloads); + confirmPayloadParams(3, 3994, slowLogPayloads); + confirmPayloadParams(4, 3995, slowLogPayloads); + confirmPayloadParams(5, 3996, slowLogPayloads); + confirmPayloadParams(6, 3997, slowLogPayloads); + confirmPayloadParams(7, 3998, slowLogPayloads); + confirmPayloadParams(8, 3999, slowLogPayloads); + confirmPayloadParams(9, 4000, slowLogPayloads); + confirmPayloadParams(10, 3987, slowLogPayloads); + confirmPayloadParams(11, 3988, slowLogPayloads); + confirmPayloadParams(12, 3989, slowLogPayloads); + confirmPayloadParams(13, 3990, slowLogPayloads); + + boolean isRingBufferCleaned = onlineSlowLogProvider.clearSlowLogPayloads(); + Assert.assertTrue(isRingBufferCleaned); + LOG.debug("cleared the ringbuffer of Online Slow Log records"); + slowLogPayloads = onlineSlowLogProvider.getSlowLogPayloads(); + // confirm ringbuffer is empty + Assert.assertEquals(slowLogPayloads.size(), 0); + } + + @Test + public void testOnlineSlowLogWithDefaultDisableConfig() throws Exception { + Configuration conf = HBASE_TESTING_UTILITY.getConfiguration(); + conf.unset(HConstants.SLOW_LOG_BUFFER_ENABLED_KEY); + onlineSlowLogProvider = new OnlineSlowLogProvider(conf); + Assert.assertEquals(onlineSlowLogProvider.getSlowLogPayloads().size(), 0); + LOG.debug("Initially ringbuffer of Slow Log records is empty"); + + for (int i = 0; i < 300; i++) { + SlowLogPayload slowLogPayload = getSlowLogPayload(i + 1); + onlineSlowLogProvider.addSlowLogPayload(slowLogPayload); + } + + Uninterruptibles.sleepUninterruptibly(2, TimeUnit.SECONDS); + + List<SlowLogPayload> slowLogPayloads = onlineSlowLogProvider.getSlowLogPayloads(); + Assert.assertEquals(slowLogPayloads.size(), 0); + } + + @Test + public void testOnlineSlowLogWithDisableConfig() throws Exception { + Configuration conf = HBASE_TESTING_UTILITY.getConfiguration(); + conf.setBoolean(HConstants.SLOW_LOG_BUFFER_ENABLED_KEY, false); + onlineSlowLogProvider = new OnlineSlowLogProvider(conf); + Assert.assertEquals(onlineSlowLogProvider.getSlowLogPayloads().size(), 0); + LOG.debug("Initially ringbuffer of Slow Log records is empty"); + + for (int i = 0; i < 300; i++) { + SlowLogPayload slowLogPayload = getSlowLogPayload(i + 1); + onlineSlowLogProvider.addSlowLogPayload(slowLogPayload); + } + + Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS); + + List<SlowLogPayload> slowLogPayloads = onlineSlowLogProvider.getSlowLogPayloads(); + Assert.assertEquals(slowLogPayloads.size(), 0); + conf.setBoolean(HConstants.SLOW_LOG_BUFFER_ENABLED_KEY, true); + } + + @Test + public void testOnlineSlowLogWithDefaultConfig() throws Exception { Review comment: Is this test method necessary? It'll have to be re-written if we change the default size later on. It's logic is redundant to your earlier test over a smaller buffer size, isn't 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 With regards, Apache Git Services