1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.mapreduce;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertTrue;
22
23 import java.io.IOException;
24 import java.security.PrivilegedExceptionAction;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.HashSet;
28 import java.util.List;
29 import java.util.Set;
30 import java.util.UUID;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.apache.hadoop.conf.Configurable;
35 import org.apache.hadoop.conf.Configuration;
36 import org.apache.hadoop.fs.FSDataOutputStream;
37 import org.apache.hadoop.fs.FileStatus;
38 import org.apache.hadoop.fs.FileSystem;
39 import org.apache.hadoop.fs.Path;
40 import org.apache.hadoop.hbase.Cell;
41 import org.apache.hadoop.hbase.CellUtil;
42 import org.apache.hadoop.hbase.HBaseTestingUtility;
43 import org.apache.hadoop.hbase.HConstants;
44 import org.apache.hadoop.hbase.LargeTests;
45 import org.apache.hadoop.hbase.client.HBaseAdmin;
46 import org.apache.hadoop.hbase.client.HTable;
47 import org.apache.hadoop.hbase.client.Result;
48 import org.apache.hadoop.hbase.client.ResultScanner;
49 import org.apache.hadoop.hbase.client.Scan;
50 import org.apache.hadoop.hbase.protobuf.generated.VisibilityLabelsProtos.VisibilityLabelsResponse;
51 import org.apache.hadoop.hbase.security.User;
52 import org.apache.hadoop.hbase.security.visibility.Authorizations;
53 import org.apache.hadoop.hbase.security.visibility.ScanLabelGenerator;
54 import org.apache.hadoop.hbase.security.visibility.SimpleScanLabelGenerator;
55 import org.apache.hadoop.hbase.security.visibility.VisibilityClient;
56 import org.apache.hadoop.hbase.security.visibility.VisibilityConstants;
57 import org.apache.hadoop.hbase.security.visibility.VisibilityController;
58 import org.apache.hadoop.hbase.security.visibility.VisibilityUtils;
59 import org.apache.hadoop.hbase.util.Bytes;
60 import org.apache.hadoop.mapred.Utils.OutputFileUtils.OutputFilesFilter;
61 import org.apache.hadoop.util.Tool;
62 import org.apache.hadoop.util.ToolRunner;
63 import org.junit.AfterClass;
64 import org.junit.BeforeClass;
65 import org.junit.Test;
66 import org.junit.experimental.categories.Category;
67
68 @Category(LargeTests.class)
69 public class TestImportTSVWithVisibilityLabels implements Configurable {
70
71 protected static final Log LOG = LogFactory.getLog(TestImportTSVWithVisibilityLabels.class);
72 protected static final String NAME = TestImportTsv.class.getSimpleName();
73 protected static HBaseTestingUtility util = new HBaseTestingUtility();
74
75
76
77
78
79 protected static final String DELETE_AFTER_LOAD_CONF = NAME + ".deleteAfterLoad";
80
81
82
83
84 protected static final String FORCE_COMBINER_CONF = NAME + ".forceCombiner";
85
86 private final String FAMILY = "FAM";
87 private final static String TOPSECRET = "topsecret";
88 private final static String PUBLIC = "public";
89 private final static String PRIVATE = "private";
90 private final static String CONFIDENTIAL = "confidential";
91 private final static String SECRET = "secret";
92 private static User SUPERUSER;
93 private static Configuration conf;
94
95 public Configuration getConf() {
96 return util.getConfiguration();
97 }
98
99 public void setConf(Configuration conf) {
100 throw new IllegalArgumentException("setConf not supported");
101 }
102
103 @BeforeClass
104 public static void provisionCluster() throws Exception {
105 conf = util.getConfiguration();
106 SUPERUSER = User.createUserForTesting(conf, "admin", new String[] { "supergroup" });
107 conf.set("hbase.superuser", "admin,"+User.getCurrent().getName());
108 conf.setInt("hfile.format.version", 3);
109 conf.set("hbase.coprocessor.master.classes", VisibilityController.class.getName());
110 conf.set("hbase.coprocessor.region.classes", VisibilityController.class.getName());
111 conf.setClass(VisibilityUtils.VISIBILITY_LABEL_GENERATOR_CLASS, SimpleScanLabelGenerator.class,
112 ScanLabelGenerator.class);
113 util.startMiniCluster();
114
115 util.waitTableEnabled(VisibilityConstants.LABELS_TABLE_NAME.getName(), 50000);
116 createLabels();
117 HBaseAdmin admin = new HBaseAdmin(util.getConfiguration());
118 util.startMiniMapReduceCluster();
119 }
120
121 private static void createLabels() throws IOException, InterruptedException {
122 PrivilegedExceptionAction<VisibilityLabelsResponse> action =
123 new PrivilegedExceptionAction<VisibilityLabelsResponse>() {
124 public VisibilityLabelsResponse run() throws Exception {
125 String[] labels = { SECRET, TOPSECRET, CONFIDENTIAL, PUBLIC, PRIVATE };
126 try {
127 VisibilityClient.addLabels(conf, labels);
128 LOG.info("Added labels ");
129 } catch (Throwable t) {
130 LOG.error("Error in adding labels" , t);
131 throw new IOException(t);
132 }
133 return null;
134 }
135 };
136 SUPERUSER.runAs(action);
137 }
138
139 @AfterClass
140 public static void releaseCluster() throws Exception {
141 util.shutdownMiniMapReduceCluster();
142 util.shutdownMiniCluster();
143 }
144
145 @Test
146 public void testMROnTable() throws Exception {
147 String tableName = "test-" + UUID.randomUUID();
148
149
150 String[] args = new String[] {
151 "-D" + ImportTsv.MAPPER_CONF_KEY
152 + "=org.apache.hadoop.hbase.mapreduce.TsvImporterMapper",
153 "-D" + ImportTsv.COLUMNS_CONF_KEY + "=HBASE_ROW_KEY,FAM:A,FAM:B,HBASE_CELL_VISIBILITY",
154 "-D" + ImportTsv.SEPARATOR_CONF_KEY + "=\u001b", tableName };
155 String data = "KEY\u001bVALUE1\u001bVALUE2\u001bsecret&private\n";
156 util.createTable(tableName, FAMILY);
157 doMROnTableTest(util, FAMILY, data, args, 1);
158 util.deleteTable(tableName);
159 }
160
161 @Test
162 public void testMROnTableWithBulkload() throws Exception {
163 String tableName = "test-" + UUID.randomUUID();
164 Path hfiles = new Path(util.getDataTestDirOnTestFS(tableName), "hfiles");
165
166 String[] args = new String[] {
167 "-D" + ImportTsv.BULK_OUTPUT_CONF_KEY + "=" + hfiles.toString(),
168 "-D" + ImportTsv.COLUMNS_CONF_KEY
169 + "=HBASE_ROW_KEY,FAM:A,FAM:B,HBASE_CELL_VISIBILITY",
170 "-D" + ImportTsv.SEPARATOR_CONF_KEY + "=\u001b", tableName };
171 String data = "KEY\u001bVALUE1\u001bVALUE2\u001bsecret&private\n";
172 util.createTable(tableName, FAMILY);
173 doMROnTableTest(util, FAMILY, data, args, 1);
174 util.deleteTable(tableName);
175 }
176
177 @Test
178 public void testBulkOutputWithTsvImporterTextMapper() throws Exception {
179 String table = "test-" + UUID.randomUUID();
180 String FAMILY = "FAM";
181 Path bulkOutputPath = new Path(util.getDataTestDir(table),"hfiles");
182
183 String[] args =
184 new String[] {
185 "-D" + ImportTsv.MAPPER_CONF_KEY
186 + "=org.apache.hadoop.hbase.mapreduce.TsvImporterTextMapper",
187 "-D" + ImportTsv.COLUMNS_CONF_KEY
188 + "=HBASE_ROW_KEY,FAM:A,FAM:B,HBASE_CELL_VISIBILITY",
189 "-D" + ImportTsv.SEPARATOR_CONF_KEY + "=\u001b",
190 "-D" + ImportTsv.BULK_OUTPUT_CONF_KEY + "=" + bulkOutputPath.toString(), table
191 };
192 String data = "KEY\u001bVALUE4\u001bVALUE8\u001bsecret&private\n";
193 doMROnTableTest(util, FAMILY, data, args, 4);
194 util.deleteTable(table);
195 }
196
197 @Test
198 public void testMRWithOutputFormat() throws Exception {
199 String tableName = "test-" + UUID.randomUUID();
200 Path hfiles = new Path(util.getDataTestDirOnTestFS(tableName), "hfiles");
201
202 String[] args = new String[] {
203 "-D" + ImportTsv.MAPPER_CONF_KEY
204 + "=org.apache.hadoop.hbase.mapreduce.TsvImporterMapper",
205 "-D" + ImportTsv.BULK_OUTPUT_CONF_KEY + "=" + hfiles.toString(),
206 "-D" + ImportTsv.COLUMNS_CONF_KEY + "=HBASE_ROW_KEY,FAM:A,FAM:B,HBASE_CELL_VISIBILITY",
207 "-D" + ImportTsv.SEPARATOR_CONF_KEY + "=\u001b", tableName };
208 String data = "KEY\u001bVALUE4\u001bVALUE8\u001bsecret&private\n";
209 util.createTable(tableName, FAMILY);
210 doMROnTableTest(util, FAMILY, data, args, 1);
211 util.deleteTable(tableName);
212 }
213
214
215
216
217
218
219
220
221
222
223
224 protected static Tool doMROnTableTest(HBaseTestingUtility util, String family, String data,
225 String[] args, int valueMultiplier) throws Exception {
226 String table = args[args.length - 1];
227 Configuration conf = new Configuration(util.getConfiguration());
228
229
230 FileSystem fs = FileSystem.get(conf);
231 Path inputPath = fs.makeQualified(new Path(util.getDataTestDirOnTestFS(table), "input.dat"));
232 FSDataOutputStream op = fs.create(inputPath, true);
233 if (data == null) {
234 data = "KEY\u001bVALUE1\u001bVALUE2\n";
235 }
236 op.write(Bytes.toBytes(data));
237 op.close();
238 LOG.debug(String.format("Wrote test data to file: %s", inputPath));
239
240 if (conf.getBoolean(FORCE_COMBINER_CONF, true)) {
241 LOG.debug("Forcing combiner.");
242 conf.setInt("min.num.spills.for.combine", 1);
243 }
244
245
246 List<String> argv = new ArrayList<String>(Arrays.asList(args));
247 argv.add(inputPath.toString());
248 Tool tool = new ImportTsv();
249 LOG.debug("Running ImportTsv with arguments: " + argv);
250 assertEquals(0, ToolRunner.run(conf, tool, argv.toArray(args)));
251
252
253
254
255 boolean createdHFiles = false;
256 String outputPath = null;
257 for (String arg : argv) {
258 if (arg.contains(ImportTsv.BULK_OUTPUT_CONF_KEY)) {
259 createdHFiles = true;
260
261 outputPath = arg.split("=")[1];
262 break;
263 }
264 }
265 LOG.debug("validating the table " + createdHFiles);
266 if (createdHFiles)
267 validateHFiles(fs, outputPath, family);
268 else
269 validateTable(conf, table, family, valueMultiplier);
270
271 if (conf.getBoolean(DELETE_AFTER_LOAD_CONF, true)) {
272 LOG.debug("Deleting test subdirectory");
273 util.cleanupDataTestDirOnTestFS(table);
274 }
275 return tool;
276 }
277
278
279
280
281 private static void validateHFiles(FileSystem fs, String outputPath, String family)
282 throws IOException {
283
284
285 LOG.debug("Validating HFiles.");
286 Set<String> configFamilies = new HashSet<String>();
287 configFamilies.add(family);
288 Set<String> foundFamilies = new HashSet<String>();
289 for (FileStatus cfStatus : fs.listStatus(new Path(outputPath), new OutputFilesFilter())) {
290 LOG.debug("The output path has files");
291 String[] elements = cfStatus.getPath().toString().split(Path.SEPARATOR);
292 String cf = elements[elements.length - 1];
293 foundFamilies.add(cf);
294 assertTrue(String.format(
295 "HFile ouput contains a column family (%s) not present in input families (%s)", cf,
296 configFamilies), configFamilies.contains(cf));
297 for (FileStatus hfile : fs.listStatus(cfStatus.getPath())) {
298 assertTrue(String.format("HFile %s appears to contain no data.", hfile.getPath()),
299 hfile.getLen() > 0);
300 }
301 }
302 }
303
304
305
306
307 private static void validateTable(Configuration conf, String tableName, String family,
308 int valueMultiplier) throws IOException {
309
310 LOG.debug("Validating table.");
311 HTable table = new HTable(conf, tableName);
312 boolean verified = false;
313 long pause = conf.getLong("hbase.client.pause", 5 * 1000);
314 int numRetries = conf.getInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 5);
315 for (int i = 0; i < numRetries; i++) {
316 try {
317 Scan scan = new Scan();
318
319 scan.addFamily(Bytes.toBytes(family));
320 scan.setAuthorizations(new Authorizations("secret","private"));
321 ResultScanner resScanner = table.getScanner(scan);
322 Result[] next = resScanner.next(5);
323 assertEquals(1, next.length);
324 for (Result res : resScanner) {
325 LOG.debug("Getting results " + res.size());
326 assertTrue(res.size() == 2);
327 List<Cell> kvs = res.listCells();
328 assertTrue(CellUtil.matchingRow(kvs.get(0), Bytes.toBytes("KEY")));
329 assertTrue(CellUtil.matchingRow(kvs.get(1), Bytes.toBytes("KEY")));
330 assertTrue(CellUtil.matchingValue(kvs.get(0), Bytes.toBytes("VALUE" + valueMultiplier)));
331 assertTrue(CellUtil.matchingValue(kvs.get(1),
332 Bytes.toBytes("VALUE" + 2 * valueMultiplier)));
333
334 }
335 verified = true;
336 break;
337 } catch (NullPointerException e) {
338
339
340 }
341 try {
342 Thread.sleep(pause);
343 } catch (InterruptedException e) {
344
345 }
346 }
347 table.close();
348 assertTrue(verified);
349 }
350
351 }