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.procedure2.store.wal;
20  
21  import java.io.IOException;
22  import java.util.Random;
23  import java.util.concurrent.atomic.AtomicLong;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.conf.Configuration;
28  import org.apache.hadoop.fs.FileSystem;
29  import org.apache.hadoop.fs.Path;
30  import org.apache.hadoop.hbase.HBaseCommonTestingUtility;
31  import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility;
32  import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility.LoadCounter;
33  import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility.TestProcedure;
34  import org.apache.hadoop.hbase.procedure2.util.StringUtils;
35  import org.apache.hadoop.hbase.testclassification.LargeTests;
36  import org.apache.hadoop.hbase.testclassification.MasterTests;
37  
38  import org.junit.After;
39  import org.junit.Before;
40  import org.junit.Test;
41  import org.junit.experimental.categories.Category;
42  
43  import static org.junit.Assert.assertEquals;
44  import static org.junit.Assert.assertFalse;
45  import static org.junit.Assert.assertTrue;
46  import static org.junit.Assert.fail;
47  
48  @Category({MasterTests.class, LargeTests.class})
49  public class TestStressWALProcedureStore {
50    private static final Log LOG = LogFactory.getLog(TestWALProcedureStore.class);
51  
52    private static final int PROCEDURE_STORE_SLOTS = 8;
53  
54    private WALProcedureStore procStore;
55  
56    private HBaseCommonTestingUtility htu;
57    private FileSystem fs;
58    private Path testDir;
59    private Path logDir;
60  
61    private void setupConfiguration(Configuration conf) {
62      conf.setBoolean(WALProcedureStore.USE_HSYNC_CONF_KEY, false);
63      conf.setInt(WALProcedureStore.PERIODIC_ROLL_CONF_KEY, 5000);
64      conf.setInt(WALProcedureStore.ROLL_THRESHOLD_CONF_KEY, 128 * 1024);
65    }
66  
67    @Before
68    public void setUp() throws IOException {
69      htu = new HBaseCommonTestingUtility();
70      setupConfiguration(htu.getConfiguration());
71  
72      testDir = htu.getDataTestDir();
73      fs = testDir.getFileSystem(htu.getConfiguration());
74      assertTrue(testDir.depth() > 1);
75  
76      logDir = new Path(testDir, "proc-logs");
77      procStore = ProcedureTestingUtility.createWalStore(htu.getConfiguration(), logDir);
78      procStore.start(PROCEDURE_STORE_SLOTS);
79      procStore.recoverLease();
80  
81      LoadCounter loader = new LoadCounter();
82      procStore.load(loader);
83      assertEquals(0, loader.getMaxProcId());
84      assertEquals(0, loader.getLoadedCount());
85      assertEquals(0, loader.getCorruptedCount());
86    }
87  
88    @After
89    public void tearDown() throws IOException {
90      procStore.stop(false);
91      fs.delete(logDir, true);
92    }
93  
94    @Test
95    public void testInsertUpdateDelete() throws Exception {
96      final long LAST_PROC_ID = 19999;
97      final Thread[] thread = new Thread[PROCEDURE_STORE_SLOTS];
98      final AtomicLong procCounter = new AtomicLong((long)Math.round(Math.random() * 100));
99      for (int i = 0; i < thread.length; ++i) {
100       thread[i] = new Thread() {
101         @Override
102         public void run() {
103           Random rand = new Random();
104           TestProcedure proc;
105           do {
106             // After HBASE-15579 there may be gap in the procId sequence, trying to simulate that.
107             long procId = procCounter.addAndGet(1 + rand.nextInt(3));
108             proc = new TestProcedure(procId);
109             // Insert
110             procStore.insert(proc, null);
111             // Update
112             for (int i = 0, nupdates = rand.nextInt(10); i <= nupdates; ++i) {
113               try { Thread.sleep(0, rand.nextInt(15)); } catch (InterruptedException e) {}
114               procStore.update(proc);
115             }
116             // Delete
117             procStore.delete(proc.getProcId());
118           } while (proc.getProcId() < LAST_PROC_ID);
119         }
120       };
121       thread[i].start();
122     }
123 
124     for (int i = 0; i < thread.length; ++i) {
125       thread[i].join();
126     }
127 
128     procStore.getStoreTracker().dump();
129     assertTrue(procCounter.get() >= LAST_PROC_ID);
130     assertTrue(procStore.getStoreTracker().isEmpty());
131     assertEquals(1, procStore.getActiveLogs().size());
132   }
133 
134   @Test
135   public void testEntrySizeLimit() throws Exception {
136     final int NITEMS = 20;
137     for (int i = 1; i <= NITEMS; ++i) {
138       final byte[] data = new byte[256 << i];
139       LOG.info(String.format("Writing %s", StringUtils.humanSize(data.length)));
140       TestProcedure proc = new TestProcedure(i, 0, data);
141       procStore.insert(proc, null);
142     }
143 
144     // check that we are able to read the big proc-blobs
145     ProcedureTestingUtility.storeRestartAndAssert(procStore, NITEMS, NITEMS, 0, 0);
146   }
147 }