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 8f0ef8e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 16 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,31 @@
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
}
return 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
}

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

Check warning on line 56 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#L56

Added line #L56 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 = processBucketBuilder(bucketIdBuilder, null);
return new AutoValue_RlqsBucketSettings(

Check warning on line 64 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#L63-L64

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

Check warning on line 67 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#L67

Added line #L67 was not covered by tests
}

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

Check warning on line 73 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#L73

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

Check warning on line 76 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#L76

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

Check warning on line 80 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#L79-L80

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

Check warning on line 82 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#L82

Added line #L82 was not covered by tests
}
bucketIdMapBuilder.put(key, value != null ? value : "");
}
ImmutableMap<String, String> bucketIdMap = bucketIdMapBuilder.build();

Check warning on line 86 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#L85-L86

Added lines #L85 - L86 were not covered by tests
return bucketIdMap.isEmpty() ? RlqsBucketId.EMPTY : RlqsBucketId.create(bucketIdMap);
}
}
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 8f0ef8e

Please sign in to comment.