diff --git a/server/src/main/java/org/elasticsearch/repositories/blobstore/BlobStoreRepository.java b/server/src/main/java/org/elasticsearch/repositories/blobstore/BlobStoreRepository.java index c8cdf0d4e0308..cde7a18d8c11c 100644 --- a/server/src/main/java/org/elasticsearch/repositories/blobstore/BlobStoreRepository.java +++ b/server/src/main/java/org/elasticsearch/repositories/blobstore/BlobStoreRepository.java @@ -211,9 +211,9 @@ public abstract class BlobStoreRepository extends AbstractLifecycleComponent imp private final boolean readOnly; - private final ChecksumBlobStoreFormat indexShardSnapshotFormat; + private ChecksumBlobStoreFormat indexShardSnapshotFormat; - private final ChecksumBlobStoreFormat indexShardSnapshotsFormat; + private ChecksumBlobStoreFormat indexShardSnapshotsFormat; private final Object lock = new Object(); @@ -234,15 +234,6 @@ protected BlobStoreRepository(RepositoryMetaData metadata, Settings settings, Na snapshotRateLimiter = getRateLimiter(metadata.settings(), "max_snapshot_bytes_per_sec", new ByteSizeValue(40, ByteSizeUnit.MB)); restoreRateLimiter = getRateLimiter(metadata.settings(), "max_restore_bytes_per_sec", new ByteSizeValue(40, ByteSizeUnit.MB)); readOnly = metadata.settings().getAsBoolean("readonly", false); - - indexShardSnapshotFormat = new ChecksumBlobStoreFormat<>(SNAPSHOT_CODEC, SNAPSHOT_NAME_FORMAT, - BlobStoreIndexShardSnapshot::fromXContent, namedXContentRegistry, isCompress()); - indexShardSnapshotsFormat = new ChecksumBlobStoreFormat<>(SNAPSHOT_INDEX_CODEC, SNAPSHOT_INDEX_NAME_FORMAT, - BlobStoreIndexShardSnapshots::fromXContent, namedXContentRegistry, isCompress()); - ByteSizeValue chunkSize = chunkSize(); - if (chunkSize != null && chunkSize.getBytes() <= 0) { - throw new IllegalArgumentException("the chunk size cannot be negative: [" + chunkSize + "]"); - } } @Override @@ -253,6 +244,15 @@ protected void doStart() { IndexMetaData::fromXContent, namedXContentRegistry, isCompress()); snapshotFormat = new ChecksumBlobStoreFormat<>(SNAPSHOT_CODEC, SNAPSHOT_NAME_FORMAT, SnapshotInfo::fromXContentInternal, namedXContentRegistry, isCompress()); + + indexShardSnapshotFormat = new ChecksumBlobStoreFormat<>(SNAPSHOT_CODEC, SNAPSHOT_NAME_FORMAT, + BlobStoreIndexShardSnapshot::fromXContent, namedXContentRegistry, isCompress()); + indexShardSnapshotsFormat = new ChecksumBlobStoreFormat<>(SNAPSHOT_INDEX_CODEC, SNAPSHOT_INDEX_NAME_FORMAT, + BlobStoreIndexShardSnapshots::fromXContent, namedXContentRegistry, isCompress()); + ByteSizeValue chunkSize = chunkSize(); + if (chunkSize != null && chunkSize.getBytes() <= 0) { + throw new IllegalArgumentException("the chunk size cannot be negative: [" + chunkSize + "]"); + } } @Override diff --git a/server/src/test/java/org/elasticsearch/repositories/blobstore/BlobStoreRepositoryTests.java b/server/src/test/java/org/elasticsearch/repositories/blobstore/BlobStoreRepositoryTests.java index 871e5071ec7b7..5f7a4afc78815 100644 --- a/server/src/test/java/org/elasticsearch/repositories/blobstore/BlobStoreRepositoryTests.java +++ b/server/src/test/java/org/elasticsearch/repositories/blobstore/BlobStoreRepositoryTests.java @@ -22,8 +22,13 @@ import org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse; import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.client.Client; +import org.elasticsearch.cluster.metadata.RepositoryMetaData; import org.elasticsearch.common.UUIDs; +import org.elasticsearch.common.blobstore.BlobPath; +import org.elasticsearch.common.blobstore.BlobStore; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.unit.ByteSizeUnit; +import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.common.xcontent.NamedXContentRegistry; import org.elasticsearch.env.Environment; import org.elasticsearch.plugins.Plugin; @@ -232,6 +237,49 @@ public void testIncompatibleSnapshotsBlobExists() throws Exception { assertEquals(0, repository.getRepositoryData().getIncompatibleSnapshotIds().size()); } + + public void testBlobStoreRepositoryConstructor() { + // simply check that compress and chunksize are not called (since they used to be) - they are not valid inside the constructor, + // call them in doStart() instead. + new BlobStoreRepository(new RepositoryMetaData("test", "test", Settings.EMPTY), Settings.EMPTY, null) { + @Override + protected boolean isCompress() { + fail("Not allowed to call isCompress in constructor"); + return false; + } + + @Override + protected ByteSizeValue chunkSize() { + fail("Not allowed to call chunkSize in constructor"); + return null; + } + + @Override + protected BlobStore createBlobStore() throws Exception { + throw new UnsupportedOperationException(); + } + + @Override + protected BlobPath basePath() { + throw new UnsupportedOperationException(); + } + }; + } + + public void testBadChunksize() throws Exception { + final Client client = client(); + final Path location = ESIntegTestCase.randomRepoPath(node().settings()); + final String repositoryName = "test-repo"; + + expectThrows(RepositoryException.class, () -> + client.admin().cluster().preparePutRepository(repositoryName) + .setType(REPO_TYPE) + .setSettings(Settings.builder().put(node().settings()) + .put("location", location) + .put("chunk_size", randomLongBetween(-10, 0), ByteSizeUnit.BYTES)) + .get()); + } + private BlobStoreRepository setupRepo() { final Client client = client(); final Path location = ESIntegTestCase.randomRepoPath(node().settings());