@@ -46,7 +46,7 @@ public class FileReferenceDownloader {
46
46
private final Downloads downloads ;
47
47
private final Duration downloadTimeout ;
48
48
private final Duration backoffInitialTime ;
49
- private final Duration rpcTimeout ;
49
+ private final Optional < Duration > rpcTimeout ; // Only used when overridden with env variable
50
50
private final File downloadDirectory ;
51
51
private final AtomicBoolean shutDown = new AtomicBoolean (false );
52
52
@@ -61,8 +61,8 @@ public class FileReferenceDownloader {
61
61
this .backoffInitialTime = backoffInitialTime ;
62
62
this .downloadDirectory = downloadDirectory ;
63
63
// Undocumented on purpose, might change or be removed at any time
64
- String timeoutString = System .getenv ("VESPA_FILE_DOWNLOAD_RPC_TIMEOUT" );
65
- this .rpcTimeout = Duration . ofSeconds ( timeoutString == null ? 30 : Integer .parseInt (timeoutString ));
64
+ var timeoutString = Optional . ofNullable ( System .getenv ("VESPA_FILE_DOWNLOAD_RPC_TIMEOUT" ) );
65
+ this .rpcTimeout = timeoutString . map ( t -> Duration . ofSeconds ( Integer .parseInt (t ) ));
66
66
}
67
67
68
68
private void waitUntilDownloadStarted (FileReferenceDownload fileReferenceDownload ) {
@@ -78,7 +78,10 @@ private void waitUntilDownloadStarted(FileReferenceDownload fileReferenceDownloa
78
78
return ;
79
79
if (FileDownloader .fileReferenceExists (fileReference , downloadDirectory ))
80
80
return ;
81
- if (startDownloadRpc (fileReferenceDownload , retryCount , connection ))
81
+ var timeout = rpcTimeout .orElse (Duration .between (Instant .now (), end ));
82
+ log .log (Level .FINE , "Wait until download of " + fileReference + " has started, retryCount " + retryCount +
83
+ " timeout" + timeout + " (request from client " + fileReferenceDownload .client () + ")" );
84
+ if ( ! timeout .isNegative () && startDownloadRpc (fileReferenceDownload , retryCount , connection , timeout ))
82
85
return ;
83
86
84
87
retryCount ++;
@@ -113,7 +116,6 @@ CompletableFuture<Optional<File>> startDownload(FileReferenceDownload fileRefere
113
116
Optional <FileReferenceDownload > inProgress = downloads .get (fileReference );
114
117
if (inProgress .isPresent ()) return inProgress .get ().future ();
115
118
116
- log .log (Level .FINE , () -> "Will download " + fileReference + " with timeout " + downloadTimeout );
117
119
downloads .add (fileReferenceDownload );
118
120
downloadExecutor .submit (() -> waitUntilDownloadStarted (fileReferenceDownload ));
119
121
return fileReferenceDownload .future ();
@@ -129,7 +131,7 @@ void startDownloadFromSource(FileReferenceDownload fileReferenceDownload, Spec s
129
131
downloadExecutor .submit (() -> {
130
132
log .log (Level .FINE , () -> "Will download " + fileReference + " with timeout " + downloadTimeout + " from " + spec );
131
133
downloads .add (fileReferenceDownload );
132
- startDownloadRpc (fileReferenceDownload , 1 , connection );
134
+ startDownloadRpc (fileReferenceDownload , 1 , connection , downloadTimeout );
133
135
downloads .remove (fileReference );
134
136
});
135
137
}
@@ -139,10 +141,9 @@ void failedDownloading(FileReference fileReference) {
139
141
downloads .remove (fileReference );
140
142
}
141
143
142
- private boolean startDownloadRpc (FileReferenceDownload fileReferenceDownload , int retryCount , Connection connection ) {
144
+ private boolean startDownloadRpc (FileReferenceDownload fileReferenceDownload , int retryCount , Connection connection , Duration timeout ) {
143
145
Request request = createRequest (fileReferenceDownload );
144
- Duration rpcTimeout = rpcTimeout (retryCount );
145
- connection .invokeSync (request , rpcTimeout );
146
+ connection .invokeSync (request , timeout );
146
147
147
148
Level logLevel = (retryCount > 3 ? Level .INFO : Level .FINE );
148
149
FileReference fileReference = fileReferenceDownload .fileReference ();
@@ -162,7 +163,7 @@ private boolean startDownloadRpc(FileReferenceDownload fileReferenceDownload, in
162
163
} else {
163
164
log .log (logLevel , "Downloading " + fileReference + " from " + address + " failed:" +
164
165
" error code " + request .errorCode () + " (" + request .errorMessage () + ")." +
165
- " (retry " + retryCount + ", rpc timeout " + rpcTimeout + ")" );
166
+ " (retry " + retryCount + ", rpc timeout " + timeout + ")" );
166
167
return false ;
167
168
}
168
169
}
@@ -177,10 +178,6 @@ private Request createRequest(FileReferenceDownload fileReferenceDownload) {
177
178
return request ;
178
179
}
179
180
180
- private Duration rpcTimeout (int retryCount ) {
181
- return Duration .ofSeconds (rpcTimeout .getSeconds ()).plus (Duration .ofSeconds (retryCount * 5L ));
182
- }
183
-
184
181
private boolean validateResponse (Request request ) {
185
182
if (request .isError ()) {
186
183
return false ;
0 commit comments