001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.ipc;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertFalse;
022import static org.junit.Assert.assertTrue;
023
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.testclassification.SmallTests;
027import org.junit.BeforeClass;
028import org.junit.ClassRule;
029import org.junit.Test;
030import org.junit.experimental.categories.Category;
031import org.mockito.Mockito;
032
033@Category(SmallTests.class)
034public class TestRpcServerTraceLogging {
035
036  @ClassRule
037  public static final HBaseClassTestRule CLASS_RULE =
038    HBaseClassTestRule.forClass(TestRpcServerTraceLogging.class);
039
040  private static final org.apache.logging.log4j.core.Logger rpcServerLog =
041    (org.apache.logging.log4j.core.Logger) org.apache.logging.log4j.LogManager
042      .getLogger(RpcServer.class);
043
044  static final String TRACE_LOG_MSG =
045    "This is dummy message for testing:: region { type: REGION_NAME value: \"hbase:meta,,1\" }"
046      + " scan { column { family: \"info\" } time_range { from: 0 to: 9223372036854775807 } "
047      + "max_versions: 1 cache_blocks: true max_result_size: 2097152 caching: 2147483647 } "
048      + "number_of_rows: 2147483647 close_scanner: false client_handles_partials: "
049      + "true client_handles_heartbeats: true track_scan_metrics: false";
050
051  static final int TRACE_LOG_LENGTH = TRACE_LOG_MSG.length();
052
053  static final RpcServer mockRpcServer = Mockito.mock(RpcServer.class);
054
055  static final Configuration conf = new Configuration(false);
056
057  @BeforeClass
058  public static void setUp() {
059    Mockito.when(mockRpcServer.getConf()).thenReturn(conf);
060    Mockito.when(mockRpcServer.truncateTraceLog(Mockito.any(String.class))).thenCallRealMethod();
061  }
062
063  @Test
064  public void testLoggingWithTraceOff() {
065    conf.setInt("hbase.ipc.trace.log.max.length", 250);
066    rpcServerLog.setLevel(org.apache.logging.log4j.Level.DEBUG);
067    String truncatedString = mockRpcServer.truncateTraceLog(TRACE_LOG_MSG);
068
069    assertEquals(150 + RpcServer.KEY_WORD_TRUNCATED.length(), truncatedString.length());
070    assertTrue(truncatedString.contains(RpcServer.KEY_WORD_TRUNCATED));
071  }
072
073  @Test
074  public void testLoggingWithTraceOn() {
075    conf.setInt("hbase.ipc.trace.log.max.length", 250);
076    rpcServerLog.setLevel(org.apache.logging.log4j.Level.TRACE);
077    String truncatedString = mockRpcServer.truncateTraceLog(TRACE_LOG_MSG);
078
079    assertEquals(250 + RpcServer.KEY_WORD_TRUNCATED.length(), truncatedString.length());
080    assertTrue(truncatedString.contains(RpcServer.KEY_WORD_TRUNCATED));
081  }
082
083  @Test
084  public void testLoggingWithTraceOnLargeMax() {
085    conf.setInt("hbase.ipc.trace.log.max.length", 2000);
086    rpcServerLog.setLevel(org.apache.logging.log4j.Level.TRACE);
087    String truncatedString = mockRpcServer.truncateTraceLog(TRACE_LOG_MSG);
088
089    assertEquals(TRACE_LOG_LENGTH, truncatedString.length());
090    assertFalse(
091      mockRpcServer.truncateTraceLog(TRACE_LOG_MSG).contains(RpcServer.KEY_WORD_TRUNCATED));
092  }
093}