View Javadoc

1   /**
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
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   * Compression validation test.  Checks compression is working.  Be sure to run
37   * on every node in your cluster.
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 ; // already passed test, dont do it again.
76        } else {
77          // failed.
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; // passes
88      } catch (Throwable t) {
89        compressionTestResults[algo.ordinal()] = false; // failure
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 }