Skip to content

Distributed Peer-to-Peer Web Search Engine and Intranet Search Appliance

Fix stale config writes during concurrent updates

Michael Peter Christenb5c969578323
diff --git a/source/net/yacy/kelondro/util/FileUtils.java b/source/net/yacy/kelondro/util/FileUtils.java
index 4487c305b..6948eaedd 100644
--- a/source/net/yacy/kelondro/util/FileUtils.java
+++ b/source/net/yacy/kelondro/util/FileUtils.java
@@ -538,6 +538,8 @@ public final class FileUtils {
final String lockKey = file.getAbsolutePath();
final Object lock = SAVE_MAP_LOCKS.computeIfAbsent(lockKey, k -> new Object());
synchronized (lock) {
+ /* Take the snapshot in write order so delayed callers cannot persist stale state. */
+ final Map<String, String> propsCopy = new HashMap<>(props);
File tf = null;
PrintWriter pw = null;
try {
@@ -553,7 +555,7 @@ public final class FileUtils {
pw = new PrintWriter(tf, StandardCharsets.UTF_8.name());
pw.println("# " + comment);
String key, value;
- for ( final Map.Entry<String, String> entry : props.entrySet() ) {
+ for ( final Map.Entry<String, String> entry : propsCopy.entrySet() ) {
key = entry.getKey();
if ( key != null ) {
key = StringUtils.replaceEach(key, unescaped_strings_in, escaped_strings_out);
diff --git a/source/net/yacy/server/serverSwitch.java b/source/net/yacy/server/serverSwitch.java
index 21fa8dd16..c5198e0e8 100644
--- a/source/net/yacy/server/serverSwitch.java
+++ b/source/net/yacy/server/serverSwitch.java
@@ -69,10 +69,10 @@ public class serverSwitch {
public final File appPath;
protected boolean firstInit;
public ConcurrentLog log;
- protected int serverJobs;
- private ConcurrentMap<String, String> configProps;
- private final ConcurrentMap<String, String> configRemoved;
- private final NavigableMap<String, BusyThread> workerThreads;
+ protected int serverJobs;
+ private ConcurrentMap<String, String> configProps;
+ private final ConcurrentMap<String, String> configRemoved;
+ private final NavigableMap<String, BusyThread> workerThreads;
private YaCyHttpServer httpserver; // implemented HttpServer
private final ConcurrentMap<String, Integer> upnpPortMap = new ConcurrentHashMap<>();
private boolean isConnectedViaUpnp;
@@ -305,11 +305,7 @@ public class serverSwitch {
// set the value
final String oldValue = this.configProps.put(key, value);
if (oldValue == null || !value.equals(oldValue)) {
- if (saveOriginContext.get() == SaveConfigOrigin.UI) {
- this.saveConfigUI();
- } else {
- this.saveConfigBot();
- }
+ this.saveConfig(saveOriginContext.get());
}
}
@@ -326,8 +322,8 @@ public class serverSwitch {
this.setConfig(key, a);
}
- public void removeConfig(final String key) {
- this.configProps.remove(key);
+ public void removeConfig(final String key) {
+ this.configProps.remove(key);
}
/**
@@ -516,8 +512,7 @@ public class serverSwitch {
}
private void saveConfig(final SaveConfigOrigin origin) {
- final ConcurrentMap<String, String> configPropsCopy = new ConcurrentHashMap<>(this.configProps);
- FileUtils.saveMap(this.configFile, configPropsCopy, this.configComment);
+ FileUtils.saveMap(this.configFile, this.configProps, this.configComment);
if (this.log != null && this.log.isFine()) {
this.log.fine("Saved config to " + this.configFile + " (origin=" + origin + ")");
}
diff --git a/test/java/net/yacy/server/serverSwitchTest.java b/test/java/net/yacy/server/serverSwitchTest.java
index 4d6efe044..381c2bcb2 100644
--- a/test/java/net/yacy/server/serverSwitchTest.java
+++ b/test/java/net/yacy/server/serverSwitchTest.java
@@ -3,6 +3,12 @@ package net.yacy.server;
import static org.junit.Assert.assertEquals;
import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
import org.junit.Rule;
import org.junit.Test;
@@ -63,4 +69,45 @@ public class serverSwitchTest {
peer = new serverSwitch(root, root, "missing.init", "settings/yacy.conf");
assertEquals(8090, peer.getPublicPort(SwitchboardConstants.SERVER_PORT, 8090));
}
+
+ @Test
+ public void concurrentConfigUpdatesRemainConsistentOnDisk() throws Exception {
+ final File root = this.temporaryFolder.newFolder("concurrent-peer");
+ final serverSwitch peer =
+ new serverSwitch(root, root, "missing.init", "settings/yacy.conf");
+ final int writerCount = 16;
+ final int updateCount = 8;
+ final CountDownLatch writersReady = new CountDownLatch(writerCount);
+ final CountDownLatch startWriters = new CountDownLatch(1);
+ final ExecutorService executor = Executors.newFixedThreadPool(writerCount);
+ final List<Future<?>> updates = new ArrayList<>();
+
+ try {
+ for (int writer = 0; writer < writerCount; writer++) {
+ final int writerId = writer;
+ updates.add(executor.submit(() -> {
+ writersReady.countDown();
+ startWriters.await();
+ for (int update = 0; update < updateCount; update++) {
+ peer.setConfig("concurrent." + writerId, Integer.toString(update));
+ }
+ return null;
+ }));
+ }
+ writersReady.await();
+ startWriters.countDown();
+ for (final Future<?> update : updates) {
+ update.get();
+ }
+ } finally {
+ executor.shutdownNow();
+ }
+
+ final serverSwitch reloaded =
+ new serverSwitch(root, root, "missing.init", "settings/yacy.conf");
+ for (int writer = 0; writer < writerCount; writer++) {
+ assertEquals(Integer.toString(updateCount - 1),
+ reloaded.getConfig("concurrent." + writer, null));
+ }
+ }
}