Skip to content

Commit 37a4ee3

Browse files
authored
Check index setting for source mode in SourceOnlySnapshotRepository (#116002)
* Check index setting for source mode in SourceOnlySnapshotRepository * update * Revert "update" This reverts commit 9bbf049.
1 parent 1c644cc commit 37a4ee3

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;
@@ -134,8 +135,9 @@ private static Metadata metadataToSnapshot(Collection<IndexId> indices, Metadata
134135
@Override
135136
public void snapshotShard(SnapshotShardContext context) {
136137
final MapperService mapperService = context.mapperService();
137-
if (mapperService.documentMapper() != null // if there is no mapping this is null
138-
&& mapperService.documentMapper().sourceMapper().isComplete() == false) {
138+
if ((mapperService.documentMapper() != null // if there is no mapping this is null
139+
&& mapperService.documentMapper().sourceMapper().isComplete() == false)
140+
|| (mapperService.documentMapper() == null && SourceFieldMapper.isStored(mapperService.getIndexSettings()) == false)) {
139141
context.onFailure(
140142
new IllegalStateException(
141143
"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
@@ -53,6 +53,7 @@
5353
import org.elasticsearch.index.engine.InternalEngineFactory;
5454
import org.elasticsearch.index.fieldvisitor.FieldsVisitor;
5555
import org.elasticsearch.index.mapper.SeqNoFieldMapper;
56+
import org.elasticsearch.index.mapper.SourceFieldMapper;
5657
import org.elasticsearch.index.mapper.SourceToParse;
5758
import org.elasticsearch.index.seqno.RetentionLeaseSyncer;
5859
import org.elasticsearch.index.seqno.SeqNoStats;
@@ -150,6 +151,55 @@ public void testSourceIncomplete() throws IOException {
150151
closeShards(shard);
151152
}
152153

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

0 commit comments

Comments
 (0)