Skip to content

Commit af02795

Browse files
authored
Check index setting for source mode in SourceOnlySnapshotRepository (#116002) (#116014)
* Check index setting for source mode in SourceOnlySnapshotRepository * update * Revert "update" This reverts commit 9bbf049. (cherry picked from commit 37a4ee3)
1 parent 1151ef4 commit af02795

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

server/src/main/java/org/elasticsearch/index/mapper/SourceFieldMapper.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ public SourceFieldMapper build() {
242242
}
243243

244244
private Mode resolveSourceMode() {
245-
// If the `index.mapper.source.mode` exists it takes precedence to determine the source mode for `_source`
245+
// If the `index.mapping.source.mode` exists it takes precedence to determine the source mode for `_source`
246246
// otherwise the mode is determined according to `_source.mode`.
247247
if (INDEX_MAPPER_SOURCE_MODE_SETTING.exists(settings)) {
248248
return INDEX_MAPPER_SOURCE_MODE_SETTING.get(settings);
@@ -439,6 +439,10 @@ public static boolean isSynthetic(IndexSettings indexSettings) {
439439
return INDEX_MAPPER_SOURCE_MODE_SETTING.get(indexSettings.getSettings()) == SourceFieldMapper.Mode.SYNTHETIC;
440440
}
441441

442+
public static boolean isStored(IndexSettings indexSettings) {
443+
return INDEX_MAPPER_SOURCE_MODE_SETTING.get(indexSettings.getSettings()) == Mode.STORED;
444+
}
445+
442446
public boolean isDisabled() {
443447
return mode == Mode.DISABLED;
444448
}

x-pack/plugin/core/src/main/java/org/elasticsearch/snapshots/sourceonly/SourceOnlySnapshotRepository.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.elasticsearch.index.engine.EngineFactory;
3434
import org.elasticsearch.index.engine.ReadOnlyEngine;
3535
import org.elasticsearch.index.mapper.MapperService;
36+
import org.elasticsearch.index.mapper.SourceFieldMapper;
3637
import org.elasticsearch.index.store.Store;
3738
import org.elasticsearch.index.translog.TranslogStats;
3839
import org.elasticsearch.repositories.FilterRepository;
@@ -139,8 +140,9 @@ private static Metadata metadataToSnapshot(Collection<IndexId> indices, Metadata
139140
@Override
140141
public void snapshotShard(SnapshotShardContext context) {
141142
final MapperService mapperService = context.mapperService();
142-
if (mapperService.documentMapper() != null // if there is no mapping this is null
143-
&& mapperService.documentMapper().sourceMapper().isComplete() == false) {
143+
if ((mapperService.documentMapper() != null // if there is no mapping this is null
144+
&& mapperService.documentMapper().sourceMapper().isComplete() == false)
145+
|| (mapperService.documentMapper() == null && SourceFieldMapper.isStored(mapperService.getIndexSettings()) == false)) {
144146
context.onFailure(
145147
new IllegalStateException(
146148
"Can't snapshot _source only on an index that has incomplete source ie. has _source disabled or filters the source"

x-pack/plugin/core/src/test/java/org/elasticsearch/snapshots/sourceonly/SourceOnlySnapshotShardTests.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import org.elasticsearch.index.engine.InternalEngineFactory;
5353
import org.elasticsearch.index.fieldvisitor.FieldsVisitor;
5454
import org.elasticsearch.index.mapper.SeqNoFieldMapper;
55+
import org.elasticsearch.index.mapper.SourceFieldMapper;
5556
import org.elasticsearch.index.mapper.SourceToParse;
5657
import org.elasticsearch.index.seqno.RetentionLeaseSyncer;
5758
import org.elasticsearch.index.seqno.SeqNoStats;
@@ -149,6 +150,55 @@ public void testSourceIncomplete() throws IOException {
149150
closeShards(shard);
150151
}
151152

153+
public void testSourceIncompleteSyntheticSourceNoDoc() throws IOException {
154+
ShardRouting shardRouting = shardRoutingBuilder(
155+
new ShardId("index", "_na_", 0),
156+
randomAlphaOfLength(10),
157+
true,
158+
ShardRoutingState.INITIALIZING
159+
).withRecoverySource(RecoverySource.EmptyStoreRecoverySource.INSTANCE).build();
160+
Settings settings = Settings.builder()
161+
.put(IndexMetadata.SETTING_VERSION_CREATED, IndexVersion.current())
162+
.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)
163+
.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1)
164+
.put(SourceFieldMapper.INDEX_MAPPER_SOURCE_MODE_SETTING.getKey(), "synthetic")
165+
.build();
166+
IndexMetadata metadata = IndexMetadata.builder(shardRouting.getIndexName()).settings(settings).primaryTerm(0, primaryTerm).build();
167+
IndexShard shard = newShard(shardRouting, metadata, null, new InternalEngineFactory());
168+
recoverShardFromStore(shard);
169+
SnapshotId snapshotId = new SnapshotId("test", "test");
170+
IndexId indexId = new IndexId(shard.shardId().getIndexName(), shard.shardId().getIndex().getUUID());
171+
SourceOnlySnapshotRepository repository = new SourceOnlySnapshotRepository(createRepository());
172+
repository.start();
173+
try (Engine.IndexCommitRef snapshotRef = shard.acquireLastIndexCommit(true)) {
174+
IndexShardSnapshotStatus indexShardSnapshotStatus = IndexShardSnapshotStatus.newInitializing(new ShardGeneration(-1L));
175+
final PlainActionFuture<ShardSnapshotResult> future = new PlainActionFuture<>();
176+
runAsSnapshot(
177+
shard.getThreadPool(),
178+
() -> repository.snapshotShard(
179+
new SnapshotShardContext(
180+
shard.store(),
181+
shard.mapperService(),
182+
snapshotId,
183+
indexId,
184+
new SnapshotIndexCommit(snapshotRef),
185+
null,
186+
indexShardSnapshotStatus,
187+
IndexVersion.current(),
188+
randomMillisUpToYear9999(),
189+
future
190+
)
191+
)
192+
);
193+
IllegalStateException illegalStateException = expectThrows(IllegalStateException.class, future::actionGet);
194+
assertEquals(
195+
"Can't snapshot _source only on an index that has incomplete source ie. has _source disabled or filters the source",
196+
illegalStateException.getMessage()
197+
);
198+
}
199+
closeShards(shard);
200+
}
201+
152202
public void testIncrementalSnapshot() throws IOException {
153203
IndexShard shard = newStartedShard();
154204
for (int i = 0; i < 10; i++) {

0 commit comments

Comments
 (0)