hbase-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jxi...@apache.org
Subject svn commit: r1556027 - in /hbase/branches/0.94/src: main/java/org/apache/hadoop/hbase/filter/ main/java/org/apache/hadoop/hbase/io/ main/java/org/apache/hadoop/hbase/util/ test/java/org/apache/hadoop/hbase/client/
Date Mon, 06 Jan 2014 20:48:43 GMT
Author: jxiang
Date: Mon Jan  6 20:48:43 2014
New Revision: 1556027

URL: http://svn.apache.org/r1556027
Log:
HBASE-10078 Dynamic Filter - Not using DynamicClassLoader when using FilterList

Modified:
    hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/filter/FilterList.java
    hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/io/HbaseObjectWritable.java
    hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/util/Classes.java
    hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/client/TestGet.java

Modified: hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/filter/FilterList.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/filter/FilterList.java?rev=1556027&r1=1556026&r2=1556027&view=diff
==============================================================================
--- hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/filter/FilterList.java (original)
+++ hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/filter/FilterList.java Mon Jan
 6 20:48:43 2014
@@ -321,7 +321,7 @@ public class FilterList implements Filte
     if (size > 0) {
       filters = new ArrayList<Filter>(size);
       for (int i = 0; i < size; i++) {
-        Filter filter = (Filter)HbaseObjectWritable.readObject(in, conf);
+        Filter filter = HbaseObjectWritable.readFilter(in, conf);
         filters.add(filter);
       }
     }

Modified: hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/io/HbaseObjectWritable.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/io/HbaseObjectWritable.java?rev=1556027&r1=1556026&r2=1556027&view=diff
==============================================================================
--- hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/io/HbaseObjectWritable.java
(original)
+++ hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/io/HbaseObjectWritable.java
Mon Jan  6 20:48:43 2014
@@ -72,6 +72,7 @@ import org.apache.hadoop.hbase.filter.Co
 import org.apache.hadoop.hbase.filter.CompareFilter;
 import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
 import org.apache.hadoop.hbase.filter.DependentColumnFilter;
+import org.apache.hadoop.hbase.filter.Filter;
 import org.apache.hadoop.hbase.filter.FirstKeyOnlyFilter;
 import org.apache.hadoop.hbase.filter.FuzzyRowFilter;
 import org.apache.hadoop.hbase.filter.InclusiveStopFilter;
@@ -93,6 +94,7 @@ import org.apache.hadoop.hbase.regionser
 import org.apache.hadoop.hbase.regionserver.wal.HLogKey;
 import org.apache.hadoop.hbase.snapshot.HSnapshotDescription;
 import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.hadoop.hbase.util.Classes;
 import org.apache.hadoop.hbase.util.ProtoUtil;
 import org.apache.hadoop.io.MapWritable;
 import org.apache.hadoop.io.ObjectWritable;
