Skip to content

Commit c08d974

Browse files
committed
Retry logic for sending log lines to Chronos Control
1 parent 4a05865 commit c08d974

2 files changed

Lines changed: 31 additions & 15 deletions

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ java {
3131
dependencies {
3232
implementation group: 'com.konghq', name: 'unirest-java', version: '3.14.5' // MIT
3333

34-
implementation group: 'commons-validator', name: 'commons-validator', version: '1.8' // Apache 2.0
34+
implementation group: 'commons-validator', name: 'commons-validator', version: '1.8.0' // Apache 2.0
3535
implementation group: 'commons-net', name: 'commons-net', version: '3.10.0' // Apache 2.0
3636
implementation group: 'commons-io', name: 'commons-io', version: '2.15.1' // Apache 2.0
3737

src/main/java/ch/unibas/dmi/dbis/chronos/agent/ChronosHttpClient.java

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,8 @@ public final class ChronosLogHandler {
839839

840840
final AtomicInteger sequenceNumber = new AtomicInteger();
841841

842+
final static int MAX_RETRIES = 1;
843+
842844

843845
public ChronosLogHandler( final ChronosJob job ) {
844846
this.executor = Executors.newSingleThreadExecutor();
@@ -855,22 +857,36 @@ public void publish( String message ) {
855857
}
856858

857859
pendingMessages.add( executor.submit( () -> {
858-
try {
859-
parameters.clear();
860-
parameters.put( "recordSequenceNumber", sequenceNumber.incrementAndGet() );
861-
parameters.put( "log", message );
862-
863-
final JSONObject jsonResponse = Unirest.post( getUrl( address, port, ChronosRestApi.JOB, query ) ).fields( parameters ).asJson().getBody().getObject();
864-
final JSONObject status = jsonResponse.getJSONObject( ChronosRestApi.STATUS_OBJECT_KEY );
865-
866-
if ( status.getInt( ChronosRestApi.STATUS_CODE_KEY ) != ChronosRestApi.STATUS_CODE__SUCCESS ) {
867-
log.warn( "Service returned: {}: {}",
868-
status.getInt( ChronosRestApi.STATUS_CODE_KEY ),
869-
status.getString( ChronosRestApi.STATUS_MESSAGE_KEY ) );
860+
int attempt = 0;
861+
while ( attempt < MAX_RETRIES ) {
862+
try {
863+
parameters.clear();
864+
parameters.put( "recordSequenceNumber", sequenceNumber.incrementAndGet() );
865+
parameters.put( "log", message );
866+
867+
JSONObject jsonResponse = Unirest.post( getUrl( address, port, ChronosRestApi.JOB, query ) )
868+
.fields( parameters ).asJson().getBody().getObject();
869+
JSONObject status = jsonResponse.getJSONObject( ChronosRestApi.STATUS_OBJECT_KEY );
870+
871+
if ( status.getInt( ChronosRestApi.STATUS_CODE_KEY ) == ChronosRestApi.STATUS_CODE__SUCCESS ) {
872+
return; // Success, exit retry loop
873+
} else {
874+
log.warn( "Service returned: {}: {}",
875+
status.getInt( ChronosRestApi.STATUS_CODE_KEY ),
876+
status.getString( ChronosRestApi.STATUS_MESSAGE_KEY ) );
877+
}
878+
} catch ( UnirestException ex ) {
879+
log.debug( "Exception while publishing log records. Attempt {}: {}", attempt + 1, MAX_RETRIES + 1, ex );
880+
}
881+
attempt++;
882+
try {
883+
TimeUnit.MILLISECONDS.sleep( 500 );
884+
} catch ( InterruptedException e ) {
885+
Thread.currentThread().interrupt();
886+
return;
870887
}
871-
} catch ( UnirestException ex ) {
872-
log.warn( "Exception while publishing log records.", ex );
873888
}
889+
log.warn( "Failed to publish log after {} attempts", MAX_RETRIES + 1 );
874890
} ) );
875891
}
876892

0 commit comments

Comments
 (0)