[ISSUE #12207] fix disk failover datasource not keep status (#12188)

* fix disk failover datasource not keep status

* fix style

* fix DiskFailoverDataSource
This commit is contained in:
shalk(xiao kun) 2024-06-26 17:50:45 +08:00 committed by GitHub
parent 8034da88e5
commit b8d13e0557
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 33 additions and 12 deletions

View File

@ -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.FailoverDataSource;
import com.alibaba.nacos.client.naming.backups.FailoverSwitch; import com.alibaba.nacos.client.naming.backups.FailoverSwitch;
import com.alibaba.nacos.client.naming.backups.NamingFailoverData; 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.cache.DiskCache;
import com.alibaba.nacos.client.naming.utils.CacheDirUtil; import com.alibaba.nacos.client.naming.utils.CacheDirUtil;
import com.alibaba.nacos.client.naming.utils.UtilAndComs; import com.alibaba.nacos.client.naming.utils.UtilAndComs;
import com.alibaba.nacos.client.utils.ConcurrentDiskUtil;
import com.alibaba.nacos.common.utils.StringUtils; import com.alibaba.nacos.common.utils.StringUtils;
import java.io.File; import java.io.File;
@ -51,16 +51,21 @@ public class DiskFailoverDataSource implements FailoverDataSource {
private static final String FAILOVER_MODE_PARAM = "failover-mode"; 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 final Map<String, String> switchParams = new ConcurrentHashMap<>();
private Map<String, FailoverData> serviceMap = new ConcurrentHashMap<>();
private String failoverDir; private String failoverDir;
private long lastModifiedMillis = 0L; private long lastModifiedMillis = 0L;
public DiskFailoverDataSource() { public DiskFailoverDataSource() {
failoverDir = CacheDirUtil.getCacheDir() + FAILOVER_DIR; failoverDir = CacheDirUtil.getCacheDir() + FAILOVER_DIR;
switchParams.put(FAILOVER_MODE_PARAM, Boolean.FALSE.toString());
} }
class FailoverFileReader implements Runnable { class FailoverFileReader implements Runnable {
@ -107,15 +112,15 @@ public class DiskFailoverDataSource implements FailoverDataSource {
File switchFile = Paths.get(failoverDir, UtilAndComs.FAILOVER_SWITCH).toFile(); File switchFile = Paths.get(failoverDir, UtilAndComs.FAILOVER_SWITCH).toFile();
if (!switchFile.exists()) { if (!switchFile.exists()) {
NAMING_LOGGER.debug("failover switch is not found, {}", switchFile.getName()); 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(); long modified = switchFile.lastModified();
if (lastModifiedMillis < modified) { if (lastModifiedMillis < modified) {
lastModifiedMillis = modified; lastModifiedMillis = modified;
String failover = ConcurrentDiskUtil String failover = ConcurrentDiskUtil.getFileContent(switchFile.getPath(), Charset.defaultCharset().toString());
.getFileContent(switchFile.getPath(), Charset.defaultCharset().toString());
if (!StringUtils.isEmpty(failover)) { if (!StringUtils.isEmpty(failover)) {
String[] lines = failover.split(DiskCache.getLineSeparator()); String[] lines = failover.split(DiskCache.getLineSeparator());
@ -125,21 +130,22 @@ public class DiskFailoverDataSource implements FailoverDataSource {
switchParams.put(FAILOVER_MODE_PARAM, Boolean.TRUE.toString()); switchParams.put(FAILOVER_MODE_PARAM, Boolean.TRUE.toString());
NAMING_LOGGER.info("failover-mode is on"); NAMING_LOGGER.info("failover-mode is on");
new FailoverFileReader().run(); new FailoverFileReader().run();
return new FailoverSwitch(Boolean.TRUE); return FAILOVER_SWITCH_TRUE;
} else if (NO_FAILOVER_MODE.equals(line1)) { } else if (NO_FAILOVER_MODE.equals(line1)) {
switchParams.put(FAILOVER_MODE_PARAM, Boolean.FALSE.toString()); switchParams.put(FAILOVER_MODE_PARAM, Boolean.FALSE.toString());
NAMING_LOGGER.info("failover-mode is off"); 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) { } catch (Throwable e) {
NAMING_LOGGER.error("[NA] failed to read failover switch.", e); NAMING_LOGGER.error("[NA] failed to read failover switch.", e);
switchParams.put(FAILOVER_MODE_PARAM, Boolean.FALSE.toString());
return FAILOVER_SWITCH_FALSE;
} }
switchParams.put(FAILOVER_MODE_PARAM, Boolean.FALSE.toString());
return new FailoverSwitch(Boolean.FALSE);
} }
@Override @Override
@ -149,5 +155,4 @@ public class DiskFailoverDataSource implements FailoverDataSource {
} }
return new ConcurrentHashMap<>(0); return new ConcurrentHashMap<>(0);
} }
}
}

View File

@ -77,4 +77,20 @@ class DiskFailoverDataSourceTest {
failoverDirField.setAccessible(true); failoverDirField.setAccessible(true);
failoverDirField.set(dataSource, failoverDir); 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());
}
}