From b7a2986d2566e362d2a04da7de66abb6e32d436a Mon Sep 17 00:00:00 2001 From: Bernd Ahlers Date: Thu, 24 Oct 2019 09:47:08 +0200 Subject: [PATCH] Ensure index clean up runs before index creation (#6634) (#6643) Move the code from the migration to the constructor. This avoids a race between our asynchronously executed migration and the DBProcessingStatusService. Fixes #6383 (cherry picked from commit 6fbf78fa2662b9cb9cc3ee733d1f591312c52a80) --- .../graylog2/migrations/MigrationsModule.java | 1 - ...114400_RemoveOldProcessingStatusIndex.java | 50 ------------------- .../processing/DBProcessingStatusService.java | 13 +++++ 3 files changed, 13 insertions(+), 51 deletions(-) delete mode 100644 graylog2-server/src/main/java/org/graylog2/migrations/V20190905114400_RemoveOldProcessingStatusIndex.java diff --git a/graylog2-server/src/main/java/org/graylog2/migrations/MigrationsModule.java b/graylog2-server/src/main/java/org/graylog2/migrations/MigrationsModule.java index cc4014e8c24c..059c28705617 100644 --- a/graylog2-server/src/main/java/org/graylog2/migrations/MigrationsModule.java +++ b/graylog2-server/src/main/java/org/graylog2/migrations/MigrationsModule.java @@ -40,6 +40,5 @@ protected void configure() { addMigration(V20190705071400_AddEventIndexSetsMigration.class); addMigration(V20190730100900_AddAlertsManagerRole.class); addMigration(V20190730000000_CreateDefaultEventsConfiguration.class); - addMigration(V20190905114400_RemoveOldProcessingStatusIndex.class); } } diff --git a/graylog2-server/src/main/java/org/graylog2/migrations/V20190905114400_RemoveOldProcessingStatusIndex.java b/graylog2-server/src/main/java/org/graylog2/migrations/V20190905114400_RemoveOldProcessingStatusIndex.java deleted file mode 100644 index b9913305f2ef..000000000000 --- a/graylog2-server/src/main/java/org/graylog2/migrations/V20190905114400_RemoveOldProcessingStatusIndex.java +++ /dev/null @@ -1,50 +0,0 @@ -/** - * This file is part of Graylog. - * - * Graylog is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Graylog is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Graylog. If not, see . - */ -package org.graylog2.migrations; - -import com.mongodb.DBCollection; -import com.mongodb.MongoException; -import org.graylog2.database.MongoConnection; -import org.graylog2.system.processing.DBProcessingStatusService; - -import javax.inject.Inject; -import java.time.ZonedDateTime; - -public class V20190905114400_RemoveOldProcessingStatusIndex extends Migration { - private final MongoConnection mongoConnection; - - @Inject - public V20190905114400_RemoveOldProcessingStatusIndex(MongoConnection mongoConnection) { - this.mongoConnection = mongoConnection; - } - @Override - public ZonedDateTime createdAt() { - return ZonedDateTime.parse("2019-09-05T11:40:00Z"); - } - - @Override - public void upgrade() { - final String OLD_INDEX_NAME = "updated_at_1_input_journal.uncommitted_entries_1_input_journal.written_messages_1m_rate_1"; - - DBCollection processing_status = mongoConnection.getDatabase().getCollection(DBProcessingStatusService.COLLECTION_NAME); - try { - processing_status.dropIndex(OLD_INDEX_NAME); - } catch (MongoException ignored) { - // index was either never created or already deleted - } - } -} diff --git a/graylog2-server/src/main/java/org/graylog2/system/processing/DBProcessingStatusService.java b/graylog2-server/src/main/java/org/graylog2/system/processing/DBProcessingStatusService.java index 76809a54583b..226263cd87ac 100644 --- a/graylog2-server/src/main/java/org/graylog2/system/processing/DBProcessingStatusService.java +++ b/graylog2-server/src/main/java/org/graylog2/system/processing/DBProcessingStatusService.java @@ -20,6 +20,7 @@ import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ImmutableList; import com.mongodb.BasicDBObject; +import com.mongodb.MongoException; import org.bson.types.ObjectId; import org.graylog.scheduler.clock.JobSchedulerClock; import org.graylog2.bindings.providers.MongoJackObjectMapperProvider; @@ -76,6 +77,18 @@ public DBProcessingStatusService(MongoConnection mongoConnection, mapper.get()); db.createIndex(new BasicDBObject(ProcessingStatusDto.FIELD_NODE_ID, 1), new BasicDBObject("unique", true)); + + // Remove the old (3.1.0) index before creating the new one. This is needed, because mongodb >= 4.2 won't allow + // the creation of identical indices with a different name. We don't use a migration, + // because it can race with the code below that creates the same index with a shorter name. + // TODO remove this in a future release (maybe at 3.5) + final String OLD_INDEX_NAME = "updated_at_1_input_journal.uncommitted_entries_1_input_journal.written_messages_1m_rate_1"; + try { + db.dropIndex(OLD_INDEX_NAME); + } catch (MongoException ignored) { + // index was either never created or already deleted + } + // Use a custom index name to avoid the automatically generated index name which will be pretty long and // might cause errors due to the 127 character index name limit. (e.g. when using a long database name) // See: https://github.com/Graylog2/graylog2-server/issues/6322