@@ -475,30 +475,32 @@ public void uploadMetadata(
475
475
RemoteSegmentMetadata .CURRENT_VERSION
476
476
);
477
477
try {
478
- IndexOutput indexOutput = storeDirectory .createOutput (metadataFilename , IOContext .DEFAULT );
479
- Map <String , String > uploadedSegments = new HashMap <>();
480
- for (String file : segmentFiles ) {
481
- if (segmentsUploadedToRemoteStore .containsKey (file )) {
482
- uploadedSegments .put (file , segmentsUploadedToRemoteStore .get (file ).toString ());
483
- } else {
484
- throw new NoSuchFileException (file );
478
+ try (IndexOutput indexOutput = storeDirectory .createOutput (metadataFilename , IOContext .DEFAULT )) {
479
+ Map <String , String > uploadedSegments = new HashMap <>();
480
+ for (String file : segmentFiles ) {
481
+ if (segmentsUploadedToRemoteStore .containsKey (file )) {
482
+ uploadedSegments .put (file , segmentsUploadedToRemoteStore .get (file ).toString ());
483
+ } else {
484
+ throw new NoSuchFileException (file );
485
+ }
485
486
}
486
- }
487
487
488
- ByteBuffersDataOutput byteBuffersIndexOutput = new ByteBuffersDataOutput ();
489
- segmentInfosSnapshot .write (new ByteBuffersIndexOutput (byteBuffersIndexOutput , "Snapshot of SegmentInfos" , "SegmentInfos" ));
490
- byte [] segmentInfoSnapshotByteArray = byteBuffersIndexOutput .toArrayCopy ();
491
-
492
- metadataStreamWrapper .writeStream (
493
- indexOutput ,
494
- new RemoteSegmentMetadata (
495
- RemoteSegmentMetadata .fromMapOfStrings (uploadedSegments ),
496
- segmentInfoSnapshotByteArray ,
497
- primaryTerm ,
498
- segmentInfosSnapshot .getGeneration ()
499
- )
500
- );
501
- indexOutput .close ();
488
+ ByteBuffersDataOutput byteBuffersIndexOutput = new ByteBuffersDataOutput ();
489
+ segmentInfosSnapshot .write (
490
+ new ByteBuffersIndexOutput (byteBuffersIndexOutput , "Snapshot of SegmentInfos" , "SegmentInfos" )
491
+ );
492
+ byte [] segmentInfoSnapshotByteArray = byteBuffersIndexOutput .toArrayCopy ();
493
+
494
+ metadataStreamWrapper .writeStream (
495
+ indexOutput ,
496
+ new RemoteSegmentMetadata (
497
+ RemoteSegmentMetadata .fromMapOfStrings (uploadedSegments ),
498
+ segmentInfoSnapshotByteArray ,
499
+ primaryTerm ,
500
+ segmentInfosSnapshot .getGeneration ()
501
+ )
502
+ );
503
+ }
502
504
storeDirectory .sync (Collections .singleton (metadataFilename ));
503
505
remoteMetadataDirectory .copyFrom (storeDirectory , metadataFilename , metadataFilename , IOContext .DEFAULT );
504
506
} finally {
0 commit comments