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  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.hadoop.fs.FileSystem;
27  import org.apache.hadoop.fs.Path;
28  import org.apache.hadoop.hbase.HBaseCommonTestingUtility;
29  import org.apache.hadoop.hbase.procedure2.store.ProcedureStore;
30  import org.apache.hadoop.hbase.testclassification.LargeTests;
31  
32  import org.junit.After;
33  import org.junit.Assert;
34  import org.junit.Before;
35  import org.junit.Ignore;
36  import org.junit.Test;
37  import org.junit.experimental.categories.Category;
38  
39  import static org.junit.Assert.assertEquals;
40  import static org.junit.Assert.assertFalse;
41  import static org.junit.Assert.assertTrue;
42  import static org.junit.Assert.fail;
43  
44  @Category(LargeTests.class)
45  public class TestProcedureReplayOrder {
46    private static final Log LOG = LogFactory.getLog(TestProcedureReplayOrder.class);
47  
48    private static final Procedure NULL_PROC = null;
49  
50    private ProcedureExecutor<Void> procExecutor;
51    private TestProcedureEnv procEnv;
52    private ProcedureStore procStore;
53  
54    private HBaseCommonTestingUtility htu;
55    private FileSystem fs;
56    private Path testDir;
57    private Path logDir;
58  
59    @Before
60    public void setUp() throws IOException {
61      htu = new HBaseCommonTestingUtility();
62      htu.getConfiguration().setInt("hbase.procedure.store.wal.sync.wait.msec", 10);
63  
64      testDir = htu.getDataTestDir();
65      fs = testDir.getFileSystem(htu.getConfiguration());
66      assertTrue(testDir.depth() > 1);
67  
68      logDir = new Path(testDir, "proc-logs");
69      procEnv = new TestProcedureEnv();
70      procStore = ProcedureTestingUtility.createWalStore(htu.getConfiguration(), fs, logDir);
71      procExecutor = new ProcedureExecutor(htu.getConfiguration(), procEnv, procStore);
72      procStore.start(24);
73      procExecutor.start(1);
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    @Test(timeout=90000)
84    public void testSingleStepReplyOrder() throws Exception {
85      // avoid the procedure to be runnable
86      procEnv.setAcquireLock(false);
87  
88      // submit the procedures
89      submitProcedures(16, 25, TestSingleStepProcedure.class);
90  
91      // restart the executor and allow the procedures to run
92      ProcedureTestingUtility.restart(procExecutor, new Runnable() {
93        @Override
94        public void run() {
95          procEnv.setAcquireLock(true);
96        }
97      });
98  
99      // wait the execution of all the procedures and
100     // assert that the execution order was sorted by procId
101     ProcedureTestingUtility.waitNoProcedureRunning(procExecutor);
102     procEnv.assertSortedExecList();
103 
104     // TODO: FIXME: This should be revisited
105   }
106 
107   @Ignore
108   @Test(timeout=90000)
109   public void testMultiStepReplyOrder() throws Exception {
110     // avoid the procedure to be runnable
111     procEnv.setAcquireLock(false);
112 
113     // submit the procedures
114     submitProcedures(16, 10, TestTwoStepProcedure.class);
115 
116     // restart the executor and allow the procedures to run
117     ProcedureTestingUtility.restart(procExecutor, new Runnable() {
118       @Override
119       public void run() {
120         procEnv.setAcquireLock(true);
121       }
122     });
123 
124     fail("TODO: FIXME: NOT IMPLEMENT REPLAY ORDER");
125   }
126 
127   private void submitProcedures(final int nthreads, final int nprocPerThread,
128       final Class<?> procClazz) throws Exception {
129     Thread[] submitThreads = new Thread[nthreads];
130     for (int i = 0; i < submitThreads.length; ++i) {
131       submitThreads[i] = new Thread() {
132         @Override
133         public void run() {
134           for (int i = 0; i < nprocPerThread; ++i) {
135             try {
136               procExecutor.submitProcedure((Procedure)procClazz.newInstance());
137             } catch (InstantiationException|IllegalAccessException e) {
138               LOG.error("unable to instantiate the procedure", e);
139               fail("failure during the proc.newInstance(): " + e.getMessage());
140             }
141           }
142         }
143       };
144     }
145 
146     for (int i = 0; i < submitThreads.length; ++i) {
147       submitThreads[i].start();
148     }
149 
150     for (int i = 0; i < submitThreads.length; ++i) {
151       submitThreads[i].join();
152     }
153   }
154 
155   private static class TestProcedureEnv {
156     private ArrayList<Long> execList = new ArrayList<Long>();
157     private boolean acquireLock = true;
158 
159     public void setAcquireLock(boolean acquireLock) {
160       this.acquireLock = acquireLock;
161     }
162 
163     public boolean canAcquireLock() {
164       return acquireLock;
165     }
166 
167     public void addToExecList(final Procedure proc) {
168       execList.add(proc.getProcId());
169     }
170 
171     public ArrayList<Long> getExecList() {
172       return execList;
173     }
174 
175     public void assertSortedExecList() {
176       LOG.debug("EXEC LIST: " + execList);
177       for (int i = 1; i < execList.size(); ++i) {
178         assertTrue("exec list not sorted: " + execList.get(i-1) + " >= " + execList.get(i),
179           execList.get(i-1) < execList.get(i));
180       }
181     }
182   }
183 
184   public static class TestSingleStepProcedure extends SequentialProcedure<TestProcedureEnv> {
185     public TestSingleStepProcedure() { }
186 
187     @Override
188     protected Procedure[] execute(TestProcedureEnv env) {
189       LOG.debug("execute procedure " + this);
190       env.addToExecList(this);
191       return null;
192     }
193 
194     protected boolean acquireLock(final TestProcedureEnv env) {
195       return env.canAcquireLock();
196     }
197 
198     @Override
199     protected void rollback(TestProcedureEnv env) { }
200 
201     @Override
202     protected boolean abort(TestProcedureEnv env) { return true; }
203   }
204 
205   public static class TestTwoStepProcedure extends SequentialProcedure<TestProcedureEnv> {
206     public TestTwoStepProcedure() { }
207 
208     @Override
209     protected Procedure[] execute(TestProcedureEnv env) {
210       LOG.debug("execute procedure " + this);
211       env.addToExecList(this);
212       return new Procedure[] { new TestSingleStepProcedure() };
213     }
214 
215     protected boolean acquireLock(final TestProcedureEnv env) {
216       return true;
217     }
218 
219     @Override
220     protected void rollback(TestProcedureEnv env) { }
221 
222     @Override
223     protected boolean abort(TestProcedureEnv env) { return true; }
224   }
225 }