|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.snapshots.mockstore; |
| 21 | + |
| 22 | +import org.apache.logging.log4j.LogManager; |
| 23 | +import org.apache.logging.log4j.Logger; |
| 24 | +import org.elasticsearch.cluster.coordination.DeterministicTaskQueue; |
| 25 | +import org.elasticsearch.cluster.metadata.MetaData; |
| 26 | +import org.elasticsearch.cluster.metadata.RepositoryMetaData; |
| 27 | +import org.elasticsearch.common.blobstore.BlobContainer; |
| 28 | +import org.elasticsearch.common.blobstore.BlobMetaData; |
| 29 | +import org.elasticsearch.common.blobstore.BlobPath; |
| 30 | +import org.elasticsearch.common.blobstore.BlobStore; |
| 31 | +import org.elasticsearch.common.collect.Tuple; |
| 32 | +import org.elasticsearch.common.io.PathUtils; |
| 33 | +import org.elasticsearch.common.io.Streams; |
| 34 | +import org.elasticsearch.common.settings.Settings; |
| 35 | +import org.elasticsearch.common.xcontent.NamedXContentRegistry; |
| 36 | +import org.elasticsearch.env.Environment; |
| 37 | +import org.elasticsearch.repositories.IndexId; |
| 38 | +import org.elasticsearch.repositories.fs.FsRepository; |
| 39 | +import org.elasticsearch.snapshots.SnapshotId; |
| 40 | + |
| 41 | +import java.io.ByteArrayInputStream; |
| 42 | +import java.io.ByteArrayOutputStream; |
| 43 | +import java.io.FileNotFoundException; |
| 44 | +import java.io.IOException; |
| 45 | +import java.io.InputStream; |
| 46 | +import java.nio.file.FileAlreadyExistsException; |
| 47 | +import java.nio.file.Files; |
| 48 | +import java.nio.file.NoSuchFileException; |
| 49 | +import java.nio.file.Path; |
| 50 | +import java.util.HashMap; |
| 51 | +import java.util.HashSet; |
| 52 | +import java.util.List; |
| 53 | +import java.util.Map; |
| 54 | +import java.util.Set; |
| 55 | + |
| 56 | +/** |
| 57 | + * Mock Repository that simulates the mechanics of an eventually consistent blobstore. |
| 58 | + */ |
| 59 | +public class MockEventuallyConsistentRepository extends FsRepository { |
| 60 | + private static final Logger logger = LogManager.getLogger(MockEventuallyConsistentRepository.class); |
| 61 | + |
| 62 | + private final DeterministicTaskQueue deterministicTaskQueue; |
| 63 | + |
| 64 | + private final Context context; |
| 65 | + |
| 66 | + public MockEventuallyConsistentRepository(RepositoryMetaData metadata, Environment environment, |
| 67 | + NamedXContentRegistry namedXContentRegistry, DeterministicTaskQueue deterministicTaskQueue, Context context) { |
| 68 | + super(overrideSettings(metadata, environment), environment, namedXContentRegistry); |
| 69 | + this.deterministicTaskQueue = deterministicTaskQueue; |
| 70 | + this.context = context; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void initializeSnapshot(SnapshotId snapshotId, List<IndexId> indices, MetaData clusterMetadata) { |
| 75 | + super.initializeSnapshot(snapshotId, indices, clusterMetadata); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + protected void assertSnapshotOrGenericThread() { |
| 80 | + // eliminate thread name check as we create repo in the test thread |
| 81 | + } |
| 82 | + |
| 83 | + private static RepositoryMetaData overrideSettings(RepositoryMetaData metadata, Environment environment) { |
| 84 | + // TODO: use another method of testing not being able to read the test file written by the master... |
| 85 | + // this is super duper hacky |
| 86 | + if (metadata.settings().getAsBoolean("localize_location", false)) { |
| 87 | + Path location = PathUtils.get(metadata.settings().get("location")); |
| 88 | + location = location.resolve(Integer.toString(environment.hashCode())); |
| 89 | + return new RepositoryMetaData(metadata.name(), metadata.type(), |
| 90 | + Settings.builder().put(metadata.settings()).put("location", location.toAbsolutePath()).build()); |
| 91 | + } else { |
| 92 | + return metadata; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + protected void doStop() { |
| 98 | + super.doStop(); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + protected BlobStore createBlobStore() throws Exception { |
| 103 | + return new MockBlobStore(super.createBlobStore()); |
| 104 | + } |
| 105 | + |
| 106 | + public static final class Context { |
| 107 | + |
| 108 | + private final Map<BlobPath, Tuple<Set<String>, Map<String, Runnable>>> state = new HashMap<>(); |
| 109 | + |
| 110 | + public Tuple<Set<String>, Map<String, Runnable>> getState(BlobPath path) { |
| 111 | + return state.computeIfAbsent(path, p -> new Tuple<>(new HashSet<>(), new HashMap<>())); |
| 112 | + } |
| 113 | + |
| 114 | + } |
| 115 | + |
| 116 | + private class MockBlobStore extends BlobStoreWrapper { |
| 117 | + |
| 118 | + MockBlobStore(BlobStore delegate) { |
| 119 | + super(delegate); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public BlobContainer blobContainer(BlobPath path) { |
| 124 | + return new MockBlobContainer(super.blobContainer(path), context.getState(path)); |
| 125 | + } |
| 126 | + |
| 127 | + private class MockBlobContainer extends BlobContainerWrapper { |
| 128 | + |
| 129 | + private final Set<String> cachedMisses; |
| 130 | + |
| 131 | + private final Map<String, Runnable> pendingWrites; |
| 132 | + |
| 133 | + MockBlobContainer(BlobContainer delegate, Tuple<Set<String>, Map<String, Runnable>> state) { |
| 134 | + super(delegate); |
| 135 | + cachedMisses = state.v1(); |
| 136 | + pendingWrites = state.v2(); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public boolean blobExists(String blobName) { |
| 141 | + ensureReadAfterWrite(blobName); |
| 142 | + final boolean result = super.blobExists(blobName); |
| 143 | + if (result == false) { |
| 144 | + cachedMisses.add(blobName); |
| 145 | + } |
| 146 | + return result; |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public InputStream readBlob(String name) throws IOException { |
| 151 | + ensureReadAfterWrite(name); |
| 152 | + return super.readBlob(name); |
| 153 | + } |
| 154 | + |
| 155 | + private void ensureReadAfterWrite(String blobName) { |
| 156 | + if (cachedMisses.contains(blobName) == false && pendingWrites.containsKey(blobName)) { |
| 157 | + pendingWrites.remove(blobName).run(); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public void deleteBlob(String blobName) throws IOException { |
| 163 | + super.deleteBlob(blobName); |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public void deleteBlobIgnoringIfNotExists(String blobName) throws IOException { |
| 168 | + super.deleteBlobIgnoringIfNotExists(blobName); |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public Map<String, BlobMetaData> listBlobs() throws IOException { |
| 173 | + return super.listBlobs(); |
| 174 | + } |
| 175 | + |
| 176 | + @Override |
| 177 | + public Map<String, BlobMetaData> listBlobsByPrefix(String blobNamePrefix) throws IOException { |
| 178 | + return super.listBlobsByPrefix(blobNamePrefix); |
| 179 | + } |
| 180 | + |
| 181 | + @Override |
| 182 | + public void writeBlob(String blobName, InputStream inputStream, long blobSize, boolean failIfAlreadyExists) |
| 183 | + throws IOException { |
| 184 | + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 185 | + Streams.copy(inputStream, baos); |
| 186 | + pendingWrites.put(blobName, () -> { |
| 187 | + try { |
| 188 | + super.writeBlob(blobName, new ByteArrayInputStream(baos.toByteArray()), blobSize, failIfAlreadyExists); |
| 189 | + if (cachedMisses.contains(blobName)) { |
| 190 | + deterministicTaskQueue.scheduleNow(() -> cachedMisses.remove(blobName)); |
| 191 | + } |
| 192 | + } catch (NoSuchFileException | FileAlreadyExistsException e) { |
| 193 | + // Ignoring, assuming a previous concurrent delete removed the parent path and that overwrites are not |
| 194 | + // detectable with this kind of store |
| 195 | + } catch (IOException e) { |
| 196 | + throw new AssertionError(e); |
| 197 | + } |
| 198 | + }); |
| 199 | + deterministicTaskQueue.scheduleNow(() -> { |
| 200 | + if (pendingWrites.containsKey(blobName)) { |
| 201 | + pendingWrites.remove(blobName).run(); |
| 202 | + } |
| 203 | + }); |
| 204 | + } |
| 205 | + |
| 206 | + @Override |
| 207 | + public void writeBlobAtomic(final String blobName, final InputStream inputStream, final long blobSize, |
| 208 | + final boolean failIfAlreadyExists) throws IOException { |
| 209 | + writeBlob(blobName, inputStream, blobSize, failIfAlreadyExists); |
| 210 | + } |
| 211 | + } |
| 212 | + } |
| 213 | +} |
0 commit comments