@@ -46,7 +46,7 @@ public class FileReferenceDownloader {
4646 private final Downloads downloads ;
4747 private final Duration downloadTimeout ;
4848 private final Duration backoffInitialTime ;
49- private final Duration rpcTimeout ;
49+ private final Optional < Duration > rpcTimeout ; // Only used when overridden with env variable
5050 private final File downloadDirectory ;
5151 private final AtomicBoolean shutDown = new AtomicBoolean (false );
5252
@@ -61,8 +61,8 @@ public class FileReferenceDownloader {
6161 this .backoffInitialTime = backoffInitialTime ;
6262 this .downloadDirectory = downloadDirectory ;
6363 // 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 ) ));
6666 }
6767
6868 private void waitUntilDownloadStarted (FileReferenceDownload fileReferenceDownload ) {
@@ -78,7 +78,10 @@ private void waitUntilDownloadStarted(FileReferenceDownload fileReferenceDownloa
7878 return ;
7979 if (FileDownloader .fileReferenceExists (fileReference , downloadDirectory ))
8080 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 ))
8285 return ;
8386
8487 retryCount ++;
@@ -113,7 +116,6 @@ CompletableFuture<Optional<File>> startDownload(FileReferenceDownload fileRefere
113116 Optional <FileReferenceDownload > inProgress = downloads .get (fileReference );
114117 if (inProgress .isPresent ()) return inProgress .get ().future ();
115118
116- log .log (Level .FINE , () -> "Will download " + fileReference + " with timeout " + downloadTimeout );
117119 downloads .add (fileReferenceDownload );
118120 downloadExecutor .submit (() -> waitUntilDownloadStarted (fileReferenceDownload ));
119121 return fileReferenceDownload .future ();
@@ -129,7 +131,7 @@ void startDownloadFromSource(FileReferenceDownload fileReferenceDownload, Spec s
129131 downloadExecutor .submit (() -> {
130132 log .log (Level .FINE , () -> "Will download " + fileReference + " with timeout " + downloadTimeout + " from " + spec );
131133 downloads .add (fileReferenceDownload );
132- startDownloadRpc (fileReferenceDownload , 1 , connection );
134+ startDownloadRpc (fileReferenceDownload , 1 , connection , downloadTimeout );
133135 downloads .remove (fileReference );
134136 });
135137 }
@@ -139,10 +141,9 @@ void failedDownloading(FileReference fileReference) {
139141 downloads .remove (fileReference );
140142 }
141143
142- private boolean startDownloadRpc (FileReferenceDownload fileReferenceDownload , int retryCount , Connection connection ) {
144+ private boolean startDownloadRpc (FileReferenceDownload fileReferenceDownload , int retryCount , Connection connection , Duration timeout ) {
143145 Request request = createRequest (fileReferenceDownload );
144- Duration rpcTimeout = rpcTimeout (retryCount );
145- connection .invokeSync (request , rpcTimeout );
146+ connection .invokeSync (request , timeout );
146147
147148 Level logLevel = (retryCount > 3 ? Level .INFO : Level .FINE );
148149 FileReference fileReference = fileReferenceDownload .fileReference ();
@@ -162,7 +163,7 @@ private boolean startDownloadRpc(FileReferenceDownload fileReferenceDownload, in
162163 } else {
163164 log .log (logLevel , "Downloading " + fileReference + " from " + address + " failed:" +
164165 " error code " + request .errorCode () + " (" + request .errorMessage () + ")." +
165- " (retry " + retryCount + ", rpc timeout " + rpcTimeout + ")" );
166+ " (retry " + retryCount + ", rpc timeout " + timeout + ")" );
166167 return false ;
167168 }
168169 }
@@ -177,10 +178,6 @@ private Request createRequest(FileReferenceDownload fileReferenceDownload) {
177178 return request ;
178179 }
179180
180- private Duration rpcTimeout (int retryCount ) {
181- return Duration .ofSeconds (rpcTimeout .getSeconds ()).plus (Duration .ofSeconds (retryCount * 5L ));
182- }
183-
184181 private boolean validateResponse (Request request ) {
185182 if (request .isError ()) {
186183 return false ;
0 commit comments