Skip to content
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
build

.idea

settings-gradle.lockfile
4 changes: 2 additions & 2 deletions grpc-circuitbreaker-utils/gradle.lockfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ io.grpc:grpc-api:1.75.0=compileClasspath,runtimeClasspath,testCompileClasspath,t
io.grpc:grpc-bom:1.75.0=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
io.grpc:grpc-context:1.75.0=runtimeClasspath,testRuntimeClasspath
io.grpc:grpc-core:1.75.0=runtimeClasspath,testRuntimeClasspath
io.netty:netty-bom:4.1.130.Final=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
io.netty:netty-bom:4.1.132.Final=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
io.perfmark:perfmark-api:0.27.0=runtimeClasspath,testRuntimeClasspath
io.vavr:vavr-match:0.10.2=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
io.vavr:vavr:0.10.2=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
Expand All @@ -39,7 +39,7 @@ org.checkerframework:checker-qual:3.33.0=compileClasspath,testCompileClasspath
org.checkerframework:checker-qual:3.43.0=runtimeClasspath,testRuntimeClasspath
org.codehaus.mojo:animal-sniffer-annotations:1.24=runtimeClasspath,testRuntimeClasspath
org.eclipse.jetty:jetty-bom:11.0.26=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.hypertrace.bom:hypertrace-bom:0.3.73=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.hypertrace.bom:hypertrace-bom:0.3.76=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.hypertrace.core.kafkastreams.framework:kafka-bom:0.6.4=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.junit.jupiter:junit-jupiter-api:5.10.0=testCompileClasspath
org.junit.jupiter:junit-jupiter-api:5.10.1=testRuntimeClasspath
Expand Down
4 changes: 2 additions & 2 deletions grpc-client-rx-utils/gradle.lockfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ io.grpc:grpc-bom:1.75.0=compileClasspath,runtimeClasspath,testCompileClasspath,t
io.grpc:grpc-context:1.75.0=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
io.grpc:grpc-core:1.75.0=runtimeClasspath,testRuntimeClasspath
io.grpc:grpc-stub:1.75.0=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
io.netty:netty-bom:4.1.130.Final=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
io.netty:netty-bom:4.1.132.Final=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
io.perfmark:perfmark-api:0.27.0=runtimeClasspath,testRuntimeClasspath
io.reactivex.rxjava3:rxjava:3.1.7=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
net.bytebuddy:byte-buddy-agent:1.14.10=testCompileClasspath,testRuntimeClasspath
Expand All @@ -30,7 +30,7 @@ org.apiguardian:apiguardian-api:1.1.2=testCompileClasspath
org.checkerframework:checker-qual:3.43.0=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.codehaus.mojo:animal-sniffer-annotations:1.24=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.eclipse.jetty:jetty-bom:11.0.26=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.hypertrace.bom:hypertrace-bom:0.3.73=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.hypertrace.bom:hypertrace-bom:0.3.76=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.hypertrace.core.kafkastreams.framework:kafka-bom:0.6.4=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.junit.jupiter:junit-jupiter-api:5.10.0=testCompileClasspath
org.junit.jupiter:junit-jupiter-api:5.10.1=testRuntimeClasspath
Expand Down
2 changes: 2 additions & 0 deletions grpc-client-utils/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ dependencies {
implementation(projects.grpcContextUtils)
implementation(commonLibs.slf4j2.api)
implementation(commonLibs.grpc.core)
implementation(commonLibs.protobuf.javautil)

annotationProcessor(commonLibs.lombok)
compileOnly(commonLibs.lombok)

testImplementation(commonLibs.junit.jupiter)
testImplementation(commonLibs.mockito.core)
testImplementation(commonLibs.grpc.stub)
testRuntimeOnly(commonLibs.grpc.netty)
}