@@ -729,6 +731,58 @@ public class HbaseObjectWritable impleme
   }
 
   /**
+   * Read a {@link Filter} which is written as a {@link Writable}
+   * or a {@link Filter} directly. A custom filter class may be loaded
+   * dynamically.
+   *
+   * @param in
+   * @param conf
+   * @return the filter
+   * @throws IOException
+   */
+  @SuppressWarnings("unchecked")
+  public static Filter readFilter(
+      DataInput in, Configuration conf) throws IOException {
+    Class<?> instanceClass = null;
+    int b = (byte)WritableUtils.readVInt(in);
+    if (b != NOT_ENCODED) {
+      instanceClass = CODE_TO_CLASS.get(b);
+      if (instanceClass == Writable.class) {
+        // In case Writable, the actual type code follows
+        b = (byte)WritableUtils.readVInt(in);
+        if (b != NOT_ENCODED) {
+          instanceClass = CODE_TO_CLASS.get(b);
+        }
+      }
+    }
+    if (b == NOT_ENCODED) {
+      String className = Text.readString(in);
+      try {
+        instanceClass = (Class<? extends Filter>)getClassByName(conf, className);
+      } catch (ClassNotFoundException cnfe) {
+        try {
+          instanceClass = Classes.getFilterClassByName(className);
+        } catch (ClassNotFoundException e) {
+          LOG.error("Can't find class " + className, e);
+          throw new DoNotRetryIOException("Can't find class " + className, e);
+        }
+      }
+    }
+    Filter filter = (Filter)WritableFactories.newInstance(
+      (Class<? extends Filter>)instanceClass, conf);
+    try {
+      filter.readFields(in);
+    } catch (IOException io) {
+      LOG.error("Error in readFields", io);
+      throw io;
+    } catch (Exception e) {
+      LOG.error("Error in readFields", e);
+      throw new IOException("Error in readFields" , e);
+    }
+    return filter;
+  }
+
+  /**
    * Try to instantiate a protocol buffer of the given message class
    * from the given input stream.
    *

Modified: hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/util/Classes.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/util/Classes.java?rev=1556027&r1=1556026&r2=1556027&view=diff
==============================================================================
--- hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/util/Classes.java (original)
+++ hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/util/Classes.java Mon Jan  6
20:48:43 2014
@@ -94,17 +94,28 @@ public class Classes {
   }
 
   /**
+   * Used to dynamically load a filter class.
+   *
+   * @param className the filter class name
+   * @return a filter class
+   * @throws ClassNotFoundException if couldn't find the class
+   */
+  @SuppressWarnings("unchecked")
+  public static Class<? extends Filter> getFilterClassByName(
+      String className) throws ClassNotFoundException {
+    return (Class<? extends Filter>) Class.forName(className, true, CLASS_LOADER);
+  }
+
+  /**
    * Used to dynamically load a filter class, and create a Writable filter.
    * This filter class most likely extends Configurable.
    *
    * @param className the filter class name.
    * @return a filter
    */
