Skip to content

Commit

Permalink
[ISSUE #12207] fix disk failover datasource not keep status (#12188)
Browse files Browse the repository at this point in the history
* fix disk failover datasource not keep status

* fix style

* fix DiskFailoverDataSource
  • Loading branch information
shalk authored Jun 26, 2024
1 parent 8034da8 commit b8d13e0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
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;
Expand All @@ -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 {
Expand Down Expand Up @@ -107,15 +112,15 @@ public FailoverSwitch getSwitch() {
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());

Expand All @@ -125,21 +130,22 @@ public FailoverSwitch getSwitch() {
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 FAILOVER_SWITCH_FALSE;
}

switchParams.put(FAILOVER_MODE_PARAM, Boolean.FALSE.toString());
return new FailoverSwitch(Boolean.FALSE);
}

@Override
Expand All @@ -149,5 +155,4 @@ public Map<String, FailoverData> getFailoverData() {
}
return new ConcurrentHashMap<>(0);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,20 @@ private void injectFailOverDir(String failoverDir) throws NoSuchFieldException,
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());
}
}

0 comments on commit b8d13e0

Please sign in to comment.