Skip to content

Commit d630961

Browse files
authored
Merge pull request #4573 from evolvedbinary/hotfix/broker-sync-readonly
Should never synchronize on a Boolean
2 parents 22e740c + 8f8129e commit d630961

File tree

1 file changed

+18
-26
lines changed

1 file changed

+18
-26
lines changed

exist-core/src/main/java/org/exist/storage/BrokerPool.java

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import com.evolvedbinary.j8fu.fsm.AtomicFSM;
2525
import com.evolvedbinary.j8fu.fsm.FSM;
2626
import com.evolvedbinary.j8fu.lazy.AtomicLazyVal;
27-
import net.jcip.annotations.GuardedBy;
2827
import net.jcip.annotations.ThreadSafe;
2928
import org.apache.logging.log4j.LogManager;
3029
import org.apache.logging.log4j.Logger;
@@ -87,6 +86,7 @@
8786
import java.util.Map.Entry;
8887
import java.util.concurrent.ConcurrentHashMap;
8988
import java.util.concurrent.ConcurrentSkipListSet;
89+
import java.util.concurrent.atomic.AtomicBoolean;
9090
import java.util.concurrent.locks.Lock;
9191
import java.util.concurrent.locks.ReentrantLock;
9292
import java.util.function.Consumer;
@@ -249,7 +249,7 @@ public String getStatus() {
249249
/**
250250
* Indicates whether the database is operating in read-only mode
251251
*/
252-
@GuardedBy("itself") private Boolean readOnly = Boolean.FALSE;
252+
private final AtomicBoolean readOnly = new AtomicBoolean();
253253

254254
@ConfigurationFieldAsAttribute("pageSize")
255255
private final int pageSize;
@@ -451,10 +451,9 @@ void initialize() throws EXistException, DatabaseConfigurationException {
451451
_initialize();
452452
} catch(final Throwable e) {
453453
// remove that file lock we may have acquired in canReadDataDir
454-
synchronized(readOnly) {
455-
if (dataLock != null && !readOnly) {
456-
dataLock.release();
457-
}
454+
455+
if (dataLock != null && !readOnly.get()) {
456+
dataLock.release();
458457
}
459458

460459
if(e instanceof EXistException) {
@@ -975,29 +974,25 @@ public NotificationService getNotificationService() {
975974
* @return <code>true</code> if transactions can be handled
976975
*/
977976
public boolean isRecoveryEnabled() {
978-
synchronized(readOnly) {
979-
return !readOnly && recoveryEnabled;
980-
}
977+
return !readOnly.get() && recoveryEnabled;
981978
}
982979

983980
@Override
984981
public boolean isReadOnly() {
985-
synchronized(readOnly) {
986-
if(!readOnly) {
987-
final long freeSpace = FileUtils.measureFileStore(dataLock.getFile(), FileStore::getUsableSpace);
988-
if (freeSpace != -1 && freeSpace < diskSpaceMin) {
989-
LOG.fatal("Partition containing DATA_DIR: {} is running out of disk space [minimum: {} free: {}]. Switching eXist-db into read-only mode to prevent data loss!", dataLock.getFile().toAbsolutePath().toString(), diskSpaceMin, freeSpace);
990-
setReadOnly();
991-
}
982+
final boolean isReadOnly = readOnly.get();
983+
if (!isReadOnly) {
984+
final long freeSpace = FileUtils.measureFileStore(dataLock.getFile(), FileStore::getUsableSpace);
985+
if (freeSpace != -1 && freeSpace < diskSpaceMin) {
986+
LOG.fatal("Partition containing DATA_DIR: {} is running out of disk space [minimum: {} free: {}]. Switching eXist-db into read-only mode to prevent data loss!", dataLock.getFile().toAbsolutePath().toString(), diskSpaceMin, freeSpace);
987+
setReadOnly();
992988
}
993-
return readOnly;
994989
}
990+
return readOnly.get();
995991
}
996992

997993
public void setReadOnly() {
998-
LOG.warn("Switching database into read-only mode!");
999-
synchronized (readOnly) {
1000-
readOnly = true;
994+
if (readOnly.compareAndSet(false, true)) {
995+
LOG.warn("Switched database into read-only mode!");
1001996
}
1002997
}
1003998

@@ -1728,12 +1723,9 @@ void shutdown(final boolean killed, final Consumer<String> shutdownInstanceConsu
17281723

17291724
//Clear the living instances container
17301725
shutdownInstanceConsumer.accept(instanceName);
1731-
1732-
synchronized (readOnly) {
1733-
if (!readOnly) {
1734-
// release the lock on the data directory
1735-
dataLock.release();
1736-
}
1726+
if (!readOnly.get()) {
1727+
// release the lock on the data directory
1728+
dataLock.release();
17371729
}
17381730

17391731
//clearing additional resources, like ThreadLocal

0 commit comments

Comments
 (0)