You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flume.apache.org by hs...@apache.org on 2012/06/01 00:28:39 UTC

svn commit: r1344932 - in /incubator/flume/trunk/flume-ng-core/src: main/java/org/apache/flume/channel/ main/java/org/apache/flume/interceptor/ test/java/org/apache/flume/interceptor/

Author: hshreedharan
Date: Thu May 31 22:28:39 2012
New Revision: 1344932

URL: http://svn.apache.org/viewvc?rev=1344932&view=rev
Log:
FLUME-1215. Implement Timestamp interceptor

(Mike Percy via Hari Shreedharan)


Added:
    incubator/flume/trunk/flume-ng-core/src/main/java/org/apache/flume/interceptor/InterceptorBuilderFactory.java   (with props)
    incubator/flume/trunk/flume-ng-core/src/main/java/org/apache/flume/interceptor/InterceptorType.java   (with props)
    incubator/flume/trunk/flume-ng-core/src/main/java/org/apache/flume/interceptor/TimestampInterceptor.java   (with props)
    incubator/flume/trunk/flume-ng-core/src/test/java/org/apache/flume/interceptor/TestTimestampInterceptor.java   (with props)
Modified:
    incubator/flume/trunk/flume-ng-core/src/main/java/org/apache/flume/channel/ChannelProcessor.java

Modified: incubator/flume/trunk/flume-ng-core/src/main/java/org/apache/flume/channel/ChannelProcessor.java
URL: http://svn.apache.org/viewvc/incubator/flume/trunk/flume-ng-core/src/main/java/org/apache/flume/channel/ChannelProcessor.java?rev=1344932&r1=1344931&r2=1344932&view=diff
==============================================================================
--- incubator/flume/trunk/flume-ng-core/src/main/java/org/apache/flume/channel/ChannelProcessor.java (original)
+++ incubator/flume/trunk/flume-ng-core/src/main/java/org/apache/flume/channel/ChannelProcessor.java Thu May 31 22:28:39 2012
@@ -35,6 +35,7 @@ import org.apache.flume.interceptor.Inte
 import org.apache.flume.interceptor.InterceptorChain;
 import org.apache.flume.Transaction;
 import org.apache.flume.conf.Configurable;
+import org.apache.flume.interceptor.InterceptorBuilderFactory;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -94,19 +95,15 @@ public class ChannelProcessor implements
         new Context(context.getSubProperties("interceptors."));
 
     // run through and instantiate all the interceptors specified in the Context
