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

enable some asynchronous logging #30

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/main/java/org/fluentd/logger/FluentLoggerFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Collections;
import java.util.Map;
import java.util.Properties;
import java.util.WeakHashMap;
Expand All @@ -33,7 +34,7 @@ public class FluentLoggerFactory {
private final Map<String, FluentLogger> loggers;

public FluentLoggerFactory() {
loggers = new WeakHashMap<String, FluentLogger>();
loggers = Collections.synchronizedMap(new WeakHashMap<String, FluentLogger>());
}

public FluentLogger getLogger(String tagPrefix) {
Expand All @@ -48,7 +49,7 @@ public FluentLogger getLogger(String tagPrefix, String host, int port, int timeo
return getLogger(tagPrefix, host, port, timeout, bufferCapacity, new ExponentialDelayReconnector());
}

public synchronized FluentLogger getLogger(String tagPrefix, String host, int port, int timeout, int bufferCapacity,
public FluentLogger getLogger(String tagPrefix, String host, int port, int timeout, int bufferCapacity,
Reconnector reconnector) {
String key = String.format("%s_%s_%d_%d_%d", new Object[] { tagPrefix, host, port, timeout, bufferCapacity });
if (loggers.containsKey(key)) {
Expand All @@ -73,6 +74,21 @@ public synchronized FluentLogger getLogger(String tagPrefix, String host, int po
}
}

public FluentLogger getLogger(String tagPrefix, String host, int port, int timeout, int bufferCapacity,
Sender sender) {
if (sender == null) {
return getLogger(tagPrefix, host, port, timeout, bufferCapacity);
}
String key = String.format("%s_%s_%d_%d_%d_%s", new Object[] { tagPrefix, host, port, timeout, bufferCapacity, sender == null ? "null" : sender .getName() });
if (loggers.containsKey(key)) {
return loggers.get(key);
} else {
FluentLogger logger = new FluentLogger(tagPrefix, sender);
loggers.put(key, logger);
return logger;
}
}

@SuppressWarnings("unchecked")
private Sender createSenderInstance(final String className, final Object[] params) throws ClassNotFoundException,
SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException,
Expand Down
129 changes: 129 additions & 0 deletions src/main/java/org/fluentd/logger/sender/AsyncRawSocketSender.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@

package org.fluentd.logger.sender;

import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.fluentd.logger.errorhandler.ErrorHandler;
import org.fluentd.logger.sender.ExponentialDelayReconnector;
import org.fluentd.logger.sender.RawSocketSender;
import org.fluentd.logger.sender.Reconnector;
import org.fluentd.logger.sender.Sender;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* An asynchronous wrapper around RawSocketSender
*
* @author mxk
*
*/
public class AsyncRawSocketSender implements Sender {

private final class EmitRunnable implements Runnable {
private final String tag;
private final Map<String, Object> data;
private final RawSocketSender sender;
private final long timestamp;

private EmitRunnable(String tag, Map<String, Object> data,
RawSocketSender sender, long timestamp) {
this.tag = tag;
this.data = data;
this.sender = sender;
this.timestamp = timestamp;
}

@Override
public void run() {
sender.emit(tag, timestamp, data);
}
}

private final class FlushRunnable implements Runnable {
private final RawSocketSender sender;

private FlushRunnable(RawSocketSender sender) {
this.sender = sender;
}

@Override
public void run() {
sender.flush();
}
}

private RawSocketSender sender;
private Reconnector reconnector;

@SuppressWarnings("unused")
private static final Logger logger = LoggerFactory.getLogger(AsyncRawSocketSender.class);

private static final ExecutorService flusher = Executors.newSingleThreadExecutor();

public AsyncRawSocketSender() {
this("localhost", 24224);
}

public AsyncRawSocketSender(String host, int port) {
this(host, port, 3 * 1000, 8 * 1024 * 1024);
}

public AsyncRawSocketSender(String host, int port, int timeout,
int bufferCapacity) {
this(host, port, timeout, bufferCapacity,
new ExponentialDelayReconnector());
}

public AsyncRawSocketSender(String host, int port, int timeout,
int bufferCapacity, Reconnector reconnector) {
this.reconnector = reconnector;
this.sender = new RawSocketSender(host, port, timeout, bufferCapacity,
reconnector);
}

@Override
public synchronized void flush() {
final RawSocketSender sender = this.sender;
flusher.execute(new FlushRunnable(sender));
}

@Override
public void close() {
sender.close();
}

@Override
public boolean emit(String tag, Map<String, Object> data) {
return emit(tag, System.currentTimeMillis() / 1000, data);
}

@Override
public boolean emit(final String tag, final long timestamp, final Map<String, Object> data) {
final RawSocketSender sender = this.sender;
flusher.execute(new EmitRunnable(tag, data, sender, timestamp));

return sender.isConnected() || reconnector.enableReconnection(System.currentTimeMillis());
}

@Override
public String getName() {
return sender.getName();
}

@Override
public boolean isConnected() {
return sender.isConnected();
}

@Override
public void setErrorHandler(ErrorHandler errorHandler) {
sender.setErrorHandler(errorHandler);
}

@Override
public void removeErrorHandler() {
sender.removeErrorHandler();
}
}