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  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.hadoop.conf.Configuration;
26  import org.apache.hadoop.fs.FileSystem;
27  import org.apache.hadoop.fs.Path;
28  import org.apache.hadoop.hbase.util.Threads;
29  import org.apache.hadoop.hbase.procedure2.store.ProcedureStore;
30  import org.apache.hadoop.hbase.procedure2.store.wal.WALProcedureStore;
31  
32  import static org.junit.Assert.assertEquals;
33  import static org.junit.Assert.assertFalse;
34  import static org.junit.Assert.assertTrue;
35  import static org.junit.Assert.fail;
36  
37  public class ProcedureTestingUtility {
38    private static final Log LOG = LogFactory.getLog(ProcedureTestingUtility.class);
39  
40    private ProcedureTestingUtility() {
41    }
42  
43    public static ProcedureStore createStore(final Configuration conf, final FileSystem fs,
44        final Path baseDir) throws IOException {
45      return createWalStore(conf, fs, baseDir);
46    }
47  
48    public static WALProcedureStore createWalStore(final Configuration conf, final FileSystem fs,
49        final Path logDir) throws IOException {
50      return new WALProcedureStore(conf, fs, logDir, new WALProcedureStore.LeaseRecovery() {
51        @Override
52        public void recoverFileLease(FileSystem fs, Path path) throws IOException {
53          // no-op
54        }
55      });
56    }
57  
58    public static <TEnv> void restart(ProcedureExecutor<TEnv> procExecutor)
59        throws Exception {
60      restart(procExecutor, null);
61    }
62  
63    public static <TEnv> void restart(ProcedureExecutor<TEnv> procExecutor,
64        Runnable beforeStartAction) throws Exception {
65      ProcedureStore procStore = procExecutor.getStore();
66      int storeThreads = procExecutor.getNumThreads();
67      int execThreads = procExecutor.getNumThreads();
68      // stop
69      procExecutor.stop();
70      procExecutor.join();
71      procStore.stop(false);
72      // nothing running...
73      if (beforeStartAction != null) {
74        beforeStartAction.run();
75      }
76      // re-start
77      procStore.start(storeThreads);
78      procExecutor.start(execThreads);
79    }
80  
81    public static <TEnv> void setKillBeforeStoreUpdate(ProcedureExecutor<TEnv> procExecutor,
82        boolean value) {
83      if (procExecutor.testing == null) {
84        procExecutor.testing = new ProcedureExecutor.Testing();
85      }
86      procExecutor.testing.killBeforeStoreUpdate = value;
87      LOG.warn("Set Kill before store update to: " + procExecutor.testing.killBeforeStoreUpdate);
88    }
89  
90    public static <TEnv> void setToggleKillBeforeStoreUpdate(ProcedureExecutor<TEnv> procExecutor,
91        boolean value) {
92      if (procExecutor.testing == null) {
93        procExecutor.testing = new ProcedureExecutor.Testing();
94      }
95      procExecutor.testing.toggleKillBeforeStoreUpdate = value;
96    }
97  
98    public static <TEnv> void toggleKillBeforeStoreUpdate(ProcedureExecutor<TEnv> procExecutor) {
99      if (procExecutor.testing == null) {
100       procExecutor.testing = new ProcedureExecutor.Testing();
101     }
102     procExecutor.testing.killBeforeStoreUpdate = !procExecutor.testing.killBeforeStoreUpdate;
103     LOG.warn("Set Kill before store update to: " + procExecutor.testing.killBeforeStoreUpdate);
104   }
105 
106   public static <TEnv> void setKillAndToggleBeforeStoreUpdate(ProcedureExecutor<TEnv> procExecutor,
107       boolean value) {
108     ProcedureTestingUtility.setKillBeforeStoreUpdate(procExecutor, value);
109     ProcedureTestingUtility.setToggleKillBeforeStoreUpdate(procExecutor, value);
110   }
111 
112   public static <TEnv> long submitAndWait(ProcedureExecutor<TEnv> procExecutor, Procedure proc) {
113     long procId = procExecutor.submitProcedure(proc);
114     waitProcedure(procExecutor, procId);
115     return procId;
116   }
117 
118   public static <TEnv> void waitProcedure(ProcedureExecutor<TEnv> procExecutor, long procId) {
119     while (!procExecutor.isFinished(procId) && procExecutor.isRunning()) {
120       Threads.sleepWithoutInterrupt(250);
121     }
122   }
123 
124   public static <TEnv> void waitNoProcedureRunning(ProcedureExecutor<TEnv> procExecutor) {
125     int stableRuns = 0;
126     while (stableRuns < 10) {
127       if (procExecutor.getActiveExecutorCount() > 0 || procExecutor.getRunnableSet().size() > 0) {
128         stableRuns = 0;
129         Threads.sleepWithoutInterrupt(100);
130       } else {
131         stableRuns++;
132         Threads.sleepWithoutInterrupt(25);
133       }
134     }
135   }
136 
137   public static <TEnv> void assertProcNotYetCompleted(ProcedureExecutor<TEnv> procExecutor,
138       long procId) {
139     assertFalse("expected a running proc", procExecutor.isFinished(procId));
140     assertEquals(null, procExecutor.getResult(procId));
141   }
142 
143   public static <TEnv> void assertProcNotFailed(ProcedureExecutor<TEnv> procExecutor,
144       long procId) {
145     ProcedureResult result = procExecutor.getResult(procId);
146     assertTrue("expected procedure result", result != null);
147     assertProcNotFailed(result);
148   }
149 
150   public static void assertProcNotFailed(final ProcedureResult result) {
151     Exception exception = result.getException();
152     String msg = exception != null ? exception.toString() : "no exception found";
153     assertFalse(msg, result.isFailed());
154   }
155 
156   public static void assertIsAbortException(final ProcedureResult result) {
157     LOG.info(result.getException());
158     assertEquals(true, result.isFailed());
159     Throwable cause = result.getException().getCause();
160     assertTrue("expected abort exception, got "+ cause,
161         cause instanceof ProcedureAbortedException);
162   }
163 }