+    InterceptorBuilderFactory factory = new InterceptorBuilderFactory();
     for (String interceptorName : interceptorNames) {
       Context interceptorContext = new Context(
           interceptorContexts.getSubProperties(interceptorName + "."));
       String type = interceptorContext.getString("type");
       try {
-        // TODO: support aliases for built-in interceptor types
-        Class<?> clazz = Class.forName(type);
-        if (Interceptor.Builder.class.isAssignableFrom(clazz)) {
-          Interceptor.Builder builder =
-              (Interceptor.Builder) clazz.newInstance();
-          builder.configure(interceptorContext);
-          interceptors.add(builder.build());
-        }
+        Interceptor.Builder builder = factory.newInstance(type);
+        builder.configure(interceptorContext);
+        interceptors.add(builder.build());
       } catch (ClassNotFoundException e) {
         LOG.error("Builder class not found. Exception follows.", e);
         throw new FlumeException("Interceptor.Builder not found.", e);

Added: incubator/flume/trunk/flume-ng-core/src/main/java/org/apache/flume/interceptor/InterceptorBuilderFactory.java
URL: http://svn.apache.org/viewvc/incubator/flume/trunk/flume-ng-core/src/main/java/org/apache/flume/interceptor/InterceptorBuilderFactory.java?rev=1344932&view=auto
==============================================================================
--- incubator/flume/trunk/flume-ng-core/src/main/java/org/apache/flume/interceptor/InterceptorBuilderFactory.java (added)
+++ incubator/flume/trunk/flume-ng-core/src/main/java/org/apache/flume/interceptor/InterceptorBuilderFactory.java Thu May 31 22:28:39 2012
@@ -0,0 +1,51 @@
+/**
+ * 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.flume.interceptor;
+
+import org.apache.flume.interceptor.Interceptor.Builder;
+
+/**
+ * Factory used to register instances of Interceptors & their builders,
+ * as well as to instantiate the builders.
+ */
+public class InterceptorBuilderFactory {
+
+  private static Class<? extends Builder> lookup(String name) {
+    try {
+      return InterceptorType.valueOf(name.toUpperCase()).getBuilderClass();
+    } catch (IllegalArgumentException e) {
+      return null;
+    }
+  }
+
+  /**
+   * Instantiate specified class, either alias or fully-qualified class name.
+   */
+  public static Builder newInstance(String name)
+      throws ClassNotFoundException, InstantiationException,
+      IllegalAccessException {
+
+    Class<? extends Builder> clazz = lookup(name);
+    if (clazz == null) {
+      clazz = (Class<? extends Builder>) Class.forName(name);
+    }
+    return clazz.newInstance();
+  }
+
+}

Propchange: incubator/flume/trunk/flume-ng-core/src/main/java/org/apache/flume/interceptor/InterceptorBuilderFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/flume/trunk/flume-ng-core/src/main/java/org/apache/flume/interceptor/InterceptorType.java
URL: http://svn.apache.org/viewvc/incubator/flume/trunk/flume-ng-core/src/main/java/org/apache/flume/interceptor/InterceptorType.java?rev=1344932&view=auto
==============================================================================
--- incubator/flume/trunk/flume-ng-core/src/main/java/org/apache/flume/interceptor/InterceptorType.java (added)
+++ incubator/flume/trunk/flume-ng-core/src/main/java/org/apache/flume/interceptor/InterceptorType.java Thu May 31 22:28:39 2012
@@ -0,0 +1,35 @@
+/**
+ * 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.flume.interceptor;
+
+public enum InterceptorType {
+
+  TIMESTAMP(org.apache.flume.interceptor.TimestampInterceptor.Builder.class);
+
+  private final Class<? extends Interceptor.Builder> builderClass;
+
+  private InterceptorType(Class<? extends Interceptor.Builder> builderClass) {
+    this.builderClass = builderClass;
+  }
+
+  public Class<? extends Interceptor.Builder> getBuilderClass() {
+    return builderClass;
+  }
+
+}

Propchange: incubator/flume/trunk/flume-ng-core/src/main/java/org/apache/flume/interceptor/InterceptorType.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/flume/trunk/flume-ng-core/src/main/java/org/apache/flume/interceptor/TimestampInterceptor.java
URL: http://svn.apache.org/viewvc/incubator/flume/trunk/flume-ng-core/src/main/java/org/apache/flume/interceptor/TimestampInterceptor.java?rev=1344932&view=auto
==============================================================================
--- incubator/flume/trunk/flume-ng-core/src/main/java/org/apache/flume/interceptor/TimestampInterceptor.java (added)
+++ incubator/flume/trunk/flume-ng-core/src/main/java/org/apache/flume/interceptor/TimestampInterceptor.java Thu May 31 22:28:39 2012
@@ -0,0 +1,107 @@
+/**
+ * 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.flume.interceptor;
+
+import java.util.List;
+import java.util.Map;
+import org.apache.flume.Context;
+import org.apache.flume.Event;
+import static org.apache.flume.interceptor.TimestampInterceptor.Constants.*;
+
+/**
+ * Simple Interceptor class that sets the current system timestamp on all events
+ * that are intercepted.
+ * By convention, this timestamp header is named "timestamp" and its format
+ * is a "stringified" long timestamp in milliseconds since the UNIX epoch.
+ */
+public class TimestampInterceptor implements Interceptor {
+
+  private final boolean preserveExisting;
+
+  /**
+   * Only {@link TimestampInterceptor.Builder} can build me
+   */
+  private TimestampInterceptor(boolean preserveExisting) {
+    this.preserveExisting = preserveExisting;
+  }
+
+  @Override
+  public void initialize() {
+    // no-op
+  }
+
+  /**
+   * Modifies events in-place.
+   */
+  @Override
+  public Event intercept(Event event) {
+    Map<String, String> headers = event.getHeaders();
+    if (preserveExisting && headers.containsKey(TIMESTAMP)) {
+      // we must preserve the existing timestamp
+    } else {
+      long now = System.currentTimeMillis();
+      headers.put(TIMESTAMP, Long.toString(now));
+    }
+    return event;
+  }
+
+  /**
+   * Delegates to {@link #intercept(Event)} in a loop.
+   * @param events
+   * @return
+   */
+  @Override
+  public List<Event> intercept(List<Event> events) {
+    for (Event event : events) {
+      intercept(event);
+    }
+    return events;
+  }
+
+  @Override
+  public void close() {
+    // no-op
+  }
+
+  /**
+   * Builder which builds new instances of the TimestampInterceptor.
+   */
+  public static class Builder implements Interceptor.Builder {
+
+    private boolean preserveExisting = PRESERVE_DFLT;
+
+    @Override
+    public Interceptor build() {
+      return new TimestampInterceptor(preserveExisting);
+    }
+
+    @Override
+    public void configure(Context context) {
+      preserveExisting = context.getBoolean(PRESERVE, PRESERVE_DFLT);
+    }
+
+  }
+
+  public static class Constants {
+    public static String TIMESTAMP = "timestamp";
+    public static String PRESERVE = "preserveExisting";
+    public static boolean PRESERVE_DFLT = false;
+  }
+
+}

Propchange: incubator/flume/trunk/flume-ng-core/src/main/java/org/apache/flume/interceptor/TimestampInterceptor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/flume/trunk/flume-ng-core/src/test/java/org/apache/flume/interceptor/TestTimestampInterceptor.java
URL: http://svn.apache.org/viewvc/incubator/flume/trunk/flume-ng-core/src/test/java/org/apache/flume/interceptor/TestTimestampInterceptor.java?rev=1344932&view=auto
==============================================================================
--- incubator/flume/trunk/flume-ng-core/src/test/java/org/apache/flume/interceptor/TestTimestampInterceptor.java (added)
+++ incubator/flume/trunk/flume-ng-core/src/test/java/org/apache/flume/interceptor/TestTimestampInterceptor.java Thu May 31 22:28:39 2012
@@ -0,0 +1,111 @@
+/**
+ * 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.flume.interceptor;
+
+import com.google.common.base.Charsets;
+import org.apache.flume.Context;
+import org.apache.flume.Event;
+import org.apache.flume.event.EventBuilder;
+import org.apache.flume.interceptor.TimestampInterceptor.Constants;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestTimestampInterceptor {
+
+  /**
+   * Ensure that the "timestamp" header gets set (to something)
+   */
+  @Test
+  public void testBasic() throws ClassNotFoundException, InstantiationException,
+      IllegalAccessException {
+
+    InterceptorBuilderFactory factory = new InterceptorBuilderFactory();
+    Interceptor.Builder builder = InterceptorBuilderFactory.newInstance(
+        InterceptorType.TIMESTAMP.toString());
+    Interceptor interceptor = builder.build();
+
+    Event event = EventBuilder.withBody("test event", Charsets.UTF_8);
+    Assert.assertNull(event.getHeaders().get(Constants.TIMESTAMP));
+
+    Long now = System.currentTimeMillis();
+    event = interceptor.intercept(event);
+    String timestampStr = event.getHeaders().get(Constants.TIMESTAMP);
+    Assert.assertNotNull(timestampStr);
+    Assert.assertTrue(Long.parseLong(timestampStr) >= now);
+  }
+
+  /**
+   * Ensure timestamp is NOT overwritten when preserveExistingTimestamp == true
+   */
+  @Test
+  public void testPreserve() throws ClassNotFoundException,
+      InstantiationException, IllegalAccessException {
+
+    Context ctx = new Context();
+    ctx.put("preserveExisting", "true");
+
+    InterceptorBuilderFactory factory = new InterceptorBuilderFactory();
+    Interceptor.Builder builder = InterceptorBuilderFactory.newInstance(
+        InterceptorType.TIMESTAMP.toString());
+    builder.configure(ctx);
+    Interceptor interceptor = builder.build();
+
+    long originalTs = 1L;
+    Event event = EventBuilder.withBody("test event", Charsets.UTF_8);
+    event.getHeaders().put(Constants.TIMESTAMP, Long.toString(originalTs));
+    Assert.assertEquals(Long.toString(originalTs),
+        event.getHeaders().get(Constants.TIMESTAMP));
+
+    Long now = System.currentTimeMillis();
+    event = interceptor.intercept(event);
+    String timestampStr = event.getHeaders().get(Constants.TIMESTAMP);
+    Assert.assertNotNull(timestampStr);
+    Assert.assertTrue(Long.parseLong(timestampStr) == originalTs);
+  }
+
+  /**
+   * Ensure timestamp IS overwritten when preserveExistingTimestamp == false
+   */
+  @Test
+  public void testClobber() throws ClassNotFoundException,
+      InstantiationException, IllegalAccessException {
+
+    Context ctx = new Context();
+    ctx.put("preserveExisting", "false"); // DEFAULT BEHAVIOR
+
+    InterceptorBuilderFactory factory = new InterceptorBuilderFactory();
+    Interceptor.Builder builder = InterceptorBuilderFactory.newInstance(
+        InterceptorType.TIMESTAMP.toString());
+    builder.configure(ctx);
+    Interceptor interceptor = builder.build();
+
+    long originalTs = 1L;
+    Event event = EventBuilder.withBody("test event", Charsets.UTF_8);
+    event.getHeaders().put(Constants.TIMESTAMP, Long.toString(originalTs));
+    Assert.assertEquals(Long.toString(originalTs),
+        event.getHeaders().get(Constants.TIMESTAMP));
+
+    Long now = System.currentTimeMillis();
+    event = interceptor.intercept(event);
+    String timestampStr = event.getHeaders().get(Constants.TIMESTAMP);
+    Assert.assertNotNull(timestampStr);
+    Assert.assertTrue(Long.parseLong(timestampStr) >= now);
+  }
+
+}

Propchange: incubator/flume/trunk/flume-ng-core/src/test/java/org/apache/flume/interceptor/TestTimestampInterceptor.java
------------------------------------------------------------------------------
    svn:eol-style = native