View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.ipc;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertFalse;
23  import static org.junit.Assert.assertTrue;
24  
25  import org.apache.hadoop.conf.Configuration;
26  import org.apache.hadoop.hbase.testclassification.SmallTests;
27  import org.apache.log4j.Level;
28  import org.apache.log4j.Logger;
29  import org.junit.BeforeClass;
30  import org.junit.ClassRule;
31  import org.junit.Test;
32  import org.junit.experimental.categories.Category;
33  import org.mockito.Mockito;
34  
35  
36  @Category(SmallTests.class)
37  public class TestRpcServerTraceLogging {
38  
39    static Logger rpcServerLog = Logger.getLogger(RpcServer.class);
40  
41    static final String TRACE_LOG_MSG =
42        "This is dummy message for testing:: region { type: REGION_NAME value: \"hbase:meta,,1\" }"
43            + " scan { column { family: \"info\" } time_range { from: 0 to: 9223372036854775807 } "
44        + "max_versions: 1 cache_blocks: true max_result_size: 2097152 caching: 2147483647 } "
45        + "number_of_rows: 2147483647 close_scanner: false client_handles_partials: "
46        + "true client_handles_heartbeats: true track_scan_metrics: false";
47  
48    static final int TRACE_LOG_LENGTH = TRACE_LOG_MSG.length();
49  
50    static final RpcServer mockRpcServer = Mockito.mock(RpcServer.class);
51  
52    static final Configuration conf = new Configuration(false);
53  
54    @BeforeClass
55    public static void setUp() {
56      Mockito.when(mockRpcServer.getConf()).thenReturn(conf);
57      Mockito.when(mockRpcServer.truncateTraceLog(Mockito.any(String.class))).thenCallRealMethod();
58    }
59  
60    @Test
61    public void testLoggingWithTraceOff() {
62      conf.setInt("hbase.ipc.trace.log.max.length", 250);
63      rpcServerLog.setLevel(Level.DEBUG);
64      String truncatedString = mockRpcServer.truncateTraceLog(TRACE_LOG_MSG);
65  
66      assertEquals(150 + RpcServer.KEY_WORD_TRUNCATED.length(), truncatedString.length());
67      assertTrue(truncatedString.contains(RpcServer.KEY_WORD_TRUNCATED));
68    }
69  
70    @Test
71    public void testLoggingWithTraceOn() {
72      conf.setInt("hbase.ipc.trace.log.max.length", 250);
73      rpcServerLog.setLevel(Level.TRACE);
74      String truncatedString = mockRpcServer.truncateTraceLog(TRACE_LOG_MSG);
75  
76      assertEquals(250 + RpcServer.KEY_WORD_TRUNCATED.length(), truncatedString.length());
77      assertTrue(truncatedString.contains(RpcServer.KEY_WORD_TRUNCATED));
78    }
79  
80    @Test
81    public void testLoggingWithTraceOnLargeMax() {
82      conf.setInt("hbase.ipc.trace.log.max.length", 2000);
83      rpcServerLog.setLevel(Level.TRACE);
84      String truncatedString = mockRpcServer.truncateTraceLog(TRACE_LOG_MSG);
85  
86      assertEquals(TRACE_LOG_LENGTH, truncatedString.length());
87      assertFalse(
88        mockRpcServer.truncateTraceLog(TRACE_LOG_MSG).contains(RpcServer.KEY_WORD_TRUNCATED));
89    }
90  }