Return-Path: X-Original-To: apmail-hbase-commits-archive@www.apache.org Delivered-To: apmail-hbase-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 7EFD310233 for ; Wed, 12 Jun 2013 20:41:36 +0000 (UTC) Received: (qmail 65130 invoked by uid 500); 12 Jun 2013 20:41:36 -0000 Delivered-To: apmail-hbase-commits-archive@hbase.apache.org Received: (qmail 65104 invoked by uid 500); 12 Jun 2013 20:41:36 -0000 Mailing-List: contact commits-help@hbase.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@hbase.apache.org Delivered-To: mailing list commits@hbase.apache.org Received: (qmail 65097 invoked by uid 99); 12 Jun 2013 20:41:36 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 12 Jun 2013 20:41:36 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 12 Jun 2013 20:41:33 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 5E00E23889FA; Wed, 12 Jun 2013 20:41:12 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1492414 - in /hbase/branches/0.95/hbase-server/src: main/java/org/apache/hadoop/hbase/regionserver/wal/ test/java/org/apache/hadoop/hbase/regionserver/wal/ Date: Wed, 12 Jun 2013 20:41:12 -0000 To: commits@hbase.apache.org From: jyates@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20130612204112.5E00E23889FA@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: jyates Date: Wed Jun 12 20:41:11 2013 New Revision: 1492414 URL: http://svn.apache.org/r1492414 Log: HBASE-8702: Make WALEditCodec pluggable Added: hbase/branches/0.95/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/wal/TestCustomWALCellCodec.java Modified: hbase/branches/0.95/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/wal/ProtobufLogReader.java hbase/branches/0.95/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/wal/ProtobufLogWriter.java hbase/branches/0.95/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/wal/WALCellCodec.java Modified: hbase/branches/0.95/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/wal/ProtobufLogReader.java URL: http://svn.apache.org/viewvc/hbase/branches/0.95/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/wal/ProtobufLogReader.java?rev=1492414&r1=1492413&r2=1492414&view=diff ============================================================================== --- hbase/branches/0.95/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/wal/ProtobufLogReader.java (original) +++ hbase/branches/0.95/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/wal/ProtobufLogReader.java Wed Jun 12 20:41:11 2013 @@ -175,7 +175,7 @@ public class ProtobufLogReader extends R @Override protected void initAfterCompression() throws IOException { - WALCellCodec codec = new WALCellCodec(this.compressionContext); + WALCellCodec codec = WALCellCodec.create(this.conf, this.compressionContext); this.cellDecoder = codec.getDecoder(this.inputStream); if (this.hasCompression) { this.byteStringUncompressor = codec.getByteStringUncompressor(); Modified: hbase/branches/0.95/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/wal/ProtobufLogWriter.java URL: http://svn.apache.org/viewvc/hbase/branches/0.95/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/wal/ProtobufLogWriter.java?rev=1492414&r1=1492413&r2=1492414&view=diff ============================================================================== --- hbase/branches/0.95/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/wal/ProtobufLogWriter.java (original) +++ hbase/branches/0.95/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/wal/ProtobufLogWriter.java Wed Jun 12 20:41:11 2013 @@ -80,7 +80,7 @@ public class ProtobufLogWriter implement output.write(ProtobufLogReader.PB_WAL_MAGIC); WALHeader.newBuilder().setHasCompression(doCompress).build().writeDelimitedTo(output); - WALCellCodec codec = new WALCellCodec(this.compressionContext); + WALCellCodec codec = WALCellCodec.create(conf, this.compressionContext); this.cellEncoder = codec.getEncoder(this.output); if (doCompress) { this.compressor = codec.getByteStringCompressor(); Modified: hbase/branches/0.95/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/wal/WALCellCodec.java URL: http://svn.apache.org/viewvc/hbase/branches/0.95/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/wal/WALCellCodec.java?rev=1492414&r1=1492413&r2=1492414&view=diff ============================================================================== --- hbase/branches/0.95/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/wal/WALCellCodec.java (original) +++ hbase/branches/0.95/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/wal/WALCellCodec.java Wed Jun 12 20:41:11 2013 @@ -22,6 +22,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.KeyValue; import org.apache.hadoop.hbase.codec.BaseDecoder; @@ -29,6 +30,7 @@ import org.apache.hadoop.hbase.codec.Bas import org.apache.hadoop.hbase.codec.Codec; import org.apache.hadoop.hbase.codec.KeyValueCodec; import org.apache.hadoop.hbase.util.Bytes; +import org.apache.hadoop.hbase.util.ReflectionUtils; import com.google.common.base.Preconditions; import com.google.protobuf.ByteString; @@ -39,6 +41,9 @@ import com.google.protobuf.ByteString; * This is a pure coincidence... they are independent and don't have to be compatible. */ public class WALCellCodec implements Codec { + /** Configuration key for the class to use when encoding cells in the WAL */ + public static final String WAL_CELL_CODEC_CLASS_KEY = "hbase.regionserver.wal.codec"; + private final CompressionContext compression; private final ByteStringUncompressor statelessUncompressor = new ByteStringUncompressor() { @Override @@ -47,10 +52,33 @@ public class WALCellCodec implements Cod } }; - public WALCellCodec(CompressionContext compression) { + /** + * Default constructor - all subclasses must implement a constructor with this signature + * if they are to be dynamically loaded from the {@link Configuration}. + * @param conf configuration to configure this + * @param compression compression the codec should support, can be null to indicate no + * compression + */ + public WALCellCodec(Configuration conf, CompressionContext compression) { this.compression = compression; } + /** + * Create and setup a {@link WALCellCodec} from the {@link Configuration} and CompressionContext, + * if they have been specified. Fully prepares the codec for use. + * @param conf {@link Configuration} to read for the user-specified codec. If none is specified, + * uses a {@link WALCellCodec}. + * @param compression compression the codec should use + * @return a {@link WALCellCodec} ready for use. + * @throws UnsupportedOperationException if the codec cannot be instantiated + */ + public static WALCellCodec create(Configuration conf, CompressionContext compression) + throws UnsupportedOperationException { + String className = conf.get(WAL_CELL_CODEC_CLASS_KEY, WALCellCodec.class.getName()); + return ReflectionUtils.instantiateWithCustomCtor(className, new Class[] { Configuration.class, + CompressionContext.class }, new Object[] { conf, compression }); + } + public interface ByteStringCompressor { ByteString compress(byte[] data, Dictionary dict) throws IOException; } Added: hbase/branches/0.95/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/wal/TestCustomWALCellCodec.java URL: http://svn.apache.org/viewvc/hbase/branches/0.95/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/wal/TestCustomWALCellCodec.java?rev=1492414&view=auto ============================================================================== --- hbase/branches/0.95/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/wal/TestCustomWALCellCodec.java (added) +++ hbase/branches/0.95/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/wal/TestCustomWALCellCodec.java Wed Jun 12 20:41:11 2013 @@ -0,0 +1,62 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF 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. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hbase.regionserver.wal; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.HConstants; +import org.apache.hadoop.hbase.SmallTests; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +/** + * Test that we can create, load, setup our own custom codec + */ +@Category(SmallTests.class) +public class TestCustomWALCellCodec { + + public static class CustomWALCellCodec extends WALCellCodec { + public Configuration conf; + public CompressionContext context; + + public CustomWALCellCodec(Configuration conf, CompressionContext compression) { + super(conf, compression); + this.conf = conf; + this.context = compression; + } + } + + /** + * Test that a custom {@link WALCellCodec} will be completely setup when it is instantiated via + * {@link WALCellCodec} + * @throws Exception on failure + */ + @Test + public void testCreatePreparesCodec() throws Exception { + Configuration conf = new Configuration(false); + conf.setClass(WALCellCodec.WAL_CELL_CODEC_CLASS_KEY, CustomWALCellCodec.class, + WALCellCodec.class); + CustomWALCellCodec codec = (CustomWALCellCodec) WALCellCodec.create(conf, null); + assertEquals("Custom codec didn't get initialized with the right configuration!", conf, + codec.conf); + assertEquals("Custom codec didn't get initialized with the right compression context!", null, + codec.context); + } +} \ No newline at end of file