You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@geode.apache.org by GitBox <gi...@apache.org> on 2021/04/17 18:08:51 UTC

[GitHub] [geode] Bill opened a new pull request #6343: GEODE-9141: fixed via ByteBufferSharing on Connection input buffer

Bill opened a new pull request #6343:
URL: https://github.com/apache/geode/pull/6343


   Thank you for submitting a contribution to Apache Geode.
   
   In order to streamline the review of the contribution we ask you
   to ensure the following steps have been taken:
   
   ### For all changes:
   - [ ] Is there a JIRA ticket associated with this PR? Is it referenced in the commit message?
   
   - [ ] Has your PR been rebased against the latest commit within the target branch (typically `develop`)?
   
   - [ ] Is your initial contribution a single, squashed commit?
   
   - [ ] Does `gradlew build` run cleanly?
   
   - [ ] Have you written or updated unit tests to verify your changes?
   
   - [ ] If adding new dependencies to the code, are these dependencies licensed in a way that is compatible for inclusion under [ASF 2.0](http://www.apache.org/legal/resolved.html#category-a)?
   
   ### Note:
   Please ensure that once the PR is submitted, check Concourse for build issues and
   submit an update to your PR as soon as possible. If you need help, please send an
   email to dev@geode.apache.org.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [geode] Bill commented on a change in pull request #6343: GEODE-9141: fixed via ByteBufferSharing on Connection input buffer

Posted by GitBox <gi...@apache.org>.
Bill commented on a change in pull request #6343:
URL: https://github.com/apache/geode/pull/6343#discussion_r618690898



##########
File path: geode-core/src/main/java/org/apache/geode/internal/tcp/Connection.java
##########
@@ -3228,28 +3221,23 @@ private void setThreadName(int dominoNumber) {
         + " local port=" + socket.getLocalPort() + " remote port=" + socket.getPort());
   }
 
