Skip to content

Commit

Permalink
Merge branch '1.20.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
gaetanmaisse committed Aug 1, 2022
2 parents 7e71ad1 + bc64d77 commit f619abd
Show file tree
Hide file tree
Showing 10 changed files with 561 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@

* respect TimeToLive parameter in StandaloneCache ([dacdbda](https://github.com/gravitee-io/gravitee-node/commit/dacdbda459754a4722af4b04c4814c330afb308c))

## [1.20.5](https://github.com/gravitee-io/gravitee-node/compare/1.20.4...1.20.5) (2022-07-28)


### Bug Fixes

* Add endpoints for generating heapdump and threaddump ([6c087c7](https://github.com/gravitee-io/gravitee-node/commit/6c087c7bc8c4ca30966bdc4880e8bf53e8c2ed39)), closes [gravitee-io/issues#8222](https://github.com/gravitee-io/issues/issues/8222)

## [1.20.4](https://github.com/gravitee-io/gravitee-node/compare/1.20.3...1.20.4) (2022-06-09)


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/**
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.node.management.http.node.heap;

import io.gravitee.common.http.HttpMethod;
import io.gravitee.common.http.HttpStatusCode;
import io.gravitee.node.management.http.endpoint.ManagementEndpoint;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import io.vertx.core.Promise;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.ext.web.RoutingContext;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @author David BRASSELY (david.brassely at graviteesource.com)
* @author GraviteeSource Team
*/
public class HeapDumpEndpoint implements ManagementEndpoint {

private final Logger LOGGER = LoggerFactory.getLogger(HeapDumpEndpoint.class);

private final long timeout = TimeUnit.SECONDS.toMillis(10);

private final Lock lock = new ReentrantLock();

private HeapDumpSupplier heapDumpSupplier;

@Override
public HttpMethod method() {
return HttpMethod.GET;
}

@Override
public String path() {
return "/heapdump";
}

@Override
public void handle(RoutingContext ctx) {
final HttpServerResponse response = ctx.response();
final boolean live = Boolean.parseBoolean(ctx.request().getParam("live", Boolean.FALSE.toString()));

ctx
.vertx()
.executeBlocking(
new Handler<Promise<File>>() {
@Override
public void handle(Promise<File> promise) {
try {
if (HeapDumpEndpoint.this.lock.tryLock(HeapDumpEndpoint.this.timeout, TimeUnit.MILLISECONDS)) {
try {
File dumpFile = dump(live);
promise.complete(dumpFile);
} catch (Exception e) {
promise.fail(e);
}
}
} catch (InterruptedException ex) {
promise.fail(ex);
} finally {
HeapDumpEndpoint.this.lock.unlock();
}
}
},
new Handler<AsyncResult<File>>() {
@Override
public void handle(AsyncResult<File> fileAsyncResult) {
if (fileAsyncResult.succeeded()) {
File file = fileAsyncResult.result();
response
.setStatusCode(HttpStatusCode.OK_200)
.setChunked(true)
.sendFile(file.getAbsolutePath())
.onComplete(
new Handler<AsyncResult<Void>>() {
@Override
public void handle(AsyncResult<Void> voidAsyncResult) {
try {
Files.delete(file.toPath());
} catch (IOException ex) {
LOGGER.warn("Failed to delete temporary heap dump file '" + file.toPath() + "'", ex);
}
}
}
);
} else {
LOGGER.error("Unable to generate heap dump.", fileAsyncResult.cause());
response
.setStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR_500)
.setChunked(true)
.send(fileAsyncResult.cause().getMessage());
}
}
}
);
}

private File dump(boolean live) throws IOException, InterruptedException {
if (this.heapDumpSupplier == null) {
this.heapDumpSupplier = createHeapDumpSupplier();
}

File file = createTempFile();
this.heapDumpSupplier.dump(file, live);
return file;
}

private File createTempFile() throws IOException {
String date = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm").format(LocalDateTime.now());
File file = File.createTempFile("heap-" + date, "." + determineDumpSuffix());
file.delete();
return file;
}

protected HeapDumpSupplier createHeapDumpSupplier() throws HeapDumpException {
try {
return new HotSpotHeapDumpSupplier();
} catch (HeapDumpException ex) {
return new OpenJ9HeapDumpSupplier();
}
}

private String determineDumpSuffix() {
if (this.heapDumpSupplier instanceof OpenJ9HeapDumpSupplier) {
return "phd";
}
return "hprof";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.node.management.http.node.heap;

/**
* @author David BRASSELY (david.brassely at graviteesource.com)
* @author GraviteeSource Team
*/
public class HeapDumpException extends RuntimeException {

public HeapDumpException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.node.management.http.node.heap;

import java.io.File;
import java.io.IOException;

/**
* @author David BRASSELY (david.brassely at graviteesource.com)
* @author GraviteeSource Team
*/
@FunctionalInterface
public interface HeapDumpSupplier {
void dump(File file, boolean live) throws IOException, InterruptedException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.node.management.http.node.heap;

import java.io.File;
import java.lang.management.ManagementFactory;
import java.lang.management.PlatformManagedObject;
import java.lang.reflect.Method;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;

/**
* @author David BRASSELY (david.brassely at graviteesource.com)
* @author GraviteeSource Team
*/
public class HotSpotHeapDumpSupplier implements HeapDumpSupplier {

private Object diagnosticMXBean;

private Method dumpHeapMethod;

@SuppressWarnings("unchecked")
HotSpotHeapDumpSupplier() {
try {
Class<?> diagnosticMXBeanClass = ClassUtils.resolveClassName("com.sun.management.HotSpotDiagnosticMXBean", null);
this.diagnosticMXBean = ManagementFactory.getPlatformMXBean((Class<PlatformManagedObject>) diagnosticMXBeanClass);
this.dumpHeapMethod = ReflectionUtils.findMethod(diagnosticMXBeanClass, "dumpHeap", String.class, Boolean.TYPE);
} catch (Throwable ex) {
throw new HeapDumpException("Unable to locate HotSpotDiagnosticMXBean", ex);
}
}

@Override
public void dump(File file, boolean live) {
ReflectionUtils.invokeMethod(this.dumpHeapMethod, this.diagnosticMXBean, file.getAbsolutePath(), live);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.node.management.http.node.heap;

import java.io.File;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.management.PlatformManagedObject;
import java.lang.reflect.Method;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;

/**
* @author David BRASSELY (david.brassely at graviteesource.com)
* @author GraviteeSource Team
*/
public class OpenJ9HeapDumpSupplier implements HeapDumpSupplier {

private Object diagnosticMXBean;

private Method dumpHeapMethod;

@SuppressWarnings("unchecked")
OpenJ9HeapDumpSupplier() {
try {
Class<?> mxBeanClass = ClassUtils.resolveClassName("openj9.lang.management.OpenJ9DiagnosticsMXBean", null);
this.diagnosticMXBean = ManagementFactory.getPlatformMXBean((Class<PlatformManagedObject>) mxBeanClass);
this.dumpHeapMethod = ReflectionUtils.findMethod(mxBeanClass, "triggerDumpToFile", String.class, String.class);
} catch (Throwable ex) {
throw new HeapDumpException("Unable to locate OpenJ9DiagnosticsMXBean", ex);
}
}

@Override
public void dump(File file, boolean live) throws IOException, InterruptedException {
ReflectionUtils.invokeMethod(this.dumpHeapMethod, this.diagnosticMXBean, "heap", file.getAbsolutePath());
}
}
Loading

0 comments on commit f619abd

Please sign in to comment.