Skip to content

Commit

Permalink
Dynamic bucket id builder processing initial logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiitk committed Oct 3, 2024
1 parent b49a7b0 commit a9a8f66
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 15 deletions.
22 changes: 14 additions & 8 deletions xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsBucketId.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,25 @@

@AutoValue
public abstract class RlqsBucketId {

Check warning on line 26 in xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsBucketId.java

View check run for this annotation

Codecov / codecov/patch

xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsBucketId.java#L26

Added line #L26 was not covered by tests
// No class loading deadlock, see
// https://github.com/google/error-prone/issues/2062#issuecomment-1566253739
public static final RlqsBucketId EMPTY = create(ImmutableMap.of());

Check warning on line 29 in xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsBucketId.java

View check run for this annotation

Codecov / codecov/patch

xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsBucketId.java#L29

Added line #L29 was not covered by tests

public abstract ImmutableMap<String, String> bucketId();

public static RlqsBucketId create(ImmutableMap<String, String> bucketId) {
return new AutoValue_RlqsBucketId(bucketId);
public static RlqsBucketId create(Map<String, String> bucketIdMap) {
if (bucketIdMap.isEmpty()) {
return EMPTY;

Check warning on line 35 in xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsBucketId.java

View check run for this annotation

Codecov / codecov/patch

xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsBucketId.java#L35

Added line #L35 was not covered by tests
}
return new AutoValue_RlqsBucketId(ImmutableMap.copyOf(bucketIdMap));

Check warning on line 37 in xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsBucketId.java

View check run for this annotation

Codecov / codecov/patch

xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsBucketId.java#L37

Added line #L37 was not covered by tests
}

public static RlqsBucketId fromEnvoyProto(BucketId envoyProto) {
ImmutableMap.Builder<String, String> bucketId = ImmutableMap.builder();
for (Map.Entry<String, String> entry : envoyProto.getBucketMap().entrySet()) {
bucketId.put(entry.getKey(), entry.getValue());
}
return RlqsBucketId.create(bucketId.build());
public final boolean isEmpty() {
return bucketId().isEmpty();

Check warning on line 41 in xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsBucketId.java

View check run for this annotation

Codecov / codecov/patch

xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsBucketId.java#L41

Added line #L41 was not covered by tests
}

public static RlqsBucketId fromEnvoyProto(BucketId envoyProto) {
return RlqsBucketId.create(ImmutableMap.copyOf(envoyProto.getBucketMap().entrySet()));

Check warning on line 45 in xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsBucketId.java

View check run for this annotation

Codecov / codecov/patch

xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsBucketId.java#L45

Added line #L45 was not covered by tests
}

@Memoized
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,32 @@
package io.grpc.xds.internal.rlqs;

import com.google.auto.value.AutoValue;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableMap;
import com.google.protobuf.Duration;
import com.google.protobuf.util.Durations;
import io.grpc.xds.internal.datatype.RateLimitStrategy;
import io.grpc.xds.internal.matchers.HttpMatchInput;
import io.grpc.xds.internal.rlqs.RlqsRateLimitResult.DenyResponse;
import java.util.function.Function;
import javax.annotation.Nullable;

@AutoValue
public abstract class RlqsBucketSettings {

Check warning on line 30 in xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsBucketSettings.java

View check run for this annotation

Codecov / codecov/patch

xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsBucketSettings.java#L30

Added line #L30 was not covered by tests
// TODO(sergiitk): [IMPL] this misses most of the parsing and implementation.

@Nullable
public abstract ImmutableMap<String, Function<HttpMatchInput, String>> bucketIdBuilder();

public RlqsBucketId toBucketId(HttpMatchInput input) {
return null;
abstract RlqsBucketId staticBucketId();

public abstract long reportingIntervalMillis();

public final RlqsBucketId toBucketId(HttpMatchInput input) {
if (bucketIdBuilder() == null) {
return staticBucketId();

Check warning on line 42 in xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsBucketSettings.java

View check run for this annotation

Codecov / codecov/patch

xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsBucketSettings.java#L42

Added line #L42 was not covered by tests
}
ImmutableMap<String, String> bucketMap = processBucketBuilder(bucketIdBuilder(), input);

Check warning on line 44 in xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsBucketSettings.java

View check run for this annotation

Codecov / codecov/patch

xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsBucketSettings.java#L44

Added line #L44 was not covered by tests
return bucketMap != null ? RlqsBucketId.create(bucketMap) : RlqsBucketId.EMPTY;
}

public RateLimitStrategy noAssignmentStrategy() {
Expand All @@ -47,11 +57,40 @@ public RateLimitStrategy expiredAssignmentStrategy() {
return null;

Check warning on line 57 in xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsBucketSettings.java

View check run for this annotation

Codecov / codecov/patch

xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsBucketSettings.java#L57

Added line #L57 was not covered by tests
}

public abstract long reportingIntervalMillis();

public static RlqsBucketSettings create(
ImmutableMap<String, Function<HttpMatchInput, String>> bucketIdBuilder,
Duration reportingInterval) {
return new AutoValue_RlqsBucketSettings(bucketIdBuilder, Durations.toMillis(reportingInterval));
// TODO(sergiitk): instead of create, use Builder pattern.
RlqsBucketId staticBucketId = generateStaticId(bucketIdBuilder);
return new AutoValue_RlqsBucketSettings(

Check warning on line 66 in xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsBucketSettings.java

View check run for this annotation

Codecov / codecov/patch

xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsBucketSettings.java#L65-L66

Added lines #L65 - L66 were not covered by tests
staticBucketId.isEmpty() ? bucketIdBuilder : null,
staticBucketId,
Durations.toMillis(reportingInterval));

Check warning on line 69 in xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsBucketSettings.java

View check run for this annotation

Codecov / codecov/patch

xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsBucketSettings.java#L69

Added line #L69 was not covered by tests
}

private static RlqsBucketId generateStaticId(
ImmutableMap<String, Function<HttpMatchInput, String>> bucketIdBuilder) {
ImmutableMap<String, String> bucketIdMap = processBucketBuilder(bucketIdBuilder, null);

Check warning on line 74 in xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsBucketSettings.java

View check run for this annotation

Codecov / codecov/patch

xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsBucketSettings.java#L74

Added line #L74 was not covered by tests
return bucketIdMap.isEmpty() ? RlqsBucketId.EMPTY : RlqsBucketId.create(bucketIdMap);
}

private static ImmutableMap<String, String> processBucketBuilder(
ImmutableMap<String, Function<HttpMatchInput, String>> bucketIdBuilder,
HttpMatchInput input) {
ImmutableMap.Builder<String, String> result = ImmutableMap.builder();

Check warning on line 81 in xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsBucketSettings.java

View check run for this annotation

Codecov / codecov/patch

xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsBucketSettings.java#L81

Added line #L81 was not covered by tests
if (input == null) {
// TODO(sergiitk): [IMPL] calculate static map
return result.build();

Check warning on line 84 in xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsBucketSettings.java

View check run for this annotation

Codecov / codecov/patch

xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsBucketSettings.java#L84

Added line #L84 was not covered by tests
}
for (String key : bucketIdBuilder.keySet()) {
Function<HttpMatchInput, String> fn = bucketIdBuilder.get(key);
String value = null;

Check warning on line 88 in xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsBucketSettings.java

View check run for this annotation

Codecov / codecov/patch

xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsBucketSettings.java#L87-L88

Added lines #L87 - L88 were not covered by tests
if (fn != null) {
value = fn.apply(input);

Check warning on line 90 in xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsBucketSettings.java

View check run for this annotation

Codecov / codecov/patch

xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsBucketSettings.java#L90

Added line #L90 was not covered by tests
}
result.put(key, value != null ? value : "");
}
return result.build();

Check warning on line 94 in xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsBucketSettings.java

View check run for this annotation

Codecov / codecov/patch

xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsBucketSettings.java#L93-L94

Added lines #L93 - L94 were not covered by tests
}
}
4 changes: 2 additions & 2 deletions xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public RlqsEngine(
public RlqsRateLimitResult rateLimit(HttpMatchInput input) {
RlqsBucketSettings bucketSettings = bucketMatchers.match(input);
RlqsBucketId bucketId = bucketSettings.toBucketId(input);

Check warning on line 58 in xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsEngine.java

View check run for this annotation

Codecov / codecov/patch

xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsEngine.java#L57-L58

Added lines #L57 - L58 were not covered by tests
// Special case when bucket id builder not set.
if (bucketId == null) {
// Special case when bucket id builder not set, or has no values.
if (bucketId.isEmpty()) {
return rateLimitWithoutReports(bucketSettings);

Check warning on line 61 in xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsEngine.java

View check run for this annotation

Codecov / codecov/patch

xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsEngine.java#L61

Added line #L61 was not covered by tests
}
RlqsBucket bucket = bucketCache.getOrCreate(bucketId, bucketSettings, newBucket -> {

Check warning on line 63 in xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsEngine.java

View check run for this annotation

Codecov / codecov/patch

xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsEngine.java#L63

Added line #L63 was not covered by tests
Expand Down

0 comments on commit a9a8f66

Please sign in to comment.