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.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertFalse;
022import static org.junit.jupiter.api.Assertions.assertTrue;
023
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.hbase.testclassification.SmallTests;
026import org.junit.jupiter.api.BeforeAll;
027import org.junit.jupiter.api.Tag;
028import org.junit.jupiter.api.Test;
029import org.mockito.Mockito;
030
031@Tag(SmallTests.TAG)
032public class TestRpcServerTraceLogging {
033
034  private static final org.apache.logging.log4j.core.Logger rpcServerLog =
035    (org.apache.logging.log4j.core.Logger) org.apache.logging.log4j.LogManager
036      .getLogger(RpcServer.class);
037
038  static final String TRACE_LOG_MSG =
039    "This is dummy message for testing:: region { type: REGION_NAME value: \"hbase:meta,,1\" }"
040      + " scan { column { family: \"info\" } time_range { from: 0 to: 9223372036854775807 } "
041      + "max_versions: 1 cache_blocks: true max_result_size: 2097152 caching: 2147483647 } "
042      + "number_of_rows: 2147483647 close_scanner: false client_handles_partials: "
043      + "true client_handles_heartbeats: true track_scan_metrics: false";
044
045  static final int TRACE_LOG_LENGTH = TRACE_LOG_MSG.length();
046
047  static final RpcServer mockRpcServer = Mockito.mock(RpcServer.class);
048
049  static final Configuration conf = new Configuration(false);
050
051  @BeforeAll
052  public static void setUp() {
053    Mockito.when(mockRpcServer.getConf()).thenReturn(conf);
054    Mockito.when(mockRpcServer.truncateTraceLog(Mockito.any(String.class))).thenCallRealMethod();
055  }
056
057  @Test
058  public void testLoggingWithTraceOff() {
059    conf.setInt("hbase.ipc.trace.log.max.length", 250);
060    rpcServerLog.setLevel(org.apache.logging.log4j.Level.DEBUG);
061    String truncatedString = mockRpcServer.truncateTraceLog(TRACE_LOG_MSG);
062
063    assertEquals(150 + RpcServer.KEY_WORD_TRUNCATED.length(), truncatedString.length());
064    assertTrue(truncatedString.contains(RpcServer.KEY_WORD_TRUNCATED));
065  }
066
067  @Test
068  public void testLoggingWithTraceOn() {
069    conf.setInt("hbase.ipc.trace.log.max.length", 250);
070    rpcServerLog.setLevel(org.apache.logging.log4j.Level.TRACE);
071    String truncatedString = mockRpcServer.truncateTraceLog(TRACE_LOG_MSG);
072
073    assertEquals(250 + RpcServer.KEY_WORD_TRUNCATED.length(), truncatedString.length());
074    assertTrue(truncatedString.contains(RpcServer.KEY_WORD_TRUNCATED));
075  }
076
077  @Test
078  public void testLoggingWithTraceOnLargeMax() {
079    conf.setInt("hbase.ipc.trace.log.max.length", 2000);
080    rpcServerLog.setLevel(org.apache.logging.log4j.Level.TRACE);
081    String truncatedString = mockRpcServer.truncateTraceLog(TRACE_LOG_MSG);
082
083    assertEquals(TRACE_LOG_LENGTH, truncatedString.length());
084    assertFalse(
085      mockRpcServer.truncateTraceLog(TRACE_LOG_MSG).contains(RpcServer.KEY_WORD_TRUNCATED));
086  }
087}