You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by "bvolpato (via GitHub)" <gi...@apache.org> on 2023/08/23 17:07:34 UTC

[GitHub] [beam] bvolpato commented on a diff in pull request #28122: fix: fix a race condition in BigtableService cache

bvolpato commented on code in PR #28122:
URL: https://github.com/apache/beam/pull/28122#discussion_r1303312575


##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigtable/BigtableServiceFactory.java:
##########
@@ -75,15 +76,15 @@ static BigtableServiceEntry create(ConfigId configId, BigtableService service) {
 
     @Override
     public void close() {
-      int refCount =
-          refCounts.getOrDefault(getConfigId().id(), new AtomicInteger(0)).decrementAndGet();
-      if (refCount < 0) {
-        LOG.error(
-            "close() Ref count is < 0, configId=" + getConfigId().id() + " refCount=" + refCount);
-      }
-      LOG.debug("close() for config id " + getConfigId().id() + ", ref count is " + refCount);
-      if (refCount == 0) {
-        synchronized (lock) {
+      synchronized (lock) {
+        int refCount =
+            refCounts.getOrDefault(getConfigId().id(), new AtomicInteger(0)).decrementAndGet();
+        if (refCount < 0) {
+          LOG.error(
+              "close() Ref count is < 0, configId=" + getConfigId().id() + " refCount=" + refCount);
+        }
+        LOG.debug("close() for config id " + getConfigId().id() + ", ref count is " + refCount);
+        if (refCount == 0) {

Review Comment:
    decrementAndCount is thread safe, so I wonder what's the actual underlying issue here.
   
   I also understand why < 0 is logging an error, but shouldn't we `.remove()` in that case too? Instead of doing `== 0` in the next if



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

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

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