@@ -84,7 +84,8 @@ public MySQLHeartbeat(PhysicalDbInstance dbInstance) {
8484 this .heartbeatTimeout = dbInstance .getDbGroupConfig ().getHeartbeatTimeout ();
8585 this .isDelayDetection = dbInstance .getDbGroupConfig ().isDelayDetection ();
8686 if (isDelayDetection ) {
87- this .heartbeatSQL = getDetectorSql (dbInstance .getDbGroupConfig ().getName (), dbInstance .getDbGroupConfig ().getDelayDatabase ());
87+ this .heartbeatSQL = getDetectorSql (dbInstance .getDbGroupConfig ().getName (),
88+ dbInstance .getDbGroupConfig ().getDelayDatabase (), dbInstance .isReadInstance ());
8889 } else {
8990 this .heartbeatSQL = source .getDbGroupConfig ().getHeartbeatSQL ();
9091 }
@@ -181,12 +182,12 @@ public void heartbeat() {
181182 }
182183 }
183184
184- private String getDetectorSql (String dbGroupName , String delayDatabase ) {
185+ private String getDetectorSql (String dbGroupName , String delayDatabase , boolean readInstance ) {
185186 String [] str = {"dble" , dbGroupName , SystemConfig .getInstance ().getInstanceName ()};
186187 String sourceName = Joiner .on ("_" ).join (str );
187188 String sqlTableName = delayDatabase + ".u_delay " ;
188189 String detectorSql ;
189- if (!source . isReadInstance () ) {
190+ if (!readInstance ) {
190191 String update = "replace into ? (source,real_timestamp,logic_timestamp) values ('?','?',?)" ;
191192 detectorSql = convert (update , Lists .newArrayList (sqlTableName , sourceName ));
192193 } else {
@@ -199,9 +200,14 @@ private String getDetectorSql(String dbGroupName, String delayDatabase) {
199200 private String convert (String template , List <String > list ) {
200201 StringBuilder sb = new StringBuilder (template );
201202 String replace = "?" ;
203+ int fromIndex = 0 ;
202204 for (String str : list ) {
203- int index = sb .indexOf (replace );
204- sb .replace (index , index + 1 , str );
205+ int index = sb .indexOf (replace , fromIndex );
206+ if (index < 0 ) {
207+ throw new IllegalArgumentException ("heartbeat sql template placeholder '?' not enough, template=" + template + ", values=" + list );
208+ }
209+ sb .replace (index , index + replace .length (), str );
210+ fromIndex = index + str .length ();
205211 }
206212 return sb .toString ();
207213 }
@@ -387,11 +393,17 @@ public long getHeartbeatTimeout() {
387393 }
388394
389395 String getHeartbeatSQL () {
390- if (isDelayDetection && !source .isReadInstance ()) {
391- return convert (heartbeatSQL , Lists .newArrayList (String .valueOf (LocalDateTime .now ()), String .valueOf (source .getDbGroup ().getLogicTimestamp ().incrementAndGet ())));
392- } else {
393- return heartbeatSQL ;
396+ if (isDelayDetection ) {
397+ boolean readInstance = source .isReadInstance ();
398+ String detectorSql = getDetectorSql (source .getDbGroupConfig ().getName (),
399+ source .getDbGroupConfig ().getDelayDatabase (), readInstance );
400+ if (!readInstance ) {
401+ return convert (detectorSql , Lists .newArrayList (String .valueOf (LocalDateTime .now ()),
402+ String .valueOf (source .getDbGroup ().getLogicTimestamp ().incrementAndGet ())));
403+ }
404+ return detectorSql ;
394405 }
406+ return heartbeatSQL ;
395407 }
396408
397409 public DbInstanceSyncRecorder getAsyncRecorder () {
0 commit comments