Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Counter steer against too noisy logs #186

Merged
15 changes: 11 additions & 4 deletions src/main/java/org/swisspush/reststorage/DefaultRedisProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.vertx.redis.client.RedisOptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.swisspush.reststorage.exception.RestStorageExceptionFactory;
import org.swisspush.reststorage.redis.RedisProvider;
import org.swisspush.reststorage.util.ModuleConfiguration;

Expand All @@ -29,6 +30,7 @@ public class DefaultRedisProvider implements RedisProvider {
private final Vertx vertx;

private final ModuleConfiguration configuration;
private final RestStorageExceptionFactory exceptionFactory;

private RedisAPI redisAPI;
private Redis redis;
Expand All @@ -37,9 +39,14 @@ public class DefaultRedisProvider implements RedisProvider {

private final AtomicReference<Promise<RedisAPI>> connectPromiseRef = new AtomicReference<>();

public DefaultRedisProvider(Vertx vertx, ModuleConfiguration configuration) {
public DefaultRedisProvider(
Vertx vertx,
ModuleConfiguration configuration,
RestStorageExceptionFactory exceptionFactory
) {
this.vertx = vertx;
this.configuration = configuration;
this.exceptionFactory = exceptionFactory;
}

@Override
Expand Down Expand Up @@ -102,9 +109,9 @@ private Future<RedisAPI> connectToRedis() {
createConnectStrings().forEach(redisOptions::addConnectionString);
redis = Redis.createClient(vertx, redisOptions);

redis.connect().onComplete( ev -> {
if( ev.failed() ) {
promise.fail(new Exception("redis.connect()", ev.cause()));
redis.connect().onComplete(ev -> {
if (ev.failed()) {
promise.fail(exceptionFactory.newException("redis.connect() failed", ev.cause()));
connecting.set(false);
return;
}
Expand Down
28 changes: 22 additions & 6 deletions src/main/java/org/swisspush/reststorage/EventBusAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.vertx.core.net.NetSocket;
import io.vertx.core.net.SocketAddress;
import org.slf4j.Logger;
import org.swisspush.reststorage.exception.RestStorageExceptionFactory;

import javax.net.ssl.SSLSession;
import javax.security.cert.X509Certificate;
Expand All @@ -34,9 +35,18 @@
public class EventBusAdapter {

private static final Logger log = getLogger(EventBusAdapter.class);
private final RestStorageExceptionFactory exceptionFactory;

public EventBusAdapter(
RestStorageExceptionFactory exceptionFactory
) {
this.exceptionFactory = exceptionFactory;
}

public void init(final Vertx vertx, String address, final Handler<HttpServerRequest> requestHandler) {
vertx.eventBus().consumer(address, (Handler<Message<Buffer>>) message -> requestHandler.handle(new MappedHttpServerRequest(vertx, message)));
vertx.eventBus().consumer(address, (Handler<Message<Buffer>>) message -> {
requestHandler.handle(new MappedHttpServerRequest(vertx, message, exceptionFactory));
});
}

private static class MappedHttpServerRequest extends HttpServerRequestInternal {
Expand All @@ -46,6 +56,7 @@ private static class MappedHttpServerRequest extends HttpServerRequestInternal {
private final String uri;
private final MultiMap requestHeaders;
private final Message<Buffer> message;
private final RestStorageExceptionFactory exceptionFactory;
private String path;
private String query;
private MultiMap params;
Expand All @@ -54,9 +65,14 @@ private static class MappedHttpServerRequest extends HttpServerRequestInternal {
private Handler<Void> endHandler;
private HttpServerResponse response;

private MappedHttpServerRequest(Vertx vertx, Message<Buffer> message) {
private MappedHttpServerRequest(
Vertx vertx,
Message<Buffer> message,
RestStorageExceptionFactory exceptionFactory
) {
this.vertx = vertx;
this.message = message;
this.exceptionFactory = exceptionFactory;
Buffer buffer = message.body();
int headerLength = buffer.getInt(0);
JsonObject header = new JsonObject(buffer.getString(4, headerLength + 4));
Expand Down Expand Up @@ -487,15 +503,15 @@ public boolean writeQueueFull() {

@Override
public HttpServerResponse drainHandler(Handler<Void> voidHandler) {
log.warn("I wish you a happy timeout as this method ignores drainHandler anyway.",
new Exception("may this stacktrace help you"));
log.warn("stacktrace", exceptionFactory.newException(
"I wish you a happy timeout as this method ignores drainHandler anyway."));
return this;
}

@Override
public HttpServerResponse exceptionHandler(Handler<Throwable> throwableHandler) {
log.warn("I wish you a happy debugging session as this method ignores exceptionHandler anyway.",
new Exception("may this stacktrace help you"));
log.warn("stacktrace", exceptionFactory.newException(
"I wish you a happy debugging session as this method ignores exceptionHandler anyway."));
return this;
}
};
Expand Down
15 changes: 10 additions & 5 deletions src/main/java/org/swisspush/reststorage/FileSystemStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.vertx.core.file.OpenOptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.swisspush.reststorage.exception.RestStorageExceptionFactory;
import org.swisspush.reststorage.util.LockMode;

import java.io.File;
Expand All @@ -24,13 +25,15 @@ public class FileSystemStorage implements Storage {

private final String root;
private final Vertx vertx;
private final RestStorageExceptionFactory exceptionFactory;
private final int rootLen;
private final FileSystemDirLister fileSystemDirLister;

private final Logger log = LoggerFactory.getLogger(FileSystemStorage.class);

public FileSystemStorage(Vertx vertx, String root) {
public FileSystemStorage(Vertx vertx, RestStorageExceptionFactory exceptionFactory, String root) {
this.vertx = vertx;
this.exceptionFactory = exceptionFactory;
this.fileSystemDirLister = new FileSystemDirLister(vertx, root);
// Unify format for simpler work.
String tmpRoot;
Expand Down Expand Up @@ -63,8 +66,9 @@ public void get(String path, String etag, final int offset, final int count, fin
fileSystem().exists(fullPath, booleanAsyncResult -> {
if( booleanAsyncResult.failed() ){
String msg = "vertx.fileSystem().exists()";
if( log.isWarnEnabled() ){
log.warn(msg, new Exception(fullPath, booleanAsyncResult.cause()));
if (log.isWarnEnabled()) {
log.warn(msg, exceptionFactory.newException("fileSystem().exists(" + fullPath + ") failed",
booleanAsyncResult.cause()));
}
Resource r = new Resource();
r.error = true;
Expand All @@ -83,8 +87,9 @@ public void get(String path, String etag, final int offset, final int count, fin
fileSystem().props(fullPath, filePropsAsyncResult -> {
if( filePropsAsyncResult.failed() ){
String msg = "vertx.fileSystem().props()";
if( log.isWarnEnabled() ){
log.warn(msg, new Exception(fullPath, filePropsAsyncResult.cause()));
if (log.isWarnEnabled()) {
log.warn(msg, exceptionFactory.newException("fileSystem().props(" + fullPath + ")",
filePropsAsyncResult.cause()));
}
Resource r = new Resource();
r.error = true;
Expand Down
28 changes: 21 additions & 7 deletions src/main/java/org/swisspush/reststorage/RestStorageHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.core.spi.ExecutorServiceFactory;
import io.vertx.core.streams.Pump;
import io.vertx.ext.auth.authentication.AuthenticationProvider;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.BasicAuthHandler;
import org.slf4j.Logger;
import org.swisspush.reststorage.exception.RestStorageExceptionFactory;
import org.swisspush.reststorage.util.*;

import java.text.DecimalFormat;
Expand All @@ -29,6 +31,7 @@ public class RestStorageHandler implements Handler<HttpServerRequest> {
private final Logger log;
private final Router router;
private final Storage storage;
private final RestStorageExceptionFactory exceptionFactory;

private final MimeTypeResolver mimeTypeResolver = new MimeTypeResolver("application/json; charset=utf-8");

Expand All @@ -43,10 +46,17 @@ public class RestStorageHandler implements Handler<HttpServerRequest> {
private final DecimalFormat decimalFormat;
private final Integer maxStorageExpandSubresources;

public RestStorageHandler(Vertx vertx, final Logger log, final Storage storage, final ModuleConfiguration config) {
public RestStorageHandler(
Vertx vertx,
Logger log,
Storage storage,
RestStorageExceptionFactory exceptionFactory,
ModuleConfiguration config
) {
this.router = Router.router(vertx);
this.log = log;
this.storage = storage;
this.exceptionFactory = exceptionFactory;
this.prefix = config.getPrefix();
this.confirmCollectionDelete = config.isConfirmCollectionDelete();
this.rejectStorageWriteOnLowMemory = config.isRejectStorageWriteOnLowMemory();
Expand All @@ -65,7 +75,8 @@ public RestStorageHandler(Vertx vertx, final Logger log, final Storage storage,
Result<Boolean, String> result = checkHttpAuthenticationConfiguration(config);
if(result.isErr()) {
router.route().handler(ctx -> {
log.warn("router.route()", new Exception(result.getErr()));
log.warn("stacktrace", exceptionFactory.newException(
"router.route() failed: " + result.getErr()));
respondWith(ctx.response(), StatusCode.INTERNAL_SERVER_ERROR, result.getErr());
});
} else if (result.getOk()) {
Expand Down Expand Up @@ -278,8 +289,10 @@ public void handle(Resource resource) {
documentResource.closeHandler.handle(null);
rsp.end();
});
documentResource.addErrorHandler(ex -> log.error("TODO error handling", new Exception(ex)));
documentResource.readStream.exceptionHandler(ex -> log.error("TODO error handling", new Exception(ex)));
documentResource.addErrorHandler((Throwable ex) -> log.error("stacktrace",
exceptionFactory.newException("TODO error handling", ex)));
documentResource.readStream.exceptionHandler((Throwable ex) -> log.error("stacktrace",
exceptionFactory.newException("TODO error handling", ex)));
pump.start();
}
}
Expand Down Expand Up @@ -487,8 +500,9 @@ private void putResource_storeContentsOfDocumentResource(RoutingContext ctx, Doc
// Caller is responsible to do any 'error', 'exists', 'rejected' checks on the
// resource. Therefore we simply go forward and store its content.
final HttpServerRequest request = ctx.request();
resource.addErrorHandler(ex -> {
if( log.isDebugEnabled() ) log.debug("Happy stacktrace just for you", new Exception(ex));
resource.addErrorHandler((Throwable ex) -> {
if (log.isDebugEnabled()) log.debug("stacktrace",
exceptionFactory.newException("DocumentResource reports error", ex));
respondWith(response, StatusCode.INTERNAL_SERVER_ERROR, ex.getMessage());
});
// Complete response when resource written.
Expand All @@ -504,7 +518,7 @@ private void putResource_storeContentsOfDocumentResource(RoutingContext ctx, Doc
resource.errorMessage = exc.getMessage();
final Handler<Throwable> resourceErrorHandler = resource.errorHandler;
if (resourceErrorHandler != null) {
resourceErrorHandler.handle(new Exception(exc));
resourceErrorHandler.handle(exceptionFactory.newException(exc));
}
});
final Pump pump = Pump.pump(request, resource.writeStream);
Expand Down
37 changes: 25 additions & 12 deletions src/main/java/org/swisspush/reststorage/RestStorageMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,31 @@
import io.vertx.core.http.HttpServerRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.swisspush.reststorage.exception.RestStorageExceptionFactory;
import org.swisspush.reststorage.redis.RedisProvider;
import org.swisspush.reststorage.redis.RedisStorage;
import org.swisspush.reststorage.util.ModuleConfiguration;

import static org.swisspush.reststorage.exception.RestStorageExceptionFactory.newRestStorageThriftyExceptionFactory;

public class RestStorageMod extends AbstractVerticle {

private final Logger log = LoggerFactory.getLogger(RestStorageMod.class);

private RedisProvider redisProvider;
private final RestStorageExceptionFactory exceptionFactory;

public RestStorageMod() {}
public RestStorageMod() {
this.exceptionFactory = newRestStorageThriftyExceptionFactory();
}

public RestStorageMod(RedisProvider redisProvider) {
public RestStorageMod(
RedisProvider redisProvider,
RestStorageExceptionFactory exceptionFactory
) {
assert exceptionFactory != null;
this.redisProvider = redisProvider;
this.exceptionFactory = exceptionFactory;
}

@Override
Expand All @@ -30,22 +41,24 @@ public void start(Promise<Void> promise) {
if (event.failed()) {
promise.fail(event.cause());
} else {
Handler<HttpServerRequest> handler = new RestStorageHandler(vertx, log, event.result(), modConfig);
Handler<HttpServerRequest> handler = new RestStorageHandler(
vertx, log, event.result(), exceptionFactory, modConfig);

if(modConfig.isHttpRequestHandlerEnabled()) {
// in Vert.x 2x 100-continues was activated per default, in vert.x 3x it is off per default.
HttpServerOptions options = new HttpServerOptions().setHandle100ContinueAutomatically(true);

vertx.createHttpServer(options).requestHandler(handler).listen(modConfig.getPort(), result -> {
if (result.succeeded()) {
new EventBusAdapter().init(vertx, modConfig.getStorageAddress(), handler);
new EventBusAdapter(exceptionFactory).init(vertx, modConfig.getStorageAddress(), handler);
promise.complete();
} else {
promise.fail(new Exception(result.cause()));
promise.fail(exceptionFactory.newException(
"vertx.HttpServer.listen(" + modConfig.getPort() + ") failed", result.cause()));
}
});
} else {
new EventBusAdapter().init(vertx, modConfig.getStorageAddress(), handler);
new EventBusAdapter(exceptionFactory).init(vertx, modConfig.getStorageAddress(), handler);
promise.complete();
}
}
Expand All @@ -57,19 +70,19 @@ private Future<Storage> createStorage(ModuleConfiguration moduleConfiguration) {

switch (moduleConfiguration.getStorageType()) {
case filesystem:
promise.complete(new FileSystemStorage(vertx, moduleConfiguration.getRoot()));
promise.complete(new FileSystemStorage(vertx, exceptionFactory, moduleConfiguration.getRoot()));
break;
case redis:
createRedisStorage(vertx, moduleConfiguration).onComplete(event -> {
if(event.succeeded()){
promise.complete(event.result());
} else {
promise.fail(new Exception(event.cause()));
promise.fail(exceptionFactory.newException("createRedisStorage() failed", event.cause()));
}
});
break;
default:
promise.fail(new RuntimeException("Storage not supported: " + moduleConfiguration.getStorageType()));
promise.fail(exceptionFactory.newException("Storage not supported: " + moduleConfiguration.getStorageType()));
}

return promise.future();
Expand All @@ -79,14 +92,14 @@ private Future<RedisStorage> createRedisStorage(Vertx vertx, ModuleConfiguration
Promise<RedisStorage> initPromise = Promise.promise();

if(redisProvider == null) {
redisProvider = new DefaultRedisProvider(vertx, moduleConfiguration);
redisProvider = new DefaultRedisProvider(vertx, moduleConfiguration, exceptionFactory);
}

redisProvider.redis().onComplete(event -> {
if(event.succeeded()) {
initPromise.complete(new RedisStorage(vertx, moduleConfiguration, redisProvider));
initPromise.complete(new RedisStorage(vertx, moduleConfiguration, redisProvider, exceptionFactory));
} else {
initPromise.fail(new Exception(event.cause()));
initPromise.fail(exceptionFactory.newException("redisProvider.redis() failed", event.cause()));
}
});

Expand Down
Loading
Loading