Skip to content

Commit

Permalink
Improve Synchronization Logging
Browse files Browse the repository at this point in the history
  • Loading branch information
hexiaofeng committed May 23, 2024
1 parent 8bf6966 commit 24980ee
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected long getDelay() {
}

@Override
public SyncResult<T, FileDigest> sync(SyncConfig config, FileDigest last) throws IOException {
public SyncResult<T, FileDigest> doSynchronize(SyncConfig config, FileDigest last) throws IOException {
if (file != null) {
FileContent content = readFile(last);
if (content != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.jd.live.agent.core.util.Waiter.MutexWaiter;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicLong;

/**
* AbstractSyncer provides a base implementation for synchronous services. It extends the
Expand All @@ -39,6 +40,8 @@
*/
public abstract class AbstractSyncer<T, M> extends AbstractService {

private static final int INTERVALS = 10;

/**
* Logger instance for this class.
*/
Expand All @@ -64,6 +67,8 @@ public abstract class AbstractSyncer<T, M> extends AbstractService {
*/
protected MutexWaiter waiter;

protected final AtomicLong counter = new AtomicLong(0);

/**
* Starts the synchronization service by initializing the daemon thread and initiating its execution.
*
Expand Down Expand Up @@ -157,16 +162,17 @@ protected long getDelay() {
protected void run(long startTime, CompletableFuture<Void> waitForInitial) {
Throwable throwable = null;
boolean synced = false;
counter.incrementAndGet();
try {
synced = syncAndUpdate();
} catch (Throwable e) {
logger.error("failed to sync and update " + getName(), e);
onFailed(e);
throwable = e;
}
if (!waitForInitial.isDone()) {
long timeout = config.getInitialTimeout();
if (timeout > 0 && (System.currentTimeMillis() - startTime > timeout)) {
waitForInitial.completeExceptionally(new InitialTimeoutException("it's timeout to initialize " + getName() +
waitForInitial.completeExceptionally(new InitialTimeoutException("It's timeout to initialize " + getName() +
(throwable == null ? "" : ", caused by " + throwable.getMessage())));
} else if (synced) {
waitForInitial.complete(null);
Expand All @@ -181,18 +187,54 @@ protected void run(long startTime, CompletableFuture<Void> waitForInitial) {
* @throws Exception If an error occurs during synchronization.
*/
protected boolean syncAndUpdate() throws Exception {
SyncResult<T, M> result = sync(config, last);
SyncResult<T, M> result = doSynchronize(config, last);
if (result != null) {
last = result.getMeta();
for (int i = 0; i < UPDATE_MAX_RETRY; i++) {
if (updateOnce(result.getData(), result.getMeta())) {
onUpdated();
return true;
}
}
} else {
onNotModified();
}
return false;
}

/**
* Determines whether the current state should trigger an output.
*
* @return {@code true} if the current count matches the output interval,
* indicating that an output action should be performed; {@code false} otherwise.
*/
protected boolean shouldPrint() {
return counter.get() % INTERVALS == 1;
}

/**
* Handles the updated state.
*/
protected void onUpdated() {

}

/**
* Handles the scenario where no modifications are detected.
*/
protected void onNotModified() {

}

/**
* Handles failure scenarios during synchronization and update processes.
*
* @param throwable The exception or error that caused the failure.
*/
protected void onFailed(Throwable throwable) {
logger.error("Failed to synchronize and update " + getName() + ". caused by " + throwable.getMessage(), throwable);
}

/**
* Retrieves the synchronization configuration.
*
Expand All @@ -217,6 +259,6 @@ protected boolean syncAndUpdate() throws Exception {
* @return A SyncResult instance containing the new data and metadata.
* @throws Exception If an error occurs during synchronization.
*/
protected abstract SyncResult<T, M> sync(SyncConfig config, M last) throws Exception;
protected abstract SyncResult<T, M> doSynchronize(SyncConfig config, M last) throws Exception;
}

Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* @param <T> The type of the data contained in the response.
*/
@Getter
public class HttpResponse<T> {
public class HttpResponse<T> implements HttpState {

/**
* The HTTP status code of the response.
Expand Down Expand Up @@ -73,4 +73,8 @@ public HttpResponse(HttpStatus status, String message, T data) {
this.data = data;
}

@Override
public int getCode() {
return status.value();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright © ${year} ${owner} (${email})
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jd.live.agent.core.util.http;


/**
* Represents the state of an HTTP response.
* This interface defines methods to access the status code and message
* associated with an HTTP response.
*/
public interface HttpState {

/**
* Retrieves the HTTP status code.
* The status code is intended to provide a numeric representation of
* the HTTP response status, indicating the outcome of the HTTP request.
*
* @return An {@code int} representing the HTTP status code.
*/
int getCode();

/**
* Retrieves the HTTP status message.
* The status message provides a textual description associated with
* the HTTP status code, offering additional context about the response.
*
* @return A {@code String} representing the HTTP status message.
*/
String getMessage();

}

0 comments on commit 24980ee

Please sign in to comment.