Skip to content

Commit

Permalink
Fixed issue where ConfigNode might recover incorrectly under SimpleCo…
Browse files Browse the repository at this point in the history
…nsensus (apache#11969)
  • Loading branch information
HxpSerein authored Jan 25, 2024
1 parent 7200e21 commit 82d0459
Showing 1 changed file with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,13 @@
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Optional;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

/** StateMachine for ConfigRegion. */
public class ConfigRegionStateMachine implements IStateMachine, IStateMachine.EventApi {
Expand Down Expand Up @@ -84,6 +87,8 @@ public class ConfigRegionStateMachine implements IStateMachine, IStateMachine.Ev
private static final long LOG_FILE_MAX_SIZE =
CONF.getConfigNodeSimpleConsensusLogSegmentSizeMax();
private final TEndPoint currentNodeTEndPoint;
private static Pattern LOG_INPROGRESS_PATTERN = Pattern.compile("\\d+");
private static Pattern LOG_PATTERN = Pattern.compile("(?<=_)(\\d+)$");

public ConfigRegionStateMachine(ConfigManager configManager, ConfigPlanExecutor executor) {
this.executor = executor;
Expand Down Expand Up @@ -323,6 +328,7 @@ private void initStandAloneConfigNode() {
dir.mkdirs();
String[] list = new File(CURRENT_FILE_DIR).list();
if (list != null && list.length != 0) {
Arrays.sort(list, new FileComparator());
for (String logFileName : list) {
File logFile =
SystemFileFactory.INSTANCE.getFile(CURRENT_FILE_DIR + File.separator + logFileName);
Expand Down Expand Up @@ -395,4 +401,21 @@ private void createLogFile(int endIndex) {
e);
}
}

static class FileComparator implements Comparator<String> {
@Override
public int compare(String filename1, String filename2) {
long id1 = parseEndIndex(filename1);
long id2 = parseEndIndex(filename2);
return Long.compare(id1, id2);
}
}

static long parseEndIndex(String filename) {
if (filename.startsWith("log_inprogress_")) {
return Long.parseLong(LOG_INPROGRESS_PATTERN.matcher(filename).group());
} else {
return Long.parseLong(LOG_PATTERN.matcher(filename).group());
}
}
}

0 comments on commit 82d0459

Please sign in to comment.