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  package org.apache.hadoop.hbase.regionserver;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertFalse;
22  import static org.junit.Assert.assertTrue;
23  import static org.mockito.Mockito.doReturn;
24  import static org.mockito.Mockito.mock;
25  import org.apache.hadoop.conf.Configuration;
26  import org.apache.hadoop.hbase.HRegionInfo;
27  import org.apache.hadoop.hbase.TableName;
28  import org.apache.hadoop.hbase.testclassification.SmallTests;
29  import org.apache.hadoop.hbase.util.Threads;
30  import org.junit.Before;
31  import org.junit.Test;
32  import org.junit.experimental.categories.Category;
33  
34  @Category(SmallTests.class)
35  public class TestMemStoreFlusher {
36    private MemStoreFlusher msf;
37  
38    @Before
39    public void setUp() throws Exception {
40      Configuration conf = new Configuration();
41      conf.set("hbase.hstore.flusher.count", "0");
42      msf = new MemStoreFlusher(conf, null);
43    }
44  
45    @Test
46    public void testReplaceDelayedFlushEntry() {
47      HRegionInfo hri = new HRegionInfo(1, TableName.valueOf("TestTable"), 0);
48      HRegion r = mock(HRegion.class);
49      doReturn(hri).when(r).getRegionInfo();
50  
51      // put a delayed task with 30s delay
52      msf.requestDelayedFlush(r, 30000, false);
53      assertEquals(1, msf.getFlushQueueSize());
54      assertTrue(msf.regionsInQueue.get(r).isDelay());
55  
56      // put a non-delayed task, then the delayed one should be replaced
57      assertTrue(msf.requestFlush(r, false));
58      assertEquals(1, msf.getFlushQueueSize());
59      assertFalse(msf.regionsInQueue.get(r).isDelay());
60    }
61  
62    @Test
63    public void testNotReplaceDelayedFlushEntryWhichExpired() {
64      HRegionInfo hri = new HRegionInfo(1, TableName.valueOf("TestTable"), 0);
65      HRegion r = mock(HRegion.class);
66      doReturn(hri).when(r).getRegionInfo();
67  
68      // put a delayed task with 100ms delay
69      msf.requestDelayedFlush(r, 100, false);
70      assertEquals(1, msf.getFlushQueueSize());
71      assertTrue(msf.regionsInQueue.get(r).isDelay());
72  
73      Threads.sleep(200);
74  
75      // put a non-delayed task, and the delayed one is expired, so it should not be replaced
76      assertFalse(msf.requestFlush(r, false));
77      assertEquals(1, msf.getFlushQueueSize());
78      assertTrue(msf.regionsInQueue.get(r).isDelay());
79    }
80  }