[ 
https://issues.apache.org/jira/browse/GEODE-8584?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17216944#comment-17216944
 ] 

ASF GitHub Bot commented on GEODE-8584:
---------------------------------------

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:
us...@infra.apache.org


> Message transmission fails with IllegalStateException in socket i/o code
> ------------------------------------------------------------------------
>
>                 Key: GEODE-8584
>                 URL: https://issues.apache.org/jira/browse/GEODE-8584
>             Project: Geode
>          Issue Type: Bug
>          Components: membership, messaging
>    Affects Versions: 1.12.0, 1.13.0, 1.14.0
>            Reporter: Bruce J Schuchardt
>            Priority: Major
>              Labels: pull-request-available
>             Fix For: 1.12.1, 1.14.0, 1.13.1
>
>
> Transmission of an update operation failed with a toData exception caused by 
> an IllegalStateException in socket i/o code.  It shouldn't be possible for 
> this exception to happen unless multiple threads are using the buffer at the 
> same time.
>  
> {noformat}
> org.apache.geode.ToDataException: toData failed on dsfid=71 msg:null
>       at 
> org.apache.geode.internal.InternalDataSerializer.writeDSFID(InternalDataSerializer.java:1509)
>       at 
> org.apache.geode.internal.tcp.MsgStreamer.writeMessage(MsgStreamer.java:249)
>       at 
> org.apache.geode.distributed.internal.direct.DirectChannel.sendToMany(DirectChannel.java:306)
>       at 
> org.apache.geode.distributed.internal.direct.DirectChannel.sendToOne(DirectChannel.java:182)
>       at 
> org.apache.geode.distributed.internal.direct.DirectChannel.send(DirectChannel.java:511)
>       at 
> org.apache.geode.distributed.internal.DistributionImpl.directChannelSend(DistributionImpl.java:346)
>       at 
> org.apache.geode.distributed.internal.DistributionImpl.send(DistributionImpl.java:291)
>       at 
> org.apache.geode.distributed.internal.ClusterDistributionManager.sendViaMembershipManager(ClusterDistributionManager.java:2053)
>       at 
> org.apache.geode.distributed.internal.ClusterDistributionManager.sendOutgoing(ClusterDistributionManager.java:1981)
>       at 
> org.apache.geode.distributed.internal.ClusterDistributionManager.sendMessage(ClusterDistributionManager.java:2018)
>       at 
> org.apache.geode.distributed.internal.ClusterDistributionManager.putOutgoing(ClusterDistributionManager.java:1083)
>       at 
> org.apache.geode.internal.cache.DistributedCacheOperation._distribute(DistributedCacheOperation.java:572)
>       at 
> org.apache.geode.internal.cache.DistributedCacheOperation.startOperation(DistributedCacheOperation.java:277)
>       at 
> org.apache.geode.internal.cache.DistributedCacheOperation.distribute(DistributedCacheOperation.java:318)
>       at 
> org.apache.geode.internal.cache.DistributedRegion.distributeUpdate(DistributedRegion.java:520)
>       at 
> org.apache.geode.internal.cache.DistributedRegion.basicPutPart3(DistributedRegion.java:498)
>       at 
> org.apache.geode.internal.cache.map.RegionMapPut.doAfterCompletionActions(RegionMapPut.java:307)
>       at 
> org.apache.geode.internal.cache.map.AbstractRegionMapPut.doPut(AbstractRegionMapPut.java:185)
>       at 
> org.apache.geode.internal.cache.map.AbstractRegionMapPut.runWhileLockedForCacheModification(AbstractRegionMapPut.java:119)
>       at 
> org.apache.geode.internal.cache.map.RegionMapPut.runWhileLockedForCacheModification(RegionMapPut.java:161)
>       at 
> org.apache.geode.internal.cache.map.AbstractRegionMapPut.put(AbstractRegionMapPut.java:169)
>       at 
> org.apache.geode.internal.cache.AbstractRegionMap.basicPut(AbstractRegionMap.java:2044)
>       at 
> org.apache.geode.internal.cache.LocalRegion.virtualPut(LocalRegion.java:5599)
>       at 
> org.apache.geode.internal.cache.DistributedRegion.virtualPut(DistributedRegion.java:393)
>       at 
> org.apache.geode.internal.cache.LocalRegion.virtualPut(LocalRegion.java:5577)
>       at 
> org.apache.geode.internal.cache.LocalRegionDataView.putEntry(LocalRegionDataView.java:157)
>       at 
> org.apache.geode.internal.cache.LocalRegion.basicPut(LocalRegion.java:5035)
>       at 
> org.apache.geode.internal.cache.LocalRegion.replaceWithCallbackArgument(LocalRegion.java:10600)
>       at 
> org.apache.geode.internal.cache.LocalRegion.replace(LocalRegion.java:10566)
>         [application code traces deleted]
> Caused by: java.lang.IllegalArgumentException
>       at java.nio.Buffer.position(Buffer.java:244)
>       at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:96)
>       at sun.nio.ch.IOUtil.write(IOUtil.java:51)
>       at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:469)
>       at 
> org.apache.geode.internal.tcp.Connection.writeFully(Connection.java:2596)
>       at 
> org.apache.geode.internal.tcp.Connection.sendPreserialized(Connection.java:1866)
>       at 
> org.apache.geode.internal.tcp.MsgStreamer.realFlush(MsgStreamer.java:330)
>       at org.apache.geode.internal.tcp.MsgStreamer.write(MsgStreamer.java:390)
>       at 
> org.apache.geode.DataSerializer.writeByteArray(DataSerializer.java:1185)
>       at 
> org.apache.geode.DataSerializer.writeByteArray(DataSerializer.java:1153)
>       at 
> org.apache.geode.internal.cache.DistributedCacheOperation.writeValue(DistributedCacheOperation.java:137)
>       at 
> org.apache.geode.internal.cache.UpdateOperation$UpdateMessage.toData(UpdateOperation.java:417)
>       at 
> org.apache.geode.internal.serialization.internal.DSFIDSerializerImpl.invokeToData(DSFIDSerializerImpl.java:213)
>       at 
> org.apache.geode.internal.serialization.internal.DSFIDSerializerImpl.write(DSFIDSerializerImpl.java:137)
>       at 
> org.apache.geode.internal.InternalDataSerializer.writeDSFID(InternalDataSerializer.java:1484)
>       ... 37 more {noformat}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to