You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@heron.apache.org by nw...@apache.org on 2019/07/10 23:45:31 UTC

[incubator-heron] branch master updated: Flip buffers using helper method (#3311)

This is an automated email from the ASF dual-hosted git repository.

nwang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-heron.git


The following commit(s) were added to refs/heads/master by this push:
     new bfe58d1  Flip buffers using helper method (#3311)
bfe58d1 is described below

commit bfe58d1e05cb3f5fb5db2f51d13592d9a65ac477
Author: Rohan Agarwal <ro...@gmail.com>
AuthorDate: Wed Jul 10 16:45:24 2019 -0700

    Flip buffers using helper method (#3311)
    
    * flip buffer using helper method
    
    * make constructor private
    
    * make class final
    
    * move constructor to top of class
    
    * no-op
    
    * empty commit to re-trigger CI
    
    * final try for flaky integration tests
---
 .../apache/heron/common/network/BufferHelper.java  | 48 ++++++++++++++++++++++
 .../heron/common/network/IncomingPacket.java       |  4 +-
 .../heron/common/network/OutgoingPacket.java       |  2 +-
 3 files changed, 51 insertions(+), 3 deletions(-)

diff --git a/heron/common/src/java/org/apache/heron/common/network/BufferHelper.java b/heron/common/src/java/org/apache/heron/common/network/BufferHelper.java
new file mode 100644
index 0000000..2849923
--- /dev/null
+++ b/heron/common/src/java/org/apache/heron/common/network/BufferHelper.java
@@ -0,0 +1,48 @@
+/**
+ * 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.heron.common.network;
+
+import java.nio.Buffer;
+import java.nio.ByteBuffer;
+
+/**
+ * Helper methods for working with {@link Buffer} objects.
+ */
+final class BufferHelper {
+
+  private BufferHelper() {
+  }
+
+  /**
+   * Flip the provided buffer.
+   * <p>
+   * This wrapper around {@link Buffer#flip()} is required because of
+   * incompatible ABI changes between Java 8 and 11. In Java 8, {@link ByteBuffer#flip()} returns
+   * a {@link Buffer}, whereas in Java 11, this method returns a {@link ByteBuffer}.
+   * <p>
+   * If this function is used, any object of {@link Buffer} (and subclasses) are first cast to
+   * {@link Buffer} objects, then flipped, thus avoiding the binary incompatibility.
+   *
+   * @param buffer The buffer to flip
+   */
+  static void flip(Buffer buffer) {
+    buffer.flip();
+  }
+}
diff --git a/heron/common/src/java/org/apache/heron/common/network/IncomingPacket.java b/heron/common/src/java/org/apache/heron/common/network/IncomingPacket.java
index 045602e..b1bf26c 100644
--- a/heron/common/src/java/org/apache/heron/common/network/IncomingPacket.java
+++ b/heron/common/src/java/org/apache/heron/common/network/IncomingPacket.java
@@ -76,7 +76,7 @@ public class IncomingPacket {
       }
       // We read the header fully
       headerRead = true;
-      header.flip();
+      BufferHelper.flip(header);
       int size = header.getInt();
       if (size > limit) {
         LOG.log(Level.SEVERE, "packet size " + size + " exceeds limit " + limit);
@@ -86,7 +86,7 @@ public class IncomingPacket {
     }
     int retval = readFromChannel(channel, data);
     if (retval == 0) {
-      data.flip();
+      BufferHelper.flip(data);
     }
     return retval;
   }
diff --git a/heron/common/src/java/org/apache/heron/common/network/OutgoingPacket.java b/heron/common/src/java/org/apache/heron/common/network/OutgoingPacket.java
index 2b91077..63983db 100644
--- a/heron/common/src/java/org/apache/heron/common/network/OutgoingPacket.java
+++ b/heron/common/src/java/org/apache/heron/common/network/OutgoingPacket.java
@@ -82,7 +82,7 @@ public class OutgoingPacket {
     buffer.put(message.toByteArray());
 
     // Make the buffer ready for writing out
-    buffer.flip();
+    BufferHelper.flip(buffer);
   }
 
   public static int sizeRequiredToPackString(String str) {