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.FileNotFoundException;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.OutputStream;
25  import java.util.Iterator;
26  import java.util.HashSet;
27  import java.util.Set;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.apache.hadoop.fs.FileSystem;
32  import org.apache.hadoop.fs.FileStatus;
33  import org.apache.hadoop.fs.Path;
34  import org.apache.hadoop.hbase.HBaseCommonTestingUtility;
35  import org.apache.hadoop.hbase.procedure2.Procedure;
36  import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility;
37  import org.apache.hadoop.hbase.procedure2.SequentialProcedure;
38  import org.apache.hadoop.hbase.testclassification.SmallTests;
39  import org.apache.hadoop.hbase.util.Bytes;
40  import org.apache.hadoop.io.IOUtils;
41  
42  import org.junit.After;
43  import org.junit.Before;
44  import org.junit.Assert;
45  import org.junit.Test;
46  import org.junit.experimental.categories.Category;
47  
48  import static org.junit.Assert.assertEquals;
49  import static org.junit.Assert.assertFalse;
50  import static org.junit.Assert.assertTrue;
51  import static org.junit.Assert.fail;
52  
53  @Category(SmallTests.class)
54  public class TestWALProcedureStore {
55    private static final Log LOG = LogFactory.getLog(TestWALProcedureStore.class);
56  
57    private static final int PROCEDURE_STORE_SLOTS = 1;
58    private static final Procedure NULL_PROC = null;
59  
60    private WALProcedureStore procStore;
61  
62    private HBaseCommonTestingUtility htu;
63    private FileSystem fs;
64    private Path testDir;
65    private Path logDir;
66  
67    @Before
68    public void setUp() throws IOException {
69      htu = new HBaseCommonTestingUtility();
70      testDir = htu.getDataTestDir();
71      fs = testDir.getFileSystem(htu.getConfiguration());
72      assertTrue(testDir.depth() > 1);
73  
74      logDir = new Path(testDir, "proc-logs");
75      procStore = ProcedureTestingUtility.createWalStore(htu.getConfiguration(), fs, logDir);
76      procStore.start(PROCEDURE_STORE_SLOTS);
77      procStore.recoverLease();
78    }
79  
80    @After
81    public void tearDown() throws IOException {
82      procStore.stop(false);
83      fs.delete(logDir, true);
84    }
85  
86    private Iterator<Procedure> storeRestart() throws Exception {
87      procStore.stop(false);
88      procStore.start(PROCEDURE_STORE_SLOTS);
89      procStore.recoverLease();
90      return procStore.load();
91    }
92  
93    @Test
94    public void testEmptyLogLoad() throws Exception {
95      Iterator<Procedure> loader = storeRestart();
96      assertEquals(0, countProcedures(loader));
97    }
98  
99    @Test
100   public void testLoad() throws Exception {
101     Set<Long> procIds = new HashSet<>();
102 
103     // Insert something in the log
104     Procedure proc1 = new TestSequentialProcedure();
105     procIds.add(proc1.getProcId());
106     procStore.insert(proc1, null);
107 
108     Procedure proc2 = new TestSequentialProcedure();
109     Procedure[] child2 = new Procedure[2];
110     child2[0] = new TestSequentialProcedure();
111     child2[1] = new TestSequentialProcedure();
112 
113     procIds.add(proc2.getProcId());
114     procIds.add(child2[0].getProcId());
115     procIds.add(child2[1].getProcId());
116     procStore.insert(proc2, child2);
117 
118     // Verify that everything is there
119     verifyProcIdsOnRestart(procIds);
120 
121     // Update and delete something
122     procStore.update(proc1);
123     procStore.update(child2[1]);
124     procStore.delete(child2[1].getProcId());
125     procIds.remove(child2[1].getProcId());
126 
127     // Verify that everything is there
128     verifyProcIdsOnRestart(procIds);
129 
130     // Remove 4 byte from the trailers
131     procStore.stop(false);
132     FileStatus[] logs = fs.listStatus(logDir);
133     assertEquals(3, logs.length);
134     for (int i = 0; i < logs.length; ++i) {
135       corruptLog(logs[i], 4);
136     }
137     verifyProcIdsOnRestart(procIds);
138   }
139 
140   @Test
141   public void testCorruptedTrailer() throws Exception {
142     // Insert something
143     for (int i = 0; i < 100; ++i) {
144       procStore.insert(new TestSequentialProcedure(), null);
145     }
146 
147     // Stop the store
148     procStore.stop(false);
149 
150     // Remove 4 byte from the trailer
151     FileStatus[] logs = fs.listStatus(logDir);
152     assertEquals(1, logs.length);
153     corruptLog(logs[0], 4);
154 
155     int count = countProcedures(storeRestart());
156     assertEquals(100, count);
157   }
158 
159   @Test
160   public void testCorruptedEntries() throws Exception {
161     // Insert something
162     for (int i = 0; i < 100; ++i) {
163       procStore.insert(new TestSequentialProcedure(), null);
164     }
165 
166     // Stop the store
167     procStore.stop(false);
168 
169     // Remove some byte from the log
170     // (enough to cut the trailer and corrupt some entries)
171     FileStatus[] logs = fs.listStatus(logDir);
172     assertEquals(1, logs.length);
173     corruptLog(logs[0], 1823);
174 
175     int count = countProcedures(storeRestart());
176     assertTrue(procStore.getCorruptedLogs() != null);
177     assertEquals(1, procStore.getCorruptedLogs().size());
178     assertEquals(85, count);
179   }
180 
181   private void corruptLog(final FileStatus logFile, final long dropBytes)
182       throws IOException {
183     assertTrue(logFile.getLen() > dropBytes);
184     LOG.debug("corrupt log " + logFile.getPath() +
185               " size=" + logFile.getLen() + " drop=" + dropBytes);
186     Path tmpPath = new Path(testDir, "corrupted.log");
187     InputStream in = fs.open(logFile.getPath());
188     OutputStream out =  fs.create(tmpPath);
189     IOUtils.copyBytes(in, out, logFile.getLen() - dropBytes, true);
190     fs.rename(tmpPath, logFile.getPath());
191   }
192 
193   private void verifyProcIdsOnRestart(final Set<Long> procIds) throws Exception {
194     int count = 0;
195     Iterator<Procedure> loader = storeRestart();
196     while (loader.hasNext()) {
197       Procedure proc = loader.next();
198       LOG.debug("loading procId=" + proc.getProcId());
199       assertTrue("procId=" + proc.getProcId() + " unexpected", procIds.contains(proc.getProcId()));
200       count++;
201     }
202     assertEquals(procIds.size(), count);
203   }
204 
205   private void assertIsEmpty(Iterator<Procedure> iterator) {
206     assertEquals(0, countProcedures(iterator));
207   }
208 
209   private int countProcedures(Iterator<Procedure> iterator) {
210     int count = 0;
211     while (iterator.hasNext()) {
212       Procedure proc = iterator.next();
213       LOG.trace("loading procId=" + proc.getProcId());
214       count++;
215     }
216     return count;
217   }
218 
219   private void assertEmptyLogDir() {
220     try {
221       FileStatus[] status = fs.listStatus(logDir);
222       assertTrue("expected empty state-log dir", status == null || status.length == 0);
223     } catch (FileNotFoundException e) {
224       fail("expected the state-log dir to be present: " + logDir);
225     } catch (IOException e) {
226       fail("got en exception on state-log dir list: " + e.getMessage());
227     }
228   }
229 
230   public static class TestSequentialProcedure extends SequentialProcedure<Void> {
231     private static long seqid = 0;
232 
233     public TestSequentialProcedure() {
234       setProcId(++seqid);
235     }
236 
237     @Override
238     protected Procedure[] execute(Void env) { return null; }
239 
240     @Override
241     protected void rollback(Void env) { }
242 
243     @Override
244     protected boolean abort(Void env) { return false; }
245 
246     @Override
247     protected void serializeStateData(final OutputStream stream) throws IOException {
248       long procId = getProcId();
249       if (procId % 2 == 0) {
250         stream.write(Bytes.toBytes(procId));
251       }
252     }
253 
254     @Override
255     protected void deserializeStateData(InputStream stream) throws IOException {
256       long procId = getProcId();
257       if (procId % 2 == 0) {
258         byte[] bProcId = new byte[8];
259         assertEquals(8, stream.read(bProcId));
260         assertEquals(procId, Bytes.toLong(bProcId));
261       } else {
262         assertEquals(0, stream.available());
263       }
264     }
265   }
266 }