-  private void compactOrResizeBuffer(int messageLength) {
-    final int oldBufferSize = inputBuffer.capacity();
-    int allocSize = messageLength + MSG_HEADER_BYTES;
-    if (oldBufferSize < allocSize) {
-      // need a bigger buffer
-      logger.info("Allocating larger network read buffer, new size is {} old size was {}.",
-          allocSize, oldBufferSize);
-      ByteBuffer oldBuffer = inputBuffer;
-      inputBuffer = getBufferPool().acquireDirectReceiveBuffer(allocSize);
-
-      if (oldBuffer != null) {
-        int oldByteCount = oldBuffer.remaining();
-        inputBuffer.put(oldBuffer);
-        inputBuffer.position(oldByteCount);
-        getBufferPool().releaseReceiveBuffer(oldBuffer);
-      }
-    } else {
-      if (inputBuffer.position() != 0) {
-        inputBuffer.compact();
+  private void compactOrResizeBuffer(int messageLength) throws IOException {
+    try (final ByteBufferSharing inputSharing = inputBufferVendor.open()) {

Review comment:
       inlining this method in my next commit…




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [geode] bschuchardt commented on a change in pull request #6343: GEODE-9141: fixed via ByteBufferSharing on Connection input buffer

Posted by GitBox <gi...@apache.org>.
bschuchardt commented on a change in pull request #6343:
URL: https://github.com/apache/geode/pull/6343#discussion_r618640708



##########
File path: geode-core/src/main/java/org/apache/geode/internal/tcp/Connection.java
##########
@@ -1617,8 +1612,18 @@ private void readMessages() {
           break;
         }
 
-        try {
-          ByteBuffer buff = getInputBuffer();
+        try (final ByteBufferSharing inputSharing = inputBufferVendor.open()) {

Review comment:
       okay, I'm convinced.  Moving it to the constructors would be a nice change if you can pull it off.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [geode] Bill commented on pull request #6343: GEODE-9141: fixed via ByteBufferSharing on Connection input buffer

Posted by GitBox <gi...@apache.org>.
Bill commented on pull request #6343:
URL: https://github.com/apache/geode/pull/6343#issuecomment-824191726


   @kamilla1201 will you please review this


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [geode] bschuchardt commented on a change in pull request #6343: GEODE-9141: fixed via ByteBufferSharing on Connection input buffer

Posted by GitBox <gi...@apache.org>.
bschuchardt commented on a change in pull request #6343:
URL: https://github.com/apache/geode/pull/6343#discussion_r617758447



##########
File path: geode-core/src/main/java/org/apache/geode/internal/tcp/Connection.java
##########
@@ -1617,8 +1612,18 @@ private void readMessages() {
           break;
         }
 
-        try {
-          ByteBuffer buff = getInputBuffer();
+        try (final ByteBufferSharing inputSharing = inputBufferVendor.open()) {

Review comment:
       If this buffer is being used for TLS doesn't it need to remain the packet size dictated by the SSLSession?   Maybe createIOFilter should set recvBufferSize to the packet size so that this code won't expand the buffer.

##########
File path: geode-core/src/main/java/org/apache/geode/internal/tcp/Connection.java
##########
@@ -3228,28 +3221,23 @@ private void setThreadName(int dominoNumber) {
         + " local port=" + socket.getLocalPort() + " remote port=" + socket.getPort());
   }
 
-  private void compactOrResizeBuffer(int messageLength) {
-    final int oldBufferSize = inputBuffer.capacity();
-    int allocSize = messageLength + MSG_HEADER_BYTES;
-    if (oldBufferSize < allocSize) {
-      // need a bigger buffer
-      logger.info("Allocating larger network read buffer, new size is {} old size was {}.",
-          allocSize, oldBufferSize);
-      ByteBuffer oldBuffer = inputBuffer;
-      inputBuffer = getBufferPool().acquireDirectReceiveBuffer(allocSize);
-
-      if (oldBuffer != null) {
-        int oldByteCount = oldBuffer.remaining();
-        inputBuffer.put(oldBuffer);
-        inputBuffer.position(oldByteCount);
-        getBufferPool().releaseReceiveBuffer(oldBuffer);
-      }
-    } else {
-      if (inputBuffer.position() != 0) {
-        inputBuffer.compact();
+  private void compactOrResizeBuffer(int messageLength) throws IOException {
+    try (final ByteBufferSharing inputSharing = inputBufferVendor.open()) {

Review comment:
       this method is only invoked in processInputBuffer, where it's already in a "try" block.  Since this is in the primary execution path for all incoming messages I think it would be good to avoid having this extraneous "try" block.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [geode] Bill merged pull request #6343: GEODE-9141: fixed via ByteBufferSharing on Connection input buffer

Posted by GitBox <gi...@apache.org>.
Bill merged pull request #6343:
URL: https://github.com/apache/geode/pull/6343


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [geode] Bill commented on a change in pull request #6343: GEODE-9141: fixed via ByteBufferSharing on Connection input buffer

Posted by GitBox <gi...@apache.org>.
Bill commented on a change in pull request #6343:
URL: https://github.com/apache/geode/pull/6343#discussion_r617965935



##########
File path: geode-core/src/main/java/org/apache/geode/internal/tcp/Connection.java
##########
@@ -1617,8 +1612,18 @@ private void readMessages() {
           break;
         }
 
-        try {
-          ByteBuffer buff = getInputBuffer();
+        try (final ByteBufferSharing inputSharing = inputBufferVendor.open()) {

Review comment:
       Before this PR, `getInputBuffer()` was called solely by this method. Once on what was line [1595](https://github.com/apache/geode/pull/6343/files#diff-69e134fe162fb8f45afd6d09f5cfc91ded364e987dac14c770a963736c431124L1595) and again on what was line [1621](https://github.com/apache/geode/pull/6343/files#diff-69e134fe162fb8f45afd6d09f5cfc91ded364e987dac14c770a963736c431124L1621).
   
   The earlier call was moved down to what's now line [1636](https://github.com/apache/geode/pull/6343/files#diff-69e134fe162fb8f45afd6d09f5cfc91ded364e987dac14c770a963736c431124L1621). What was the later call is now right after this comment. The code is a direct copy/paste of the old `getInputBuffer()` code.
   
   So in this way I feel like this was a mere refactoring. Doesn't mean the resulting code is "good" but it should mean it behaves the same as before.
   
   Now `expand{Read/Write}BufferIfNeeded()` only ever _expands_ a buffer. And those methods are called in exactly three places in this class:
   
   1. `createIoFilter()` which is called if this is a sender or receiver doing TLS
   2. `compactOrResizeBuffer()` which is called after receiving a non-TLS message
   3.  this code in `readMessages()` which can be called to receive a message in both the TLS and non-TLS case
   
   So I think I just convinced myself that this code below that neither of us likes, is only executed in the non-TLS case. And as a result, I think your concern about this allocation running afoul of buffer sizes in the TLS case, is addressed.
   
   I'd like this a lot better if the size calculation could be moved into the (2) constructors and we could pre-allocate the right size buffer. I see that `recvBufferSize` is set (indirectly) during construction so that one's available. And the `owner` is certainly available.
   
   How would you feel about me taking a whack at moving this into the constructors? I feel like I can move the TLS buffer size calculation into the constructors too. If I can do both of those we can eliminate the whole null-handling complication (in byte buffer sharing) entirely.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [geode] Bill commented on a change in pull request #6343: GEODE-9141: fixed via ByteBufferSharing on Connection input buffer

Posted by GitBox <gi...@apache.org>.
Bill commented on a change in pull request #6343:
URL: https://github.com/apache/geode/pull/6343#discussion_r617956355



##########
File path: geode-core/src/main/java/org/apache/geode/internal/tcp/Connection.java
##########
@@ -3228,28 +3221,23 @@ private void setThreadName(int dominoNumber) {
         + " local port=" + socket.getLocalPort() + " remote port=" + socket.getPort());
   }
 
-  private void compactOrResizeBuffer(int messageLength) {
-    final int oldBufferSize = inputBuffer.capacity();
-    int allocSize = messageLength + MSG_HEADER_BYTES;
-    if (oldBufferSize < allocSize) {
-      // need a bigger buffer
-      logger.info("Allocating larger network read buffer, new size is {} old size was {}.",
-          allocSize, oldBufferSize);
-      ByteBuffer oldBuffer = inputBuffer;
-      inputBuffer = getBufferPool().acquireDirectReceiveBuffer(allocSize);
-
-      if (oldBuffer != null) {
-        int oldByteCount = oldBuffer.remaining();
-        inputBuffer.put(oldBuffer);
-        inputBuffer.position(oldByteCount);
-        getBufferPool().releaseReceiveBuffer(oldBuffer);
-      }
-    } else {
-      if (inputBuffer.position() != 0) {
-        inputBuffer.compact();
+  private void compactOrResizeBuffer(int messageLength) throws IOException {
+    try (final ByteBufferSharing inputSharing = inputBufferVendor.open()) {

Review comment:
       In order to avoid the try-with-resources block here, I could pass in the `ByteBufferSharing` from the caller. I'm reluctant to do that though because it expands the scope of that try-with-resources block in the caller. It's clear enough while we're reviewing the PR right now, but I've been avoiding passing those `AutoCloseable`s around. The protocol works, but only if everybody follows the rules. Passing them around kinda breaks (or at least bends) a rule.
   
   This compact method is called once per buffer. Since the caller holds the lock, there will be no contention on the lock in the `open()` call here. There is possible contention incrementing the reference count (contending with former lock holders decrementing the count). The extra close call just decrements an atomic.
   
   From that description, do you think it would be better to pass in the `ByteBufferSharing` or do you think it would be reasonable to let this merge and we let performance benchmarks tell us if it's hurting us? Or alternately, do you think I should run benchmarks on this branch ahead of merging?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [geode] Bill commented on a change in pull request #6343: GEODE-9141: fixed via ByteBufferSharing on Connection input buffer

Posted by GitBox <gi...@apache.org>.
Bill commented on a change in pull request #6343:
URL: https://github.com/apache/geode/pull/6343#discussion_r618692681



##########
File path: geode-core/src/main/java/org/apache/geode/internal/tcp/Connection.java
##########
@@ -1617,8 +1612,18 @@ private void readMessages() {
           break;
         }
 
-        try {
-          ByteBuffer buff = getInputBuffer();
+        try (final ByteBufferSharing inputSharing = inputBufferVendor.open()) {

Review comment:
       moving initial buffer size calculation "up" (not quite to constructors—but into `createIOFilter()` which is about as good), in next commit…




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [geode] Bill commented on a change in pull request #6343: GEODE-9141: fixed via ByteBufferSharing on Connection input buffer

Posted by GitBox <gi...@apache.org>.
Bill commented on a change in pull request #6343:
URL: https://github.com/apache/geode/pull/6343#discussion_r618692681



##########
File path: geode-core/src/main/java/org/apache/geode/internal/tcp/Connection.java
##########
@@ -1617,8 +1612,18 @@ private void readMessages() {
           break;
         }
 
-        try {
-          ByteBuffer buff = getInputBuffer();
+        try (final ByteBufferSharing inputSharing = inputBufferVendor.open()) {

Review comment:
       moving initial buffer size calculation "up" (not quite to constructors—but into `createIoFilter()` which is about as good), in next commit…




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org