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