Skip to content

Commit

Permalink
Disable Vert.x TCCL management
Browse files Browse the repository at this point in the history
This breaks dev mode, and in general is not needed as Quarkus can
perform it's own TCCL management when required. It also provides a
slight performance boost.

Fixes quarkusio#18299
  • Loading branch information
stuartwdouglas committed Jul 25, 2021
1 parent 0bbf3ec commit 3c469c9
Show file tree
Hide file tree
Showing 9 changed files with 285 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;

import org.graalvm.nativeimage.ImageInfo;
Expand Down Expand Up @@ -102,30 +103,34 @@ public void accept(String loggerName, CleanupFilterConfig config) {
}
});
}
LogCleanupFilter cleanupFiler = new LogCleanupFilter(filterElements);
for (Handler handler : LogManager.getLogManager().getLogger("").getHandlers()) {
handler.setFilter(cleanupFiler);
}

final ArrayList<Handler> handlers = new ArrayList<>(3 + additionalHandlers.size());

if (config.console.enable) {
final Handler consoleHandler = configureConsoleHandler(config.console, consoleConfig, errorManager, filterElements,
final Handler consoleHandler = configureConsoleHandler(config.console, consoleConfig, errorManager, cleanupFiler,
possibleFormatters, possibleBannerSupplier, launchMode);
errorManager = consoleHandler.getErrorManager();
handlers.add(consoleHandler);
}

if (config.file.enable) {
handlers.add(configureFileHandler(config.file, errorManager, filterElements));
handlers.add(configureFileHandler(config.file, errorManager, cleanupFiler));
}

if (config.syslog.enable) {
final Handler syslogHandler = configureSyslogHandler(config.syslog, errorManager, filterElements);
final Handler syslogHandler = configureSyslogHandler(config.syslog, errorManager, cleanupFiler);
if (syslogHandler != null) {
handlers.add(syslogHandler);
}
}

if (!categories.isEmpty()) {
Map<String, Handler> namedHandlers = createNamedHandlers(config, consoleConfig, possibleFormatters, errorManager,
filterElements, launchMode);
cleanupFiler, launchMode);

Map<String, Handler> additionalNamedHandlersMap;
if (additionalNamedHandlers.isEmpty()) {
Expand Down Expand Up @@ -166,7 +171,7 @@ public void accept(String categoryName, CategoryConfig config) {
if (optional.isPresent()) {
final Handler handler = optional.get();
handler.setErrorManager(errorManager);
handler.setFilter(new LogCleanupFilter(filterElements));
handler.setFilter(cleanupFiler);
handlers.add(handler);
}
}
Expand All @@ -191,18 +196,20 @@ public static void initializeBuildTimeLogging(LogConfig config, LogBuildTimeConf
filterElements.add(
new LogCleanupFilterElement(entry.getKey(), entry.getValue().targetLevel, entry.getValue().ifStartsWith));
}
LogCleanupFilter logCleanupFilter = new LogCleanupFilter(filterElements);

final ArrayList<Handler> handlers = new ArrayList<>(3);

if (config.console.enable) {
final Handler consoleHandler = configureConsoleHandler(config.console, consoleConfig, errorManager, filterElements,
final Handler consoleHandler = configureConsoleHandler(config.console, consoleConfig, errorManager,
logCleanupFilter,
Collections.emptyList(), new RuntimeValue<>(Optional.empty()), launchMode);
errorManager = consoleHandler.getErrorManager();
handlers.add(consoleHandler);
}

Map<String, Handler> namedHandlers = createNamedHandlers(config, consoleConfig, Collections.emptyList(), errorManager,
filterElements, launchMode);
logCleanupFilter, launchMode);

for (Map.Entry<String, CategoryConfig> entry : categories.entrySet()) {
final CategoryBuildTimeConfig buildCategory = isSubsetOf(entry.getKey(), buildConfig.categories);
Expand Down Expand Up @@ -262,15 +269,15 @@ private static CategoryBuildTimeConfig isSubsetOf(String categoryName, Map<Strin

private static Map<String, Handler> createNamedHandlers(LogConfig config, ConsoleRuntimeConfig consoleRuntimeConfig,
List<RuntimeValue<Optional<Formatter>>> possibleFormatters, ErrorManager errorManager,
List<LogCleanupFilterElement> filterElements, LaunchMode launchMode) {
LogCleanupFilter cleanupFilter, LaunchMode launchMode) {
Map<String, Handler> namedHandlers = new HashMap<>();
for (Entry<String, ConsoleConfig> consoleConfigEntry : config.consoleHandlers.entrySet()) {
ConsoleConfig namedConsoleConfig = consoleConfigEntry.getValue();
if (!namedConsoleConfig.enable) {
continue;
}
final Handler consoleHandler = configureConsoleHandler(namedConsoleConfig, consoleRuntimeConfig, errorManager,
filterElements,
cleanupFilter,
possibleFormatters, null, launchMode);
addToNamedHandlers(namedHandlers, consoleHandler, consoleConfigEntry.getKey());
}
Expand All @@ -279,15 +286,15 @@ private static Map<String, Handler> createNamedHandlers(LogConfig config, Consol
if (!namedFileConfig.enable) {
continue;
}
final Handler fileHandler = configureFileHandler(namedFileConfig, errorManager, filterElements);
final Handler fileHandler = configureFileHandler(namedFileConfig, errorManager, cleanupFilter);
addToNamedHandlers(namedHandlers, fileHandler, fileConfigEntry.getKey());
}
for (Entry<String, SyslogConfig> sysLogConfigEntry : config.syslogHandlers.entrySet()) {
SyslogConfig namedSyslogConfig = sysLogConfigEntry.getValue();
if (!namedSyslogConfig.enable) {
continue;
}
final Handler syslogHandler = configureSyslogHandler(namedSyslogConfig, errorManager, filterElements);
final Handler syslogHandler = configureSyslogHandler(namedSyslogConfig, errorManager, cleanupFilter);
if (syslogHandler != null) {
addToNamedHandlers(namedHandlers, syslogHandler, sysLogConfigEntry.getKey());
}
Expand Down Expand Up @@ -341,7 +348,7 @@ public void initializeLoggingForImageBuild() {

private static Handler configureConsoleHandler(final ConsoleConfig config, ConsoleRuntimeConfig consoleRuntimeConfig,
final ErrorManager defaultErrorManager,
final List<LogCleanupFilterElement> filterElements,
final LogCleanupFilter cleanupFilter,
final List<RuntimeValue<Optional<Formatter>>> possibleFormatters,
final RuntimeValue<Optional<Supplier<String>>> possibleBannerSupplier, LaunchMode launchMode) {
Formatter formatter = null;
Expand Down Expand Up @@ -384,7 +391,7 @@ private static Handler configureConsoleHandler(final ConsoleConfig config, Conso
config.stderr ? ConsoleHandler.Target.SYSTEM_ERR : ConsoleHandler.Target.SYSTEM_OUT, formatter);
consoleHandler.setLevel(config.level);
consoleHandler.setErrorManager(defaultErrorManager);
consoleHandler.setFilter(new LogCleanupFilter(filterElements));
consoleHandler.setFilter(cleanupFilter);

Handler handler = config.async.enable ? createAsyncHandler(config.async, config.level, consoleHandler)
: consoleHandler;
Expand Down Expand Up @@ -421,7 +428,7 @@ public void close() throws SecurityException {
}

private static Handler configureFileHandler(final FileConfig config, final ErrorManager errorManager,
final List<LogCleanupFilterElement> filterElements) {
final LogCleanupFilter cleanupFilter) {
FileHandler handler = new FileHandler();
FileConfig.RotationConfig rotationConfig = config.rotation;
if ((rotationConfig.maxFileSize.isPresent() || rotationConfig.rotateOnBoot)
Expand Down Expand Up @@ -454,7 +461,7 @@ private static Handler configureFileHandler(final FileConfig config, final Error
}
handler.setErrorManager(errorManager);
handler.setLevel(config.level);
handler.setFilter(new LogCleanupFilter(filterElements));
handler.setFilter(cleanupFilter);
if (config.async.enable) {
return createAsyncHandler(config.async, config.level, handler);
}
Expand All @@ -463,7 +470,7 @@ private static Handler configureFileHandler(final FileConfig config, final Error

private static Handler configureSyslogHandler(final SyslogConfig config,
final ErrorManager errorManager,
final List<LogCleanupFilterElement> filterElements) {
final LogCleanupFilter logCleanupFilter) {
try {
final SyslogHandler handler = new SyslogHandler(config.endpoint.getHostString(), config.endpoint.getPort());
handler.setAppName(config.appName.orElse(getProcessName()));
Expand All @@ -478,7 +485,7 @@ private static Handler configureSyslogHandler(final SyslogConfig config,
final PatternFormatter formatter = new PatternFormatter(config.format);
handler.setFormatter(formatter);
handler.setErrorManager(errorManager);
handler.setFilter(new LogCleanupFilter(filterElements));
handler.setFilter(logCleanupFilter);
if (config.async.enable) {
return createAsyncHandler(config.async, config.level, handler);
}
Expand Down
2 changes: 1 addition & 1 deletion extensions/hibernate-reactive/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-deployment</artifactId>
<artifactId>quarkus-resteasy-reactive-jsonb-deployment</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.quarkus.hibernate.reactive.dev;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name = "known_fruits")
@NamedQuery(name = "Fruits.findAll", query = "SELECT f FROM Fruit f ORDER BY f.name")
public class Fruit {

@Id
@SequenceGenerator(name = "fruitsSequence", sequenceName = "known_fruits_id_seq", allocationSize = 1, initialValue = 10)
@GeneratedValue(generator = "fruitsSequence")
private Integer id;

@Column(length = 40, unique = true)
private String name;

public Fruit() {
}

public Fruit(String name) {
this.name = name;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return "Fruit{" + id + "," + name + '}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package io.quarkus.hibernate.reactive.dev;

import static javax.ws.rs.core.Response.Status.CREATED;
import static javax.ws.rs.core.Response.Status.NOT_FOUND;
import static javax.ws.rs.core.Response.Status.NO_CONTENT;

import java.util.List;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

import org.hibernate.reactive.mutiny.Mutiny;
import org.jboss.logging.Logger;
import org.jboss.resteasy.reactive.RestPath;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

import io.smallrye.mutiny.Uni;

@Path("fruits")
@ApplicationScoped
@Produces("application/json")
@Consumes("application/json")
public class FruitMutinyResource {
private static final Logger LOGGER = Logger.getLogger(FruitMutinyResource.class);

@Inject
Mutiny.SessionFactory sf;

@GET
public Uni<List<Fruit>> get() {
return sf.withTransaction((s, t) -> s
.createNamedQuery("Fruits.findAll", Fruit.class)
.getResultList());
}

@GET
@Path("{id}")
public Uni<Fruit> getSingle(@RestPath Integer id) {
return sf.withTransaction((s, t) -> s.find(Fruit.class, id));
}

@POST
public Uni<Response> create(Fruit fruit) {
if (fruit == null || fruit.getId() != null) {
throw new WebApplicationException("Id was invalidly set on request.", 422);
}

return sf.withTransaction((s, t) -> s.persist(fruit))
.replaceWith(() -> Response.ok(fruit).status(CREATED).build());
}

@PUT
@Path("{id}")
public Uni<Response> update(@RestPath Integer id, Fruit fruit) {
if (fruit == null || fruit.getName() == null) {
throw new WebApplicationException("Fruit name was not set on request.", 422);
}

return sf.withTransaction((s, t) -> s.find(Fruit.class, id)
// If entity exists then update it
.onItem().ifNotNull().invoke(entity -> entity.setName(fruit.getName()))
.onItem().ifNotNull().transform(entity -> Response.ok(entity).build())
// If entity not found return the appropriate response
.onItem().ifNull()
.continueWith(() -> Response.ok().status(NOT_FOUND).build()));
}

@DELETE
@Path("{id}")
public Uni<Response> delete(@RestPath Integer id) {
return sf.withTransaction((s, t) -> s.find(Fruit.class, id)
// If entity exists then delete it
.onItem().ifNotNull()
.transformToUni(entity -> s.remove(entity)
.replaceWith(() -> Response.ok().status(NO_CONTENT).build()))
// If entity not found return the appropriate response
.onItem().ifNull().continueWith(() -> Response.ok().status(NOT_FOUND).build()));
}

/**
* Create a HTTP response from an exception.
*
* Response Example:
*
* <pre>
* HTTP/1.1 422 Unprocessable Entity
* Content-Length: 111
* Content-Type: application/json
*
* {
* "code": 422,
* "error": "Fruit name was not set on request.",
* "exceptionType": "javax.ws.rs.WebApplicationException"
* }
* </pre>
*/
@Provider
public static class ErrorMapper implements ExceptionMapper<Exception> {

@Inject
ObjectMapper objectMapper;

@Override
public Response toResponse(Exception exception) {
LOGGER.error("Failed to handle request", exception);

int code = 500;
if (exception instanceof WebApplicationException) {
code = ((WebApplicationException) exception).getResponse().getStatus();
}

ObjectNode exceptionJson = objectMapper.createObjectNode();
exceptionJson.put("exceptionType", exception.getClass().getName());
exceptionJson.put("code", code);

if (exception.getMessage() != null) {
exceptionJson.put("error", exception.getMessage());
}

return Response.status(code)
.entity(exceptionJson)
.build();
}

}
}
Loading

0 comments on commit 3c469c9

Please sign in to comment.