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

perf: batch selector wakeup call when adding bulk requests #158

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.net.InetSocketAddress;
import java.nio.channels.CancelledKeyException;
import java.nio.channels.ClosedSelectorException;
import java.nio.channels.Selector;
import java.util.Collection;
import java.util.ConcurrentModificationException;
import java.util.List;
Expand All @@ -17,12 +18,14 @@

public class EVCacheConnection extends MemcachedConnection {
private static final Logger log = LoggerFactory.getLogger(EVCacheConnection.class);
private final net.spy.memcached.compat.log.Logger spyLogger;

public EVCacheConnection(String name, int bufSize, ConnectionFactory f,
List<InetSocketAddress> a, Collection<ConnectionObserver> obs,
FailureMode fm, OperationFactory opfactory) throws IOException {
super(bufSize, f, a, obs, fm, opfactory);
setName(name);
spyLogger = super.getLogger();
}

@Override
Expand Down Expand Up @@ -76,9 +79,42 @@ protected void addOperation(final MemcachedNode node, final Operation o) {

@Override
public void addOperations(Map<MemcachedNode, Operation> ops) {
super.addOperations(ops);
for (MemcachedNode node : ops.keySet()) {
((EVCacheNode) node).incrOps();
// Directly inline this operation, copied down from parent implementation.
final String OVERALL_REQUEST_METRIC = "[MEM] Request Rate: All";

// We want to avoid selector.wakeup() being called repeatedly if we can avoid it.
// The epoll selector takes a lock underneath even if it determines that no system
// call is required. This is unfortunately contending with the memcached loop which
// is prevented from progressing the select() while a wakeup() check is in flight.

boolean addedOne = false;
try {
for (Map.Entry<MemcachedNode, Operation> me : ops.entrySet()) {
MemcachedNode node = me.getKey();
Operation o = me.getValue();

if (!node.isAuthenticated()) {
retryOperation(o);
continue;
}

addedOne = true;

o.setHandlingNode(node);
o.initialize();
node.addOp(o);
addedQueue.offer(node);

((EVCacheNode) node).incrOps();
metrics.markMeter(OVERALL_REQUEST_METRIC);
// retain the existing logger configuration
spyLogger.debug("Added %s to %s", o, node);
}
} finally {
if (addedOne) {
Selector s = selector.wakeup();
assert s == selector : "Wakeup returned the wrong selector.";
}
}
}

Expand Down
Loading