* fix disk failover datasource not keep status * fix style * fix DiskFailoverDataSource
This commit is contained in:
parent
8034da88e5
commit
b8d13e0557
@ -21,10 +21,10 @@ import com.alibaba.nacos.client.naming.backups.FailoverData;
|
||||
import com.alibaba.nacos.client.naming.backups.FailoverDataSource;
|
||||
import com.alibaba.nacos.client.naming.backups.FailoverSwitch;
|
||||
import com.alibaba.nacos.client.naming.backups.NamingFailoverData;
|
||||
import com.alibaba.nacos.client.utils.ConcurrentDiskUtil;
|
||||
import com.alibaba.nacos.client.naming.cache.DiskCache;
|
||||
import com.alibaba.nacos.client.naming.utils.CacheDirUtil;
|
||||
import com.alibaba.nacos.client.naming.utils.UtilAndComs;
|
||||
import com.alibaba.nacos.client.utils.ConcurrentDiskUtil;
|
||||
import com.alibaba.nacos.common.utils.StringUtils;
|
||||
|
||||
import java.io.File;
|
||||
@ -51,16 +51,21 @@ public class DiskFailoverDataSource implements FailoverDataSource {
|
||||
|
||||
private static final String FAILOVER_MODE_PARAM = "failover-mode";
|
||||
|
||||
private Map<String, FailoverData> serviceMap = new ConcurrentHashMap<>();
|
||||
private static final FailoverSwitch FAILOVER_SWITCH_FALSE = new FailoverSwitch(Boolean.FALSE);
|
||||
|
||||
private static final FailoverSwitch FAILOVER_SWITCH_TRUE = new FailoverSwitch(Boolean.TRUE);
|
||||
|
||||
private final Map<String, String> switchParams = new ConcurrentHashMap<>();
|
||||
|
||||
private Map<String, FailoverData> serviceMap = new ConcurrentHashMap<>();
|
||||
|
||||
private String failoverDir;
|
||||
|
||||
private long lastModifiedMillis = 0L;
|
||||
|
||||
public DiskFailoverDataSource() {
|
||||
failoverDir = CacheDirUtil.getCacheDir() + FAILOVER_DIR;
|
||||
switchParams.put(FAILOVER_MODE_PARAM, Boolean.FALSE.toString());
|
||||
}
|
||||
|
||||
class FailoverFileReader implements Runnable {
|
||||
@ -107,15 +112,15 @@ public class DiskFailoverDataSource implements FailoverDataSource {
|
||||
File switchFile = Paths.get(failoverDir, UtilAndComs.FAILOVER_SWITCH).toFile();
|
||||
if (!switchFile.exists()) {
|
||||
NAMING_LOGGER.debug("failover switch is not found, {}", switchFile.getName());
|
||||
return new FailoverSwitch(Boolean.FALSE);
|
||||
switchParams.put(FAILOVER_MODE_PARAM, Boolean.FALSE.toString());
|
||||
return FAILOVER_SWITCH_FALSE;
|
||||
}
|
||||
|
||||
long modified = switchFile.lastModified();
|
||||
|
||||
if (lastModifiedMillis < modified) {
|
||||
lastModifiedMillis = modified;
|
||||
String failover = ConcurrentDiskUtil
|
||||
.getFileContent(switchFile.getPath(), Charset.defaultCharset().toString());
|
||||
String failover = ConcurrentDiskUtil.getFileContent(switchFile.getPath(), Charset.defaultCharset().toString());
|
||||
if (!StringUtils.isEmpty(failover)) {
|
||||
String[] lines = failover.split(DiskCache.getLineSeparator());
|
||||
|
||||
@ -125,21 +130,22 @@ public class DiskFailoverDataSource implements FailoverDataSource {
|
||||
switchParams.put(FAILOVER_MODE_PARAM, Boolean.TRUE.toString());
|
||||
NAMING_LOGGER.info("failover-mode is on");
|
||||
new FailoverFileReader().run();
|
||||
return new FailoverSwitch(Boolean.TRUE);
|
||||
return FAILOVER_SWITCH_TRUE;
|
||||
} else if (NO_FAILOVER_MODE.equals(line1)) {
|
||||
switchParams.put(FAILOVER_MODE_PARAM, Boolean.FALSE.toString());
|
||||
NAMING_LOGGER.info("failover-mode is off");
|
||||
return FAILOVER_SWITCH_FALSE;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return switchParams.get(FAILOVER_MODE_PARAM).equals(Boolean.TRUE.toString()) ? FAILOVER_SWITCH_TRUE : FAILOVER_SWITCH_FALSE;
|
||||
|
||||
} catch (Throwable e) {
|
||||
NAMING_LOGGER.error("[NA] failed to read failover switch.", e);
|
||||
}
|
||||
|
||||
switchParams.put(FAILOVER_MODE_PARAM, Boolean.FALSE.toString());
|
||||
return new FailoverSwitch(Boolean.FALSE);
|
||||
return FAILOVER_SWITCH_FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -149,5 +155,4 @@ public class DiskFailoverDataSource implements FailoverDataSource {
|
||||
}
|
||||
return new ConcurrentHashMap<>(0);
|
||||
}
|
||||
|
||||
}
|
@ -77,4 +77,20 @@ class DiskFailoverDataSourceTest {
|
||||
failoverDirField.setAccessible(true);
|
||||
failoverDirField.set(dataSource, failoverDir);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetSwitchForFailoverEnabledKeep() throws NoSuchFieldException, IllegalAccessException {
|
||||
String dir = DiskFailoverDataSourceTest.class.getResource("/").getPath() + "/failover_test/enabled";
|
||||
injectFailOverDir(dir);
|
||||
assertTrue(dataSource.getSwitch().getEnabled());
|
||||
assertTrue(dataSource.getSwitch().getEnabled());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetSwitchForFailoverDisabledKeep() throws NoSuchFieldException, IllegalAccessException {
|
||||
String dir = DiskFailoverDataSourceTest.class.getResource("/").getPath() + "/failover_test/disabled";
|
||||
injectFailOverDir(dir);
|
||||
assertFalse(dataSource.getSwitch().getEnabled());
|
||||
assertFalse(dataSource.getSwitch().getEnabled());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user