1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.constraint;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
24
25 import java.util.List;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.hadoop.hbase.HBaseTestingUtility;
30 import org.apache.hadoop.hbase.HColumnDescriptor;
31 import org.apache.hadoop.hbase.HTableDescriptor;
32 import org.apache.hadoop.hbase.MediumTests;
33 import org.apache.hadoop.hbase.TableName;
34 import org.apache.hadoop.hbase.client.HTable;
35 import org.apache.hadoop.hbase.client.Put;
36 import org.apache.hadoop.hbase.client.RetriesExhaustedWithDetailsException;
37 import org.apache.hadoop.hbase.util.Bytes;
38 import org.junit.After;
39 import org.junit.AfterClass;
40 import org.junit.BeforeClass;
41 import org.junit.Test;
42 import org.junit.experimental.categories.Category;
43
44
45
46
47 @Category(MediumTests.class)
48 public class TestConstraint {
49 private static final Log LOG = LogFactory
50 .getLog(TestConstraint.class);
51
52 private static HBaseTestingUtility util;
53 private static final byte[] tableName = Bytes.toBytes("test");
54 private static final byte[] dummy = Bytes.toBytes("dummy");
55 private static final byte[] row1 = Bytes.toBytes("r1");
56 private static final byte[] test = Bytes.toBytes("test");
57
58 @BeforeClass
59 public static void setUpBeforeClass() throws Exception {
60 util = new HBaseTestingUtility();
61 util.startMiniCluster();
62 }
63
64
65
66
67
68 @SuppressWarnings("unchecked")
69 @Test
70 public void testConstraintPasses() throws Exception {
71
72
73 HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName));
74 for (byte[] family : new byte[][] { dummy, test }) {
75 desc.addFamily(new HColumnDescriptor(family));
76 }
77
78 Constraints.add(desc, CheckWasRunConstraint.class);
79
80 util.getHBaseAdmin().createTable(desc);
81 HTable table = new HTable(util.getConfiguration(), tableName);
82 table.setAutoFlush(true);
83
84
85 Put put = new Put(row1);
86 byte[] value = Integer.toString(10).getBytes();
87 put.add(dummy, new byte[0], value);
88 table.put(put);
89
90 assertTrue(CheckWasRunConstraint.wasRun);
91 }
92
93
94
95
96
97 @SuppressWarnings("unchecked")
98 @Test(timeout = 60000)
99 public void testConstraintFails() throws Exception {
100
101
102
103 HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName));
104 for (byte[] family : new byte[][] { dummy, test }) {
105 desc.addFamily(new HColumnDescriptor(family));
106 }
107
108
109 Constraints.add(desc, AllFailConstraint.class);
110
111 util.getHBaseAdmin().createTable(desc);
112 HTable table = new HTable(util.getConfiguration(), tableName);
113 table.setAutoFlush(true);
114
115
116 Put put = new Put(row1);
117 put.add(dummy, new byte[0], "fail".getBytes());
118 LOG.warn("Doing put in table");
119 try {
120 table.put(put);
121 fail("This put should not have suceeded - AllFailConstraint was not run!");
122 } catch (RetriesExhaustedWithDetailsException e) {
123 List<Throwable> causes = e.getCauses();
124 assertEquals(
125 "More than one failure cause - should only be the failure constraint exception",
126 1, causes.size());
127 Throwable t = causes.get(0);
128 assertEquals(ConstraintException.class, t.getClass());
129 }
130 table.close();
131 }
132
133
134
135
136
137 @SuppressWarnings("unchecked")
138 @Test
139 public void testDisableConstraint() throws Throwable {
140
141 HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName));
142
143 for (byte[] family : new byte[][] { dummy, test }) {
144 desc.addFamily(new HColumnDescriptor(family));
145 }
146
147 Constraints.add(desc, CheckWasRunConstraint.class);
148
149
150 Constraints.add(desc, AllFailConstraint.class);
151
152
153 Constraints.disableConstraint(desc, AllFailConstraint.class);
154
155 util.getHBaseAdmin().createTable(desc);
156 HTable table = new HTable(util.getConfiguration(), tableName);
157 table.setAutoFlush(true);
158
159
160 Put put = new Put(row1);
161 put.add(dummy, new byte[0], "pass".getBytes());
162 table.put(put);
163
164 assertTrue(CheckWasRunConstraint.wasRun);
165 }
166
167
168
169
170
171 @SuppressWarnings("unchecked")
172 @Test
173 public void testDisableConstraints() throws Throwable {
174
175 HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName));
176
177 for (byte[] family : new byte[][] { dummy, test }) {
178 desc.addFamily(new HColumnDescriptor(family));
179 }
180
181 Constraints.add(desc, CheckWasRunConstraint.class);
182
183
184 Constraints.disable(desc);
185
186 util.getHBaseAdmin().createTable(desc);
187 HTable table = new HTable(util.getConfiguration(), tableName);
188 table.setAutoFlush(true);
189
190
191 Put put = new Put(row1);
192 put.add(dummy, new byte[0], "pass".getBytes());
193 LOG.warn("Doing put in table");
194 table.put(put);
195
196 assertFalse(CheckWasRunConstraint.wasRun);
197 }
198
199
200
201
202
203 @Test
204 public void testIsUnloaded() throws Exception {
205
206 HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName));
207
208 for (byte[] family : new byte[][] { dummy, test }) {
209 desc.addFamily(new HColumnDescriptor(family));
210 }
211
212 Constraints.add(desc, RuntimeFailConstraint.class);
213
214 Constraints.add(desc, CheckWasRunConstraint.class);
215 CheckWasRunConstraint.wasRun = false;
216
217 util.getHBaseAdmin().createTable(desc);
218 HTable table = new HTable(util.getConfiguration(), tableName);
219 table.setAutoFlush(true);
220
221
222 Put put = new Put(row1);
223 put.add(dummy, new byte[0], "pass".getBytes());
224
225 try{
226 table.put(put);
227 fail("RuntimeFailConstraint wasn't triggered - this put shouldn't work!");
228 } catch (Exception e) {
229 }
230
231
232 table.put(put);
233
234 assertFalse(CheckWasRunConstraint.wasRun);
235 table.close();
236 }
237
238 @After
239 public void cleanup() throws Exception {
240
241 CheckWasRunConstraint.wasRun = false;
242 util.getHBaseAdmin().disableTable(tableName);
243 util.getHBaseAdmin().deleteTable(tableName);
244 }
245
246 @AfterClass
247 public static void tearDownAfterClass() throws Exception {
248 util.shutdownMiniCluster();
249 }
250
251
252
253
254 public static class CheckWasRunConstraint extends BaseConstraint {
255 public static boolean wasRun = false;
256
257 @Override
258 public void check(Put p) {
259 wasRun = true;
260 }
261 }
262
263 }