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.namequeues;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import java.io.IOException;
024import java.util.Collections;
025import java.util.List;
026import java.util.Set;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.HBaseTestingUtility;
029import org.apache.hadoop.hbase.HConstants;
030import org.apache.hadoop.hbase.ServerName;
031import org.apache.hadoop.hbase.TableName;
032import org.apache.hadoop.hbase.client.Admin;
033import org.apache.hadoop.hbase.client.LogEntry;
034import org.apache.hadoop.hbase.client.OnlineLogRecord;
035import org.apache.hadoop.hbase.client.ResultScanner;
036import org.apache.hadoop.hbase.client.Scan;
037import org.apache.hadoop.hbase.client.ServerType;
038import org.apache.hadoop.hbase.client.Table;
039import org.apache.hadoop.hbase.regionserver.HRegionServer;
040import org.apache.hadoop.hbase.testclassification.MediumTests;
041import org.apache.hadoop.hbase.testclassification.RegionServerTests;
042import org.apache.hadoop.hbase.util.Bytes;
043import org.junit.AfterClass;
044import org.junit.BeforeClass;
045import org.junit.ClassRule;
046import org.junit.Rule;
047import org.junit.Test;
048import org.junit.experimental.categories.Category;
049import org.junit.rules.TestName;
050
051@Category({ RegionServerTests.class, MediumTests.class })
052public class TestTooLargeLog {
053
054  @ClassRule
055  public static final HBaseClassTestRule CLASS_RULE =
056    HBaseClassTestRule.forClass(TestTooLargeLog.class);
057
058  protected final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
059  protected static Admin ADMIN;
060
061  @Rule
062  public TestName name = new TestName();
063
064  @BeforeClass
065  public static void setUpBeforeClass() throws Exception {
066    // Slow log needs to be enabled initially to spin up the SlowLogQueueService
067    TEST_UTIL.getConfiguration().setBoolean(HConstants.SLOW_LOG_BUFFER_ENABLED_KEY, true);
068    TEST_UTIL.getConfiguration().setInt("hbase.ipc.warn.response.size",
069      HConstants.DEFAULT_BLOCKSIZE / 2);
070    TEST_UTIL.startMiniCluster(1);
071    ADMIN = TEST_UTIL.getAdmin();
072  }
073
074  @AfterClass
075  public static void afterClass() throws Exception {
076    TEST_UTIL.shutdownMiniCluster();
077  }
078
079  /**
080   * Tests that we can trigger based on blocks scanned, and also that we properly pass the block
081   * bytes scanned value through to the client.
082   */
083  @Test
084  public void testLogLargeBlockBytesScanned() throws IOException {
085    // Turn off slow log buffer for initial loadTable, because we were seeing core dump
086    // issues coming from that slow log entry. We will re-enable below.
087    HRegionServer regionServer = TEST_UTIL.getHBaseCluster().getRegionServer(0);
088    regionServer.getConfiguration().setBoolean(HConstants.SLOW_LOG_BUFFER_ENABLED_KEY, false);
089    regionServer.getRSRpcServices().onConfigurationChange(regionServer.getConfiguration());
090
091    byte[] family = Bytes.toBytes("0");
092    Table table = TEST_UTIL.createTable(TableName.valueOf("testLogLargeBlockBytesScanned"), family);
093    TEST_UTIL.loadTable(table, family);
094    TEST_UTIL.flush(table.getName());
095
096    Set<ServerName> server = Collections.singleton(regionServer.getServerName());
097    Admin admin = TEST_UTIL.getAdmin();
098
099    // Turn on slow log so we capture large scan below
100    regionServer.getConfiguration().setBoolean(HConstants.SLOW_LOG_BUFFER_ENABLED_KEY, true);
101    regionServer.getRSRpcServices().onConfigurationChange(regionServer.getConfiguration());
102
103    Scan scan = new Scan();
104    scan.setCaching(1);
105
106    try (ResultScanner scanner = table.getScanner(scan)) {
107      scanner.next();
108    }
109
110    List<LogEntry> entries = admin.getLogEntries(server, "LARGE_LOG", ServerType.REGION_SERVER, 100,
111      Collections.emptyMap());
112
113    assertEquals(1, entries.size());
114
115    OnlineLogRecord record = (OnlineLogRecord) entries.get(0);
116
117    assertTrue("expected " + record.getBlockBytesScanned() + " to be >= 100",
118      record.getBlockBytesScanned() >= 100);
119    assertTrue("expected " + record.getResponseSize() + " to be < 100",
120      record.getResponseSize() < 100);
121    assertTrue("expected " + record.getFsReadTime() + " to be > 0", record.getFsReadTime() > 0);
122  }
123}