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

google-cloud-logging: Optimize pendingWrites #4697

Merged
merged 1 commit into from
Mar 19, 2019
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 @@ -70,11 +70,9 @@
import com.google.logging.v2.WriteLogEntriesResponse;
import com.google.protobuf.Empty;
import java.util.ArrayList;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
Expand All @@ -83,9 +81,7 @@ class LoggingImpl extends BaseService<LoggingOptions> implements Logging {

private static final int FLUSH_WAIT_TIMEOUT_SECONDS = 6;
private final LoggingRpc rpc;
private final Object writeLock = new Object();
private final Set<ApiFuture<Void>> pendingWrites =
Collections.newSetFromMap(new IdentityHashMap<ApiFuture<Void>, Boolean>());
private final Map<Object, ApiFuture<Void>> pendingWrites = new ConcurrentHashMap<>();

private volatile Synchronicity writeSynchronicity = Synchronicity.ASYNC;
private volatile Severity flushSeverity = null;
Expand Down Expand Up @@ -575,9 +571,7 @@ public void flush() {
// BUG(1795): We should force batcher to issue RPC call for buffered messages,
// so the code below doesn't wait uselessly.
ArrayList<ApiFuture<Void>> writesToFlush = new ArrayList<>();
synchronized (writeLock) {
writesToFlush.addAll(pendingWrites);
}
writesToFlush.addAll(pendingWrites.values());

try {
ApiFutures.allAsList(writesToFlush).get(FLUSH_WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS);
Expand All @@ -596,16 +590,13 @@ private void writeLogEntries(Iterable<LogEntry> logEntries, WriteOption... write
case ASYNC:
default:
final ApiFuture<Void> writeFuture = writeAsync(logEntries, writeOptions);
synchronized (writeLock) {
pendingWrites.add(writeFuture);
}
final Object pendingKey = new Object();
pendingWrites.put(pendingKey, writeFuture);
ApiFutures.addCallback(
writeFuture,
new ApiFutureCallback<Void>() {
private void removeFromPending() {
synchronized (writeLock) {
pendingWrites.remove(writeFuture);
}
pendingWrites.remove(pendingKey);
}

@Override
Expand Down Expand Up @@ -711,8 +702,6 @@ public void close() throws Exception {

@VisibleForTesting
int getNumPendingWrites() {
synchronized (writeLock) {
return pendingWrites.size();
}
return pendingWrites.size();
}
}