1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.util;
21
22 import java.io.IOException;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.hadoop.conf.Configuration;
27 import org.apache.hadoop.fs.FileSystem;
28 import org.apache.hadoop.fs.Path;
29 import org.apache.hadoop.hbase.HBaseConfiguration;
30 import org.apache.hadoop.hbase.io.hfile.CacheConfig;
31 import org.apache.hadoop.hbase.io.hfile.Compression;
32 import org.apache.hadoop.hbase.io.hfile.HFile;
33 import org.apache.hadoop.io.compress.Compressor;
34
35
36
37
38
39 public class CompressionTest {
40 static final Log LOG = LogFactory.getLog(CompressionTest.class);
41
42 public static boolean testCompression(String codec) {
43 codec = codec.toLowerCase();
44
45 Compression.Algorithm a;
46
47 try {
48 a = Compression.getCompressionAlgorithmByName(codec);
49 } catch (IllegalArgumentException e) {
50 LOG.warn("Codec type: " + codec + " is not known");
51 return false;
52 }
53
54 try {
55 testCompression(a);
56 return true;
57 } catch (IOException ignored) {
58 LOG.warn("Can't instantiate codec: " + codec, ignored);
59 return false;
60 }
61 }
62
63 private final static Boolean[] compressionTestResults
64 = new Boolean[Compression.Algorithm.values().length];
65 static {
66 for (int i = 0 ; i < compressionTestResults.length ; ++i) {
67 compressionTestResults[i] = null;
68 }
69 }
70
71 public static void testCompression(Compression.Algorithm algo)
72 throws IOException {
73 if (compressionTestResults[algo.ordinal()] != null) {
74 if (compressionTestResults[algo.ordinal()]) {
75 return ;
76 } else {
77
78 throw new IOException("Compression algorithm '" + algo.getName() + "'" +
79 " previously failed test.");
80 }
81 }
82
83 Configuration conf = HBaseConfiguration.create();
84 try {
85 Compressor c = algo.getCompressor();
86 algo.returnCompressor(c);
87 compressionTestResults[algo.ordinal()] = true;
88 } catch (Throwable t) {
89 compressionTestResults[algo.ordinal()] = false;
90 throw new IOException(t);
91 }
92 }
93
94 protected static Path path = new Path(".hfile-comp-test");
95
96 public static void usage() {
97 System.err.println(
98 "Usage: CompressionTest <path> none|gz|lzo|snappy\n" +
99 "\n" +
100 "For example:\n" +
101 " hbase " + CompressionTest.class + " file:///tmp/testfile gz\n");
102 System.exit(1);
103 }
104
105 public static void doSmokeTest(FileSystem fs, Path path, String codec)
106 throws Exception {
107 Configuration conf = HBaseConfiguration.create();
108 HFile.Writer writer = HFile.getWriterFactoryNoCache(conf)
109 .withPath(fs, path)
110 .withCompression(codec)
111 .create();
112 writer.append(Bytes.toBytes("testkey"), Bytes.toBytes("testval"));
113 writer.appendFileInfo(Bytes.toBytes("infokey"), Bytes.toBytes("infoval"));
114 writer.close();
115
116 HFile.Reader reader = HFile.createReader(fs, path, new CacheConfig(conf));
117 reader.loadFileInfo();
118 byte[] key = reader.getFirstKey();
119 boolean rc = Bytes.toString(key).equals("testkey");
120 reader.close();
121
122 if (!rc) {
123 throw new Exception("Read back incorrect result: " +
124 Bytes.toStringBinary(key));
125 }
126 }
127
128 public static void main(String[] args) throws Exception {
129 if (args.length != 2) {
130 usage();
131 System.exit(1);
132 }
133
134 Configuration conf = new Configuration();
135 Path path = new Path(args[0]);
136 FileSystem fs = path.getFileSystem(conf);
137 try {
138 doSmokeTest(fs, path, args[1]);
139 } finally {
140 fs.delete(path, false);
141 }
142 System.out.println("SUCCESS");
143 }
144 }