|
22 | 22 | import org.apache.lucene.store.Directory;
|
23 | 23 | import org.apache.lucene.util.IOUtils;
|
24 | 24 | import org.apache.lucene.util.TestUtil;
|
25 |
| -import org.elasticsearch.Version; |
26 | 25 | import org.elasticsearch.cluster.metadata.RepositoryMetaData;
|
27 |
| -import org.elasticsearch.cluster.node.DiscoveryNode; |
28 |
| -import org.elasticsearch.cluster.routing.RecoverySource; |
29 | 26 | import org.elasticsearch.cluster.routing.ShardRouting;
|
30 | 27 | import org.elasticsearch.cluster.routing.ShardRoutingHelper;
|
31 |
| -import org.elasticsearch.cluster.routing.ShardRoutingState; |
32 | 28 | import org.elasticsearch.common.UUIDs;
|
33 |
| -import org.elasticsearch.common.blobstore.BlobContainer; |
34 |
| -import org.elasticsearch.common.blobstore.BlobMetaData; |
35 |
| -import org.elasticsearch.common.blobstore.BlobPath; |
36 |
| -import org.elasticsearch.common.blobstore.BlobStore; |
37 |
| -import org.elasticsearch.common.blobstore.support.PlainBlobMetaData; |
38 |
| -import org.elasticsearch.common.io.Streams; |
39 | 29 | import org.elasticsearch.common.settings.Settings;
|
40 |
| -import org.elasticsearch.common.xcontent.NamedXContentRegistry; |
41 |
| -import org.elasticsearch.index.Index; |
42 |
| -import org.elasticsearch.index.engine.Engine; |
| 30 | +import org.elasticsearch.env.Environment; |
| 31 | +import org.elasticsearch.env.TestEnvironment; |
43 | 32 | import org.elasticsearch.index.shard.IndexShard;
|
44 | 33 | import org.elasticsearch.index.shard.IndexShardState;
|
45 | 34 | import org.elasticsearch.index.shard.IndexShardTestCase;
|
46 | 35 | import org.elasticsearch.index.shard.ShardId;
|
47 |
| -import org.elasticsearch.index.snapshots.IndexShardSnapshotStatus; |
48 | 36 | import org.elasticsearch.index.store.Store;
|
49 | 37 | import org.elasticsearch.index.store.StoreFileMetaData;
|
50 |
| -import org.elasticsearch.indices.recovery.RecoveryState; |
51 | 38 | import org.elasticsearch.repositories.IndexId;
|
| 39 | +import org.elasticsearch.repositories.Repository; |
| 40 | +import org.elasticsearch.repositories.fs.FsRepository; |
52 | 41 | import org.elasticsearch.snapshots.Snapshot;
|
53 | 42 | import org.elasticsearch.snapshots.SnapshotId;
|
54 | 43 |
|
55 |
| -import java.io.ByteArrayInputStream; |
56 |
| -import java.io.ByteArrayOutputStream; |
57 |
| -import java.io.FileNotFoundException; |
58 | 44 | import java.io.IOException;
|
59 |
| -import java.io.InputStream; |
60 | 45 | import java.nio.file.Files;
|
| 46 | +import java.nio.file.Path; |
61 | 47 | import java.util.Arrays;
|
62 |
| -import java.util.HashMap; |
63 | 48 | import java.util.List;
|
64 |
| -import java.util.Map; |
65 |
| -import java.util.stream.Collectors; |
66 | 49 |
|
67 | 50 | import static org.elasticsearch.cluster.routing.RecoverySource.StoreRecoverySource.EXISTING_STORE_INSTANCE;
|
68 |
| -import static org.elasticsearch.cluster.routing.TestShardRouting.newShardRouting; |
69 | 51 |
|
70 | 52 | /**
|
71 | 53 | * This class tests the behavior of {@link BlobStoreRepository} when it
|
@@ -96,7 +78,7 @@ public void testRestoreSnapshotWithExistingFiles() throws IOException {
|
96 | 78 | assertDocCount(shard, numDocs);
|
97 | 79 |
|
98 | 80 | // snapshot the shard
|
99 |
| - final BlobStoreRepository repository = createRepository(); |
| 81 | + final Repository repository = createRepository(); |
100 | 82 | final Snapshot snapshot = new Snapshot(repository.getMetadata().name(), new SnapshotId(randomAlphaOfLength(10), "_uuid"));
|
101 | 83 | snapshotShard(shard, snapshot, repository);
|
102 | 84 |
|
@@ -143,135 +125,19 @@ public void testRestoreSnapshotWithExistingFiles() throws IOException {
|
143 | 125 | }
|
144 | 126 | }
|
145 | 127 |
|
146 |
| - /** Recover a shard from a snapshot using a given repository **/ |
147 |
| - private void recoverShardFromSnapshot(final IndexShard shard, |
148 |
| - final Snapshot snapshot, |
149 |
| - final BlobStoreRepository repository) throws IOException { |
150 |
| - final Version version = Version.CURRENT; |
151 |
| - final ShardId shardId = shard.shardId(); |
152 |
| - final String index = shard.shardId().getIndexName(); |
153 |
| - final IndexId indexId = new IndexId(index, UUIDs.randomBase64UUID()); |
154 |
| - final DiscoveryNode node = new DiscoveryNode(randomAlphaOfLength(25), buildNewFakeTransportAddress(), version); |
155 |
| - final RecoverySource.SnapshotRecoverySource recoverySource = new RecoverySource.SnapshotRecoverySource(snapshot, version, index); |
156 |
| - final ShardRouting shardRouting = newShardRouting(shardId, node.getId(), true, recoverySource, ShardRoutingState.INITIALIZING); |
157 |
| - |
158 |
| - shard.markAsRecovering("from snapshot", new RecoveryState(shardRouting, node, null)); |
159 |
| - repository.restoreShard(shard, snapshot.getSnapshotId(), version, indexId, shard.shardId(), shard.recoveryState()); |
160 |
| - } |
161 |
| - |
162 |
| - /** Snapshot a shard using a given repository **/ |
163 |
| - private void snapshotShard(final IndexShard shard, |
164 |
| - final Snapshot snapshot, |
165 |
| - final BlobStoreRepository repository) throws IOException { |
166 |
| - final IndexShardSnapshotStatus snapshotStatus = new IndexShardSnapshotStatus(); |
167 |
| - try (Engine.IndexCommitRef indexCommitRef = shard.acquireIndexCommit(true)) { |
168 |
| - Index index = shard.shardId().getIndex(); |
169 |
| - IndexId indexId = new IndexId(index.getName(), index.getUUID()); |
170 |
| - |
171 |
| - repository.snapshotShard(shard, snapshot.getSnapshotId(), indexId, indexCommitRef.getIndexCommit(), snapshotStatus); |
172 |
| - } |
173 |
| - assertEquals(IndexShardSnapshotStatus.Stage.DONE, snapshotStatus.stage()); |
174 |
| - assertEquals(shard.snapshotStoreMetadata().size(), snapshotStatus.numberOfFiles()); |
175 |
| - assertNull(snapshotStatus.failure()); |
176 |
| - } |
177 |
| - |
178 |
| - |
179 |
| - /** |
180 |
| - * A {@link BlobStoreRepository} implementation that works in memory. |
181 |
| - * |
182 |
| - * It implements only the methods required by the tests and is not thread safe. |
183 |
| - */ |
184 |
| - class MemoryBlobStoreRepository extends BlobStoreRepository { |
185 |
| - |
186 |
| - private final Map<String, byte[]> files = new HashMap<>(); |
187 |
| - |
188 |
| - MemoryBlobStoreRepository(final RepositoryMetaData metadata, final Settings settings, final NamedXContentRegistry registry) { |
189 |
| - super(metadata, settings, registry); |
190 |
| - } |
191 |
| - |
192 |
| - @Override |
193 |
| - protected BlobStore blobStore() { |
194 |
| - return new BlobStore() { |
195 |
| - @Override |
196 |
| - public BlobContainer blobContainer(BlobPath path) { |
197 |
| - return new BlobContainer() { |
198 |
| - @Override |
199 |
| - public BlobPath path() { |
200 |
| - return new BlobPath(); |
201 |
| - } |
202 |
| - |
203 |
| - @Override |
204 |
| - public boolean blobExists(String blobName) { |
205 |
| - return files.containsKey(blobName); |
206 |
| - } |
207 |
| - |
208 |
| - @Override |
209 |
| - public InputStream readBlob(String blobName) throws IOException { |
210 |
| - if (blobExists(blobName) == false) { |
211 |
| - throw new FileNotFoundException(blobName); |
212 |
| - } |
213 |
| - return new ByteArrayInputStream(files.get(blobName)); |
214 |
| - } |
215 |
| - |
216 |
| - @Override |
217 |
| - public void writeBlob(String blobName, InputStream in, long blobSize) throws IOException { |
218 |
| - try (ByteArrayOutputStream out = new ByteArrayOutputStream((int) blobSize)) { |
219 |
| - Streams.copy(in, out); |
220 |
| - files.put(blobName, out.toByteArray()); |
221 |
| - } |
222 |
| - } |
223 |
| - |
224 |
| - @Override |
225 |
| - public void deleteBlob(String blobName) throws IOException { |
226 |
| - files.remove(blobName); |
227 |
| - } |
228 |
| - |
229 |
| - @Override |
230 |
| - public Map<String, BlobMetaData> listBlobs() throws IOException { |
231 |
| - final Map<String, BlobMetaData> blobs = new HashMap<>(files.size()); |
232 |
| - files.forEach((key, value) -> blobs.put(key, new PlainBlobMetaData(key, value.length))); |
233 |
| - return blobs; |
234 |
| - } |
235 |
| - |
236 |
| - @Override |
237 |
| - public Map<String, BlobMetaData> listBlobsByPrefix(String blobNamePrefix) throws IOException { |
238 |
| - return listBlobs().entrySet().stream() |
239 |
| - .filter(e -> e.getKey().startsWith(blobNamePrefix)) |
240 |
| - .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); |
241 |
| - } |
242 |
| - |
243 |
| - @Override |
244 |
| - public void move(String sourceBlobName, String targetBlobName) throws IOException { |
245 |
| - byte[] bytes = files.remove(sourceBlobName); |
246 |
| - if (bytes == null) { |
247 |
| - throw new FileNotFoundException(sourceBlobName); |
248 |
| - } |
249 |
| - files.put(targetBlobName, bytes); |
250 |
| - } |
251 |
| - }; |
252 |
| - } |
253 |
| - |
254 |
| - @Override |
255 |
| - public void delete(BlobPath path) throws IOException { |
256 |
| - throw new UnsupportedOperationException("MemoryBlobStoreRepository does not support this method"); |
257 |
| - } |
258 |
| - |
259 |
| - @Override |
260 |
| - public void close() throws IOException { |
261 |
| - files.clear(); |
262 |
| - } |
263 |
| - }; |
264 |
| - } |
265 |
| - |
266 |
| - @Override |
267 |
| - protected BlobPath basePath() { |
268 |
| - return new BlobPath(); |
269 |
| - } |
| 128 | + /** Create a {@link Repository} with a random name **/ |
| 129 | + private Repository createRepository() throws IOException { |
| 130 | + Settings settings = Settings.builder().put("location", randomAlphaOfLength(10)).build(); |
| 131 | + RepositoryMetaData repositoryMetaData = new RepositoryMetaData(randomAlphaOfLength(10), FsRepository.TYPE, settings); |
| 132 | + return new FsRepository(repositoryMetaData, createEnvironment(), xContentRegistry()); |
270 | 133 | }
|
271 | 134 |
|
272 |
| - /** Create a {@link BlobStoreRepository} with a random name **/ |
273 |
| - private BlobStoreRepository createRepository() { |
274 |
| - String name = randomAlphaOfLength(10); |
275 |
| - return new MemoryBlobStoreRepository(new RepositoryMetaData(name, "in-memory", Settings.EMPTY), Settings.EMPTY, xContentRegistry()); |
| 135 | + /** Create a {@link Environment} with random path.home and path.repo **/ |
| 136 | + private Environment createEnvironment() { |
| 137 | + Path home = createTempDir(); |
| 138 | + return TestEnvironment.newEnvironment(Settings.builder() |
| 139 | + .put(Environment.PATH_HOME_SETTING.getKey(), home.toAbsolutePath()) |
| 140 | + .put(Environment.PATH_REPO_SETTING.getKey(), home.resolve("repo").toAbsolutePath()) |
| 141 | + .build()); |
276 | 142 | }
|
277 | 143 | }
|
0 commit comments