51
51
import org .elasticsearch .common .component .AbstractLifecycleComponent ;
52
52
import org .elasticsearch .common .compress .NotXContentException ;
53
53
import org .elasticsearch .common .io .stream .BytesStreamOutput ;
54
- import org .elasticsearch .common .io .stream .OutputStreamStreamOutput ;
55
- import org .elasticsearch .common .io .stream .StreamOutput ;
56
54
import org .elasticsearch .common .lucene .Lucene ;
57
55
import org .elasticsearch .common .lucene .store .InputStreamIndexInput ;
58
56
import org .elasticsearch .common .metrics .CounterMetric ;
63
61
import org .elasticsearch .common .util .set .Sets ;
64
62
import org .elasticsearch .common .xcontent .LoggingDeprecationHandler ;
65
63
import org .elasticsearch .common .xcontent .NamedXContentRegistry ;
66
- import org .elasticsearch .common .xcontent .XContentBuilder ;
67
64
import org .elasticsearch .common .xcontent .XContentFactory ;
68
65
import org .elasticsearch .common .xcontent .XContentHelper ;
69
66
import org .elasticsearch .common .xcontent .XContentParser ;
@@ -374,10 +371,7 @@ public void initializeSnapshot(SnapshotId snapshotId, List<IndexId> indices, Met
374
371
375
372
// write the index metadata for each index in the snapshot
376
373
for (IndexId index : indices ) {
377
- final IndexMetaData indexMetaData = clusterMetaData .index (index .getName ());
378
- final BlobPath indexPath = basePath ().add ("indices" ).add (index .getId ());
379
- final BlobContainer indexMetaDataBlobContainer = blobStore ().blobContainer (indexPath );
380
- indexMetaDataFormat .write (indexMetaData , indexMetaDataBlobContainer , snapshotId .getUUID ());
374
+ indexMetaDataFormat .write (clusterMetaData .index (index .getName ()), indexContainer (index ), snapshotId .getUUID ());
381
375
}
382
376
} catch (IOException ex ) {
383
377
throw new SnapshotCreationException (metadata .name (), snapshotId , ex );
@@ -425,7 +419,7 @@ public void deleteSnapshot(SnapshotId snapshotId, long repositoryStateId, Action
425
419
snapshotId ,
426
420
ActionListener .map (listener , v -> {
427
421
try {
428
- blobStore ().blobContainer (basePath (). add ( "indices" )).deleteBlobsIgnoringIfNotExists (
422
+ blobStore ().blobContainer (indicesPath ( )).deleteBlobsIgnoringIfNotExists (
429
423
unreferencedIndices .stream ().map (IndexId ::getId ).collect (Collectors .toList ()));
430
424
} catch (IOException e ) {
431
425
logger .warn (() ->
@@ -477,9 +471,8 @@ protected void doRun() {
477
471
}
478
472
479
473
private void deleteIndexMetaDataBlobIgnoringErrors (SnapshotId snapshotId , IndexId indexId ) {
480
- BlobContainer indexMetaDataBlobContainer = blobStore ().blobContainer (basePath ().add ("indices" ).add (indexId .getId ()));
481
474
try {
482
- indexMetaDataFormat .delete (indexMetaDataBlobContainer , snapshotId .getUUID ());
475
+ indexMetaDataFormat .delete (indexContainer ( indexId ) , snapshotId .getUUID ());
483
476
} catch (IOException ex ) {
484
477
logger .warn (() -> new ParameterizedMessage ("[{}] failed to delete metadata for index [{}]" ,
485
478
snapshotId , indexId .getName ()), ex );
@@ -540,8 +533,19 @@ public MetaData getSnapshotGlobalMetaData(final SnapshotId snapshotId) {
540
533
541
534
@ Override
542
535
public IndexMetaData getSnapshotIndexMetaData (final SnapshotId snapshotId , final IndexId index ) throws IOException {
543
- final BlobPath indexPath = basePath ().add ("indices" ).add (index .getId ());
544
- return indexMetaDataFormat .read (blobStore ().blobContainer (indexPath ), snapshotId .getUUID ());
536
+ return indexMetaDataFormat .read (indexContainer (index ), snapshotId .getUUID ());
537
+ }
538
+
539
+ private BlobPath indicesPath () {
540
+ return basePath ().add ("indices" );
541
+ }
542
+
543
+ private BlobContainer indexContainer (IndexId indexId ) {
544
+ return blobStore ().blobContainer (indicesPath ().add (indexId .getId ()));
545
+ }
546
+
547
+ private BlobContainer shardContainer (IndexId indexId , ShardId shardId ) {
548
+ return blobStore ().blobContainer (indicesPath ().add (indexId .getId ()).add (Integer .toString (shardId .getId ())));
545
549
}
546
550
547
551
/**
@@ -589,10 +593,9 @@ public String startVerification() {
589
593
String seed = UUIDs .randomBase64UUID ();
590
594
byte [] testBytes = Strings .toUTF8Bytes (seed );
591
595
BlobContainer testContainer = blobStore ().blobContainer (basePath ().add (testBlobPrefix (seed )));
592
- String blobName = "master.dat" ;
593
596
BytesArray bytes = new BytesArray (testBytes );
594
597
try (InputStream stream = bytes .streamInput ()) {
595
- testContainer .writeBlobAtomic (blobName , stream , bytes .length (), true );
598
+ testContainer .writeBlobAtomic ("master.dat" , stream , bytes .length (), true );
596
599
}
597
600
return seed ;
598
601
}
@@ -665,7 +668,7 @@ public RepositoryData getRepositoryData() {
665
668
}
666
669
}
667
670
668
- public static String testBlobPrefix (String seed ) {
671
+ private static String testBlobPrefix (String seed ) {
669
672
return TESTS_FILE + seed ;
670
673
}
671
674
@@ -685,19 +688,10 @@ protected void writeIndexGen(final RepositoryData repositoryData, final long rep
685
688
"] - possibly due to simultaneous snapshot deletion requests" );
686
689
}
687
690
final long newGen = currentGen + 1 ;
688
- final BytesReference snapshotsBytes ;
689
- try (BytesStreamOutput bStream = new BytesStreamOutput ()) {
690
- try (StreamOutput stream = new OutputStreamStreamOutput (bStream )) {
691
- XContentBuilder builder = XContentFactory .contentBuilder (XContentType .JSON , stream );
692
- repositoryData .snapshotsToXContent (builder );
693
- builder .close ();
694
- }
695
- snapshotsBytes = bStream .bytes ();
696
- }
697
691
// write the index file
698
692
final String indexBlob = INDEX_FILE_PREFIX + Long .toString (newGen );
699
693
logger .debug ("Repository [{}] writing new index generational blob [{}]" , metadata .name (), indexBlob );
700
- writeAtomic (indexBlob , snapshotsBytes , true );
694
+ writeAtomic (indexBlob , BytesReference . bytes ( repositoryData . snapshotsToXContent ( XContentFactory . jsonBuilder ())) , true );
701
695
// write the current generation to the index-latest file
702
696
final BytesReference genBytes ;
703
697
try (BytesStreamOutput bStream = new BytesStreamOutput ()) {
@@ -724,16 +718,9 @@ protected void writeIndexGen(final RepositoryData repositoryData, final long rep
724
718
*/
725
719
void writeIncompatibleSnapshots (RepositoryData repositoryData ) throws IOException {
726
720
assert isReadOnly () == false ; // can not write to a read only repository
727
- final BytesReference bytes ;
728
- try (BytesStreamOutput bStream = new BytesStreamOutput ()) {
729
- try (StreamOutput stream = new OutputStreamStreamOutput (bStream );
730
- XContentBuilder builder = XContentFactory .contentBuilder (XContentType .JSON , stream )) {
731
- repositoryData .incompatibleSnapshotsToXContent (builder );
732
- }
733
- bytes = bStream .bytes ();
734
- }
735
721
// write the incompatible snapshots blob
736
- writeAtomic (INCOMPATIBLE_SNAPSHOTS_BLOB , bytes , false );
722
+ writeAtomic (INCOMPATIBLE_SNAPSHOTS_BLOB ,
723
+ BytesReference .bytes (repositoryData .incompatibleSnapshotsToXContent (XContentFactory .jsonBuilder ())), false );
737
724
}
738
725
739
726
/**
@@ -826,9 +813,8 @@ public void restoreShard(Store store, SnapshotId snapshotId,
826
813
Version version , IndexId indexId , ShardId snapshotShardId , RecoveryState recoveryState ) {
827
814
ShardId shardId = store .shardId ();
828
815
final Context context = new Context (snapshotId , indexId , shardId , snapshotShardId );
829
- BlobPath path = basePath ().add ("indices" ).add (indexId .getId ()).add (Integer .toString (snapshotShardId .getId ()));
830
- BlobContainer blobContainer = blobStore ().blobContainer (path );
831
- final RestoreContext snapshotContext = new RestoreContext (shardId , snapshotId , recoveryState , blobContainer );
816
+ final RestoreContext snapshotContext =
817
+ new RestoreContext (shardId , snapshotId , recoveryState , shardContainer (indexId , snapshotShardId ));
832
818
try {
833
819
BlobStoreIndexShardSnapshot snapshot = context .loadSnapshot ();
834
820
SnapshotFiles snapshotFiles = new SnapshotFiles (snapshot .snapshot (), snapshot .indexFiles ());
@@ -904,8 +890,7 @@ private class Context {
904
890
Context (SnapshotId snapshotId , IndexId indexId , ShardId shardId , ShardId snapshotShardId ) {
905
891
this .snapshotId = snapshotId ;
906
892
this .shardId = shardId ;
907
- blobContainer = blobStore ().blobContainer (basePath ().add ("indices" ).add (indexId .getId ())
908
- .add (Integer .toString (snapshotShardId .getId ())));
893
+ blobContainer = shardContainer (indexId , snapshotShardId );
909
894
}
910
895
911
896
/**
0 commit comments