Lalant commented on code in PR #7379:
URL: https://github.com/apache/ignite-3/pull/7379#discussion_r2697948489
##########
modules/replicator/src/main/java/org/apache/ignite/internal/replicator/PlacementDriverMessageProcessor.java:
##########
@@ -259,18 +272,137 @@ private CompletableFuture<LeaseGrantedMessageResponse>
proposeLeaseRedirect(Inte
* @return Future that is completed when local storage catches up the
index that is actual for leader on the moment of request.
*/
private CompletableFuture<Void> waitForActualState(HybridTimestamp
startTime, long expirationTime) {
- LOG.info("Waiting for actual storage state, group=" + groupId);
+ LOG.info("Waiting for actual storage state [groupId={},
expirationTime={}, timeoutMs={}, leaseStartTime={}]",
+ groupId, expirationTime, expirationTime -
currentTimeMillis(), startTime);
replicaReservationClosure.accept(groupId, startTime);
- long timeout = expirationTime - currentTimeMillis();
- if (timeout <= 0) {
- return failedFuture(new TimeoutException());
+ TimeTracker readIndexTimeTracker = new TimeTracker(
+ expirationTime,
+ groupId,
+ "Timeout is expired before raft index reading started");
+ if (readIndexTimeTracker.isExpired()) {
+ return readIndexTimeTracker.getFailedFuture();
+ }
+
+ return retryOperationUntilSuccessOrTimeout(raftClient::readIndex,
readIndexTimeTracker.getTimeoutMs(), executor)
+ .whenComplete((raftIndex, readIndexError) -> {
+ if (readIndexError != null) {
+ LOG.warn("Failed to read index from raft leader"
+ + " [groupId={}, , expirationTime={},
timeoutMs={}, durationMs={}]", readIndexError,
+ groupId, expirationTime,
readIndexTimeTracker.getTimeoutMs(), readIndexTimeTracker.getDurationMs());
+ } else {
+ LOG.info("Successfully read index from raft leader "
+ + "[groupId={}, expirationTime={},
timeoutMs={}, durationMs={}]",
+ groupId, expirationTime,
readIndexTimeTracker.getTimeoutMs(), readIndexTimeTracker.getDurationMs());
+ }
+ })
+ .thenCompose(raftIndex -> {
+ // Recalculate remaining time after readIndex completes.
+ TimeTracker storageIndexUpdateTimeTracker = new
TimeTracker(
+ expirationTime,
+ groupId,
+ "Timeout is expired before storage index tracking
started");
+ if (storageIndexUpdateTimeTracker.isExpired()) {
+ return storageIndexUpdateTimeTracker.getFailedFuture();
+ }
+
+ return storageIndexTracker.waitFor(raftIndex)
+
.orTimeout(storageIndexUpdateTimeTracker.getTimeoutMs(), MILLISECONDS)
+ .whenComplete((v, storageIndexTrackerError) -> {
+ if (storageIndexTrackerError != null) {
+ LOG.warn("Failed to wait for storage index
to reach raft leader"
+ + " [groupId={},
expirationTime={}, timeoutMs={}, durationMs={}]",
+ storageIndexTrackerError, groupId,
expirationTime,
+
storageIndexUpdateTimeTracker.getTimeoutMs(),
+
storageIndexUpdateTimeTracker.getDurationMs());
+ } else {
+ LOG.info("Successfully waited for storage
index to reach raft leader"
+ + " [groupId={},
expirationTime={}, timeoutMs={}, durationMs={}]",
+ groupId, expirationTime,
+
storageIndexUpdateTimeTracker.getTimeoutMs(),
+
storageIndexUpdateTimeTracker.getDurationMs());
+ }
+ });
+ });
+ }
+
+ /**
+ * Tracks time for timeout operations. Calculates remaining time based on
expiration time and provides utilities
+ * for checking expiration and creating timeout exceptions with detailed
messages.
+ */
+ private static class TimeTracker {
+ /** Expiration time in milliseconds. */
+ private final long expirationTime;
+
+ /** Replication group ID for logging purposes. */
+ private final ReplicationGroupId groupId;
+
+ /** Start time when this tracker was created. */
+ private final long startTime;
+
+ /** Base message for timeout exception. */
+ private final String message;
+
+ /**
+ * Creates a time tracker.
+ *
+ * @param expirationTime Expiration time in milliseconds.
+ * @param groupId Replication group ID for logging purposes.
+ * @param message Base message for timeout exception.
+ */
+ private TimeTracker(long expirationTime, ReplicationGroupId groupId,
String message) {
+ this.expirationTime = expirationTime;
+ this.groupId = groupId;
+ this.startTime = currentTimeMillis();
+ this.message = message;
+ }
+
+ /**
+ * Checks if the timeout has expired.
+ *
+ * @return {@code true} if the timeout has expired (timeLeft < 0),
{@code false} otherwise.
+ */
+ public boolean isExpired() {
+ return getTimeoutMs() < 0;
+ }
+
+ /**
+ * Creates a failed future with a timeout exception that includes
detailed information.
+ *
+ * @return A failed future with {@link TimeoutException} containing
the base message and details.
+ */
+ public CompletableFuture<Void> getFailedFuture() {
+ return failedFuture(new TimeoutException(message +
getMessageDetails()));
+ }
+
+ /**
+ * Formats message details including group ID, expiration time, and
current time.
+ *
+ * @return Formatted message details.
+ */
+ private String getMessageDetails() {
+ return format(" [groupId={}, expirationTime={}, timeoutMs={},
durationMs={}].",
+ groupId, expirationTime, getTimeoutMs(), getDurationMs());
}
- return retryOperationUntilSuccess(raftClient::readIndex, e ->
currentTimeMillis() > expirationTime, executor)
- .orTimeout(timeout, TimeUnit.MILLISECONDS)
- .thenCompose(storageIndexTracker::waitFor);
+ /**
+ * Returns the duration since this tracker was created.
+ *
+ * @return Duration in milliseconds since the tracker was created.
+ */
+ public long getDurationMs() {
+ return currentTimeMillis() - startTime;
+ }
+
+ /**
+ * Returns the remaining time until expiration in milliseconds.
+ *
+ * @return Remaining time in milliseconds. Can be negative if already
expired.
+ */
+ public long getTimeoutMs() {
+ return expirationTime - currentTimeMillis();
+ }
Review Comment:
I will
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]