Expand Down
51 changes: 29 additions & 22 deletions grpc-client-utils/gradle.lockfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,47 @@ com.fasterxml.jackson.core:jackson-databind:2.21.1=runtimeClasspath,testRuntimeC
com.fasterxml.jackson:jackson-bom:2.21.1=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.android:annotations:4.1.1.4=runtimeClasspath,testRuntimeClasspath
com.google.code.findbugs:jsr305:3.0.2=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.code.gson:gson:2.13.1=runtimeClasspath,testRuntimeClasspath
com.google.errorprone:error_prone_annotations:2.30.0=compileClasspath,testCompileClasspath
com.google.errorprone:error_prone_annotations:2.38.0=runtimeClasspath,testRuntimeClasspath
com.google.guava:failureaccess:1.0.2=runtimeClasspath,testRuntimeClasspath
com.google.guava:guava:33.3.1-android=runtimeClasspath,testRuntimeClasspath
com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava=runtimeClasspath,testRuntimeClasspath
com.google.j2objc:j2objc-annotations:3.0.0=runtimeClasspath,testRuntimeClasspath
com.google.code.gson:gson:2.13.1=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.errorprone:error_prone_annotations:2.38.0=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.guava:failureaccess:1.0.1=compileClasspath
com.google.guava:failureaccess:1.0.2=runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.guava:guava-parent:32.1.2-jre=compileClasspath
com.google.guava:guava:32.1.2-jre=compileClasspath
com.google.guava:guava:33.3.1-android=runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.j2objc:j2objc-annotations:2.8=compileClasspath
com.google.j2objc:j2objc-annotations:3.0.0=runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.protobuf:protobuf-java-util:3.25.8=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.protobuf:protobuf-java:3.25.8=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
io.grpc:grpc-api:1.75.0=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
io.grpc:grpc-bom:1.75.0=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
io.grpc:grpc-context:1.75.0=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
io.grpc:grpc-core:1.75.0=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
io.grpc:grpc-inprocess:1.75.0=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
io.grpc:grpc-netty:1.75.0=testRuntimeClasspath
io.grpc:grpc-stub:1.75.0=testCompileClasspath,testRuntimeClasspath
io.grpc:grpc-util:1.75.0=testRuntimeClasspath
io.netty:netty-bom:4.1.130.Final=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
io.netty:netty-buffer:4.1.130.Final=testRuntimeClasspath
io.netty:netty-codec-http2:4.1.130.Final=testRuntimeClasspath
io.netty:netty-codec-http:4.1.130.Final=testRuntimeClasspath
io.netty:netty-codec-socks:4.1.130.Final=testRuntimeClasspath
io.netty:netty-codec:4.1.130.Final=testRuntimeClasspath
io.netty:netty-common:4.1.130.Final=testRuntimeClasspath
io.netty:netty-handler-proxy:4.1.130.Final=testRuntimeClasspath
io.netty:netty-handler:4.1.130.Final=testRuntimeClasspath
io.netty:netty-resolver:4.1.130.Final=testRuntimeClasspath
io.netty:netty-transport-native-unix-common:4.1.130.Final=testRuntimeClasspath
io.netty:netty-transport:4.1.130.Final=testRuntimeClasspath
io.netty:netty-bom:4.1.132.Final=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
io.netty:netty-buffer:4.1.132.Final=testRuntimeClasspath
io.netty:netty-codec-http2:4.1.132.Final=testRuntimeClasspath
io.netty:netty-codec-http:4.1.132.Final=testRuntimeClasspath
io.netty:netty-codec-socks:4.1.132.Final=testRuntimeClasspath
io.netty:netty-codec:4.1.132.Final=testRuntimeClasspath
io.netty:netty-common:4.1.132.Final=testRuntimeClasspath
io.netty:netty-handler-proxy:4.1.132.Final=testRuntimeClasspath
io.netty:netty-handler:4.1.132.Final=testRuntimeClasspath
io.netty:netty-resolver:4.1.132.Final=testRuntimeClasspath
io.netty:netty-transport-native-unix-common:4.1.132.Final=testRuntimeClasspath
io.netty:netty-transport:4.1.132.Final=testRuntimeClasspath
io.perfmark:perfmark-api:0.27.0=runtimeClasspath,testRuntimeClasspath
net.bytebuddy:byte-buddy-agent:1.14.10=testCompileClasspath,testRuntimeClasspath
net.bytebuddy:byte-buddy:1.14.10=testCompileClasspath,testRuntimeClasspath
org.apiguardian:apiguardian-api:1.1.2=testCompileClasspath
org.checkerframework:checker-qual:3.43.0=runtimeClasspath,testRuntimeClasspath
org.codehaus.mojo:animal-sniffer-annotations:1.24=runtimeClasspath,testRuntimeClasspath
org.checkerframework:checker-qual:3.33.0=compileClasspath
org.checkerframework:checker-qual:3.43.0=runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.codehaus.mojo:animal-sniffer-annotations:1.24=runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.eclipse.jetty:jetty-bom:11.0.26=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.hypertrace.bom:hypertrace-bom:0.3.73=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.hypertrace.bom:hypertrace-bom:0.3.76=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.hypertrace.core.kafkastreams.framework:kafka-bom:0.6.4=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.junit.jupiter:junit-jupiter-api:5.10.0=testCompileClasspath,testRuntimeClasspath
org.junit.jupiter:junit-jupiter-engine:5.10.0=testRuntimeClasspath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ public class GrpcChannelConfig {
Integer maxInboundMessageSize;

@Singular List<ClientInterceptor> clientInterceptors;

GrpcServiceConfig serviceConfig;

boolean enableRetry;
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ protected ManagedChannel configureAndBuildChannel(
if (config.getMaxInboundMessageSize() != null) {
builder.maxInboundMessageSize(config.getMaxInboundMessageSize());
}
if (config.getServiceConfig() != null) {
builder.defaultServiceConfig(config.getServiceConfig().toMap());
}
if (config.isEnableRetry()) {
builder.enableRetry();
}
this.registryConfig.getDefaultInterceptors().forEach(builder::intercept);
return builder.intercept(config.getClientInterceptors()).build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.hypertrace.core.grpcutils.client;

import static java.util.stream.Collectors.toUnmodifiableList;

import com.google.protobuf.util.Durations;
import io.grpc.Status;
import java.time.Duration;
import java.util.List;
import java.util.Map;
import lombok.Builder;
import lombok.Singular;
import lombok.Value;

@Value
@Builder
public class GrpcRetryPolicy {
private static final String MAX_ATTEMPTS = "maxAttempts";
private static final String INITIAL_BACKOFF = "initialBackoff";
private static final String MAX_BACKOFF = "maxBackoff";
private static final String BACKOFF_MULTIPLIER = "backoffMultiplier";
private static final String RETRYABLE_STATUS_CODES = "retryableStatusCodes";

int maxAttempts;
Duration initialBackoff;
Duration maxBackoff;
double backoffMultiplier;
@Singular List<Status.Code> retryableStatusCodes;

Map<String, Object> toMap() {
return Map.of(
MAX_ATTEMPTS, (double) maxAttempts,
INITIAL_BACKOFF, Durations.toString(Durations.fromMillis(initialBackoff.toMillis())),
MAX_BACKOFF, Durations.toString(Durations.fromMillis(maxBackoff.toMillis())),
BACKOFF_MULTIPLIER, backoffMultiplier,
RETRYABLE_STATUS_CODES,
retryableStatusCodes.stream().map(Enum::name).collect(toUnmodifiableList()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.hypertrace.core.grpcutils.client;

import java.util.List;
import java.util.Map;
import lombok.Builder;
import lombok.Value;

@Value
@Builder
public class GrpcServiceConfig {
private static final String METHOD_CONFIG = "methodConfig";
private static final String NAME = "name";
private static final String RETRY_POLICY = "retryPolicy";

GrpcRetryPolicy retryPolicy;

Map<String, Object> toMap() {
return Map.of(
METHOD_CONFIG, List.of(Map.of(NAME, List.of(Map.of()), RETRY_POLICY, retryPolicy.toMap())));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package org.hypertrace.core.grpcutils.client;

import static org.junit.jupiter.api.Assertions.assertEquals;

import io.grpc.CallOptions;
import io.grpc.ManagedChannel;
import io.grpc.MethodDescriptor;
import io.grpc.MethodDescriptor.MethodType;
import io.grpc.Server;
import io.grpc.ServerServiceDefinition;
import io.grpc.Status;
import io.grpc.inprocess.InProcessServerBuilder;
import io.grpc.stub.ClientCalls;
import io.grpc.stub.ServerCalls;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.time.Duration;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.Test;

class GrpcChannelRegistryRetryTest {

@Test
void retriesOnUnavailableWhenRetryPolicyConfigured() throws Exception {
String serverName = UUID.randomUUID().toString();
AtomicInteger callCount = new AtomicInteger(0);
MethodDescriptor<byte[], byte[]> method =
MethodDescriptor.<byte[], byte[]>newBuilder()
.setType(MethodType.UNARY)
.setFullMethodName("test.Service/Call")
.setRequestMarshaller(inputStreamMarshaller())
.setResponseMarshaller(inputStreamMarshaller())
.build();

// Fail first 2 attempts with UNAVAILABLE, succeed on 3rd
Server server =
InProcessServerBuilder.forName(serverName)
.addService(
ServerServiceDefinition.builder("test.Service")
.addMethod(
method,
ServerCalls.asyncUnaryCall(
(request, responseObserver) -> {
if (callCount.incrementAndGet() < 3) {
responseObserver.onError(Status.UNAVAILABLE.asRuntimeException());
} else {
responseObserver.onNext(new byte[0]);
responseObserver.onCompleted();
}
}))
.build())
.build()
.start();

GrpcServiceConfig serviceConfig =
GrpcServiceConfig.builder()
.retryPolicy(
GrpcRetryPolicy.builder()
.maxAttempts(3)
.initialBackoff(Duration.ofMillis(10))
.maxBackoff(Duration.ofMillis(100))
.backoffMultiplier(2.0)
.retryableStatusCode(Status.Code.UNAVAILABLE)
.build())
.build();

InProcessGrpcChannelRegistry registry = new InProcessGrpcChannelRegistry();
ManagedChannel channel =
registry.forName(
serverName,
GrpcChannelConfig.builder().serviceConfig(serviceConfig).enableRetry(true).build());

ClientCalls.blockingUnaryCall(channel, method, CallOptions.DEFAULT, new byte[0]);

assertEquals(3, callCount.get());
server.shutdown();
registry.shutdown();
}

private static MethodDescriptor.Marshaller<byte[]> inputStreamMarshaller() {
return new MethodDescriptor.Marshaller<>() {
@Override
public InputStream stream(byte[] value) {
return new ByteArrayInputStream(value);
}

@Override
public byte[] parse(InputStream stream) {
return new byte[0];
}
};
}
}
4 changes: 2 additions & 2 deletions grpc-context-utils/gradle.lockfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ io.grpc:grpc-api:1.75.0=compileClasspath,runtimeClasspath,testCompileClasspath,t
io.grpc:grpc-bom:1.75.0=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
io.grpc:grpc-context:1.75.0=runtimeClasspath,testRuntimeClasspath
io.grpc:grpc-core:1.75.0=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
io.netty:netty-bom:4.1.130.Final=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
io.netty:netty-bom:4.1.132.Final=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
io.perfmark:perfmark-api:0.27.0=runtimeClasspath,testRuntimeClasspath
net.bytebuddy:byte-buddy-agent:1.14.10=testCompileClasspath,testRuntimeClasspath
net.bytebuddy:byte-buddy:1.14.10=testCompileClasspath,testRuntimeClasspath
Expand All @@ -33,7 +33,7 @@ org.checkerframework:checker-qual:3.33.0=compileClasspath,testCompileClasspath
org.checkerframework:checker-qual:3.43.0=runtimeClasspath,testRuntimeClasspath
org.codehaus.mojo:animal-sniffer-annotations:1.24=runtimeClasspath,testRuntimeClasspath
org.eclipse.jetty:jetty-bom:11.0.26=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.hypertrace.bom:hypertrace-bom:0.3.73=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.hypertrace.bom:hypertrace-bom:0.3.76=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.hypertrace.core.kafkastreams.framework:kafka-bom:0.6.4=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.junit.jupiter:junit-jupiter-api:5.10.0=testCompileClasspath
org.junit.jupiter:junit-jupiter-api:5.10.1=testRuntimeClasspath
Expand Down
4 changes: 2 additions & 2 deletions grpc-server-rx-utils/gradle.lockfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ com.google.j2objc:j2objc-annotations:3.0.0=compileClasspath,runtimeClasspath,tes
io.grpc:grpc-api:1.75.0=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
io.grpc:grpc-bom:1.75.0=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
io.grpc:grpc-stub:1.75.0=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
io.netty:netty-bom:4.1.130.Final=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
io.netty:netty-bom:4.1.132.Final=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
io.reactivex.rxjava3:rxjava:3.1.7=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
net.bytebuddy:byte-buddy-agent:1.14.10=testCompileClasspath,testRuntimeClasspath
net.bytebuddy:byte-buddy:1.14.10=testCompileClasspath,testRuntimeClasspath
org.apiguardian:apiguardian-api:1.1.2=testCompileClasspath
org.checkerframework:checker-qual:3.43.0=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.codehaus.mojo:animal-sniffer-annotations:1.24=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.eclipse.jetty:jetty-bom:11.0.26=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.hypertrace.bom:hypertrace-bom:0.3.73=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.hypertrace.bom:hypertrace-bom:0.3.76=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.hypertrace.core.kafkastreams.framework:kafka-bom:0.6.4=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.junit.jupiter:junit-jupiter-api:5.10.0=testCompileClasspath
org.junit.jupiter:junit-jupiter-api:5.10.1=testRuntimeClasspath
Expand Down
Loading
Loading