You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@zookeeper.apache.org by GitBox <gi...@apache.org> on 2019/03/27 01:54:41 UTC

[GitHub] [zookeeper] BELUGABEHR commented on a change in pull request #870: ZOOKEEPER-3335: Prefer ArrayList over LinkedList

BELUGABEHR commented on a change in pull request #870: ZOOKEEPER-3335: Prefer ArrayList over LinkedList
URL: https://github.com/apache/zookeeper/pull/870#discussion_r269380411
 
 

 ##########
 File path: zookeeper-server/src/main/java/org/apache/zookeeper/server/ZKDatabase.java
 ##########
 @@ -187,18 +190,21 @@ public ReentrantReadWriteLock getLogLock() {
     }
 
 
-    public synchronized List<Proposal> getCommittedLog() {
+    public synchronized Collection<Proposal> getCommittedLog() {
+        final Collection<Proposal> result;
         ReadLock rl = logLock.readLock();
-        // only make a copy if this thread isn't already holding a lock
-        if(logLock.getReadHoldCount() <=0) {
+        // make a copy if this thread is not already holding a lock
+        if (logLock.getReadHoldCount() > 0) {
+          result = this.committedLog;
+        } else {
+            rl.lock();
             try {
-                rl.lock();
-                return new LinkedList<Proposal>(this.committedLog);
+                result = new ArrayList<>(this.committedLog);
             } finally {
                 rl.unlock();
             }
         }
-        return this.committedLog;
+        return Collections.unmodifiableCollection(result);
 
 Review comment:
   Thanks @enixon !
   
   In one case, the caller is getting a point-in-time copy of the committedLog queue, and in the other case, the caller is getting the real thing.  That's already a bit unexpected, but I figured at the very least, the caller should not be allowed to modify the real thing outside the scope of this class.

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


With regards,
Apache Git Services