Search in sources :

Example 6 with Writer

use of org.elasticsearch.gateway.PersistedClusterStateService.Writer in project crate by crate.

the class PersistedClusterStateServiceTests method testPersistsAndReloadsIndexMetadataForMultipleIndices.

public void testPersistsAndReloadsIndexMetadataForMultipleIndices() throws IOException {
    try (NodeEnvironment nodeEnvironment = newNodeEnvironment(createDataPaths())) {
        final PersistedClusterStateService persistedClusterStateService = newPersistedClusterStateService(nodeEnvironment);
        final long term = randomLongBetween(1L, Long.MAX_VALUE);
        final String addedIndexUuid = UUIDs.randomBase64UUID(random());
        final String updatedIndexUuid = UUIDs.randomBase64UUID(random());
        final String deletedIndexUuid = UUIDs.randomBase64UUID(random());
        try (Writer writer = persistedClusterStateService.createWriter()) {
            final ClusterState clusterState = loadPersistedClusterState(persistedClusterStateService);
            writeState(writer, 0L, ClusterState.builder(clusterState).metadata(Metadata.builder(clusterState.metadata()).version(clusterState.metadata().version() + 1).coordinationMetadata(CoordinationMetadata.builder(clusterState.coordinationMetadata()).term(term).build()).put(IndexMetadata.builder("updated").version(// -1 because it's incremented in .put()
            randomLongBetween(0L, Long.MAX_VALUE - 1) - 1).settings(Settings.builder().put(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 1).put(IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), 1).put(IndexMetadata.SETTING_INDEX_VERSION_CREATED.getKey(), Version.CURRENT).put(IndexMetadata.SETTING_INDEX_UUID, updatedIndexUuid))).put(IndexMetadata.builder("deleted").version(// -1 because it's incremented in .put()
            randomLongBetween(0L, Long.MAX_VALUE - 1) - 1).settings(Settings.builder().put(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 1).put(IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), 1).put(IndexMetadata.SETTING_INDEX_VERSION_CREATED.getKey(), Version.CURRENT).put(IndexMetadata.SETTING_INDEX_UUID, deletedIndexUuid)))).incrementVersion().build(), clusterState);
        }
        try (Writer writer = persistedClusterStateService.createWriter()) {
            final ClusterState clusterState = loadPersistedClusterState(persistedClusterStateService);
            assertThat(clusterState.metadata().indices().size(), equalTo(2));
            assertThat(clusterState.metadata().index("updated").getIndexUUID(), equalTo(updatedIndexUuid));
            assertThat(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.get(clusterState.metadata().index("updated").getSettings()), equalTo(1));
            assertThat(clusterState.metadata().index("deleted").getIndexUUID(), equalTo(deletedIndexUuid));
            writeState(writer, 0L, ClusterState.builder(clusterState).metadata(Metadata.builder(clusterState.metadata()).version(clusterState.metadata().version() + 1).remove("deleted").put(IndexMetadata.builder("updated").settings(Settings.builder().put(clusterState.metadata().index("updated").getSettings()).put(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 2))).put(IndexMetadata.builder("added").version(// -1 because it's incremented in .put()
            randomLongBetween(0L, Long.MAX_VALUE - 1) - 1).settings(Settings.builder().put(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 1).put(IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), 1).put(IndexMetadata.SETTING_INDEX_VERSION_CREATED.getKey(), Version.CURRENT).put(IndexMetadata.SETTING_INDEX_UUID, addedIndexUuid)))).incrementVersion().build(), clusterState);
        }
        final ClusterState clusterState = loadPersistedClusterState(persistedClusterStateService);
        assertThat(clusterState.metadata().indices().size(), equalTo(2));
        assertThat(clusterState.metadata().index("updated").getIndexUUID(), equalTo(updatedIndexUuid));
        assertThat(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.get(clusterState.metadata().index("updated").getSettings()), equalTo(2));
        assertThat(clusterState.metadata().index("added").getIndexUUID(), equalTo(addedIndexUuid));
        assertThat(clusterState.metadata().index("deleted"), nullValue());
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) NodeEnvironment(org.elasticsearch.env.NodeEnvironment) Matchers.containsString(org.hamcrest.Matchers.containsString) Writer(org.elasticsearch.gateway.PersistedClusterStateService.Writer) IndexWriter(org.apache.lucene.index.IndexWriter)

Example 7 with Writer

use of org.elasticsearch.gateway.PersistedClusterStateService.Writer in project crate by crate.

the class PersistedClusterStateServiceTests method testPersistsAndReloadsGlobalMetadata.

