weiqingy commented on code in PR #531:
URL: https://github.com/apache/flink-agents/pull/531#discussion_r2825683020
##########
integrations/mcp/src/main/java/org/apache/flink/agents/integrations/mcp/MCPServer.java:
##########
@@ -257,28 +347,117 @@ private void validateHttpUrl() {
}
}
+ /**
+ * Execute an operation with retry logic.
+ *
+ * @param operation The operation to execute
+ * @param operationName Name of the operation for error messages
+ * @return The result of the operation
+ * @throws RuntimeException if all retries fail
+ */
+ private <T> T executeWithRetry(Callable<T> operation, String
operationName) {
+ int attempt = 0;
+ long backoff = initialBackoffMs;
+ Exception lastException = null;
+
+ while (attempt <= maxRetries) {
+ try {
+ return operation.call();
+
+ } catch (Exception e) {
+ lastException = e;
+ attempt++;
+
+ if (!isRetryable(e)) {
+ throw new RuntimeException(
+ String.format(
+ "MCP operation '%s' failed: %s",
operationName, e.getMessage()),
+ e);
+ }
+
+ if (attempt > maxRetries) {
+ break;
+ }
+
+ // Exponential backoff with jitter
+ try {
+ long jitter = RANDOM.nextInt((int) (backoff / 10) + 1);
+ long sleepTime = backoff + jitter;
+ Thread.sleep(sleepTime);
+ } catch (InterruptedException ie) {
+ Thread.currentThread().interrupt();
+ throw new RuntimeException(
+ "Interrupted while retrying MCP operation: " +
operationName, ie);
+ }
+
+ backoff = Math.min(backoff * 2, maxBackoffMs);
+ }
+ }
+
+ // All retries exhausted
+ throw new RuntimeException(
+ String.format(
+ "MCP operation '%s' failed after %d retries: %s",
+ operationName, maxRetries, lastException.getMessage()),
+ lastException);
+ }
+
+ /**
+ * Check if an exception is retryable.
+ *
+ * @param e The exception to check
+ * @return true if the operation should be retried
+ */
+ private boolean isRetryable(Exception e) {
+ // Network-related errors are retryable
+ if (e instanceof SocketTimeoutException || e instanceof
ConnectException) {
+ return true;
+ }
+ String message = e.getMessage();
+ if (message != null) {
+ if (message.contains("503")) {
Review Comment:
Checking message.contains("503") or message.contains("429") is fragile - a
tool called "room-503-monitor" or an error message mentioning "429 items
processed" would match. The MCP Java SDK likely throws typed exceptions or has
status codes accessible. If not, at minimum the pattern should be more specific
(e.g., "HTTP 503" or regex "\b503\b").
--
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]