Skip to content

Commit

Permalink
GL shutdown on catchable OutOfMemoryError (#15889)
Browse files Browse the repository at this point in the history
* GL shutdown on catchable OutOfMemoryError

* Changelog added

* Changelog renamed, because of mixed-repo situation

* New entry in UPGRADING.md
  • Loading branch information
luk-kaminski authored Jul 10, 2023
1 parent f16102e commit 07a1515
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
5 changes: 5 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ Upgrading to Graylog 5.2.x
- GreyNoise Full IP Lookup [Enterprise] Data Adapter can no longer be used with a free GreyNoise Community API tokens.
- GreyNoise Quick IP Lookup Data Adapter can no longer be used with a free GreyNoise Community API tokens.

## Shutdown of Graylog on OutOfMemoryError
Because of an error in HttpCore 4.4.12, which is required by Elasticsearch and older versions of Opensearch, OutOfMemoryError errors were not properly handled.
The Reactor was stopped, which prevented proper Graylog operation and the reason (OutOfMemoryError) was not clearly visible.
From now on, Graylog will shutdown on OutOfMemoryError, trying to log some basic information about the thread and memory consumption during this event.

## Java API Changes
The following Java Code API changes have been made.

Expand Down
5 changes: 5 additions & 0 deletions changelog/unreleased/pr-15889.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type = "fixed" # One of: a(dded), c(hanged), d(eprecated), r(emoved), f(ixed), s(ecurity)
message = "Fix problem with STOPPED reactor on OutOfMemoryError. This error stops the whole GL instance now, instead of constantly logging errors related to stopped reactor."

issues = ["graylog-plugin-enterprise#4980"]
pulls = ["15889"]
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.graylog2.bindings.providers.SecureFreemarkerConfigProvider;
import org.graylog2.bindings.providers.SystemJobFactoryProvider;
import org.graylog2.bindings.providers.SystemJobManagerProvider;
import org.graylog2.bootstrap.uncaughtexeptions.DefaultUncaughtExceptionHandlerCreator;
import org.graylog2.cluster.ClusterConfigServiceImpl;
import org.graylog2.cluster.leader.FakeLeaderElectionModule;
import org.graylog2.cluster.leader.LeaderElectionModule;
Expand Down Expand Up @@ -178,6 +179,7 @@ private void bindFactoryModules() {
}

private void bindSingletons() {
bind(DefaultUncaughtExceptionHandlerCreator.class).asEagerSingleton();
bind(SystemJobManager.class).toProvider(SystemJobManagerProvider.class);
bind(DefaultSecurityManager.class).toProvider(DefaultSecurityManagerProvider.class).asEagerSingleton();
bind(SystemJobFactory.class).toProvider(SystemJobFactoryProvider.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program 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
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*/
package org.graylog2.bootstrap.uncaughtexeptions;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Singleton;

@Singleton
public class DefaultUncaughtExceptionHandlerCreator {

private static final Logger LOG = LoggerFactory.getLogger(DefaultUncaughtExceptionHandlerCreator.class);

public DefaultUncaughtExceptionHandlerCreator() {
Thread.setDefaultUncaughtExceptionHandler((t, e) -> {
if (!(e instanceof ThreadDeath)) {
defaultHandling(t, e);
if (e instanceof OutOfMemoryError) {
outOfMemoryHandling(t);
}
}
});
}

private void outOfMemoryHandling(final Thread t) {
LOG.error("OutOfMemoryError encountered in thread " + t.getName() + ", Graylog instance will be shut down.");
final Runtime runtime = Runtime.getRuntime();
LOG.info("Free JVM memory : " + runtime.freeMemory());
LOG.info("Total JVM memory : " + runtime.totalMemory());
LOG.info("Max JVM memory : " + runtime.maxMemory());

System.exit(1);
}

private void defaultHandling(final Thread t, final Throwable e) {
//see ThreadGrooup.uncaughtException -> we don't want to remove the well-known "printStackTrace" uncaught exception handling
System.err.print("Exception in thread \""
+ t.getName() + "\" ");
e.printStackTrace(System.err);
}
}

0 comments on commit 07a1515

Please sign in to comment.