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;
20  
21  import java.io.IOException;
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.concurrent.TimeoutException;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
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.store.ProcedureStore;
32  import org.apache.hadoop.hbase.protobuf.generated.ProcedureProtos.ProcedureState;
33  import org.apache.hadoop.hbase.testclassification.SmallTests;
34  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
35  
36  import org.junit.After;
37  import org.junit.Before;
38  import org.junit.Assert;
39  import org.junit.Test;
40  import org.junit.experimental.categories.Category;
41  
42  import static org.junit.Assert.assertEquals;
43  import static org.junit.Assert.assertFalse;
44  import static org.junit.Assert.assertTrue;
45  import static org.junit.Assert.fail;
46  
47  @Category(SmallTests.class)
48  public class TestProcedureExecution {
49    private static final Log LOG = LogFactory.getLog(TestProcedureExecution.class);
50  
51    private static final int PROCEDURE_EXECUTOR_SLOTS = 1;
52    private static final Procedure NULL_PROC = null;
53  
54    private ProcedureExecutor<Void> procExecutor;
55    private ProcedureStore procStore;
56  
57    private HBaseCommonTestingUtility htu;
58    private FileSystem fs;
59    private Path testDir;
60    private Path logDir;
61  
62    @Before
63    public void setUp() throws IOException {
64      htu = new HBaseCommonTestingUtility();
65      testDir = htu.getDataTestDir();
66      fs = testDir.getFileSystem(htu.getConfiguration());
67      assertTrue(testDir.depth() > 1);
68  
69      logDir = new Path(testDir, "proc-logs");
70      procStore = ProcedureTestingUtility.createWalStore(htu.getConfiguration(), fs, logDir);
71      procExecutor = new ProcedureExecutor(htu.getConfiguration(), null, procStore);
72      procStore.start(PROCEDURE_EXECUTOR_SLOTS);
73      procExecutor.start(PROCEDURE_EXECUTOR_SLOTS);
74    }
75  
76    @After
77    public void tearDown() throws IOException {
78      procExecutor.stop();
79      procStore.stop(false);
80      fs.delete(logDir, true);
81    }
82  
83    private static class TestProcedureException extends Exception {
84      public TestProcedureException(String msg) { super(msg); }
85    }
86  
87    public static class TestSequentialProcedure extends SequentialProcedure<Void> {
88      private final Procedure[] subProcs;
89      private final List<String> state;
90      private final Exception failure;
91      private final String name;
92  
93      public TestSequentialProcedure() {
94        throw new UnsupportedOperationException("recovery should not be triggered here");
95      }
96  
97      public TestSequentialProcedure(String name, List<String> state, Procedure... subProcs) {
98        this.state = state;
99        this.subProcs = subProcs;
100       this.name = name;
101       this.failure = null;
102     }
103 
104     public TestSequentialProcedure(String name, List<String> state, Exception failure) {
105       this.state = state;
106       this.subProcs = null;
107       this.name = name;
108       this.failure = failure;
109     }
110 
111     @Override
112     protected Procedure[] execute(Void env) {
113       state.add(name + "-execute");
114       if (failure != null) {
115         setFailure(new RemoteProcedureException(name + "-failure", failure));
116         return null;
117       }
118       return subProcs;
119     }
120 
121     @Override
122     protected void rollback(Void env) {
123       state.add(name + "-rollback");
124     }
125 
126     @Override
127     protected boolean abort(Void env) {
128       state.add(name + "-abort");
129       return true;
130     }
131   }
132 
133   @Test(timeout=30000)
134   public void testBadSubprocList() {
135     List<String> state = new ArrayList<String>();
136     Procedure subProc2 = new TestSequentialProcedure("subProc2", state);
137     Procedure subProc1 = new TestSequentialProcedure("subProc1", state, subProc2, NULL_PROC);
138     Procedure rootProc = new TestSequentialProcedure("rootProc", state, subProc1);
139     long rootId = ProcedureTestingUtility.submitAndWait(procExecutor, rootProc);
140 
141     // subProc1 has a "null" subprocedure which is catched as InvalidArgument
142     // failed state with 2 execute and 2 rollback
143     LOG.info(state);
144     ProcedureResult result = procExecutor.getResult(rootId);
145     LOG.info(result.getException());
146     assertTrue(state.toString(), result.isFailed());
147     assertTrue(result.getException().toString(),
148       result.getException().getCause() instanceof IllegalArgumentException);
149 
150     assertEquals(state.toString(), 4, state.size());
151     assertEquals("rootProc-execute", state.get(0));
152     assertEquals("subProc1-execute", state.get(1));
153     assertEquals("subProc1-rollback", state.get(2));
154     assertEquals("rootProc-rollback", state.get(3));
155   }
156 
157   @Test(timeout=30000)
158   public void testSingleSequentialProc() {
159     List<String> state = new ArrayList<String>();
160     Procedure subProc2 = new TestSequentialProcedure("subProc2", state);
161     Procedure subProc1 = new TestSequentialProcedure("subProc1", state, subProc2);
162     Procedure rootProc = new TestSequentialProcedure("rootProc", state, subProc1);
163     long rootId = ProcedureTestingUtility.submitAndWait(procExecutor, rootProc);
164 
165     // successful state, with 3 execute
166     LOG.info(state);
167     ProcedureResult result = procExecutor.getResult(rootId);
168     ProcedureTestingUtility.assertProcNotFailed(result);
169     assertEquals(state.toString(), 3, state.size());
170   }
171 
172   @Test(timeout=30000)
173   public void testSingleSequentialProcRollback() {
174     List<String> state = new ArrayList<String>();
175     Procedure subProc2 = new TestSequentialProcedure("subProc2", state,
176                                                      new TestProcedureException("fail test"));
177     Procedure subProc1 = new TestSequentialProcedure("subProc1", state, subProc2);
178     Procedure rootProc = new TestSequentialProcedure("rootProc", state, subProc1);
179     long rootId = ProcedureTestingUtility.submitAndWait(procExecutor, rootProc);
180 
181     // the 3rd proc fail, rollback after 2 successful execution
182     LOG.info(state);
183     ProcedureResult result = procExecutor.getResult(rootId);
184     LOG.info(result.getException());
185     assertTrue(state.toString(), result.isFailed());
186     assertTrue(result.getException().toString(),
187       result.getException().getCause() instanceof TestProcedureException);
188 
189     assertEquals(state.toString(), 6, state.size());
190     assertEquals("rootProc-execute", state.get(0));
191     assertEquals("subProc1-execute", state.get(1));
192     assertEquals("subProc2-execute", state.get(2));
193     assertEquals("subProc2-rollback", state.get(3));
194     assertEquals("subProc1-rollback", state.get(4));
195     assertEquals("rootProc-rollback", state.get(5));
196   }
197 
198   public static class TestFaultyRollback extends SequentialProcedure<Void> {
199     private int retries = 0;
200 
201     public TestFaultyRollback() { }
202 
203     @Override
204     protected Procedure[] execute(Void env) {
205       setFailure("faulty-rollback-test", new TestProcedureException("test faulty rollback"));
206       return null;
207     }
208 
209     @Override
210     protected void rollback(Void env) throws IOException {
211       if (++retries < 3) {
212         LOG.info("inject rollback failure " + retries);
213         throw new IOException("injected failure number " + retries);
214       }
215       LOG.info("execute non faulty rollback step retries=" + retries);
216     }
217 
218     @Override
219     protected boolean abort(Void env) { return false; }
220   }
221 
222   @Test(timeout=30000)
223   public void testRollbackRetriableFailure() {
224     long procId = ProcedureTestingUtility.submitAndWait(procExecutor, new TestFaultyRollback());
225 
226     ProcedureResult result = procExecutor.getResult(procId);
227     LOG.info(result.getException());
228     assertTrue("expected a failure", result.isFailed());
229     assertTrue(result.getException().toString(),
230       result.getException().getCause() instanceof TestProcedureException);
231   }
232 
233   public static class TestWaitingProcedure extends SequentialProcedure<Void> {
234     private final List<String> state;
235     private final boolean hasChild;
236     private final String name;
237 
238     public TestWaitingProcedure() {
239       throw new UnsupportedOperationException("recovery should not be triggered here");
240     }
241 
242     public TestWaitingProcedure(String name, List<String> state, boolean hasChild) {
243       this.hasChild = hasChild;
244       this.state = state;
245       this.name = name;
246     }
247 
248     @Override
249     protected Procedure[] execute(Void env) {
250       state.add(name + "-execute");
251       setState(ProcedureState.WAITING_TIMEOUT);
252       return hasChild ? new Procedure[] { new TestWaitChild(name, state) } : null;
253     }
254 
255     @Override
256     protected void rollback(Void env) {
257       state.add(name + "-rollback");
258     }
259 
260     @Override
261     protected boolean abort(Void env) {
262       state.add(name + "-abort");
263       return true;
264     }
265 
266     public static class TestWaitChild extends SequentialProcedure<Void> {
267       private final List<String> state;
268       private final String name;
269 
270       public TestWaitChild() {
271         throw new UnsupportedOperationException("recovery should not be triggered here");
272       }
273 
274       public TestWaitChild(String name, List<String> state) {
275         this.name = name;
276         this.state = state;
277       }
278 
279       @Override
280       protected Procedure[] execute(Void env) {
281         state.add(name + "-child-execute");
282         return null;
283       }
284 
285       @Override
286       protected void rollback(Void env) {
287         state.add(name + "-child-rollback");
288       }
289 
290       @Override
291       protected boolean abort(Void env) {
292         state.add(name + "-child-abort");
293         return true;
294       }
295     }
296   }
297 
298   @Test(timeout=30000)
299   public void testAbortTimeout() {
300     final int PROC_TIMEOUT_MSEC = 2500;
301     List<String> state = new ArrayList<String>();
302     Procedure proc = new TestWaitingProcedure("wproc", state, false);
303     proc.setTimeout(PROC_TIMEOUT_MSEC);
304     long startTime = EnvironmentEdgeManager.currentTime();
305     long rootId = ProcedureTestingUtility.submitAndWait(procExecutor, proc);
306     long execTime = EnvironmentEdgeManager.currentTime() - startTime;
307     LOG.info(state);
308     assertTrue("we didn't wait enough execTime=" + execTime, execTime >= PROC_TIMEOUT_MSEC);
309     ProcedureResult result = procExecutor.getResult(rootId);
310     LOG.info(result.getException());
311     assertTrue(state.toString(), result.isFailed());
312     assertTrue(result.getException().toString(),
313                result.getException().getCause() instanceof TimeoutException);
314     assertEquals(state.toString(), 2, state.size());
315     assertEquals("wproc-execute", state.get(0));
316     assertEquals("wproc-rollback", state.get(1));
317   }
318 
319   @Test(timeout=30000)
320   public void testAbortTimeoutWithChildren() {
321     List<String> state = new ArrayList<String>();
322     Procedure proc = new TestWaitingProcedure("wproc", state, true);
323     proc.setTimeout(2500);
324     long rootId = ProcedureTestingUtility.submitAndWait(procExecutor, proc);
325     LOG.info(state);
326     ProcedureResult result = procExecutor.getResult(rootId);
327     LOG.info(result.getException());
328     assertTrue(state.toString(), result.isFailed());
329     assertTrue(result.getException().toString(),
330                result.getException().getCause() instanceof TimeoutException);
331     assertEquals(state.toString(), 4, state.size());
332     assertEquals("wproc-execute", state.get(0));
333     assertEquals("wproc-child-execute", state.get(1));
334     assertEquals("wproc-child-rollback", state.get(2));
335     assertEquals("wproc-rollback", state.get(3));
336   }
337 }