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


##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigtable/BigtableServiceFactory.java:
##########
@@ -105,8 +104,12 @@ BigtableServiceEntry getServiceForReading(
       BigtableServiceEntry entry = entries.get(configId.id());
       if (entry != null) {
         // When entry is not null, refCount.get(configId.id()) should always 
exist.
-        // Do a getOrDefault to avoid unexpected NPEs.
-        refCounts.getOrDefault(configId.id(), new 
AtomicInteger(0)).getAndIncrement();
+        // Doing a putIfAbsent to avoid NPE.
+        AtomicInteger count = refCounts.putIfAbsent(configId.id(), new 
AtomicInteger(0));
+        if (count == null) {
+          LOG.error("entry is not null but refCount of config Id " + 
configId.id() + " is null.");
+        }

Review Comment:
   I believe you've experienced the racing and there was a reason to add it, 
though, so this should be fine -- but it appears that this branch is spurious / 
never reachable.
   
   It appears that what the previous code could simply be fixed by using 
putIfAbsent?
   
   ```
   refCounts.putIfAbsent(configId.id(), new AtomicInteger(0)).getAndIncrement();
   ```
   
   You may not even need `refCounts.put(configId.id(), new AtomicInteger(1));` 
which is done later, so you have only 1 AtomicInteger per config id
   
   



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to