-
Notifications
You must be signed in to change notification settings - Fork 3
fix: chunk down flag assigned to not hit the max grpc limit #310
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| package com.spotify.confidence; | ||
|
|
||
| import com.google.common.annotations.VisibleForTesting; | ||
| import com.spotify.confidence.shaded.flags.resolver.v1.InternalFlagLoggerServiceGrpc; | ||
| import com.spotify.confidence.shaded.flags.resolver.v1.WriteFlagLogsRequest; | ||
| import com.spotify.confidence.shaded.iam.v1.AuthServiceGrpc; | ||
|
|
@@ -8,14 +9,41 @@ | |
| import io.grpc.ManagedChannel; | ||
| import io.grpc.ManagedChannelBuilder; | ||
| import java.time.Duration; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| @FunctionalInterface | ||
| interface FlagLogWriter { | ||
| void write(WriteFlagLogsRequest request); | ||
| } | ||
|
|
||
| public class GrpcWasmFlagLogger implements WasmFlagLogger { | ||
| private static final String CONFIDENCE_DOMAIN = "edge-grpc.spotify.com"; | ||
| private static final Logger logger = LoggerFactory.getLogger(GrpcWasmFlagLogger.class); | ||
| // Max number of flag_assigned entries per chunk to avoid exceeding gRPC max message size | ||
| private static final int MAX_FLAG_ASSIGNED_PER_CHUNK = 1000; | ||
| private final InternalFlagLoggerServiceGrpc.InternalFlagLoggerServiceBlockingStub stub; | ||
| private final ExecutorService executorService; | ||
| private final FlagLogWriter writer; | ||
|
|
||
| @VisibleForTesting | ||
| public GrpcWasmFlagLogger(ApiSecret apiSecret, FlagLogWriter writer) { | ||
| final var channel = createConfidenceChannel(); | ||
| final AuthServiceGrpc.AuthServiceBlockingStub authService = | ||
| AuthServiceGrpc.newBlockingStub(channel); | ||
| final TokenHolder tokenHolder = | ||
| new TokenHolder(apiSecret.clientId(), apiSecret.clientSecret(), authService); | ||
| final Channel authenticatedChannel = | ||
| ClientInterceptors.intercept(channel, new JwtAuthClientInterceptor(tokenHolder)); | ||
| this.stub = InternalFlagLoggerServiceGrpc.newBlockingStub(authenticatedChannel); | ||
| this.executorService = Executors.newCachedThreadPool(); | ||
| this.writer = writer; | ||
| } | ||
|
|
||
| public GrpcWasmFlagLogger(ApiSecret apiSecret) { | ||
| final var channel = createConfidenceChannel(); | ||
|
|
@@ -26,15 +54,88 @@ public GrpcWasmFlagLogger(ApiSecret apiSecret) { | |
| final Channel authenticatedChannel = | ||
| ClientInterceptors.intercept(channel, new JwtAuthClientInterceptor(tokenHolder)); | ||
| this.stub = InternalFlagLoggerServiceGrpc.newBlockingStub(authenticatedChannel); | ||
| this.executorService = Executors.newCachedThreadPool(); | ||
| this.writer = | ||
| request -> | ||
| executorService.submit( | ||
| () -> { | ||
| try { | ||
| final var ignore = stub.writeFlagLogs(request); | ||
| logger.debug( | ||
| "Successfully sent flag log with {} entries", | ||
| request.getFlagAssignedCount()); | ||
| } catch (Exception e) { | ||
| logger.error("Failed to write flag logs", e); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public void write(WriteFlagLogsRequest request) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given that this now contains a lot more logic I think we should write tests for it. |
||
| if (request.getClientResolveInfoList().isEmpty() && request.getFlagAssignedList().isEmpty()) { | ||
| if (request.getClientResolveInfoList().isEmpty() | ||
| && request.getFlagAssignedList().isEmpty() | ||
| && request.getFlagResolveInfoList().isEmpty()) { | ||
| logger.debug("Skipping empty flag log request"); | ||
| return; | ||
| } | ||
| final var ignore = stub.writeFlagLogs(request); | ||
|
|
||
| final int flagAssignedCount = request.getFlagAssignedCount(); | ||
|
|
||
| // If flag_assigned list is small enough, send everything as-is | ||
| if (flagAssignedCount <= MAX_FLAG_ASSIGNED_PER_CHUNK) { | ||
| sendAsync(request); | ||
| return; | ||
| } | ||
|
|
||
| // Split flag_assigned into chunks and send each chunk asynchronously | ||
| logger.debug( | ||
| "Splitting {} flag_assigned entries into chunks of {}", | ||
| flagAssignedCount, | ||
| MAX_FLAG_ASSIGNED_PER_CHUNK); | ||
|
|
||
| final List<WriteFlagLogsRequest> chunks = createFlagAssignedChunks(request); | ||
| for (WriteFlagLogsRequest chunk : chunks) { | ||
| sendAsync(chunk); | ||
| } | ||
| } | ||
|
|
||
| private List<WriteFlagLogsRequest> createFlagAssignedChunks(WriteFlagLogsRequest request) { | ||
| final List<WriteFlagLogsRequest> chunks = new ArrayList<>(); | ||
| final int totalFlags = request.getFlagAssignedCount(); | ||
|
|
||
| for (int i = 0; i < totalFlags; i += MAX_FLAG_ASSIGNED_PER_CHUNK) { | ||
| final int end = Math.min(i + MAX_FLAG_ASSIGNED_PER_CHUNK, totalFlags); | ||
| final WriteFlagLogsRequest.Builder chunkBuilder = | ||
| WriteFlagLogsRequest.newBuilder() | ||
| .addAllFlagAssigned(request.getFlagAssignedList().subList(i, end)); | ||
|
|
||
| // Include telemetry and resolve info only in the first chunk | ||
| if (i == 0) { | ||
| if (request.hasTelemetryData()) { | ||
| chunkBuilder.setTelemetryData(request.getTelemetryData()); | ||
| } | ||
| chunkBuilder | ||
| .addAllClientResolveInfo(request.getClientResolveInfoList()) | ||
| .addAllFlagResolveInfo(request.getFlagResolveInfoList()); | ||
nicklasl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| chunks.add(chunkBuilder.build()); | ||
| } | ||
|
|
||
| return chunks; | ||
| } | ||
|
|
||
| private void sendAsync(WriteFlagLogsRequest request) { | ||
| writer.write(request); | ||
| } | ||
|
|
||
| /** | ||
| * Shutdown the executor service. This will allow any pending async writes to complete. Call this | ||
| * when the application is shutting down. | ||
| */ | ||
| @Override | ||
| public void shutdown() { | ||
| executorService.shutdown(); | ||
| } | ||
|
|
||
| private static ManagedChannel createConfidenceChannel() { | ||
|
|
||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.