Skip to content

Commit a3a4bd7

Browse files
Some Cleanup in BlobStoreRepository (#43323)
* Some Cleanup in BlobStoreRepository * Extracted from #42833: * Dry up index and shard path handling * Shorten XContent handling
1 parent 631c63c commit a3a4bd7

File tree

2 files changed

+26
-41
lines changed

2 files changed

+26
-41
lines changed

server/src/main/java/org/elasticsearch/repositories/RepositoryData.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,8 +442,7 @@ public static RepositoryData snapshotsFromXContent(final XContentParser parser,
442442
/**
443443
* Writes the incompatible snapshot ids to x-content.
444444
*/
445-
public void incompatibleSnapshotsToXContent(final XContentBuilder builder) throws IOException {
446-
445+
public XContentBuilder incompatibleSnapshotsToXContent(XContentBuilder builder) throws IOException {
447446
builder.startObject();
448447
// write the incompatible snapshots list
449448
builder.startArray(INCOMPATIBLE_SNAPSHOTS);
@@ -452,6 +451,7 @@ public void incompatibleSnapshotsToXContent(final XContentBuilder builder) throw
452451
}
453452
builder.endArray();
454453
builder.endObject();
454+
return builder;
455455
}
456456

457457
/**

server/src/main/java/org/elasticsearch/repositories/blobstore/BlobStoreRepository.java

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@
5151
import org.elasticsearch.common.component.AbstractLifecycleComponent;
5252
import org.elasticsearch.common.compress.NotXContentException;
5353
import org.elasticsearch.common.io.stream.BytesStreamOutput;
54-
import org.elasticsearch.common.io.stream.OutputStreamStreamOutput;
55-
import org.elasticsearch.common.io.stream.StreamOutput;
5654
import org.elasticsearch.common.lucene.Lucene;
5755
import org.elasticsearch.common.lucene.store.InputStreamIndexInput;
5856
import org.elasticsearch.common.metrics.CounterMetric;
@@ -63,7 +61,6 @@
6361
import org.elasticsearch.common.util.set.Sets;
6462
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
6563
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
66-
import org.elasticsearch.common.xcontent.XContentBuilder;
6764
import org.elasticsearch.common.xcontent.XContentFactory;
6865
import org.elasticsearch.common.xcontent.XContentHelper;
6966
import org.elasticsearch.common.xcontent.XContentParser;
@@ -374,10 +371,7 @@ public void initializeSnapshot(SnapshotId snapshotId, List<IndexId> indices, Met
374371

375372
// write the index metadata for each index in the snapshot
376373
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());
381375
}
382376
} catch (IOException ex) {
383377
throw new SnapshotCreationException(metadata.name(), snapshotId, ex);
@@ -425,7 +419,7 @@ public void deleteSnapshot(SnapshotId snapshotId, long repositoryStateId, Action
425419
snapshotId,
426420
ActionListener.map(listener, v -> {
427421
try {
428-
blobStore().blobContainer(basePath().add("indices")).deleteBlobsIgnoringIfNotExists(
422+
blobStore().blobContainer(indicesPath()).deleteBlobsIgnoringIfNotExists(
429423
unreferencedIndices.stream().map(IndexId::getId).collect(Collectors.toList()));
430424
} catch (IOException e) {
431425
logger.warn(() ->
@@ -477,9 +471,8 @@ protected void doRun() {
477471
}
478472

479473
private void deleteIndexMetaDataBlobIgnoringErrors(SnapshotId snapshotId, IndexId indexId) {
480-
BlobContainer indexMetaDataBlobContainer = blobStore().blobContainer(basePath().add("indices").add(indexId.getId()));
481474
try {
482-
indexMetaDataFormat.delete(indexMetaDataBlobContainer, snapshotId.getUUID());
475+
indexMetaDataFormat.delete(indexContainer(indexId), snapshotId.getUUID());
483476
} catch (IOException ex) {
484477
logger.warn(() -> new ParameterizedMessage("[{}] failed to delete metadata for index [{}]",
485478
snapshotId, indexId.getName()), ex);
@@ -540,8 +533,19 @@ public MetaData getSnapshotGlobalMetaData(final SnapshotId snapshotId) {
540533

541534
@Override
542535
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())));
545549
}
546550

547551
/**
@@ -589,10 +593,9 @@ public String startVerification() {
589593
String seed = UUIDs.randomBase64UUID();
590594
byte[] testBytes = Strings.toUTF8Bytes(seed);
591595
BlobContainer testContainer = blobStore().blobContainer(basePath().add(testBlobPrefix(seed)));
592-
String blobName = "master.dat";
593596
BytesArray bytes = new BytesArray(testBytes);
594597
try (InputStream stream = bytes.streamInput()) {
595-
testContainer.writeBlobAtomic(blobName, stream, bytes.length(), true);
598+
testContainer.writeBlobAtomic("master.dat", stream, bytes.length(), true);
596599
}
597600
return seed;
598601
}
@@ -665,7 +668,7 @@ public RepositoryData getRepositoryData() {
665668
}
666669
}
667670

668-
public static String testBlobPrefix(String seed) {
671+
private static String testBlobPrefix(String seed) {
669672
return TESTS_FILE + seed;
670673
}
671674

@@ -685,19 +688,10 @@ protected void writeIndexGen(final RepositoryData repositoryData, final long rep
685688
"] - possibly due to simultaneous snapshot deletion requests");
686689
}
687690
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-
}
697691
// write the index file
698692
final String indexBlob = INDEX_FILE_PREFIX + Long.toString(newGen);
699693
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);
701695
// write the current generation to the index-latest file
702696
final BytesReference genBytes;
703697
try (BytesStreamOutput bStream = new BytesStreamOutput()) {
@@ -724,16 +718,9 @@ protected void writeIndexGen(final RepositoryData repositoryData, final long rep
724718
*/
725719
void writeIncompatibleSnapshots(RepositoryData repositoryData) throws IOException {
726720
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-
}
735721
// 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);
737724
}
738725

739726
/**
@@ -826,9 +813,8 @@ public void restoreShard(Store store, SnapshotId snapshotId,
826813
Version version, IndexId indexId, ShardId snapshotShardId, RecoveryState recoveryState) {
827814
ShardId shardId = store.shardId();
828815
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));
832818
try {
833819
BlobStoreIndexShardSnapshot snapshot = context.loadSnapshot();
834820
SnapshotFiles snapshotFiles = new SnapshotFiles(snapshot.snapshot(), snapshot.indexFiles());
@@ -904,8 +890,7 @@ private class Context {
904890
Context(SnapshotId snapshotId, IndexId indexId, ShardId shardId, ShardId snapshotShardId) {
905891
this.snapshotId = snapshotId;
906892
this.shardId = shardId;
907-
blobContainer = blobStore().blobContainer(basePath().add("indices").add(indexId.getId())
908-
.add(Integer.toString(snapshotShardId.getId())));
893+
blobContainer = shardContainer(indexId, snapshotShardId);
909894
}
910895

911896
/**

0 commit comments

Comments
 (0)