-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
RFC: logging: experimental batching writer #2910
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
google-cloud-logging/src/main/java/com/google/cloud/logging/BatchingWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/* | ||
* Copyright 2018 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.google.cloud.logging; | ||
|
||
import com.google.api.core.ApiFuture; | ||
import com.google.api.core.ApiFutureCallback; | ||
import com.google.api.core.ApiFutures; | ||
import com.google.common.base.Preconditions; | ||
import com.google.logging.v2.WriteLogEntriesRequest; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.ScheduledThreadPoolExecutor; | ||
import java.util.concurrent.Semaphore; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
class BatchingWriter { | ||
|
||
interface Rpc { | ||
ApiFuture<Void> call(WriteLogEntriesRequest request); | ||
} | ||
|
||
private final Rpc rpc; | ||
private final int batchSize; | ||
private final Semaphore pending; | ||
private final WriteLogEntriesRequest requestPrototype; | ||
private final ScheduledThreadPoolExecutor executor; | ||
|
||
// Use Boolean, since concurrent maps don't support nulls. | ||
private final ConcurrentHashMap<ApiFuture<Void>, Boolean> pendingWrites = | ||
new ConcurrentHashMap<>(); | ||
|
||
private final ArrayList<com.google.logging.v2.LogEntry> currentBatch; | ||
|
||
private Future<?> flushJob = null; | ||
|
||
BatchingWriter( | ||
Rpc rpc, | ||
int batchSize, | ||
int maxPending, | ||
WriteLogEntriesRequest requestPrototype, | ||
ScheduledThreadPoolExecutor executor) { | ||
Preconditions.checkArgument(batchSize > 0, "batchSize must be positive"); | ||
this.batchSize = batchSize; | ||
Preconditions.checkArgument(maxPending > 0, "maxPending must be positive"); | ||
this.pending = new Semaphore(maxPending); | ||
|
||
this.rpc = Preconditions.checkNotNull(rpc); | ||
this.requestPrototype = Preconditions.checkNotNull(requestPrototype); | ||
this.executor = Preconditions.checkNotNull(executor); | ||
this.currentBatch = new ArrayList<>(batchSize); | ||
} | ||
|
||
synchronized void startJob() { | ||
Preconditions.checkArgument(flushJob == null, "job already started"); | ||
flushJob = | ||
executor.scheduleWithFixedDelay( | ||
new Runnable() { | ||
@Override | ||
public void run() { | ||
initFlush(); | ||
} | ||
}, | ||
100, | ||
100, | ||
TimeUnit.MILLISECONDS); | ||
} | ||
|
||
synchronized void stopJob() { | ||
Preconditions.checkArgument(flushJob != null, "job not started"); | ||
flushJob.cancel(false); | ||
} | ||
|
||
void add(com.google.logging.v2.LogEntry entry) throws InterruptedException { | ||
pending.acquire(1); | ||
synchronized (currentBatch) { | ||
currentBatch.add(entry); | ||
if (currentBatch.size() == batchSize) { | ||
final WriteLogEntriesRequest request = | ||
requestPrototype.toBuilder().addAllEntries(currentBatch).build(); | ||
currentBatch.clear(); | ||
|
||
// Whoever calls send serializes the proto; so we do it off-thread. | ||
// This gives better CPU utilization if there are few producer threads | ||
// on a many-core machine. | ||
executor.execute( | ||
new Runnable() { | ||
@Override | ||
public void run() { | ||
send(request); | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
|
||
void initFlush() { | ||
WriteLogEntriesRequest request; | ||
synchronized (currentBatch) { | ||
request = requestPrototype.toBuilder().addAllEntries(currentBatch).build(); | ||
currentBatch.clear(); | ||
} | ||
send(request); | ||
} | ||
|
||
private void send(WriteLogEntriesRequest request) { | ||
final int count = request.getEntriesCount(); | ||
if (count == 0) { | ||
return; | ||
} | ||
|
||
final ApiFuture<Void> writeFuture = rpc.call(request); | ||
pendingWrites.put(writeFuture, Boolean.TRUE); | ||
ApiFutures.addCallback( | ||
writeFuture, | ||
new ApiFutureCallback<Void>() { | ||
private void onBoth() { | ||
pendingWrites.remove(writeFuture); | ||
pending.release(count); | ||
} | ||
|
||
@Override | ||
public void onSuccess(Void v) { | ||
onBoth(); | ||
} | ||
|
||
@Override | ||
public void onFailure(Throwable t) { | ||
// Report failure. | ||
onBoth(); | ||
} | ||
}); | ||
} | ||
|
||
List<ApiFuture<Void>> pendingRpcs() { | ||
return new ArrayList<>(pendingWrites.keySet()); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
google-cloud-logging/src/test/java/com/google/cloud/logging/BatchingWriterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright 2018 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.google.cloud.logging; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import com.google.api.core.ApiFuture; | ||
import com.google.api.core.SettableApiFuture; | ||
import com.google.logging.v2.WriteLogEntriesRequest; | ||
import java.util.List; | ||
import java.util.concurrent.ScheduledThreadPoolExecutor; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
public class BatchingWriterTest { | ||
|
||
private static final WriteLogEntriesRequest EMPTY_REQUEST = | ||
WriteLogEntriesRequest.newBuilder().build(); | ||
private static final com.google.logging.v2.LogEntry EMPTY_ENTRY = | ||
com.google.logging.v2.LogEntry.newBuilder().build(); | ||
private static ScheduledThreadPoolExecutor EXECUTOR; | ||
|
||
@BeforeClass | ||
public static void beforeClass() { | ||
// Executor is never used. | ||
EXECUTOR = new ScheduledThreadPoolExecutor(1); | ||
EXECUTOR.shutdownNow(); | ||
} | ||
|
||
@Test | ||
public void testEmptyWrite() { | ||
BatchingWriter.Rpc rpc = | ||
new BatchingWriter.Rpc() { | ||
@Override | ||
public ApiFuture<Void> call(WriteLogEntriesRequest request) { | ||
throw new UnsupportedOperationException("should never be called"); | ||
} | ||
}; | ||
|
||
BatchingWriter writer = new BatchingWriter(rpc, 10, 10, EMPTY_REQUEST, EXECUTOR); | ||
writer.initFlush(); | ||
|
||
// If there's no message, there's no RPC. | ||
assertThat(writer.pendingRpcs()).isEmpty(); | ||
} | ||
|
||
@Test | ||
public void testFlush() throws Exception { | ||
final SettableApiFuture<Void> fakeFuture = SettableApiFuture.create(); | ||
BatchingWriter.Rpc rpc = | ||
new BatchingWriter.Rpc() { | ||
@Override | ||
public ApiFuture<Void> call(WriteLogEntriesRequest request) { | ||
return fakeFuture; | ||
} | ||
}; | ||
|
||
BatchingWriter writer = new BatchingWriter(rpc, 10, 10, EMPTY_REQUEST, EXECUTOR); | ||
writer.add(EMPTY_ENTRY); | ||
writer.initFlush(); | ||
|
||
List<ApiFuture<Void>> futures = writer.pendingRpcs(); | ||
assertThat(futures).hasSize(1); | ||
assertThat(futures.get(0).isDone()).isFalse(); | ||
|
||
fakeFuture.set(null); | ||
assertThat(futures.get(0).isDone()).isTrue(); | ||
assertThat(writer.pendingRpcs()).isEmpty(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.