@@ -113,13 +113,14 @@ public ResponseEntity<FileMetadata> uploadFile(MultipartHttpServletRequest reque
113
113
var multipartFile = getFileFromRequest (request );
114
114
try (InputStream in = new BufferedInputStream (multipartFile .getInputStream (), INPUT_STREAM_BUFFER_SIZE )) {
115
115
var startTime = LocalDateTime .now ();
116
- FileEntry fileEntry = fileService .addFile (spaceGuid , namespace , multipartFile .getOriginalFilename (), in , multipartFile .getSize ());
116
+ FileEntry fileEntry = fileService .addFile (spaceGuid , namespace , multipartFile .getOriginalFilename (), in ,
117
+ multipartFile .getSize ());
117
118
FileMetadata file = parseFileEntry (fileEntry );
118
119
AuditLoggingProvider .getFacade ()
119
120
.logConfigCreate (file );
120
121
var endTime = LocalDateTime .now ();
121
- LOGGER .trace (Messages .UPLOADED_FILE , file .getId (), file .getName (), file .getSize (), file .getDigest (),
122
- file . getDigestAlgorithm (), ChronoUnit .MILLIS .between (startTime , endTime ));
122
+ LOGGER .trace (Messages .UPLOADED_FILE , file .getId (), file .getName (), file .getSize (), file .getDigest (), file . getDigestAlgorithm (),
123
+ ChronoUnit .MILLIS .between (startTime , endTime ));
123
124
return ResponseEntity .status (HttpStatus .CREATED )
124
125
.body (file );
125
126
} catch (Exception e ) {
@@ -145,19 +146,32 @@ public ResponseEntity<Void> startUploadFromUrl(String spaceGuid, String namespac
145
146
var entry = createJobEntry (spaceGuid , namespace , urlWithoutUserInfo );
146
147
LOGGER .debug (Messages .CREATING_ASYNC_UPLOAD_JOB , urlWithoutUserInfo , entry .getId ());
147
148
try {
149
+ uploadJobService .add (entry );
148
150
deployFromUrlExecutor .execute (() -> uploadFileFromUrl (entry , spaceGuid , namespace , decodedUrl ));
149
151
} catch (RejectedExecutionException ignored ) {
150
152
LOGGER .debug (Messages .ASYNC_UPLOAD_JOB_REJECTED , entry .getId ());
153
+ deleteAsyncJobEntry (entry );
151
154
return ResponseEntity .status (HttpStatus .TOO_MANY_REQUESTS )
152
155
.header (HttpHeaders .RETRY_AFTER , RETRY_AFTER_SECONDS )
153
156
.build ();
154
157
}
155
158
return ResponseEntity .accepted ()
156
- .header ("x-cf-app-instance" , configuration .getApplicationGuid () + ":" + configuration .getApplicationInstanceIndex ())
159
+ .header ("x-cf-app-instance" ,
160
+ configuration .getApplicationGuid () + ":" + configuration .getApplicationInstanceIndex ())
157
161
.header (HttpHeaders .LOCATION , getLocationHeader (spaceGuid , entry .getId ()))
158
162
.build ();
159
163
}
160
164
165
+ private void deleteAsyncJobEntry (AsyncUploadJobEntry entry ) {
166
+ try {
167
+ uploadJobService .createQuery ()
168
+ .id (entry .getId ())
169
+ .delete ();
170
+ } catch (Exception e ) {
171
+ LOGGER .error (Messages .ERROR_OCCURRED_WHILE_DELETING_JOB_ENTRY , e );
172
+ }
173
+ }
174
+
161
175
private String getLocationHeader (String spaceGuid , String jobId ) {
162
176
return "spaces/" + spaceGuid + "/files/jobs/" + jobId ;
163
177
}
@@ -259,8 +273,8 @@ private AsyncUploadJobEntry getJob(String id, String spaceGuid, String namespace
259
273
try {
260
274
return uploadJobService .createQuery ()
261
275
.id (id )
262
- //even though the ID fully qualifies the job, we add these filters
263
- //to prevent accessing a job from another space, namespace or a different user
276
+ // even though the ID fully qualifies the job, we add these filters
277
+ // to prevent accessing a job from another space, namespace or a different user
264
278
.spaceGuid (spaceGuid )
265
279
.user (SecurityContextUtil .getUsername ())
266
280
.namespace (namespace )
@@ -280,29 +294,32 @@ private AsyncUploadResult createErrorResult(String error) {
280
294
private void uploadFileFromUrl (AsyncUploadJobEntry jobEntry , String spaceGuid , String namespace , String fileUrl ) {
281
295
var counter = new AtomicLong (0 );
282
296
jobCounters .put (jobEntry .getId (), counter );
297
+ LOGGER .debug (Messages .STARTING_DOWNLOAD_OF_MTAR , jobEntry .getUrl ());
298
+ var startTime = LocalDateTime .now ();
299
+ AsyncUploadJobEntry jobEntryWithTimestamp = ImmutableAsyncUploadJobEntry .copyOf (jobEntry )
300
+ .withState (State .RUNNING )
301
+ .withStartedAt (startTime );
283
302
try {
284
- uploadJobService .add (jobEntry );
285
- LOGGER .debug (Messages .STARTING_DOWNLOAD_OF_MTAR , jobEntry .getUrl ());
286
- var startTime = LocalDateTime .now ();
287
- uploadJobService .update (jobEntry , ImmutableAsyncUploadJobEntry .copyOf (jobEntry )
288
- .withState (State .RUNNING )
289
- .withStartedAt (startTime ));
290
- FileEntry fileEntry = resilientOperationExecutor .execute ((CheckedSupplier <FileEntry >) () -> doUploadFileFromUrl (spaceGuid , namespace , fileUrl , counter ));
291
- LOGGER .trace (Messages .UPLOADED_MTAR_FROM_REMOTE_ENDPOINT , jobEntry .getUrl (),
303
+ jobEntryWithTimestamp = uploadJobService .update (jobEntry , jobEntryWithTimestamp );
304
+ FileEntry fileEntry = resilientOperationExecutor .execute ((CheckedSupplier <FileEntry >) () -> doUploadFileFromUrl (spaceGuid ,
305
+ namespace ,
306
+ fileUrl ,
307
+ counter ));
308
+ LOGGER .trace (Messages .UPLOADED_MTAR_FROM_REMOTE_ENDPOINT_AND_JOB_ID , jobEntry .getUrl (), jobEntry .getId (),
292
309
ChronoUnit .MILLIS .between (startTime , LocalDateTime .now ()));
293
310
294
311
var descriptor = fileService .processFileContent (spaceGuid , fileEntry .getId (), this ::extractDeploymentDescriptor );
295
312
LOGGER .debug (Messages .ASYNC_UPLOAD_JOB_FINISHED , jobEntry .getId ());
296
- uploadJobService .update (jobEntry , ImmutableAsyncUploadJobEntry .copyOf (jobEntry )
297
- .withFileId (fileEntry .getId ())
298
- .withMtaId (descriptor .getId ())
299
- .withFinishedAt (LocalDateTime .now ())
300
- .withState (State .FINISHED ));
313
+ uploadJobService .update (jobEntryWithTimestamp , ImmutableAsyncUploadJobEntry .copyOf (jobEntryWithTimestamp )
314
+ .withFileId (fileEntry .getId ())
315
+ .withMtaId (descriptor .getId ())
316
+ .withFinishedAt (LocalDateTime .now ())
317
+ .withState (State .FINISHED ));
301
318
} catch (Exception e ) {
302
319
LOGGER .error (MessageFormat .format (Messages .ASYNC_UPLOAD_JOB_FAILED , jobEntry .getId (), e .getMessage ()), e );
303
- uploadJobService .update (jobEntry , ImmutableAsyncUploadJobEntry .copyOf (jobEntry )
304
- .withError (e .getMessage ())
305
- .withState (State .ERROR ));
320
+ uploadJobService .update (jobEntryWithTimestamp , ImmutableAsyncUploadJobEntry .copyOf (jobEntryWithTimestamp )
321
+ .withError (e .getMessage ())
322
+ .withState (State .ERROR ));
306
323
}
307
324
}
308
325
@@ -325,11 +342,11 @@ private FileEntry doUploadFileFromUrl(String spaceGuid, String namespace, String
325
342
326
343
String fileName = extractFileName (fileUrl );
327
344
FileUtils .validateFileHasExtension (fileName );
328
- counter .set (0 ); //reset counter on retry
345
+ counter .set (0 ); // reset counter on retry
329
346
// Normal stream returned from the http response always returns 0 when InputStream::available() is executed which seems to break
330
347
// JClods library: https://issues.apache.org/jira/browse/JCLOUDS-1623
331
348
try (CountingInputStream source = new CountingInputStream (response .body (), counter );
332
- BufferedInputStream bufferedContent = new BufferedInputStream (source , INPUT_STREAM_BUFFER_SIZE )) {
349
+ BufferedInputStream bufferedContent = new BufferedInputStream (source , INPUT_STREAM_BUFFER_SIZE )) {
333
350
LOGGER .debug (Messages .UPLOADING_MTAR_STREAM_FROM_REMOTE_ENDPOINT , response .uri ());
334
351
return fileService .addFile (spaceGuid , namespace , fileName , bufferedContent , fileSize );
335
352
}
@@ -342,8 +359,8 @@ private HttpResponse<InputStream> callRemoteEndpointWithRetry(HttpClient client,
342
359
var response = client .send (request , BodyHandlers .ofInputStream ());
343
360
if (response .statusCode () / 100 != 2 ) {
344
361
String error = readErrorBodyFromResponse (response );
345
- throw new SLException (MessageFormat .format (Messages .ERROR_FROM_REMOTE_MTAR_ENDPOINT , request .uri (),
346
- response . statusCode (), error ));
362
+ throw new SLException (MessageFormat .format (Messages .ERROR_FROM_REMOTE_MTAR_ENDPOINT , request .uri (), response . statusCode (),
363
+ error ));
347
364
}
348
365
return response ;
349
366
});
0 commit comments