Skip to content

Commit 76fa0d5

Browse files
Pass XContentParserConfiguration into ChecksumBlobStoreFormat
Passes an optional parameter into `ChecksumBlobStoreFormat.read` to specify the XContentParserConfiguration of the parser Relates to: ES-12539
1 parent 52feb00 commit 76fa0d5

File tree

2 files changed

+44
-14
lines changed

2 files changed

+44
-14
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@
144144
import org.elasticsearch.xcontent.XContentBuilder;
145145
import org.elasticsearch.xcontent.XContentFactory;
146146
import org.elasticsearch.xcontent.XContentParser;
147+
import org.elasticsearch.xcontent.XContentParserConfiguration;
147148
import org.elasticsearch.xcontent.XContentType;
148149

149150
import java.io.BufferedInputStream;
@@ -1326,8 +1327,12 @@ private void determineShardCount(ActionListener<Void> listener) {
13261327

13271328
private void getOneShardCount(String indexMetaGeneration) {
13281329
try {
1330+
// As per ES-12539, there is no need to load the entire IndexMetadata object just to read the shard count
1331+
// Instead, we read the minimum fields necessary, including the setting index.number_of_shards
1332+
XContentParserConfiguration xContentParserConfiguration = XContentParserConfiguration.EMPTY.withDeprecationHandler(LoggingDeprecationHandler.INSTANCE)
1333+
.withFiltering(Set.of("*.settings", "*.mapping_version", "*.settings_version", "*.aliases_version"), null, false);
13291334
updateShardCount(
1330-
INDEX_METADATA_FORMAT.read(getProjectRepo(), indexContainer, indexMetaGeneration, namedXContentRegistry)
1335+
INDEX_METADATA_FORMAT.read(getProjectRepo(), indexContainer, indexMetaGeneration, namedXContentRegistry, xContentParserConfiguration)
13311336
.getNumberOfShards()
13321337
);
13331338
} catch (Exception ex) {

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

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,33 @@ public ChecksumBlobStoreFormat(
114114

115115
/**
116116
* Reads and parses the blob with given name, applying name translation using the {link #blobName} method
117-
*
118-
* @param blobContainer blob container
119-
* @param name name to be translated into
120-
* @return parsed blob object
117+
* @param projectRepo the project repository context, used for deserialization
118+
* @param blobContainer the {@link BlobContainer} from which to read the blob
119+
* @param name the logical name of the blob to read (will be formatted to the actual blob name)
120+
* @param namedXContentRegistry the {@link NamedXContentRegistry} to use for parser construction
121+
* @return the deserialized object of type {@code T} read from the blob
122+
* @throws IOException if an I/O error occurs while reading or parsing the blob
121123
*/
122124
public T read(ProjectRepo projectRepo, BlobContainer blobContainer, String name, NamedXContentRegistry namedXContentRegistry)
125+
throws IOException {
126+
return read(projectRepo, blobContainer, name, namedXContentRegistry, null);
127+
}
128+
129+
/**
130+
* Reads and parses the blob with given name, applying name translation using the {link #blobName} method
131+
* @param projectRepo the project repository context, used for deserialization
132+
* @param blobContainer the {@link BlobContainer} from which to read the blob
133+
* @param name the logical name of the blob to read (will be formatted to the actual blob name)
134+
* @param namedXContentRegistry the {@link NamedXContentRegistry} to use for parser construction
135+
* @param xContentParserConfiguration An optional {@link XContentParserConfiguration} to use when deserializing the blob
136+
* @return the deserialized object of type {@code T} read from the blob
137+
* @throws IOException if an I/O error occurs while reading or parsing the blob
138+
*/
139+
public T read(ProjectRepo projectRepo, BlobContainer blobContainer, String name, NamedXContentRegistry namedXContentRegistry, XContentParserConfiguration xContentParserConfiguration)
123140
throws IOException {
124141
String blobName = blobName(name);
125142
try (InputStream in = blobContainer.readBlob(OperationPurpose.SNAPSHOT_METADATA, blobName)) {
126-
return deserialize(projectRepo, namedXContentRegistry, in);
143+
return deserialize(projectRepo, namedXContentRegistry, in, xContentParserConfiguration);
127144
}
128145
}
129146

@@ -132,6 +149,10 @@ public String blobName(String name) {
132149
}
133150

134151
public T deserialize(ProjectRepo projectRepo, NamedXContentRegistry namedXContentRegistry, InputStream input) throws IOException {
152+
return deserialize(projectRepo, namedXContentRegistry, input, null);
153+
}
154+
155+
public T deserialize(ProjectRepo projectRepo, NamedXContentRegistry namedXContentRegistry, InputStream input, XContentParserConfiguration xContentParserConfiguration) throws IOException {
135156
final DeserializeMetaBlobInputStream deserializeMetaBlobInputStream = new DeserializeMetaBlobInputStream(input);
136157
try {
137158
CodecUtil.checkHeader(new InputStreamDataInput(deserializeMetaBlobInputStream), codec, VERSION, VERSION);
@@ -149,8 +170,10 @@ public T deserialize(ProjectRepo projectRepo, NamedXContentRegistry namedXConten
149170
deserializeMetaBlobInputStream.verifyFooter();
150171
try (
151172
XContentParser parser = XContentHelper.createParserNotCompressed(
152-
XContentParserConfiguration.EMPTY.withRegistry(namedXContentRegistry)
153-
.withDeprecationHandler(LoggingDeprecationHandler.INSTANCE),
173+
xContentParserConfiguration == null
174+
? XContentParserConfiguration.EMPTY.withRegistry(namedXContentRegistry)
175+
.withDeprecationHandler(LoggingDeprecationHandler.INSTANCE)
176+
: xContentParserConfiguration,
154177
bytesReference,
155178
XContentType.SMILE
156179
)
@@ -160,8 +183,10 @@ public T deserialize(ProjectRepo projectRepo, NamedXContentRegistry namedXConten
160183
} catch (Exception e) {
161184
try (
162185
XContentParser parser = XContentHelper.createParserNotCompressed(
163-
XContentParserConfiguration.EMPTY.withRegistry(namedXContentRegistry)
164-
.withDeprecationHandler(LoggingDeprecationHandler.INSTANCE),
186+
xContentParserConfiguration == null
187+
? XContentParserConfiguration.EMPTY.withRegistry(namedXContentRegistry)
188+
.withDeprecationHandler(LoggingDeprecationHandler.INSTANCE)
189+
: xContentParserConfiguration,
165190
bytesReference,
166191
XContentType.SMILE
167192
)
@@ -392,10 +417,10 @@ public void close() {
392417
// in order to write the footer we need to prevent closing the actual index input.
393418
}
394419
};
395-
XContentBuilder builder = XContentFactory.contentBuilder(
396-
XContentType.SMILE,
397-
compress ? CompressorFactory.COMPRESSOR.threadLocalOutputStream(indexOutputOutputStream) : indexOutputOutputStream
398-
)
420+
XContentBuilder builder = XContentFactory.contentBuilder(
421+
XContentType.SMILE,
422+
compress ? CompressorFactory.COMPRESSOR.threadLocalOutputStream(indexOutputOutputStream) : indexOutputOutputStream
423+
)
399424
) {
400425
ToXContent.Params params = extraParams.isEmpty()
401426
? SNAPSHOT_ONLY_FORMAT_PARAMS

0 commit comments

Comments
 (0)