Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1110 from Graylog2/fix-cluster-event-bus
Fix cluster event bus
- Loading branch information
Showing
16 changed files
with
876 additions
and
236 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
graylog2-server/src/main/java/org/graylog2/events/ClusterEventCleanupPeriodical.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/** | ||
* 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 <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.graylog2.events; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.common.primitives.Ints; | ||
import com.mongodb.WriteConcern; | ||
import org.graylog2.bindings.providers.MongoJackObjectMapperProvider; | ||
import org.graylog2.database.MongoConnection; | ||
import org.graylog2.plugin.periodical.Periodical; | ||
import org.joda.time.DateTime; | ||
import org.joda.time.DateTimeZone; | ||
import org.mongojack.DBQuery; | ||
import org.mongojack.JacksonDBCollection; | ||
import org.mongojack.WriteResult; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.inject.Inject; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
public class ClusterEventCleanupPeriodical extends Periodical { | ||
private static final Logger LOG = LoggerFactory.getLogger(ClusterEventCleanupPeriodical.class); | ||
private static final String COLLECTION_NAME = ClusterEventPeriodical.COLLECTION_NAME; | ||
|
||
@VisibleForTesting | ||
static final long DEFAULT_MAX_EVENT_AGE = TimeUnit.HOURS.toMillis(1L); | ||
|
||
private final JacksonDBCollection<ClusterEvent, String> dbCollection; | ||
private final long maxEventAge; | ||
|
||
@Inject | ||
public ClusterEventCleanupPeriodical(final MongoJackObjectMapperProvider mapperProvider, | ||
final MongoConnection mongoConnection) { | ||
this(JacksonDBCollection.wrap(mongoConnection.getDatabase().getCollection(COLLECTION_NAME), | ||
ClusterEvent.class, String.class, mapperProvider.get()), DEFAULT_MAX_EVENT_AGE); | ||
} | ||
|
||
ClusterEventCleanupPeriodical(final JacksonDBCollection<ClusterEvent, String> dbCollection, final long maxEventAge) { | ||
this.dbCollection = checkNotNull(dbCollection); | ||
this.maxEventAge = maxEventAge; | ||
} | ||
|
||
@Override | ||
public boolean runsForever() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean stopOnGracefulShutdown() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean masterOnly() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean startOnThisNode() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isDaemon() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public int getInitialDelaySeconds() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public int getPeriodSeconds() { | ||
return Ints.saturatedCast(TimeUnit.MINUTES.toSeconds(5L)); | ||
} | ||
|
||
@Override | ||
protected Logger getLogger() { | ||
return LOG; | ||
} | ||
|
||
@Override | ||
public void doRun() { | ||
try { | ||
LOG.debug("Removing stale events from MongoDB collection \"{}\"", COLLECTION_NAME); | ||
|
||
final long timestamp = DateTime.now(DateTimeZone.UTC).getMillis() - maxEventAge; | ||
final DBQuery.Query query = DBQuery.lessThan("timestamp", timestamp); | ||
final WriteResult<ClusterEvent, String> writeResult = dbCollection.remove(query, WriteConcern.MAJORITY); | ||
|
||
LOG.debug("Removed {} stale events from \"{}\"", writeResult.getN(), COLLECTION_NAME); | ||
} catch (Exception e) { | ||
LOG.warn("Error while removing stale cluster events from MongoDB", e); | ||
} | ||
} | ||
} |
Oops, something went wrong.