-  @SuppressWarnings("unchecked")
   public static Filter createWritableForName(String className) {
     try {
-      Class<? extends Filter> clazz =
-        (Class<? extends Filter>) Class.forName(className, true, CLASS_LOADER);
+      Class<? extends Filter> clazz = getFilterClassByName(className);
       return (Filter)WritableFactories.newInstance(clazz, new Configuration());
     } catch (ClassNotFoundException e) {
       throw new RuntimeException("Can't find class " + className);
@@ -118,11 +129,9 @@ public class Classes {
    * @param className the filter class name.
    * @return a filter
    */
-  @SuppressWarnings("unchecked")
   public static Filter createForName(String className) {
     try {
-      Class<? extends Filter> clazz =
-        (Class<? extends Filter>)Class.forName(className, true, CLASS_LOADER);
+      Class<? extends Filter> clazz = getFilterClassByName(className);
       return (Filter)clazz.newInstance();
     } catch (ClassNotFoundException e) {
       throw new RuntimeException("Can't find class " + className);

Modified: hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/client/TestGet.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/client/TestGet.java?rev=1556027&r1=1556026&r2=1556027&view=diff
==============================================================================
--- hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/client/TestGet.java (original)
+++ hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/client/TestGet.java Mon Jan
 6 20:48:43 2014
@@ -20,7 +20,9 @@
 
 package org.apache.hadoop.hbase.client;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
 import java.io.ByteArrayInputStream;
@@ -33,10 +35,14 @@ import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.util.Arrays;
+import java.util.List;
 
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.hbase.HBaseConfiguration;
 import org.apache.hadoop.hbase.SmallTests;
+import org.apache.hadoop.hbase.filter.Filter;
+import org.apache.hadoop.hbase.filter.FilterList;
+import org.apache.hadoop.hbase.filter.KeyOnlyFilter;
 import org.apache.hadoop.hbase.util.Base64;
 import org.apache.hadoop.hbase.util.Bytes;
 import org.junit.Assert;
@@ -52,23 +58,35 @@ public class TestGet {
   private static final String WRITABLE_GET =
     "AgD//////////wAAAAEBD3Rlc3QuTW9ja0ZpbHRlcgEAAAAAAAAAAH//////////AQAAAAAAAAAA";
 
+  private static final String WRITABLE_GET_WITH_FILTER_LIST =
+    "AgD//////////wAAAAEBKW9yZy5hcGFjaGUuaGFkb29wLmhiYXNlLmZpbHRlci5GaWx0ZXJMaXN0" +
+    "AAAAAAMOAA90ZXN0Lk1vY2tGaWx0ZXIOAA1teS5Nb2NrRmlsdGVyDkYAAQAAAAAAAAAAf///////" +
+    "//8BAAAAAAAAAAA=";
+
   private static final String MOCK_FILTER_JAR =
-    "UEsDBBQACAgIACmBi0IAAAAAAAAAAAAAAAAJAAQATUVUQS1JTkYv/soAAAMAUEsHCAAAAAACAAAA" +
-    "AAAAAFBLAwQUAAgICAApgYtCAAAAAAAAAAAAAAAAFAAAAE1FVEEtSU5GL01BTklGRVNULk1G803M" +
+    "UEsDBBQACAgIADRQI0QAAAAAAAAAAAAAAAAJAAQATUVUQS1JTkYv/soAAAMAUEsHCAAAAAACAAAA" +
+    "AAAAAFBLAwQUAAgICAA0UCNEAAAAAAAAAAAAAAAAFAAAAE1FVEEtSU5GL01BTklGRVNULk1G803M" +
     "y0xLLS7RDUstKs7Mz7NSMNQz4OVyLkpNLElN0XWqBAmY6xnEG1gqaPgXJSbnpCo45xcV5BcllgCV" +
-    "a/Jy8XIBAFBLBwgxyqRbQwAAAEQAAABQSwMECgAACAAAbICLQgAAAAAAAAAAAAAAAAUAAAB0ZXN0" +
-    "L1BLAwQUAAgICAAcgItCAAAAAAAAAAAAAAAAFQAAAHRlc3QvTW9ja0ZpbHRlci5jbGFzc41Qy07C" +
-    "QBS9A4VKBZGHoO7cgQvHmLjCuPBBQlJloWE/tCMdLZ1mOlV/y5WJCz/AjzLeDqCRYOIs7uuce87N" +
-    "fHy+vQPAEezakCNQ1TzR9Ep6D30Raq5ssAh0pZpQFjMv4DRgvpQxDcYs4fTOcOiMeoYTAsUTEQl9" +
-    "SiDf6Y4IWOfS5w7koVSGAhTRwBURv06nY65u2TjEjborPRaOmBJZPx9aOhAJgZq7dE+PgKM48/uC" +
-    "hz4SWh33nj0yKiS9YJoNoj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AFMCAAB0ZXN0L01vY2tGaWx0ZXIuY2xh" +
+    "c3NQSwUGAAAAAAQABAABAQAA5wMAAAAA";
 
   @Test
   public void testAttributesSerialization() throws IOException {
@@ -156,8 +174,15 @@ public class TestGet {
       fail("Should not be able to load the filter class");
     } catch (RuntimeException re) {
       String msg = re.getMessage();
-      Assert.assertTrue(msg != null
-        && msg.contains("Can't find class test.MockFilter"));
+      assertTrue(msg != null && msg.contains("Can't find class test.MockFilter"));
+    }
+
+    dis = ByteStreams.newDataInput(Base64.decode(WRITABLE_GET_WITH_FILTER_LIST));
+    try {
+      get.readFields(dis);
+      fail("Should not be able to load the filter class");
+    } catch (IOException ioe) {
+      assertTrue(ioe.getCause() instanceof ClassNotFoundException);
     }
 
     FileOutputStream fos = new FileOutputStream(jarFile);
@@ -166,8 +191,17 @@ public class TestGet {
 
     dis = ByteStreams.newDataInput(Base64.decode(WRITABLE_GET));
     get.readFields(dis);
-    Assert.assertEquals("test.MockFilter",
-      get.getFilter().getClass().getName());
+    assertEquals("test.MockFilter", get.getFilter().getClass().getName());
+
+    get = new Get();
+    dis = ByteStreams.newDataInput(Base64.decode(WRITABLE_GET_WITH_FILTER_LIST));
+    get.readFields(dis);
+    assertTrue(get.getFilter() instanceof FilterList);
+    List<Filter> filters = ((FilterList)get.getFilter()).getFilters();
+    assertEquals(3, filters.size());
+    assertEquals("test.MockFilter", filters.get(0).getClass().getName());
+    assertEquals("my.MockFilter", filters.get(1).getClass().getName());
+    assertTrue(filters.get(2) instanceof KeyOnlyFilter);
   }
 
   @org.junit.Rule



Mime
View raw message