Skip to content

Commit 7d63a44

Browse files
Vladimir Kotalahornace
Vladimir Kotal
authored andcommitted
replace HashSet with concurrent set
fixes #3397
1 parent 5ec8ffa commit 7d63a44

File tree

1 file changed

+15
-29
lines changed

1 file changed

+15
-29
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/index/PendingFileCompleter.java

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
/*
2121
* Copyright (c) 2017, 2020, Chris Fraire <[email protected]>.
22+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
2223
*/
2324
package org.opengrok.indexer.index;
2425

@@ -36,11 +37,11 @@
3637
import java.time.Duration;
3738
import java.time.Instant;
3839
import java.util.Comparator;
39-
import java.util.HashSet;
4040
import java.util.List;
4141
import java.util.Map;
4242
import java.util.Set;
4343
import java.util.TreeSet;
44+
import java.util.concurrent.ConcurrentHashMap;
4445
import java.util.logging.Level;
4546
import java.util.logging.Logger;
4647
import java.util.stream.Collectors;
@@ -55,23 +56,15 @@
5556
* be executed.
5657
* <p>
5758
* {@link PendingFileCompleter} is not generally thread-safe, as only
58-
* {@link #add(org.opengrok.indexer.index.PendingFileRenaming)} is expected
59-
* to be run in parallel; that method is thread-safe -- but only among other
60-
* callers of the same method.
59+
* {@link #add(org.opengrok.indexer.index.PendingFileRenaming)},
60+
* {@link #add(org.opengrok.indexer.index.PendingSymlinkage)} and
61+
* {@link #add(org.opengrok.indexer.index.PendingFileDeletion)} are expected
62+
* to be run in parallel; these methods are thread-safe w.r.t. underlying data structures.
6163
* <p>
62-
* No methods are thread-safe between each other. E.g.,
64+
* No other methods are thread-safe between each other. E.g.,
6365
* {@link #complete()} should only be called by a single thread after all
6466
* additions of {@link PendingSymlinkage}s, {@link PendingFileDeletion}s, and
6567
* {@link PendingFileRenaming}s are indicated.
66-
* <p>
67-
* {@link #add(org.opengrok.indexer.index.PendingSymlinkage)} should only
68-
* be called in serial from a single thread in an isolated stage.
69-
* <p>
70-
* {@link #add(org.opengrok.indexer.index.PendingFileDeletion)} should only
71-
* be called in serial from a single thread in an isolated stage.
72-
* <p>
73-
* {@link #add(org.opengrok.indexer.index.PendingFileRenaming)}, as noted,
74-
* can be called in parallel in an isolated stage.
7568
*/
7669
class PendingFileCompleter {
7770

@@ -82,10 +75,7 @@ class PendingFileCompleter {
8275
*/
8376
public static final String PENDING_EXTENSION = ".org_opengrok";
8477

85-
private static final Logger LOGGER =
86-
LoggerFactory.getLogger(PendingFileCompleter.class);
87-
88-
private final Object INSTANCE_LOCK = new Object();
78+
private static final Logger LOGGER = LoggerFactory.getLogger(PendingFileCompleter.class);
8979

9080
private volatile boolean completing;
9181

@@ -110,11 +100,11 @@ class PendingFileCompleter {
110100
return cmp;
111101
};
112102

113-
private final Set<PendingFileDeletion> deletions = new HashSet<>();
103+
private final Set<PendingFileDeletion> deletions = ConcurrentHashMap.newKeySet();
114104

115-
private final Set<PendingFileRenaming> renames = new HashSet<>();
105+
private final Set<PendingFileRenaming> renames = ConcurrentHashMap.newKeySet();
116106

117-
private final Set<PendingSymlinkage> linkages = new HashSet<>();
107+
private final Set<PendingSymlinkage> linkages = ConcurrentHashMap.newKeySet();
118108

119109
/**
120110
* Adds the specified element to this instance's set if it is not already
@@ -148,9 +138,7 @@ public boolean add(PendingSymlinkage e) {
148138

149139
/**
150140
* Adds the specified element to this instance's set if it is not already
151-
* present, and also remove any pending deletion for the same absolute
152-
* path -- all in a thread-safe manner among other callers of this same
153-
* method (and only this method).
141+
* present, and also remove any pending deletion for the same absolute path.
154142
* @param e element to be added to this set
155143
* @return {@code true} if this instance's set did not already contain the
156144
* specified element
@@ -160,11 +148,9 @@ public boolean add(PendingFileRenaming e) {
160148
if (completing) {
161149
throw new IllegalStateException("complete() is running");
162150
}
163-
synchronized (INSTANCE_LOCK) {
164-
boolean rc = renames.add(e);
165-
deletions.remove(new PendingFileDeletion(e.getAbsolutePath()));
166-
return rc;
167-
}
151+
boolean rc = renames.add(e);
152+
deletions.remove(new PendingFileDeletion(e.getAbsolutePath()));
153+
return rc;
168154
}
169155

170156
/**

0 commit comments

Comments
 (0)