-
Notifications
You must be signed in to change notification settings - Fork 355
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stop filling monitoring queues when processor fails
Signed-off-by: Jorge Bescos Gascon <[email protected]>
- Loading branch information
Showing
5 changed files
with
277 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved. | ||
This program and the accompanying materials are made available under the | ||
terms of the Eclipse Public License v. 2.0, which is available at | ||
http://www.eclipse.org/legal/epl-2.0. | ||
This Source Code may also be made available under the following Secondary | ||
Licenses when the conditions for such availability set forth in the | ||
Eclipse Public License v. 2.0 are satisfied: GNU General Public License, | ||
version 2 with the GNU Classpath Exception, which is available at | ||
https://www.gnu.org/software/classpath/license.html. | ||
SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>project</artifactId> | ||
<groupId>org.glassfish.jersey.tests.integration</groupId> | ||
<version>2.34-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>jersey-4697</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.glassfish.jersey.test-framework.providers</groupId> | ||
<artifactId>jersey-test-framework-provider-bundle</artifactId> | ||
<type>pom</type> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-all</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
197 changes: 197 additions & 0 deletions
197
...t/java/org/glassfish/jersey/tests/integration/jersey4697/MonitoringEventListenerTest.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,197 @@ | ||
/* | ||
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0, which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the | ||
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License, | ||
* version 2 with the GNU Classpath Exception, which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
*/ | ||
|
||
package org.glassfish.jersey.tests.integration.jersey4697; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.lang.management.ManagementFactory; | ||
import java.lang.reflect.Method; | ||
import java.util.Collection; | ||
import java.util.Map; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import javax.inject.Inject; | ||
import javax.management.JMX; | ||
import javax.management.MBeanServer; | ||
import javax.management.ObjectName; | ||
import javax.management.openmbean.CompositeDataSupport; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.core.Application; | ||
import javax.ws.rs.core.Response; | ||
import javax.ws.rs.ext.ExceptionMapper; | ||
import javax.ws.rs.ext.Provider; | ||
|
||
import org.glassfish.jersey.internal.inject.InjectionManager; | ||
import org.glassfish.jersey.internal.inject.Providers; | ||
import org.glassfish.jersey.server.ResourceConfig; | ||
import org.glassfish.jersey.server.ServerProperties; | ||
import org.glassfish.jersey.server.internal.monitoring.MonitoringEventListener; | ||
import org.glassfish.jersey.server.monitoring.ApplicationEventListener; | ||
import org.glassfish.jersey.server.monitoring.ExceptionMapperMXBean; | ||
import org.glassfish.jersey.server.monitoring.RequestEvent; | ||
import org.glassfish.jersey.server.monitoring.RequestEventListener; | ||
import org.glassfish.jersey.test.JerseyTest; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class MonitoringEventListenerTest extends JerseyTest { | ||
|
||
private static final long TIMEOUT = 60000; // 1 minute | ||
private static final String MBEAN_EXCEPTION = | ||
"org.glassfish.jersey:type=MonitoringEventListenerTest,subType=Global,exceptions=ExceptionMapper"; | ||
|
||
@Path("/example") | ||
public static class ExampleResource { | ||
@Inject | ||
private InjectionManager injectionManager; | ||
@GET | ||
@Path("/error") | ||
public Response error() { | ||
throw new RuntimeException("Any exception to be counted in ExceptionMapper"); | ||
} | ||
@GET | ||
@Path("/poison") | ||
public Response poison() { | ||
MonitoringEventListener monitoringEventListener = listener(); | ||
RequestEvent requestEvent = mock(RequestEvent.class); | ||
when(requestEvent.getType()).thenReturn(RequestEvent.Type.START); | ||
RequestEventListener eventListener = monitoringEventListener.onRequest(requestEvent); | ||
RequestEvent poisonEvent = mock(RequestEvent.class); | ||
when(poisonEvent.getType()).thenReturn(RequestEvent.Type.EXCEPTION_MAPPING_FINISHED); | ||
when(poisonEvent.getExceptionMapper()) | ||
.thenThrow(new IllegalStateException("This causes the scheduler to stop working")); | ||
eventListener.onEvent(poisonEvent); | ||
return Response.ok().build(); | ||
} | ||
@GET | ||
@Path("/queueSize") | ||
public Response queueSize() throws Exception { | ||
MonitoringEventListener monitoringEventListener = listener(); | ||
Method method = MonitoringEventListener.class.getDeclaredMethod("getExceptionMapperEvents"); | ||
method.setAccessible(true); | ||
Collection<?> queue = (Collection<?>) method.invoke(monitoringEventListener); | ||
return Response.ok(queue.size()).build(); | ||
} | ||
private MonitoringEventListener listener() { | ||
Iterable<ApplicationEventListener> listeners = | ||
Providers.getAllProviders(injectionManager, ApplicationEventListener.class); | ||
for (ApplicationEventListener listener : listeners) { | ||
if (listener instanceof MonitoringEventListener) { | ||
return (MonitoringEventListener) listener; | ||
} | ||
} | ||
throw new IllegalStateException("MonitoringEventListener was not found"); | ||
} | ||
} | ||
|
||
@Provider | ||
public static class RuntimeExceptionMapper implements ExceptionMapper<RuntimeException> { | ||
@Override | ||
public Response toResponse(RuntimeException e) { | ||
return Response.status(500).entity("RuntimeExceptionMapper: " + e.getMessage()).build(); | ||
} | ||
} | ||
|
||
@Override | ||
protected Application configure() { | ||
ResourceConfig resourceConfig = new ResourceConfig(ExampleResource.class); | ||
// Need to map the exception to be counted by ExceptionMapper | ||
resourceConfig.register(RuntimeExceptionMapper.class); | ||
resourceConfig.property(ServerProperties.MONITORING_ENABLED, true); | ||
resourceConfig.property(ServerProperties.MONITORING_STATISTICS_ENABLED, true); | ||
resourceConfig.property(ServerProperties.MONITORING_STATISTICS_MBEANS_ENABLED, true); | ||
resourceConfig.property(ServerProperties.MONITORING_STATISTICS_REFRESH_INTERVAL, 1); | ||
resourceConfig.setApplicationName("MonitoringEventListenerTest"); | ||
return resourceConfig; | ||
} | ||
|
||
@Test | ||
public void exceptionInScheduler() throws Exception { | ||
final Long ERRORS_BEFORE_FAIL = 10L; | ||
// Send some requests to process some statistics. | ||
request(ERRORS_BEFORE_FAIL); | ||
// Give some time to the scheduler to collect data. | ||
waitTillQueueIsEmpty(); | ||
// Make the scheduler to fail. No more statistics are collected. | ||
makeFailure(); | ||
// Sending again requests | ||
request(20); | ||
// Explicit wait | ||
Thread.sleep(1000); | ||
// No new events should be accepted because scheduler is not working. | ||
queueIsEmpty(); | ||
Long monitoredErrors = mappedErrorsFromJMX(MBEAN_EXCEPTION); | ||
assertEquals(ERRORS_BEFORE_FAIL, monitoredErrors); | ||
} | ||
|
||
private void waitTillQueueIsEmpty() throws InterruptedException { | ||
final CountDownLatch latch = new CountDownLatch(1); | ||
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); | ||
scheduler.scheduleAtFixedRate(() -> { | ||
Response response = target("/example/queueSize").request().get(); | ||
int queueSize = response.readEntity(Integer.class); | ||
if (queueSize == 0) { | ||
latch.countDown(); | ||
} | ||
}, 0, 10, TimeUnit.MILLISECONDS); | ||
boolean wait = latch.await(TIMEOUT, TimeUnit.MILLISECONDS); | ||
assertTrue("The queue was not empty in the given time", wait); | ||
scheduler.shutdown(); | ||
} | ||
|
||
private void makeFailure() { | ||
Response response = target("/example/poison").request().get(); | ||
assertEquals(200, response.getStatus()); | ||
} | ||
|
||
private void queueIsEmpty() { | ||
Response response = target("/example/queueSize").request().get(); | ||
assertEquals(200, response.getStatus()); | ||
assertEquals(Integer.valueOf(0), response.readEntity(Integer.class)); | ||
} | ||
|
||
private Long mappedErrorsFromJMX(String name) throws Exception { | ||
Long monitoredErrors = null; | ||
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); | ||
ObjectName objectName = new ObjectName(name); | ||
ExceptionMapperMXBean bean = JMX.newMBeanProxy(mbs, objectName, ExceptionMapperMXBean.class); | ||
Map<?, ?> counter = bean.getExceptionMapperCount(); | ||
CompositeDataSupport value = (CompositeDataSupport) counter.entrySet().iterator().next().getValue(); | ||
for (Object obj : value.values()) { | ||
if (obj instanceof Long) { | ||
// Messy way to get the errors, but generic types doesn't match and there is no nice way | ||
monitoredErrors = (Long) obj; | ||
break; | ||
} | ||
} | ||
return monitoredErrors; | ||
} | ||
|
||
private void request(long requests) { | ||
for (long i = 0; i < requests; i++) { | ||
Response response = target("/example/error").request().get(); | ||
assertEquals(500, response.getStatus()); | ||
} | ||
} | ||
} |
Oops, something went wrong.