hbase-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jya...@apache.org
Subject svn commit: r1492518 - in /hbase/branches/0.94/src: main/java/org/apache/hadoop/hbase/regionserver/wal/ main/java/org/apache/hadoop/hbase/replication/regionserver/ test/java/org/apache/hadoop/hbase/regionserver/wal/
Date Thu, 13 Jun 2013 02:51:22 GMT
Author: jyates
Date: Thu Jun 13 02:51:22 2013
New Revision: 1492518

URL: http://svn.apache.org/r1492518
Log:
HBASE-8702: Make WALEditCodec pluggable

Added:
    hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/regionserver/wal/TestCustomWALEditCodec.java
Modified:
    hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/regionserver/wal/SequenceFileLogReader.java
    hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/regionserver/wal/SequenceFileLogWriter.java
    hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/regionserver/wal/WALEdit.java
    hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/regionserver/wal/WALEditCodec.java
    hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/replication/regionserver/ReplicationHLogReaderManager.java
    hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/regionserver/wal/FaultySequenceFileLogReader.java

Modified: hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/regionserver/wal/SequenceFileLogReader.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/regionserver/wal/SequenceFileLogReader.java?rev=1492518&r1=1492517&r2=1492518&view=diff
==============================================================================
--- hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/regionserver/wal/SequenceFileLogReader.java
(original)
+++ hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/regionserver/wal/SequenceFileLogReader.java
Thu Jun 13 02:51:22 2013
@@ -194,7 +194,7 @@ public class SequenceFileLogReader imple
     }
 
     // setup the codec
-    this.codec = new WALEditCodec(compressionContext);
+    this.codec = WALEditCodec.create(conf, compressionContext);
   }
 
   @Override

Modified: hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/regionserver/wal/SequenceFileLogWriter.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/regionserver/wal/SequenceFileLogWriter.java?rev=1492518&r1=1492517&r2=1492518&view=diff
==============================================================================
--- hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/regionserver/wal/SequenceFileLogWriter.java
(original)
+++ hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/regionserver/wal/SequenceFileLogWriter.java
Thu Jun 13 02:51:22 2013
@@ -195,7 +195,8 @@ public class SequenceFileLogWriter imple
       LOG.debug("using new createWriter -- HADOOP-6840");
     }
 
-    this.codec = new WALEditCodec(compressionContext);
+    // setup the WALEditCodec
+    this.codec = WALEditCodec.create(conf, compressionContext);
     this.writer_out = getSequenceFilePrivateFSDataOutputStreamAccessible();
     this.syncFs = getSyncFs();
     this.hflush = getHFlush();

Modified: hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/regionserver/wal/WALEdit.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/regionserver/wal/WALEdit.java?rev=1492518&r1=1492517&r2=1492518&view=diff
==============================================================================
--- hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/regionserver/wal/WALEdit.java
(original)
+++ hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/regionserver/wal/WALEdit.java
Thu Jun 13 02:51:22 2013
@@ -81,7 +81,7 @@ public class WALEdit implements Writable
   // default to decoding uncompressed data - needed for replication, which enforces that
   // uncompressed edits are sent across the wire. In the regular case (reading/writing WAL),
the
   // codec will be setup by the reader/writer class, not here.
-  private WALEditCodec codec = new WALEditCodec(null);
+  private WALEditCodec codec = new WALEditCodec();
 
   public WALEdit() {
   }

Modified: hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/regionserver/wal/WALEditCodec.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/regionserver/wal/WALEditCodec.java?rev=1492518&r1=1492517&r2=1492518&view=diff
==============================================================================
--- hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/regionserver/wal/WALEditCodec.java
(original)
+++ hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/regionserver/wal/WALEditCodec.java
Thu Jun 13 02:51:22 2013
@@ -19,9 +19,11 @@ package org.apache.hadoop.hbase.regionse
 
 import java.io.DataInputStream;
 import java.io.DataOutputStream;
+import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 
+import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.hbase.codec.Codec;
 import org.apache.hadoop.hbase.codec.Decoder;
 import org.apache.hadoop.hbase.codec.Encoder;
