tried too fix serverSwitch synchronization problems

see also: http://www.yacy-forum.de/viewtopic.php?p=16110#16110

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@1499 6c8d7289-2bf4-0310-a012-ef5d649a1542
pull/1/head
orbiter 19 years ago
parent 3419b3bcdd
commit ec5d88664a

@ -53,6 +53,8 @@ import de.anomic.server.serverAbstractSwitch;
public abstract class serverAbstractSwitch implements serverSwitch { public abstract class serverAbstractSwitch implements serverSwitch {
private static final long delayBetweenSave = 2000;
// configuration management // configuration management
private final File configFile; private final File configFile;
private Map configProps; private Map configProps;
@ -64,6 +66,7 @@ public abstract class serverAbstractSwitch implements serverSwitch {
private final TreeMap switchActions; private final TreeMap switchActions;
protected serverLog log; protected serverLog log;
protected int serverJobs; protected int serverJobs;
private long lastTimeSaved;
public serverAbstractSwitch(String rootPath, String initPath, String configPath) { public serverAbstractSwitch(String rootPath, String initPath, String configPath) {
// we initialize the switchboard with a property file, // we initialize the switchboard with a property file,
@ -130,6 +133,9 @@ public abstract class serverAbstractSwitch implements serverSwitch {
// init busy state control // init busy state control
serverJobs = 0; serverJobs = 0;
// save control
lastTimeSaved = System.currentTimeMillis();
} }
// a logger for this switchboard // a logger for this switchboard
@ -146,57 +152,62 @@ public abstract class serverAbstractSwitch implements serverSwitch {
} }
public void setConfig(String key, String value) { public void setConfig(String key, String value) {
// perform action before setting new value // perform action before setting new value
Map.Entry entry; synchronized (configProps) {
serverSwitchAction action; Map.Entry entry;
Iterator i = switchActions.entrySet().iterator(); serverSwitchAction action;
while (i.hasNext()) { Iterator i = switchActions.entrySet().iterator();
entry = (Map.Entry) i.next(); while (i.hasNext()) {
action = (serverSwitchAction) entry.getValue(); entry = (Map.Entry) i.next();
try { action = (serverSwitchAction) entry.getValue();
action.doBevoreSetConfig(key, value); try {
} catch (Exception e) { action.doBevoreSetConfig(key, value);
log.logSevere("serverAction bevoreSetConfig '" + action.getShortDescription() + "' failed with exception: " + e.getMessage()); } catch (Exception e) {
} log.logSevere("serverAction bevoreSetConfig '" + action.getShortDescription() + "' failed with exception: " + e.getMessage());
} }
}
// set the value
Object oldValue = configProps.put(key, value); // set the value
saveConfig(); Object oldValue = configProps.put(key, value);
saveConfig();
// perform actions afterwards
i = switchActions.entrySet().iterator(); // perform actions afterwards
while (i.hasNext()) { i = switchActions.entrySet().iterator();
entry = (Map.Entry) i.next(); while (i.hasNext()) {
action = (serverSwitchAction) entry.getValue(); entry = (Map.Entry) i.next();
try { action = (serverSwitchAction) entry.getValue();
action.doAfterSetConfig(key, value, (oldValue==null)?null:(String)oldValue); try {
} catch (Exception e) { action.doAfterSetConfig(key, value, (oldValue == null) ? null : (String) oldValue);
log.logSevere("serverAction afterSetConfig '" + action.getShortDescription() + "' failed with exception: " + e.getMessage()); } catch (Exception e) {
} log.logSevere("serverAction afterSetConfig '" + action.getShortDescription() + "' failed with exception: " + e.getMessage());
} }
}
}
} }
public String getConfig(String key, String dflt) { public String getConfig(String key, String dflt) {
// get the value synchronized (configProps) {
Object s = configProps.get(key); // get the value
Object s = configProps.get(key);
// do action
Map.Entry entry; // do action
serverSwitchAction action; Map.Entry entry;
Iterator i = switchActions.entrySet().iterator(); serverSwitchAction action;
while (i.hasNext()) { Iterator i = switchActions.entrySet().iterator();
entry = (Map.Entry) i.next(); while (i.hasNext()) {
action = (serverSwitchAction) entry.getValue(); entry = (Map.Entry) i.next();
try { action = (serverSwitchAction) entry.getValue();
action.doWhenGetConfig(key, (s==null)?null:(String)s, dflt); try {
} catch (Exception e) { action.doWhenGetConfig(key, (s == null) ? null : (String) s, dflt);
log.logSevere("serverAction whenGetConfig '" + action.getShortDescription() + "' failed with exception: " + e.getMessage()); } catch (Exception e) {
} log.logSevere("serverAction whenGetConfig '" + action.getShortDescription() + "' failed with exception: " + e.getMessage());
} }
}
// return value
if (s == null) return dflt; else return (String)s; // return value
if (s == null) return dflt;
else return (String) s;
}
} }
public long getConfigLong(String key, long dflt) { public long getConfigLong(String key, long dflt) {
@ -212,11 +223,16 @@ public abstract class serverAbstractSwitch implements serverSwitch {
} }
private void saveConfig() { private void saveConfig() {
try { if (System.currentTimeMillis() > this.lastTimeSaved + delayBetweenSave) {
serverFileUtils.saveMap(configFile, configProps, configComment); try {
} catch (IOException e) { synchronized (configProps) {
System.out.println("ERROR: cannot write config file " + configFile.toString() + ": " + e.getMessage()); serverFileUtils.saveMap(configFile, configProps, configComment);
} }
} catch (IOException e) {
System.out.println("ERROR: cannot write config file " + configFile.toString() + ": " + e.getMessage());
}
this.lastTimeSaved = System.currentTimeMillis();
}
} }
public Map getRemoved() { public Map getRemoved() {

@ -433,13 +433,13 @@ public final class yacy {
run.addShutdownHook(new shutdownHookThread(Thread.currentThread(), sb)); run.addShutdownHook(new shutdownHookThread(Thread.currentThread(), sb));
// save information about available memory after all initializations // save information about available memory after all initializations
try { //try {
sb.setConfig("memoryFreeAfterInitBGC", Runtime.getRuntime().freeMemory()); sb.setConfig("memoryFreeAfterInitBGC", Runtime.getRuntime().freeMemory());
sb.setConfig("memoryTotalAfterInitBGC", Runtime.getRuntime().totalMemory()); sb.setConfig("memoryTotalAfterInitBGC", Runtime.getRuntime().totalMemory());
System.gc(); System.gc();
sb.setConfig("memoryFreeAfterInitAGC", Runtime.getRuntime().freeMemory()); sb.setConfig("memoryFreeAfterInitAGC", Runtime.getRuntime().freeMemory());
sb.setConfig("memoryTotalAfterInitAGC", Runtime.getRuntime().totalMemory()); sb.setConfig("memoryTotalAfterInitAGC", Runtime.getRuntime().totalMemory());
} catch (ConcurrentModificationException e) {} //} catch (ConcurrentModificationException e) {}
// wait for server shutdown // wait for server shutdown
try { try {

Loading…
Cancel
Save