-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Add Support for EnvoyRateLimiter Implementation #37573
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
Open
tarun-google
wants to merge
7
commits into
apache:master
Choose a base branch
from
tarun-google:ratelimiter_impl
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,023
−0
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cc9cdf0
Add EnvoyRateLimiter Implementation
tarun-google 68a8209
Add example
tarun-google 80bb035
fix style check
tarun-google 7dae5fa
simplify teardown
tarun-google b4ca256
add more jitter
tarun-google ce5c509
handle thread interrupt
tarun-google 7163322
add connection keep-alive configs
tarun-google 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
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
131 changes: 131 additions & 0 deletions
131
examples/java/src/main/java/org/apache/beam/examples/RateLimiterSimple.java
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 |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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 org.apache.beam.examples; | ||
|
|
||
| import java.util.stream.Collectors; | ||
| import java.util.stream.IntStream; | ||
| import org.apache.beam.sdk.Pipeline; | ||
| import org.apache.beam.sdk.io.components.ratelimiter.EnvoyRateLimiterContext; | ||
| import org.apache.beam.sdk.io.components.ratelimiter.EnvoyRateLimiterFactory; | ||
| import org.apache.beam.sdk.io.components.ratelimiter.RateLimiter; | ||
| import org.apache.beam.sdk.io.components.ratelimiter.RateLimiterContext; | ||
| import org.apache.beam.sdk.io.components.ratelimiter.RateLimiterFactory; | ||
| import org.apache.beam.sdk.io.components.ratelimiter.RateLimiterOptions; | ||
| import org.apache.beam.sdk.options.Description; | ||
| import org.apache.beam.sdk.options.PipelineOptions; | ||
| import org.apache.beam.sdk.options.PipelineOptionsFactory; | ||
| import org.apache.beam.sdk.transforms.Create; | ||
| import org.apache.beam.sdk.transforms.DoFn; | ||
| import org.apache.beam.sdk.transforms.ParDo; | ||
| import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions; | ||
| import org.checkerframework.checker.nullness.qual.Nullable; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** | ||
| * A simple example demonstrating how to use the {@link RateLimiter} in a custom {@link DoFn}. | ||
| * | ||
| * <p>This pipeline creates a small set of elements and processes them using a DoFn that calls an | ||
| * external service (simulated). The processing is rate-limited using an Envoy Rate Limit Service. | ||
| * | ||
| * <p>To run this example, you need a running Envoy Rate Limit Service. | ||
| */ | ||
| public class RateLimiterSimple { | ||
|
|
||
| public interface Options extends PipelineOptions { | ||
| @Description("Address of the Envoy Rate Limit Service(eg:localhost:8081)") | ||
| String getRateLimiterAddress(); | ||
|
|
||
| void setRateLimiterAddress(String value); | ||
|
|
||
| @Description("Domain for the Rate Limit Service(eg:mydomain)") | ||
| String getRateLimiterDomain(); | ||
|
|
||
| void setRateLimiterDomain(String value); | ||
| } | ||
|
|
||
| static class CallExternalServiceFn extends DoFn<String, String> { | ||
| private final String rlsAddress; | ||
| private final String rlsDomain; | ||
| private transient @Nullable RateLimiter rateLimiter; | ||
| private static final Logger LOG = LoggerFactory.getLogger(CallExternalServiceFn.class); | ||
|
|
||
| public CallExternalServiceFn(String rlsAddress, String rlsDomain) { | ||
| this.rlsAddress = rlsAddress; | ||
| this.rlsDomain = rlsDomain; | ||
| } | ||
|
|
||
| @Setup | ||
| public void setup() { | ||
| // Create the RateLimiterOptions. | ||
| RateLimiterOptions options = RateLimiterOptions.builder().setAddress(rlsAddress).build(); | ||
|
|
||
| // Static RateLimtier with pre-configured domain and descriptors | ||
| RateLimiterFactory factory = new EnvoyRateLimiterFactory(options); | ||
| RateLimiterContext context = | ||
| EnvoyRateLimiterContext.builder() | ||
| .setDomain(rlsDomain) | ||
| .addDescriptor("database", "users") | ||
| .build(); | ||
| this.rateLimiter = factory.getLimiter(context); | ||
| } | ||
|
|
||
| @Teardown | ||
| public void teardown() { | ||
| if (rateLimiter != null) { | ||
| try { | ||
| rateLimiter.close(); | ||
| } catch (Exception e) { | ||
| LOG.warn("Failed to close RateLimiter", e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @ProcessElement | ||
| public void processElement(ProcessContext c) throws Exception { | ||
| String element = c.element(); | ||
| try { | ||
| Preconditions.checkNotNull(rateLimiter).allow(1); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException("Failed to acquire rate limit token", e); | ||
| } | ||
|
|
||
| // Simulate external API call | ||
| LOG.info("Processing: " + element); | ||
| Thread.sleep(100); | ||
| c.output("Processed: " + element); | ||
| } | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| Options options = PipelineOptionsFactory.fromArgs(args).withValidation().as(Options.class); | ||
| Pipeline p = Pipeline.create(options); | ||
|
|
||
| p.apply( | ||
| "CreateItems", | ||
| Create.of( | ||
| IntStream.range(0, 100).mapToObj(i -> "item" + i).collect(Collectors.toList()))) | ||
| .apply( | ||
| "CallExternalService", | ||
| ParDo.of( | ||
| new CallExternalServiceFn( | ||
| options.getRateLimiterAddress(), options.getRateLimiterDomain()))); | ||
|
|
||
| p.run().waitUntilFinish(); | ||
| } | ||
| } | ||
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
46 changes: 46 additions & 0 deletions
46
...ponents/src/main/java/org/apache/beam/sdk/io/components/ratelimiter/EnvoyRateLimiter.java
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 |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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 org.apache.beam.sdk.io.components.ratelimiter; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| /** | ||
| * A lightweight handle for an Envoy-based rate limiter. | ||
| * | ||
| * <p>Delegates work to the {@link EnvoyRateLimiterFactory} using the baked-in {@link | ||
| * EnvoyRateLimiterContext}. | ||
| */ | ||
| public class EnvoyRateLimiter implements RateLimiter { | ||
| private final EnvoyRateLimiterFactory factory; | ||
| private final EnvoyRateLimiterContext context; | ||
|
|
||
| public EnvoyRateLimiter(EnvoyRateLimiterFactory factory, EnvoyRateLimiterContext context) { | ||
| this.factory = factory; | ||
| this.context = context; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean allow(int permits) throws IOException, InterruptedException { | ||
| return factory.allow(context, permits); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() throws Exception { | ||
| factory.close(); | ||
| } | ||
| } |
65 changes: 65 additions & 0 deletions
65
.../src/main/java/org/apache/beam/sdk/io/components/ratelimiter/EnvoyRateLimiterContext.java
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 |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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 org.apache.beam.sdk.io.components.ratelimiter; | ||
|
|
||
| import com.google.auto.value.AutoValue; | ||
| import java.util.Map; | ||
| import org.apache.beam.sdk.schemas.AutoValueSchema; | ||
| import org.apache.beam.sdk.schemas.annotations.DefaultSchema; | ||
| import org.apache.beam.sdk.schemas.annotations.SchemaFieldDescription; | ||
| import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableMap; | ||
| import org.checkerframework.checker.nullness.qual.NonNull; | ||
|
|
||
| /** | ||
| * Context for an Envoy Rate Limiter call. | ||
| * | ||
| * <p>Contains the domain and descriptors required to define a specific rate limit bucket. | ||
| */ | ||
| @DefaultSchema(AutoValueSchema.class) | ||
| @AutoValue | ||
| public abstract class EnvoyRateLimiterContext implements RateLimiterContext { | ||
|
|
||
| @SchemaFieldDescription("Domain of the rate limiter.") | ||
| public abstract String getDomain(); | ||
|
|
||
| @SchemaFieldDescription("Descriptors for the rate limiter.") | ||
| public abstract ImmutableMap<String, String> getDescriptors(); | ||
|
|
||
| public static Builder builder() { | ||
| return new AutoValue_EnvoyRateLimiterContext.Builder(); | ||
| } | ||
|
|
||
| @AutoValue.Builder | ||
| public abstract static class Builder { | ||
| public abstract Builder setDomain(@NonNull String domain); | ||
|
|
||
| public abstract ImmutableMap.Builder<String, String> descriptorsBuilder(); | ||
|
|
||
| public Builder addDescriptor(@NonNull String key, @NonNull String value) { | ||
| descriptorsBuilder().put(key, value); | ||
| return this; | ||
| } | ||
|
|
||
| public Builder setDescriptors(@NonNull Map<String, String> descriptors) { | ||
| descriptorsBuilder().putAll(descriptors); | ||
| return this; | ||
| } | ||
|
|
||
| public abstract EnvoyRateLimiterContext build(); | ||
| } | ||
| } |
Oops, something went wrong.
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.