HBASE-20708 Remove the usage of RecoverMetaProcedure in master startup
[hbase.git] / hbase-procedure / src / test / java / org / apache / hadoop / hbase / procedure2 / TestProcedureMetrics.java
blob2acb7ddcf3392040833bdf288881d124f5d1db2e
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.hadoop.hbase.procedure2;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertNotEquals;
22 import static org.junit.Assert.assertTrue;
24 import java.io.IOException;
25 import org.apache.hadoop.fs.FileSystem;
26 import org.apache.hadoop.fs.Path;
27 import org.apache.hadoop.hbase.HBaseClassTestRule;
28 import org.apache.hadoop.hbase.HBaseCommonTestingUtility;
29 import org.apache.hadoop.hbase.procedure2.store.ProcedureStore;
30 import org.apache.hadoop.hbase.testclassification.MasterTests;
31 import org.apache.hadoop.hbase.testclassification.SmallTests;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.ClassRule;
35 import org.junit.Test;
36 import org.junit.experimental.categories.Category;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 @Category({MasterTests.class, SmallTests.class})
41 public class TestProcedureMetrics {
43 @ClassRule
44 public static final HBaseClassTestRule CLASS_RULE =
45 HBaseClassTestRule.forClass(TestProcedureMetrics.class);
47 private static final Logger LOG = LoggerFactory.getLogger(TestProcedureMetrics.class);
49 private static final int PROCEDURE_EXECUTOR_SLOTS = 1;
51 private TestProcEnv procEnv;
52 private static ProcedureExecutor<TestProcEnv> procExecutor;
53 private ProcedureStore procStore;
55 private HBaseCommonTestingUtility htu;
56 private FileSystem fs;
57 private Path testDir;
58 private Path logDir;
60 private static int beginCount = 0;
61 private static int successCount = 0;
62 private static int failedCount = 0;
65 @Before
66 public void setUp() throws IOException {
67 htu = new HBaseCommonTestingUtility();
68 testDir = htu.getDataTestDir();
69 fs = testDir.getFileSystem(htu.getConfiguration());
70 assertTrue(testDir.depth() > 1);
72 logDir = new Path(testDir, "proc-logs");
73 procEnv = new TestProcEnv();
74 procStore = ProcedureTestingUtility.createStore(htu.getConfiguration(), logDir);
75 procExecutor = new ProcedureExecutor<TestProcEnv>(htu.getConfiguration(), procEnv, procStore);
76 procExecutor.testing = new ProcedureExecutor.Testing();
77 procStore.start(PROCEDURE_EXECUTOR_SLOTS);
78 ProcedureTestingUtility.initAndStartWorkers(procExecutor, PROCEDURE_EXECUTOR_SLOTS, true);
81 @After
82 public void tearDown() throws IOException {
83 procExecutor.stop();
84 procStore.stop(false);
85 fs.delete(logDir, true);
88 @Test
89 public void testMetricForSimpleProcedure() throws Exception {
90 // procedure that executes successfully
91 ProcedureMetrics proc = new ProcedureMetrics(true);
92 long id = ProcedureTestingUtility.submitAndWait(procExecutor, proc);
93 assertNotEquals("ProcId zero!", 0, id);
94 beginCount++;
95 successCount++;
96 ProcedureTestingUtility.waitProcedure(procExecutor, proc);
97 assertEquals("beginCount doesn't match!", beginCount, proc.beginCount);
98 assertEquals("successCount doesn't match!", successCount, proc.successCount);
99 assertEquals("failedCont doesn't match!", failedCount, proc.failedCount);
102 @Test
103 public void testMetricsForFailedProcedure() throws Exception {
104 // procedure that fails
105 ProcedureMetrics proc = new ProcedureMetrics(false);
106 long id = ProcedureTestingUtility.submitAndWait(procExecutor, proc);
107 assertNotEquals("ProcId zero!", 0, id);
108 beginCount++;
109 failedCount++;
110 ProcedureTestingUtility.waitProcedure(procExecutor, proc);
111 assertEquals("beginCount doesn't match!", beginCount, proc.beginCount);
112 assertEquals("successCount doesn't match!", successCount, proc.successCount);
113 assertEquals("failedCont doesn't match!", failedCount, proc.failedCount);
116 @Test
117 public void testMetricForYieldProcedure() throws Exception {
118 // procedure that yields
119 ProcedureMetrics proc = new ProcedureMetrics(true, true);
120 long id = ProcedureTestingUtility.submitAndWait(procExecutor, proc);
121 assertNotEquals("ProcId zero!", 0, id);
122 beginCount++;
123 successCount++;
124 ProcedureTestingUtility.waitProcedure(procExecutor, proc);
125 assertEquals("beginCount doesn't match!", beginCount, proc.beginCount);
126 assertEquals("successCount doesn't match!", successCount, proc.successCount);
127 assertEquals("failedCont doesn't match!", failedCount, proc.failedCount);
130 @Test
131 public void testMetricForFailedYiledProcedure() {
132 // procedure that yields and fails
133 ProcedureMetrics proc = new ProcedureMetrics(false, true);
134 long id = ProcedureTestingUtility.submitAndWait(procExecutor, proc);
135 assertNotEquals("ProcId zero!", 0, id);
136 beginCount++;
137 failedCount++;
138 ProcedureTestingUtility.waitProcedure(procExecutor, proc);
139 assertEquals("beginCount doesn't match!", beginCount, proc.beginCount);
140 assertEquals("successCount doesn't match!", successCount, proc.successCount);
141 assertEquals("failedCont doesn't match!", failedCount, proc.failedCount);
144 @Test
145 public void testMetricForProcedureWithChildren() throws Exception {
146 // Procedure that yileds with one of the sub-procedures that fail
147 int subProcCount = 10;
148 int failChildIndex = 2;
149 int yiledChildIndex = -1;
150 ProcedureMetrics[] subprocs = new ProcedureMetrics[subProcCount];
151 for (int i = 0; i < subProcCount; ++i) {
152 subprocs[i] = new ProcedureMetrics(failChildIndex != i, yiledChildIndex == i, 3);
155 ProcedureMetrics proc = new ProcedureMetrics(true, true, 3, subprocs);
156 long id = ProcedureTestingUtility.submitAndWait(procExecutor, proc);
157 assertNotEquals("ProcId zero!", 0, id);
158 beginCount += subProcCount + 1;
159 successCount += subProcCount - (failChildIndex + 1);
160 if (failChildIndex >= 0) {
161 failedCount += subProcCount + 1;
162 } else {
163 successCount++;
165 ProcedureTestingUtility.waitProcedure(procExecutor, proc);
166 assertEquals("beginCount doesn't match!", beginCount, proc.beginCount);
167 assertEquals("successCount doesn't match!", successCount, proc.successCount);
168 assertEquals("failedCont doesn't match!", failedCount, proc.failedCount);
171 private static class TestProcEnv {
172 public boolean toggleKillBeforeStoreUpdate = false;
173 public boolean triggerRollbackOnChild = false;
176 public static class ProcedureMetrics extends SequentialProcedure<TestProcEnv> {
177 public static long beginCount = 0;
178 public static long successCount = 0;
179 public static long failedCount = 0;
181 private boolean success;
182 private boolean yield;
183 private int yieldCount;
184 private int yieldNum;
186 private ProcedureMetrics[] subprocs = null;
188 public ProcedureMetrics() {
189 this(true);
192 public ProcedureMetrics(boolean success) {
193 this(success, true);
196 public ProcedureMetrics(boolean success, boolean yield) {
197 this(success, yield, 1);
200 public ProcedureMetrics(boolean success, boolean yield, int yieldCount) {
201 this(success, yield, yieldCount, null);
204 public ProcedureMetrics(boolean success, ProcedureMetrics[] subprocs) {
205 this(success, false, 1, subprocs);
208 public ProcedureMetrics(boolean success, boolean yield, int yieldCount,
209 ProcedureMetrics[] subprocs) {
210 this.success = success;
211 this.yield = yield;
212 this.yieldCount = yieldCount;
213 this.subprocs = subprocs;
214 yieldNum = 0;
217 @Override
218 protected void updateMetricsOnSubmit(TestProcEnv env) {
219 beginCount++;
222 @Override
223 protected Procedure[] execute(TestProcEnv env) throws ProcedureYieldException,
224 ProcedureSuspendedException, InterruptedException {
225 if (this.yield) {
226 if (yieldNum < yieldCount) {
227 yieldNum++;
228 throw new ProcedureYieldException();
231 if (!this.success) {
232 setFailure("Failed", new InterruptedException("Failed"));
233 return null;
235 return subprocs;
238 @Override
239 protected void rollback(TestProcEnv env) throws IOException, InterruptedException {
243 @Override
244 protected boolean abort(TestProcEnv env) {
245 return false;
248 @Override
249 protected void updateMetricsOnFinish(final TestProcEnv env, final long time,
250 boolean success) {
251 if (success) {
252 successCount++;
253 } else {
254 failedCount++;