public void testPersistsAndReloadsGlobalMetadata() throws IOException {
    try (NodeEnvironment nodeEnvironment = newNodeEnvironment(createDataPaths())) {
        final PersistedClusterStateService persistedClusterStateService = newPersistedClusterStateService(nodeEnvironment);
        final String clusterUUID = UUIDs.randomBase64UUID(random());
        final long version = randomLongBetween(1L, Long.MAX_VALUE);
        ClusterState clusterState = loadPersistedClusterState(persistedClusterStateService);
        try (Writer writer = persistedClusterStateService.createWriter()) {
            writer.writeFullStateAndCommit(0L, ClusterState.builder(clusterState).metadata(Metadata.builder(clusterState.metadata()).clusterUUID(clusterUUID).clusterUUIDCommitted(true).version(version)).incrementVersion().build());
            clusterState = loadPersistedClusterState(persistedClusterStateService);
            assertThat(clusterState.metadata().clusterUUID(), equalTo(clusterUUID));
            assertTrue(clusterState.metadata().clusterUUIDCommitted());
            assertThat(clusterState.metadata().version(), equalTo(version));
        }
        try (Writer writer = persistedClusterStateService.createWriter()) {
            writer.writeFullStateAndCommit(0L, ClusterState.builder(clusterState).metadata(Metadata.builder(clusterState.metadata()).clusterUUID(clusterUUID).clusterUUIDCommitted(true).version(version + 1)).incrementVersion().build());
        }
        clusterState = loadPersistedClusterState(persistedClusterStateService);
        assertThat(clusterState.metadata().clusterUUID(), equalTo(clusterUUID));
        assertTrue(clusterState.metadata().clusterUUIDCommitted());
        assertThat(clusterState.metadata().version(), equalTo(version + 1));
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) NodeEnvironment(org.elasticsearch.env.NodeEnvironment) Matchers.containsString(org.hamcrest.Matchers.containsString) Writer(org.elasticsearch.gateway.PersistedClusterStateService.Writer) IndexWriter(org.apache.lucene.index.IndexWriter)

Example 8 with Writer

use of org.elasticsearch.gateway.PersistedClusterStateService.Writer in project crate by crate.

the class PersistedClusterStateServiceTests method testFailsIfGlobalMetadataIsMissing.

public void testFailsIfGlobalMetadataIsMissing() throws IOException {
    try (NodeEnvironment nodeEnvironment = newNodeEnvironment(createDataPaths())) {
        try (Writer writer = newPersistedClusterStateService(nodeEnvironment).createWriter()) {
            final ClusterState clusterState = loadPersistedClusterState(newPersistedClusterStateService(nodeEnvironment));
            writeState(writer, 0L, ClusterState.builder(clusterState).version(randomLongBetween(1L, Long.MAX_VALUE)).build(), clusterState);
        }
        final Path brokenPath = randomFrom(nodeEnvironment.nodeDataPaths());
        try (Directory directory = new SimpleFSDirectory(brokenPath.resolve(PersistedClusterStateService.METADATA_DIRECTORY_NAME))) {
            final IndexWriterConfig indexWriterConfig = new IndexWriterConfig();
            indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
            try (IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig)) {
                indexWriter.commit();
            }
        }
        final String message = expectThrows(IllegalStateException.class, () -> newPersistedClusterStateService(nodeEnvironment).loadBestOnDiskState()).getMessage();
        assertThat(message, allOf(containsString("no global metadata found"), containsString(brokenPath.toString())));
    }
}
Also used : Path(java.nio.file.Path) ClusterState(org.elasticsearch.cluster.ClusterState) NodeEnvironment(org.elasticsearch.env.NodeEnvironment) IndexWriter(org.apache.lucene.index.IndexWriter) Matchers.containsString(org.hamcrest.Matchers.containsString) SimpleFSDirectory(org.apache.lucene.store.SimpleFSDirectory) Writer(org.elasticsearch.gateway.PersistedClusterStateService.Writer) IndexWriter(org.apache.lucene.index.IndexWriter) Directory(org.apache.lucene.store.Directory) SimpleFSDirectory(org.apache.lucene.store.SimpleFSDirectory) FilterDirectory(org.apache.lucene.store.FilterDirectory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 9 with Writer

use of org.elasticsearch.gateway.PersistedClusterStateService.Writer in project crate by crate.

the class PersistedClusterStateServiceTests method testClosesWriterOnFatalError.

public void testClosesWriterOnFatalError() throws IOException {
    final AtomicBoolean throwException = new AtomicBoolean();
    try (NodeEnvironment nodeEnvironment = newNodeEnvironment(createDataPaths())) {
        final PersistedClusterStateService persistedClusterStateService = new PersistedClusterStateService(nodeEnvironment, xContentRegistry(), BigArrays.NON_RECYCLING_INSTANCE, new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), () -> 0L) {

            @Override
            Directory createDirectory(Path path) throws IOException {
                return new FilterDirectory(super.createDirectory(path)) {

                    @Override
                    public void sync(Collection<String> names) {
                        throw new OutOfMemoryError("simulated");
                    }
                };
            }
        };
        try (Writer writer = persistedClusterStateService.createWriter()) {
            final ClusterState clusterState = loadPersistedClusterState(persistedClusterStateService);
            final long newTerm = randomNonNegativeLong();
            final ClusterState newState = ClusterState.builder(clusterState).metadata(Metadata.builder(clusterState.metadata()).clusterUUID(UUIDs.randomBase64UUID(random())).clusterUUIDCommitted(true).version(randomLongBetween(1L, Long.MAX_VALUE))).incrementVersion().build();
            throwException.set(true);
            assertThat(expectThrows(OutOfMemoryError.class, () -> {
                if (randomBoolean()) {
                    writeState(writer, newTerm, newState, clusterState);
                } else {
                    writer.commit(newTerm, newState.version());
                }
            }).getMessage(), containsString("simulated"));
            assertFalse(writer.isOpen());
        }
        // check if we can open writer again
        try (Writer ignored = persistedClusterStateService.createWriter()) {
        }
    }
}
Also used : Path(java.nio.file.Path) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClusterState(org.elasticsearch.cluster.ClusterState) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) NodeEnvironment(org.elasticsearch.env.NodeEnvironment) FilterDirectory(org.apache.lucene.store.FilterDirectory) Collection(java.util.Collection) Writer(org.elasticsearch.gateway.PersistedClusterStateService.Writer) IndexWriter(org.apache.lucene.index.IndexWriter)

