|
| 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 | +package org.elasticsearch.snapshots.mockstore; |
| 20 | + |
| 21 | +import org.elasticsearch.cluster.metadata.RepositoryMetaData; |
| 22 | +import org.elasticsearch.common.blobstore.BlobContainer; |
| 23 | +import org.elasticsearch.common.settings.Settings; |
| 24 | +import org.elasticsearch.env.Environment; |
| 25 | +import org.elasticsearch.env.TestEnvironment; |
| 26 | +import org.elasticsearch.repositories.blobstore.BlobStoreRepository; |
| 27 | +import org.elasticsearch.test.ESTestCase; |
| 28 | +import org.elasticsearch.threadpool.ThreadPool; |
| 29 | + |
| 30 | +import java.io.ByteArrayInputStream; |
| 31 | +import java.io.IOException; |
| 32 | +import java.io.InputStream; |
| 33 | +import java.nio.file.NoSuchFileException; |
| 34 | +import java.nio.file.Path; |
| 35 | +import java.util.Arrays; |
| 36 | + |
| 37 | +import static org.elasticsearch.env.Environment.PATH_HOME_SETTING; |
| 38 | +import static org.elasticsearch.node.Node.NODE_NAME_SETTING; |
| 39 | +import static org.hamcrest.Matchers.equalTo; |
| 40 | +import static org.mockito.Mockito.mock; |
| 41 | + |
| 42 | +public class MockEventuallyConsistentRepositoryTests extends ESTestCase { |
| 43 | + |
| 44 | + private Environment environment; |
| 45 | + |
| 46 | + @Override |
| 47 | + public void setUp() throws Exception { |
| 48 | + super.setUp(); |
| 49 | + final Path tempDir = createTempDir(); |
| 50 | + final String nodeName = "testNode"; |
| 51 | + environment = TestEnvironment.newEnvironment(Settings.builder() |
| 52 | + .put(NODE_NAME_SETTING.getKey(), nodeName) |
| 53 | + .put(PATH_HOME_SETTING.getKey(), tempDir.resolve(nodeName).toAbsolutePath()) |
| 54 | + .put(Environment.PATH_REPO_SETTING.getKey(), tempDir.resolve("repo").toAbsolutePath()) |
| 55 | + .build()); |
| 56 | + } |
| 57 | + |
| 58 | + public void testReadAfterWriteConsistently() throws IOException { |
| 59 | + MockEventuallyConsistentRepository.Context blobStoreContext = new MockEventuallyConsistentRepository.Context(); |
| 60 | + try (BlobStoreRepository repository = new MockEventuallyConsistentRepository( |
| 61 | + new RepositoryMetaData("testRepo", "mockEventuallyConsistent", Settings.EMPTY), environment, |
| 62 | + xContentRegistry(), mock(ThreadPool.class), blobStoreContext)) { |
| 63 | + repository.start(); |
| 64 | + final BlobContainer blobContainer = repository.blobStore().blobContainer(repository.basePath()); |
| 65 | + final String blobName = randomAlphaOfLength(10); |
| 66 | + final int lengthWritten = randomIntBetween(1, 100); |
| 67 | + final byte[] blobData = randomByteArrayOfLength(lengthWritten); |
| 68 | + blobContainer.writeBlob(blobName, new ByteArrayInputStream(blobData), lengthWritten, true); |
| 69 | + try (InputStream in = blobContainer.readBlob(blobName)) { |
| 70 | + final byte[] readBytes = new byte[lengthWritten + 1]; |
| 71 | + final int lengthSeen = in.read(readBytes); |
| 72 | + assertThat(lengthSeen, equalTo(lengthWritten)); |
| 73 | + assertArrayEquals(blobData, Arrays.copyOf(readBytes, lengthWritten)); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public void testReadAfterWriteAfterReadThrows() throws IOException { |
| 79 | + MockEventuallyConsistentRepository.Context blobStoreContext = new MockEventuallyConsistentRepository.Context(); |
| 80 | + try (BlobStoreRepository repository = new MockEventuallyConsistentRepository( |
| 81 | + new RepositoryMetaData("testRepo", "mockEventuallyConsistent", Settings.EMPTY), environment, |
| 82 | + xContentRegistry(), mock(ThreadPool.class), blobStoreContext)) { |
| 83 | + repository.start(); |
| 84 | + final BlobContainer blobContainer = repository.blobStore().blobContainer(repository.basePath()); |
| 85 | + final String blobName = randomAlphaOfLength(10); |
| 86 | + final int lengthWritten = randomIntBetween(1, 100); |
| 87 | + final byte[] blobData = randomByteArrayOfLength(lengthWritten); |
| 88 | + assertMissing(blobContainer, blobName); |
| 89 | + blobContainer.writeBlob(blobName, new ByteArrayInputStream(blobData), lengthWritten, true); |
| 90 | + assertThrowsOnInconsistentRead(blobContainer, blobName); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + public void testReadAfterDeleteAfterWriteThrows() throws IOException { |
| 95 | + MockEventuallyConsistentRepository.Context blobStoreContext = new MockEventuallyConsistentRepository.Context(); |
| 96 | + try (BlobStoreRepository repository = new MockEventuallyConsistentRepository( |
| 97 | + new RepositoryMetaData("testRepo", "mockEventuallyConsistent", Settings.EMPTY), environment, |
| 98 | + xContentRegistry(), mock(ThreadPool.class), blobStoreContext)) { |
| 99 | + repository.start(); |
| 100 | + final BlobContainer blobContainer = repository.blobStore().blobContainer(repository.basePath()); |
| 101 | + final String blobName = randomAlphaOfLength(10); |
| 102 | + final int lengthWritten = randomIntBetween(1, 100); |
| 103 | + final byte[] blobData = randomByteArrayOfLength(lengthWritten); |
| 104 | + blobContainer.writeBlob(blobName, new ByteArrayInputStream(blobData), lengthWritten, true); |
| 105 | + blobContainer.deleteBlob(blobName); |
| 106 | + assertThrowsOnInconsistentRead(blobContainer, blobName); |
| 107 | + blobStoreContext.forceConsistent(); |
| 108 | + assertMissing(blobContainer, blobName); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + private static void assertThrowsOnInconsistentRead(BlobContainer blobContainer, String blobName) throws IOException { |
| 113 | + try (InputStream in = blobContainer.readBlob(blobName)) { |
| 114 | + fail("Inconsistent read should throw"); |
| 115 | + } catch (AssertionError assertionError) { |
| 116 | + assertThat(assertionError.getMessage(), equalTo("Inconsistent read on [" + blobName + ']')); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private static void assertMissing(BlobContainer container, String blobName) throws IOException { |
| 121 | + try (InputStream in = container.readBlob(blobName)) { |
| 122 | + fail("Reading a non-existent blob should throw"); |
| 123 | + } catch (NoSuchFileException expected) { |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments