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 2020/10/18 17:18:36 UTC

[GitHub] [geode] Bill opened a new pull request #5638: GEODE-8584: AutoCloseable for ByteBuffer Locking

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


   A third attempt at fixing GEODE-8584 locking granularity.
   
   This is a refinement of the [Finer-Grained Locking PR](https://github.com/apache/geode/pull/5632). That PR split the single lock into two.  With that other PR, client code in e.g. .`MsgReader` had to do all this ceremony:
   
   ```
   synchronized (ioFilter.getInputSyncObject()) {
     ioFilter.setInputInUse(true);
     try {
       ByteBuffer unwrappedBuffer = readAtLeast(Connection.MSG_HEADER_BYTES);
   
       // do stuff with unwrappedBuffer
   
     } finally {
   	ioFilter.setInputInUse(false);
     }
   }
   ```
   
   @dschneider-pivotal noticed that this followed the `AutoCloseable` pattern. So this PR makes good on that observation and introduces the auto-closeable `ByteBufferSharing`. We now return a `ByteBufferSharing` from methods that used to return the input `ByteBuffer`. Callers acquire it in a try-with-resources and rely on the `AutoCloseable` protocol to take care of lock closing and reference counting (decrement).
   
   With this PR that same `MsgReader` code looks like this:
   
   ```
   try (final ByteBufferSharing sharedBuffer = ioFilter
       .readAtLeast(conn.getSocket().getChannel(), Connection.MSG_HEADER_BYTES, peerNetData)) {
     ByteBuffer unwrappedBuffer = sharedBuffer.getBuffer();
   
     // do stuff with unwrappedBuffer
     
   }
   ```      
   
   - [ ] 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 #5638: GEODE-8584: AutoCloseable for ByteBuffer Locking

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



##########
File path: geode-core/src/main/java/org/apache/geode/internal/net/ByteBufferSharingImpl.java
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.geode.internal.net;
+
+import java.nio.ByteBuffer;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
+/**
+ * An {@link AutoCloseable} meant to be acquired in a try-with-resources statement. The resource (a
+ * {@link ByteBuffer}) is available (for reading and modification) in the scope of the
+ * try-with-resources. Collaborates with {@link ByteBufferReferencing} to ensure final dereference
+ * returns the {@link ByteBuffer} to the pool.
+ */
+class ByteBufferSharingImpl implements ByteBufferSharing {
+
+  private final ByteBuffer buffer;

Review comment:
       This is addressed in the latest commit. Now the "referencing" owns the `ByteBuffer` referencing 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 pull request #5638: GEODE-8584: AutoCloseable for ByteBuffer Locking

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


   These ideas made it into the first and second PRs for GEODE-8652 and were ported everywhere.


----------------------------------------------------------------
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 #5638: GEODE-8584: AutoCloseable for ByteBuffer Locking

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



##########
File path: geode-core/src/main/java/org/apache/geode/internal/net/ByteBufferSharingImpl.java
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.geode.internal.net;
+
+import java.nio.ByteBuffer;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
+/**
+ * An {@link AutoCloseable} meant to be acquired in a try-with-resources statement. The resource (a
+ * {@link ByteBuffer}) is available (for reading and modification) in the scope of the
+ * try-with-resources. Collaborates with {@link ByteBufferReferencing} to ensure final dereference
+ * returns the {@link ByteBuffer} to the pool.
+ */
+class ByteBufferSharingImpl implements ByteBufferSharing {
+
+  private final ByteBuffer buffer;
+  private final Lock lock;
+  private final ByteBufferReferencing referencing;
+
+  /**
+   * This constructor is for use only by the owner of the shared resource (a {@link ByteBuffer}).
+   * Furthermore, this constructor is only for use in constructing the very first {@link
+   * ByteBufferSharingImpl} for the resource. Subsequent instances must be constructed via the copy
+   * constructor.
+   *
+   * A resource owner calls this constructor and retains the reference and never hands it out.
+   * Instead, the owner hands out copies, constructed via the copy constructor.
+   *
+   * This constructor acquires no lock and does not modify the reference count through the {@link
+   * ByteBufferReferencing} (passed in here.)
+   */
+  ByteBufferSharingImpl(final ByteBuffer buffer,
+      final ByteBufferReferencing referencing) {
+    this(buffer, new ReentrantLock(), referencing);
+  }
+
+  /**
+   * This constructor is for use only by the owner of the shared resource. It's used for handing out
+   * references to the shared resource. So it does reference counting and also acquires a lock.
+   *
+   * Resource owners will typically return the result of this constructor, to a caller. That caller
+   * binds that reference to a variable in a try-with-resources statement and relies on the
+   * AutoCloseable protocol to invoke close() on the object at the end of the block.
+   */
+  ByteBufferSharingImpl(final ByteBufferSharingImpl other) {

Review comment:
       This is addressed in the latest commit. This copy-constructor was replaced by this method:
   
   ```
   ByteBuffer alias()
   ```
   
   The resource-owner returns the result of invoking `alias()` everywhere it hands out an alias. No more construction!




----------------------------------------------------------------
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 #5638: GEODE-8584: AutoCloseable for ByteBuffer Locking

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



##########
File path: geode-core/src/main/java/org/apache/geode/internal/net/ByteBufferSharingImpl.java
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.geode.internal.net;
+
+import java.nio.ByteBuffer;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
+/**
+ * An {@link AutoCloseable} meant to be acquired in a try-with-resources statement. The resource (a
+ * {@link ByteBuffer}) is available (for reading and modification) in the scope of the
+ * try-with-resources. Collaborates with {@link ByteBufferReferencing} to ensure final dereference
+ * returns the {@link ByteBuffer} to the pool.
+ */
+class ByteBufferSharingImpl implements ByteBufferSharing {
+
+  private final ByteBuffer buffer;
+  private final Lock lock;
+  private final ByteBufferReferencing referencing;
+
+  /**
+   * This constructor is for use only by the owner of the shared resource (a {@link ByteBuffer}).
+   * Furthermore, this constructor is only for use in constructing the very first {@link
+   * ByteBufferSharingImpl} for the resource. Subsequent instances must be constructed via the copy
+   * constructor.
+   *
+   * A resource owner calls this constructor and retains the reference and never hands it out.
+   * Instead, the owner hands out copies, constructed via the copy constructor.
+   *
+   * This constructor acquires no lock and does not modify the reference count through the {@link
+   * ByteBufferReferencing} (passed in here.)
+   */
+  ByteBufferSharingImpl(final ByteBuffer buffer,
+      final ByteBufferReferencing referencing) {
+    this(buffer, new ReentrantLock(), referencing);
+  }
+
+  /**
+   * This constructor is for use only by the owner of the shared resource. It's used for handing out
+   * references to the shared resource. So it does reference counting and also acquires a lock.
+   *
+   * Resource owners will typically return the result of this constructor, to a caller. That caller
+   * binds that reference to a variable in a try-with-resources statement and relies on the
+   * AutoCloseable protocol to invoke close() on the object at the end of the block.
+   */
+  ByteBufferSharingImpl(final ByteBufferSharingImpl other) {
+    this(other.buffer, other.lock, other.referencing);
+    referencing.addReference();
+    lock.lock();
+  }
+
+  private ByteBufferSharingImpl(final ByteBuffer buffer,
+      final Lock lock,
+      final ByteBufferReferencing referencing) {
+    this.buffer = buffer;
+    this.lock = lock;
+    this.referencing = referencing;
+  }
+
+  @Override
+  public ByteBuffer getBuffer() {
+    return buffer;
+  }
+
+  @Override
+  public void close() {
+    final int usages = referencing.dropReference();
+    if (usages > 0) {
+      /*
+       * We don't unlock when the very last usage goes away. The resource owner holds the first
+       * usage and there is no lock associated with that one. Subsequent usages have a lock and
+       * so require a corresponding unlock.
+       */
+      lock.unlock();

Review comment:
       Talked to @dschneider-pivotal and we agreed this is not an issue.




----------------------------------------------------------------
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] dschneider-pivotal commented on a change in pull request #5638: GEODE-8584: AutoCloseable for ByteBuffer Locking

Posted by GitBox <gi...@apache.org>.
dschneider-pivotal commented on a change in pull request #5638:
URL: https://github.com/apache/geode/pull/5638#discussion_r507920984



##########
File path: geode-core/src/main/java/org/apache/geode/internal/net/ByteBufferSharingImpl.java
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.geode.internal.net;
+
+import java.nio.ByteBuffer;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
+/**
+ * An {@link AutoCloseable} meant to be acquired in a try-with-resources statement. The resource (a
+ * {@link ByteBuffer}) is available (for reading and modification) in the scope of the
+ * try-with-resources. Collaborates with {@link ByteBufferReferencing} to ensure final dereference
+ * returns the {@link ByteBuffer} to the pool.
+ */
+class ByteBufferSharingImpl implements ByteBufferSharing {
+
+  private final ByteBuffer buffer;
+  private final Lock lock;
+  private final ByteBufferReferencing referencing;
+
+  /**
+   * This constructor is for use only by the owner of the shared resource (a {@link ByteBuffer}).
+   * Furthermore, this constructor is only for use in constructing the very first {@link
+   * ByteBufferSharingImpl} for the resource. Subsequent instances must be constructed via the copy
+   * constructor.
+   *
+   * A resource owner calls this constructor and retains the reference and never hands it out.
+   * Instead, the owner hands out copies, constructed via the copy constructor.
+   *
+   * This constructor acquires no lock and does not modify the reference count through the {@link
+   * ByteBufferReferencing} (passed in here.)
+   */
+  ByteBufferSharingImpl(final ByteBuffer buffer,
+      final ByteBufferReferencing referencing) {
+    this(buffer, new ReentrantLock(), referencing);
+  }
+
+  /**
+   * This constructor is for use only by the owner of the shared resource. It's used for handing out
+   * references to the shared resource. So it does reference counting and also acquires a lock.
+   *
+   * Resource owners will typically return the result of this constructor, to a caller. That caller
+   * binds that reference to a variable in a try-with-resources statement and relies on the
+   * AutoCloseable protocol to invoke close() on the object at the end of the block.
+   */
+  ByteBufferSharingImpl(final ByteBufferSharingImpl other) {

Review comment:
       Always creating a new instance at the start of try-with-resources is not needed. In the case of this object I think it would be okay for the same instance to be used by multiple threads concurrently and each one to call close concurrently on the same instance. If so this would address Jakes concern about object allocation.

##########
File path: geode-core/src/main/java/org/apache/geode/internal/net/ByteBufferSharingImpl.java
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.geode.internal.net;
+
+import java.nio.ByteBuffer;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
+/**
+ * An {@link AutoCloseable} meant to be acquired in a try-with-resources statement. The resource (a
+ * {@link ByteBuffer}) is available (for reading and modification) in the scope of the
+ * try-with-resources. Collaborates with {@link ByteBufferReferencing} to ensure final dereference
+ * returns the {@link ByteBuffer} to the pool.
+ */
+class ByteBufferSharingImpl implements ByteBufferSharing {
+
+  private final ByteBuffer buffer;
+  private final Lock lock;
+  private final ByteBufferReferencing referencing;
+
+  /**
+   * This constructor is for use only by the owner of the shared resource (a {@link ByteBuffer}).
+   * Furthermore, this constructor is only for use in constructing the very first {@link
+   * ByteBufferSharingImpl} for the resource. Subsequent instances must be constructed via the copy
+   * constructor.
+   *
+   * A resource owner calls this constructor and retains the reference and never hands it out.
+   * Instead, the owner hands out copies, constructed via the copy constructor.
+   *
+   * This constructor acquires no lock and does not modify the reference count through the {@link
+   * ByteBufferReferencing} (passed in here.)
+   */
+  ByteBufferSharingImpl(final ByteBuffer buffer,
+      final ByteBufferReferencing referencing) {
+    this(buffer, new ReentrantLock(), referencing);
+  }
+
+  /**
+   * This constructor is for use only by the owner of the shared resource. It's used for handing out
+   * references to the shared resource. So it does reference counting and also acquires a lock.
+   *
+   * Resource owners will typically return the result of this constructor, to a caller. That caller
+   * binds that reference to a variable in a try-with-resources statement and relies on the
+   * AutoCloseable protocol to invoke close() on the object at the end of the block.
+   */
+  ByteBufferSharingImpl(final ByteBufferSharingImpl other) {
+    this(other.buffer, other.lock, other.referencing);
+    referencing.addReference();
+    lock.lock();
+  }
+
+  private ByteBufferSharingImpl(final ByteBuffer buffer,
+      final Lock lock,
+      final ByteBufferReferencing referencing) {
+    this.buffer = buffer;
+    this.lock = lock;
+    this.referencing = referencing;
+  }
+
+  @Override
+  public ByteBuffer getBuffer() {
+    return buffer;
+  }
+
+  @Override
+  public void close() {
+    final int usages = referencing.dropReference();
+    if (usages > 0) {
+      /*
+       * We don't unlock when the very last usage goes away. The resource owner holds the first
+       * usage and there is no lock associated with that one. Subsequent usages have a lock and
+       * so require a corresponding unlock.
+       */
+      lock.unlock();

Review comment:
       I think this introduces a bug in the case when "close" finds the reference still in use. In that case "close" will call unlock but that will fail because ReentrantLock requires that unlock be called by the same thread that called lock (I think). In general it wold be better if the thread that called lock at the beginning of the "try-with-resource" was also the caller of "unlock"

##########
File path: geode-core/src/main/java/org/apache/geode/internal/net/ByteBufferSharingImpl.java
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.geode.internal.net;
+
+import java.nio.ByteBuffer;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
+/**
+ * An {@link AutoCloseable} meant to be acquired in a try-with-resources statement. The resource (a
+ * {@link ByteBuffer}) is available (for reading and modification) in the scope of the
+ * try-with-resources. Collaborates with {@link ByteBufferReferencing} to ensure final dereference
+ * returns the {@link ByteBuffer} to the pool.
+ */
+class ByteBufferSharingImpl implements ByteBufferSharing {
+
+  private final ByteBuffer buffer;

Review comment:
       I think it would be better to obtain the buffer from "referencing" instead of having an extra copy of it here.




----------------------------------------------------------------
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 closed pull request #5638: GEODE-8584: AutoCloseable for ByteBuffer Locking

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


   


----------------------------------------------------------------
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