@@ -146,19 +146,19 @@ public IoTConsensusServerImpl(
146146 this .backgroundTaskService = backgroundTaskService ;
147147 this .config = config ;
148148 this .consensusGroupId = thisNode .getGroupId ().toString ();
149- consensusReqReader = (ConsensusReqReader ) stateMachine .read (new GetConsensusReqReaderPlan ());
149+ this .consensusReqReader =
150+ (ConsensusReqReader ) stateMachine .read (new GetConsensusReqReaderPlan ());
150151 this .searchIndex = new AtomicLong (consensusReqReader .getCurrentSearchIndex ());
151152 this .ioTConsensusServerMetrics = new IoTConsensusServerMetrics (this );
152153 this .logDispatcher = new LogDispatcher (this , clientManager );
153- // see message in logs for details
154- checkAndUpdateSearchIndex ();
155154 }
156155
157156 public IStateMachine getStateMachine () {
158157 return stateMachine ;
159158 }
160159
161160 public void start () {
161+ checkAndUpdateIndex ();
162162 MetricService .getInstance ().addMetricSet (this .ioTConsensusServerMetrics );
163163 stateMachine .start ();
164164 logDispatcher .start ();
@@ -830,9 +830,19 @@ public void cleanupLocalSnapshot() {
830830 }
831831 }
832832
833+ void checkAndUpdateIndex () {
834+ // Since the underlying wal does not persist safelyDeletedSearchIndex, IoTConsensus needs to
835+ // update wal with its syncIndex recovered from the consensus layer when initializing.
836+ // This prevents wal from being piled up if the safelyDeletedSearchIndex is not updated after
837+ // the restart and Leader migration occurs
838+ checkAndUpdateSafeDeletedSearchIndex ();
839+ // see message in logs for details
840+ checkAndUpdateSearchIndex ();
841+ }
842+
833843 /**
834- * If there is only one replica, set it to Long.MAX_VALUE.、 If there are multiple replicas, get
835- * the latest SafelyDeletedSearchIndex again. This enables wal to be deleted in a timely manner.
844+ * If there is only one replica, set it to Long.MAX_VALUE. If there are multiple replicas, get the
845+ * latest SafelyDeletedSearchIndex again. This enables wal to be deleted in a timely manner.
836846 */
837847 void checkAndUpdateSafeDeletedSearchIndex () {
838848 if (configuration .isEmpty ()) {
0 commit comments