|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | +package org.elasticsearch.xpack.searchablesnapshots; |
| 7 | + |
| 8 | +import org.apache.lucene.index.IndexWriter; |
| 9 | +import org.apache.lucene.index.IndexWriterConfig; |
| 10 | +import org.apache.lucene.store.Directory; |
| 11 | +import org.elasticsearch.cluster.metadata.IndexMetaData; |
| 12 | +import org.elasticsearch.cluster.metadata.RepositoryMetaData; |
| 13 | +import org.elasticsearch.common.Strings; |
| 14 | +import org.elasticsearch.common.blobstore.BlobContainer; |
| 15 | +import org.elasticsearch.common.settings.Setting; |
| 16 | +import org.elasticsearch.common.settings.Settings; |
| 17 | +import org.elasticsearch.index.IndexSettings; |
| 18 | +import org.elasticsearch.index.seqno.SequenceNumbers; |
| 19 | +import org.elasticsearch.index.shard.ShardPath; |
| 20 | +import org.elasticsearch.index.snapshots.blobstore.BlobStoreIndexShardSnapshot; |
| 21 | +import org.elasticsearch.index.store.SearchableSnapshotDirectory; |
| 22 | +import org.elasticsearch.index.translog.Translog; |
| 23 | +import org.elasticsearch.plugins.IndexStorePlugin; |
| 24 | +import org.elasticsearch.repositories.FilterRepository; |
| 25 | +import org.elasticsearch.repositories.IndexId; |
| 26 | +import org.elasticsearch.repositories.RepositoriesService; |
| 27 | +import org.elasticsearch.repositories.Repository; |
| 28 | +import org.elasticsearch.repositories.blobstore.BlobStoreRepository; |
| 29 | +import org.elasticsearch.snapshots.SnapshotId; |
| 30 | + |
| 31 | +import java.io.IOException; |
| 32 | +import java.util.HashMap; |
| 33 | +import java.util.Map; |
| 34 | +import java.util.function.Function; |
| 35 | +import java.util.function.Supplier; |
| 36 | + |
| 37 | +import static org.elasticsearch.index.IndexModule.INDEX_STORE_TYPE_SETTING; |
| 38 | + |
| 39 | +/** |
| 40 | + * A repository that wraps a {@link BlobStoreRepository} to add settings to the index metadata during a restore to identify the source |
| 41 | + * snapshot and index in order to create a {@link SearchableSnapshotDirectory} (and corresponding empty translog) to search these shards |
| 42 | + * without needing to fully restore them. |
| 43 | + */ |
| 44 | +public class SearchableSnapshotRepository extends FilterRepository { |
| 45 | + |
| 46 | + public static final String TYPE = "searchable"; |
| 47 | + |
| 48 | + public static final Setting<String> SNAPSHOT_REPOSITORY_SETTING = |
| 49 | + Setting.simpleString("index.store.snapshot.repository_name", Setting.Property.IndexScope, Setting.Property.PrivateIndex); |
| 50 | + public static final Setting<String> SNAPSHOT_SNAPSHOT_NAME_SETTING = |
| 51 | + Setting.simpleString("index.store.snapshot.snapshot_name", Setting.Property.IndexScope, Setting.Property.PrivateIndex); |
| 52 | + public static final Setting<String> SNAPSHOT_SNAPSHOT_ID_SETTING = |
| 53 | + Setting.simpleString("index.store.snapshot.snapshot_uuid", Setting.Property.IndexScope, Setting.Property.PrivateIndex); |
| 54 | + public static final Setting<String> SNAPSHOT_INDEX_ID_SETTING = |
| 55 | + Setting.simpleString("index.store.snapshot.index_uuid", Setting.Property.IndexScope, Setting.Property.PrivateIndex); |
| 56 | + |
| 57 | + public static final String SNAPSHOT_DIRECTORY_FACTORY_KEY = "snapshot"; |
| 58 | + |
| 59 | + private static final Setting<String> DELEGATE_TYPE |
| 60 | + = new Setting<>("delegate_type", "", Function.identity(), Setting.Property.NodeScope); |
| 61 | + |
| 62 | + private final BlobStoreRepository blobStoreRepository; |
| 63 | + |
| 64 | + public SearchableSnapshotRepository(Repository in) { |
| 65 | + super(in); |
| 66 | + if (in instanceof BlobStoreRepository == false) { |
| 67 | + throw new IllegalArgumentException("Repository [" + in + "] does not support searchable snapshots" ); |
| 68 | + } |
| 69 | + blobStoreRepository = (BlobStoreRepository) in; |
| 70 | + } |
| 71 | + |
| 72 | + private Directory makeDirectory(IndexSettings indexSettings, ShardPath shardPath) throws IOException { |
| 73 | + |
| 74 | + IndexId indexId = new IndexId(indexSettings.getIndex().getName(), SNAPSHOT_INDEX_ID_SETTING.get(indexSettings.getSettings())); |
| 75 | + BlobContainer blobContainer = blobStoreRepository.shardContainer(indexId, shardPath.getShardId().id()); |
| 76 | + |
| 77 | + SnapshotId snapshotId = new SnapshotId(SNAPSHOT_SNAPSHOT_NAME_SETTING.get(indexSettings.getSettings()), |
| 78 | + SNAPSHOT_SNAPSHOT_ID_SETTING.get(indexSettings.getSettings())); |
| 79 | + BlobStoreIndexShardSnapshot snapshot = blobStoreRepository.loadShardSnapshot(blobContainer, snapshotId); |
| 80 | + |
| 81 | + final SearchableSnapshotDirectory searchableSnapshotDirectory = new SearchableSnapshotDirectory(snapshot, blobContainer); |
| 82 | + final InMemoryNoOpCommitDirectory inMemoryNoOpCommitDirectory = new InMemoryNoOpCommitDirectory(searchableSnapshotDirectory); |
| 83 | + |
| 84 | + try (IndexWriter indexWriter = new IndexWriter(inMemoryNoOpCommitDirectory, new IndexWriterConfig())) { |
| 85 | + final Map<String, String> userData = new HashMap<>(); |
| 86 | + indexWriter.getLiveCommitData().forEach(e -> userData.put(e.getKey(), e.getValue())); |
| 87 | + |
| 88 | + final String translogUUID = Translog.createEmptyTranslog(shardPath.resolveTranslog(), |
| 89 | + Long.parseLong(userData.get(SequenceNumbers.LOCAL_CHECKPOINT_KEY)), |
| 90 | + shardPath.getShardId(), 0L); |
| 91 | + |
| 92 | + userData.put(Translog.TRANSLOG_UUID_KEY, translogUUID); |
| 93 | + indexWriter.setLiveCommitData(userData.entrySet()); |
| 94 | + indexWriter.commit(); |
| 95 | + } |
| 96 | + |
| 97 | + return inMemoryNoOpCommitDirectory; |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public IndexMetaData getSnapshotIndexMetaData(SnapshotId snapshotId, IndexId index) throws IOException { |
| 102 | + final IndexMetaData indexMetaData = super.getSnapshotIndexMetaData(snapshotId, index); |
| 103 | + final IndexMetaData.Builder builder = IndexMetaData.builder(indexMetaData); |
| 104 | + builder.settings(Settings.builder().put(indexMetaData.getSettings()).put(getIndexSettings(blobStoreRepository, snapshotId, index))); |
| 105 | + return builder.build(); |
| 106 | + } |
| 107 | + |
| 108 | + public static Settings getIndexSettings(Repository repository, SnapshotId snapshotId, IndexId indexId) { |
| 109 | + return Settings.builder() |
| 110 | + .put(SNAPSHOT_REPOSITORY_SETTING.getKey(), repository.getMetadata().name()) |
| 111 | + .put(SNAPSHOT_SNAPSHOT_NAME_SETTING.getKey(), snapshotId.getName()) |
| 112 | + .put(SNAPSHOT_SNAPSHOT_ID_SETTING.getKey(), snapshotId.getUUID()) |
| 113 | + .put(SNAPSHOT_INDEX_ID_SETTING.getKey(), indexId.getId()) |
| 114 | + .put(INDEX_STORE_TYPE_SETTING.getKey(), SNAPSHOT_DIRECTORY_FACTORY_KEY) |
| 115 | + .put(IndexMetaData.SETTING_BLOCKS_WRITE, true) |
| 116 | + .build(); |
| 117 | + } |
| 118 | + |
| 119 | + static Factory getRepositoryFactory() { |
| 120 | + return new Repository.Factory() { |
| 121 | + @Override |
| 122 | + public Repository create(RepositoryMetaData metadata) { |
| 123 | + throw new UnsupportedOperationException(); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public Repository create(RepositoryMetaData metaData, Function<String, Factory> typeLookup) throws Exception { |
| 128 | + String delegateType = DELEGATE_TYPE.get(metaData.settings()); |
| 129 | + if (Strings.hasLength(delegateType) == false) { |
| 130 | + throw new IllegalArgumentException(DELEGATE_TYPE.getKey() + " must be set"); |
| 131 | + } |
| 132 | + Repository.Factory factory = typeLookup.apply(delegateType); |
| 133 | + return new SearchableSnapshotRepository(factory.create(new RepositoryMetaData(metaData.name(), |
| 134 | + delegateType, metaData.settings()), typeLookup)); |
| 135 | + } |
| 136 | + }; |
| 137 | + } |
| 138 | + |
| 139 | + public static IndexStorePlugin.DirectoryFactory newDirectoryFactory(final Supplier<RepositoriesService> repositoriesService) { |
| 140 | + return (indexSettings, shardPath) -> { |
| 141 | + final RepositoriesService repositories = repositoriesService.get(); |
| 142 | + assert repositories != null; |
| 143 | + |
| 144 | + final Repository repository = repositories.repository(SNAPSHOT_REPOSITORY_SETTING.get(indexSettings.getSettings())); |
| 145 | + if (repository instanceof SearchableSnapshotRepository == false) { |
| 146 | + throw new IllegalArgumentException("Repository [" + repository + "] is not searchable" ); |
| 147 | + } |
| 148 | + |
| 149 | + return ((SearchableSnapshotRepository)repository).makeDirectory(indexSettings, shardPath); |
| 150 | + }; |
| 151 | + } |
| 152 | +} |
0 commit comments