Updated Branches: refs/heads/trunk bad9e9ab8 -> 250c45790
FLUME-1622: MemoryChannel throws NPE if the event has no body (Hari Shreedharan via Brock Noland) Project: http://git-wip-us.apache.org/repos/asf/flume/repo Commit: http://git-wip-us.apache.org/repos/asf/flume/commit/250c4579 Tree: http://git-wip-us.apache.org/repos/asf/flume/tree/250c4579 Diff: http://git-wip-us.apache.org/repos/asf/flume/diff/250c4579 Branch: refs/heads/trunk Commit: 250c4579033b36f2ea2e66a581e1c0834c627732 Parents: bad9e9a Author: Brock Noland <[email protected]> Authored: Thu Oct 18 11:37:57 2012 -0500 Committer: Brock Noland <[email protected]> Committed: Thu Oct 18 11:37:57 2012 -0500 ---------------------------------------------------------------------- .../org/apache/flume/channel/MemoryChannel.java | 7 +++- .../apache/flume/channel/TestMemoryChannel.java | 28 +++++++++++++++ 2 files changed, 34 insertions(+), 1 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/flume/blob/250c4579/flume-ng-core/src/main/java/org/apache/flume/channel/MemoryChannel.java ---------------------------------------------------------------------- diff --git a/flume-ng-core/src/main/java/org/apache/flume/channel/MemoryChannel.java b/flume-ng-core/src/main/java/org/apache/flume/channel/MemoryChannel.java index a656c8b..06c90d9 100644 --- a/flume-ng-core/src/main/java/org/apache/flume/channel/MemoryChannel.java +++ b/flume-ng-core/src/main/java/org/apache/flume/channel/MemoryChannel.java @@ -330,6 +330,11 @@ public class MemoryChannel extends BasicChannelSemantics { private long estimateEventSize(Event event) { - return event.getBody().length; + byte[] body = event.getBody(); + if(body != null && body.length != 0) { + return body.length; + } + //Each event occupies at least 1 slot, so return 1. + return 1; } } http://git-wip-us.apache.org/repos/asf/flume/blob/250c4579/flume-ng-core/src/test/java/org/apache/flume/channel/TestMemoryChannel.java ---------------------------------------------------------------------- diff --git a/flume-ng-core/src/test/java/org/apache/flume/channel/TestMemoryChannel.java b/flume-ng-core/src/test/java/org/apache/flume/channel/TestMemoryChannel.java index 4af4a40..e1a61c2 100644 --- a/flume-ng-core/src/test/java/org/apache/flume/channel/TestMemoryChannel.java +++ b/flume-ng-core/src/test/java/org/apache/flume/channel/TestMemoryChannel.java @@ -411,4 +411,32 @@ public class TestMemoryChannel { //success } } + + /* + * This would cause a NPE without FLUME-1622. + */ + @Test + public void testNullEmptyEvent() { + Context context = new Context(); + Map<String, String> parms = new HashMap<String, String>(); + parms.put("byteCapacity", "2000"); + parms.put("byteCapacityBufferPercentage", "20"); + context.putAll(parms); + Configurables.configure(channel, context); + + Transaction tx = channel.getTransaction(); + tx.begin(); + //This line would cause a NPE without FLUME-1622. + channel.put(EventBuilder.withBody(null)); + tx.commit(); + tx.close(); + + tx = channel.getTransaction(); + tx.begin(); + channel.put(EventBuilder.withBody(new byte[0])); + tx.commit(); + tx.close(); + + + } }
