2020package org .apache .iotdb .db .pipe .agent .runtime ;
2121
2222import org .apache .iotdb .commons .consensus .index .impl .SimpleProgressIndex ;
23- import org .apache .iotdb .commons .exception .StartupException ;
2423import org .apache .iotdb .commons .file .SystemFileFactory ;
2524import org .apache .iotdb .db .conf .IoTDBConfig ;
2625import org .apache .iotdb .db .conf .IoTDBDescriptor ;
3130import org .slf4j .LoggerFactory ;
3231
3332import java .io .File ;
33+ import java .io .FileOutputStream ;
3434import java .io .IOException ;
3535import java .nio .charset .StandardCharsets ;
3636import java .util .concurrent .atomic .AtomicLong ;
@@ -55,17 +55,21 @@ public class SimpleProgressIndexAssigner {
5555 private int rebootTimes = 0 ;
5656 private final AtomicLong insertionRequestId = new AtomicLong (1 );
5757
58- public void start () throws StartupException {
58+ public void start () {
5959 isSimpleConsensusEnable =
6060 IOTDB_CONFIG .getDataRegionConsensusProtocolClass ().equals (SIMPLE_CONSENSUS );
61- LOGGER .info ("Start SimpleProgressIndexAssigner ..." );
61+ LOGGER .info ("Starting SimpleProgressIndexAssigner ..." );
6262
6363 try {
6464 makeDirIfNecessary ();
6565 parseRebootTimes ();
6666 recordRebootTimes ();
67+ LOGGER .info (
68+ "SimpleProgressIndexAssigner started successfully. isSimpleConsensusEnable: {}, rebootTimes: {}" ,
69+ isSimpleConsensusEnable ,
70+ rebootTimes );
6771 } catch (Exception e ) {
68- throw new StartupException ( e );
72+ LOGGER . error ( "Cannot start SimpleProgressIndexAssigner because of {}" , e . getMessage (), e );
6973 }
7074 }
7175
@@ -86,15 +90,27 @@ private void parseRebootTimes() {
8690 try {
8791 String content = FileUtils .readFileToString (file , StandardCharsets .UTF_8 );
8892 rebootTimes = Integer .parseInt (content );
89- } catch (IOException e ) {
90- LOGGER .error ("Cannot parse reboot times from file {}" , file .getAbsolutePath (), e );
91- rebootTimes = 0 ;
93+ } catch (final Exception e ) {
94+ rebootTimes = (int ) (System .currentTimeMillis () / 1000 );
95+ LOGGER .error (
96+ "Cannot parse reboot times from file {}, set the current time in seconds ({}) as the reboot times" ,
97+ file .getAbsolutePath (),
98+ rebootTimes );
9299 }
93100 }
94101
95- private void recordRebootTimes () throws IOException {
96- File file = SystemFileFactory .INSTANCE .getFile (PIPE_SYSTEM_DIR + REBOOT_TIMES_FILE_NAME );
97- FileUtils .writeStringToFile (file , String .valueOf (rebootTimes + 1 ), StandardCharsets .UTF_8 );
102+ private void recordRebootTimes () {
103+ final File file = SystemFileFactory .INSTANCE .getFile (PIPE_SYSTEM_DIR + REBOOT_TIMES_FILE_NAME );
104+ try (final FileOutputStream fos = new FileOutputStream (file , false )) {
105+ fos .write (String .valueOf (rebootTimes + 1 ).getBytes (StandardCharsets .UTF_8 ));
106+ fos .flush ();
107+ fos .getFD ().sync ();
108+ } catch (final Exception e ) {
109+ LOGGER .error (
110+ "Cannot record reboot times {} to file {}, the reboot times will not be updated" ,
111+ rebootTimes ,
112+ file .getAbsolutePath ());
113+ }
98114 }
99115
100116 public void assignIfNeeded (InsertNode insertNode ) {
0 commit comments