Example 10 with Writer

use of org.elasticsearch.gateway.PersistedClusterStateService.Writer in project crate by crate.

the class PersistedClusterStateServiceTests method testFailsIfGlobalMetadataIsDuplicated.

public void testFailsIfGlobalMetadataIsDuplicated() throws IOException {
    // if someone attempted surgery on the metadata index by hand, e.g. deleting broken segments, then maybe the global metadata
    // is duplicated
    final Path[] dataPaths1 = createDataPaths();
    final Path[] dataPaths2 = createDataPaths();
    final Path[] combinedPaths = Stream.concat(Arrays.stream(dataPaths1), Arrays.stream(dataPaths2)).toArray(Path[]::new);
    try (NodeEnvironment nodeEnvironment = newNodeEnvironment(combinedPaths)) {
        try (Writer writer = newPersistedClusterStateService(nodeEnvironment).createWriter()) {
            final ClusterState clusterState = loadPersistedClusterState(newPersistedClusterStateService(nodeEnvironment));
            writeState(writer, 0L, ClusterState.builder(clusterState).version(randomLongBetween(1L, Long.MAX_VALUE)).build(), clusterState);
        }
        final Path brokenPath = randomFrom(nodeEnvironment.nodeDataPaths());
        final Path dupPath = randomValueOtherThan(brokenPath, () -> randomFrom(nodeEnvironment.nodeDataPaths()));
        try (Directory directory = new SimpleFSDirectory(brokenPath.resolve(PersistedClusterStateService.METADATA_DIRECTORY_NAME));
            Directory dupDirectory = new SimpleFSDirectory(dupPath.resolve(PersistedClusterStateService.METADATA_DIRECTORY_NAME))) {
            try (IndexWriter indexWriter = new IndexWriter(directory, new IndexWriterConfig())) {
                indexWriter.addIndexes(dupDirectory);
                indexWriter.commit();
            }
        }
        final String message = expectThrows(IllegalStateException.class, () -> newPersistedClusterStateService(nodeEnvironment).loadBestOnDiskState()).getMessage();
        assertThat(message, allOf(containsString("duplicate global metadata found"), containsString(brokenPath.toString())));
    }
}
Also used : Path(java.nio.file.Path) ClusterState(org.elasticsearch.cluster.ClusterState) NodeEnvironment(org.elasticsearch.env.NodeEnvironment) IndexWriter(org.apache.lucene.index.IndexWriter) Matchers.containsString(org.hamcrest.Matchers.containsString) SimpleFSDirectory(org.apache.lucene.store.SimpleFSDirectory) Writer(org.elasticsearch.gateway.PersistedClusterStateService.Writer) IndexWriter(org.apache.lucene.index.IndexWriter) Directory(org.apache.lucene.store.Directory) SimpleFSDirectory(org.apache.lucene.store.SimpleFSDirectory) FilterDirectory(org.apache.lucene.store.FilterDirectory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Aggregations

IndexWriter (org.apache.lucene.index.IndexWriter)16 NodeEnvironment (org.elasticsearch.env.NodeEnvironment)16 Writer (org.elasticsearch.gateway.PersistedClusterStateService.Writer)16 ClusterState (org.elasticsearch.cluster.ClusterState)15 Matchers.containsString (org.hamcrest.Matchers.containsString)11 Path (java.nio.file.Path)10 FilterDirectory (org.apache.lucene.store.FilterDirectory)9 ClusterSettings (org.elasticsearch.common.settings.ClusterSettings)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)6 Directory (org.apache.lucene.store.Directory)6 SimpleFSDirectory (org.apache.lucene.store.SimpleFSDirectory)6 IOException (java.io.IOException)5 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)5 ArrayList (java.util.ArrayList)4 Collection (java.util.Collection)4 AtomicLong (java.util.concurrent.atomic.AtomicLong)4 Term (org.apache.lucene.index.Term)4 IOContext (org.apache.lucene.store.IOContext)4 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)4