HADOOP-15612. Improve exception when tfile fails to load LzoCodec. (gera)

This commit is contained in:
Gera Shegalov 2018-07-17 00:05:39 -07:00
parent ea2c6c8c9a
commit 6bec03cfc8
2 changed files with 53 additions and 12 deletions

View File

@ -5,9 +5,9 @@
* licenses this file to you under the Apache License, Version 2.0 (the * licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License. * "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
@ -24,6 +24,7 @@ import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import com.google.common.annotations.VisibleForTesting;
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.compress.CodecPool; import org.apache.hadoop.io.compress.CodecPool;
import org.apache.hadoop.io.compress.CompressionCodec; import org.apache.hadoop.io.compress.CompressionCodec;
@ -78,25 +79,33 @@ public final class Compression {
public enum Algorithm { public enum Algorithm {
LZO(TFile.COMPRESSION_LZO) { LZO(TFile.COMPRESSION_LZO) {
private transient boolean checked = false; private transient boolean checked = false;
private transient ClassNotFoundException cnf;
private transient boolean reinitCodecInTests;
private static final String defaultClazz = private static final String defaultClazz =
"org.apache.hadoop.io.compress.LzoCodec"; "org.apache.hadoop.io.compress.LzoCodec";
private transient String clazz;
private transient CompressionCodec codec = null; private transient CompressionCodec codec = null;
private String getLzoCodecClass() {
String extClazzConf = conf.get(CONF_LZO_CLASS);
String extClazz = (extClazzConf != null) ?
extClazzConf : System.getProperty(CONF_LZO_CLASS);
return (extClazz != null) ? extClazz : defaultClazz;
}
@Override @Override
public synchronized boolean isSupported() { public synchronized boolean isSupported() {
if (!checked) { if (!checked || reinitCodecInTests) {
checked = true; checked = true;
String extClazzConf = conf.get(CONF_LZO_CLASS); reinitCodecInTests = conf.getBoolean("test.reload.lzo.codec", false);
String extClazz = (extClazzConf != null) ? clazz = getLzoCodecClass();
extClazzConf : System.getProperty(CONF_LZO_CLASS);
String clazz = (extClazz != null) ? extClazz : defaultClazz;
try { try {
LOG.info("Trying to load Lzo codec class: " + clazz); LOG.info("Trying to load Lzo codec class: " + clazz);
codec = codec =
(CompressionCodec) ReflectionUtils.newInstance(Class (CompressionCodec) ReflectionUtils.newInstance(Class
.forName(clazz), conf); .forName(clazz), conf);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
// that is okay cnf = e;
} }
} }
return codec != null; return codec != null;
@ -105,9 +114,9 @@ public final class Compression {
@Override @Override
CompressionCodec getCodec() throws IOException { CompressionCodec getCodec() throws IOException {
if (!isSupported()) { if (!isSupported()) {
throw new IOException( throw new IOException(String.format(
"LZO codec class not specified. Did you forget to set property " "LZO codec %s=%s could not be loaded", CONF_LZO_CLASS, clazz),
+ CONF_LZO_CLASS + "?"); cnf);
} }
return codec; return codec;

View File

@ -17,14 +17,28 @@
*/ */
package org.apache.hadoop.io.file.tfile; package org.apache.hadoop.io.file.tfile;
import org.junit.Test; import org.apache.hadoop.io.compress.CompressionCodec;
import org.apache.hadoop.test.LambdaTestUtils;
import org.junit.*;
import java.io.IOException; import java.io.IOException;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
public class TestCompression { public class TestCompression {
@BeforeClass
public static void resetConfigBeforeAll() {
Compression.Algorithm.LZO.conf.setBoolean("test.reload.lzo.codec", true);
}
@AfterClass
public static void resetConfigAfterAll() {
Compression.Algorithm.LZO.conf.setBoolean("test.reload.lzo.codec", false);
}
/** /**
* Regression test for HADOOP-11418. * Regression test for HADOOP-11418.
* Verify we can set a LZO codec different from default LZO codec. * Verify we can set a LZO codec different from default LZO codec.
@ -38,4 +52,22 @@ public class TestCompression {
assertEquals(defaultCodec, assertEquals(defaultCodec,
Compression.Algorithm.LZO.getCodec().getClass().getName()); Compression.Algorithm.LZO.getCodec().getClass().getName());
} }
@Test
public void testMisconfiguredLZOCodec() throws Exception {
// Dummy codec
String defaultCodec = "org.apache.hadoop.io.compress.InvalidLzoCodec";
Compression.Algorithm.conf.set(
Compression.Algorithm.CONF_LZO_CLASS, defaultCodec);
IOException ioEx = LambdaTestUtils.intercept(
IOException.class,
defaultCodec,
() -> Compression.Algorithm.LZO.getCodec());
if (!(ioEx.getCause() instanceof ClassNotFoundException)) {
throw ioEx;
}
}
} }