@@ -29,10 +31,27 @@ import org.apache.hadoop.hbase.codec.Key
 import org.apache.hadoop.hbase.regionserver.wal.KeyValueCompression.CompressedKvEncoder;
 
 public class WALEditCodec implements Codec {
+  /** Configuration key for a custom class to use when serializing the WALEdits to the HLog
*/
+  public static final String WAL_EDIT_CODEC_CLASS_KEY = "hbase.regionserver.wal.codec";
+
   private CompressionContext compression;
 
-  public WALEditCodec(CompressionContext compression) {
-    this.compression = compression;
+  /**
+   * Nullary Constructor - all subclass must support this to load from configuration. Setup
can be
+   * completed in the {@link #init} method.
+   * <p>
+   * This implementation defaults to having no compression on the resulting {@link Encoder}/
+   * {@link Decoder}, though it can be added via {@link #setCompression(CompressionContext)}
+   */
+  public WALEditCodec() {
+  }
+
+  /**
+   * Initialize <tt>this</tt> - called exactly once after the object is instantiated
and before any
+   * other method in this class. By default, does nothing.
+   * @param conf {@link Configuration} from which to configure <tt>this</tt>
+   */
+  public void init(Configuration conf) {
   }
 
   public void setCompression(CompressionContext compression) {
@@ -52,4 +71,29 @@ public class WALEditCodec implements Cod
         (compression == null) ? new KeyValueCodec.KeyValueEncoder((DataOutputStream) os)
         : new CompressedKvEncoder((DataOutputStream) os, compression);
   }
+
+  /**
+   * Create and setup a {@link WALEditCodec} from the {@link Configuration}, if one has been
+   * specified. Fully prepares the codec for use in serialization.
+   * @param conf {@link Configuration} to read for the user-specified codec. If none is specified,
+   *          uses a {@link WALEditCodec}.
+   * @param compressionContext compression to setup on the codec.
+   * @return a {@link WALEditCodec} ready for use.
+   * @throws IOException if the codec cannot be created
+   */
+  public static WALEditCodec create(Configuration conf, CompressionContext compressionContext)
+      throws IOException {
+    Class<? extends WALEditCodec> codecClazz = conf.getClass(WALEditCodec.WAL_EDIT_CODEC_CLASS_KEY,
+      WALEditCodec.class, WALEditCodec.class);
+    try {
+      WALEditCodec codec = codecClazz.newInstance();
+      codec.init(conf);
+      codec.setCompression(compressionContext);
+      return codec;
+    } catch (InstantiationException e) {
+      throw new IOException("Couldn't instantiate the configured WALEditCodec!", e);
+    } catch (IllegalAccessException e) {
+      throw new IOException("Couldn't instantiate the configured WALEditCodec!", e);
+    }
+  }
 }
\ No newline at end of file

Modified: hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/replication/regionserver/ReplicationHLogReaderManager.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/replication/regionserver/ReplicationHLogReaderManager.java?rev=1492518&r1=1492517&r2=1492518&view=diff
==============================================================================
--- hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/replication/regionserver/ReplicationHLogReaderManager.java
(original)
+++ hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/replication/regionserver/ReplicationHLogReaderManager.java
Thu Jun 13 02:51:22 2013
@@ -37,7 +37,7 @@ import java.io.IOException;
 public class ReplicationHLogReaderManager {
 
   private static final Log LOG = LogFactory.getLog(ReplicationHLogReaderManager.class);
-  private final WALEditCodec nonCompressingCodec = new WALEditCodec(null);
+  private final WALEditCodec nonCompressingCodec = new WALEditCodec();
   private final FileSystem fs;
   private final Configuration conf;
   private long position = 0;

Modified: hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/regionserver/wal/FaultySequenceFileLogReader.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/regionserver/wal/FaultySequenceFileLogReader.java?rev=1492518&r1=1492517&r2=1492518&view=diff
==============================================================================
--- hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/regionserver/wal/FaultySequenceFileLogReader.java
(original)
+++ hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/regionserver/wal/FaultySequenceFileLogReader.java
Thu Jun 13 02:51:22 2013
@@ -39,6 +39,8 @@ public class FaultySequenceFileLogReader
     return FailureType.valueOf(conf.get("faultysequencefilelogreader.failuretype", "NONE"));
   }
 
+  WALEditCodec codec = new WALEditCodec();
+
   @Override
   public HLog.Entry next(HLog.Entry reuse) throws IOException {
     this.entryStart = this.reader.getPosition();
@@ -49,7 +51,8 @@ public class FaultySequenceFileLogReader
         HLogKey key = HLog.newKey(conf);
         WALEdit val = new WALEdit();
         HLog.Entry e = new HLog.Entry(key, val);
-        e.getEdit().setCodec(new WALEditCodec(compressionContext));
+        codec.setCompression(compressionContext);
+        e.getEdit().setCodec(codec);
         if (compressionContext != null) {
           e.getKey().setCompressionContext(compressionContext);
         }

Added: hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/regionserver/wal/TestCustomWALEditCodec.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/regionserver/wal/TestCustomWALEditCodec.java?rev=1492518&view=auto
==============================================================================
--- hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/regionserver/wal/TestCustomWALEditCodec.java
(added)
+++ hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/regionserver/wal/TestCustomWALEditCodec.java
Thu Jun 13 02:51:22 2013
@@ -0,0 +1,61 @@
+/**
+ * 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.assertTrue;
+
+import org.apache.hadoop.conf.Configuration;
+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 TestCustomWALEditCodec {
+
+  public static class CustomWALEditCodec extends WALEditCodec {
+    public boolean initialized = false;
+    public boolean compressionSet = false;
+
+    @Override
+    public void init(Configuration conf) {
+      this.initialized = true;
+    }
+
+    @Override
+    public void setCompression(CompressionContext compression) {
+      this.compressionSet = true;
+    }
+  }
+
+  /**
+   * Test that a custom WALEditCodec will be completely setup when it is instantiated via
+   * {@link WALEditCodec}
+   * @throws Exception on failure
+   */
+  @Test
+  public void testCreatePreparesCodec() throws Exception {
+    Configuration conf = new Configuration(false);
+    conf.setClass(WALEditCodec.WAL_EDIT_CODEC_CLASS_KEY, CustomWALEditCodec.class, WALEditCodec.class);
+    CustomWALEditCodec codec = (CustomWALEditCodec) WALEditCodec.create(conf, null);
+    assertTrue("Custom codec didn't get initialized", codec.initialized);
+    assertTrue("Custom codec didn't have compression set", codec.compressionSet);
+  }
+}
\ No newline at end of file



Mime
View raw message