Skip to content
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

kafka-broker-dispatcher rate limiter #4208

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
16 changes: 16 additions & 0 deletions control-plane/pkg/reconciler/trigger/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"strings"
"sync"
"strconv"

"go.uber.org/zap"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -53,6 +54,7 @@ import (

const (
deliveryOrderAnnotation = "kafka.eventing.knative.dev/delivery.order"
triggerVReplicasAnnotation = "kafka.eventing.knative.dev/vreplicas"
)

type FlagsHolder struct {
Expand Down Expand Up @@ -325,6 +327,10 @@ func (r *Reconciler) reconcileTriggerEgress(ctx context.Context, broker *eventin
Namespace: trigger.GetNamespace(),
Name: trigger.GetName(),
},
FeatureFlags: &contract.EgressFeatureFlags{
EnableRateLimiter: r.KafkaFeatureFlags.IsDispatcherRateLimiterEnabled(),
EnableOrderedExecutorMetrics: r.KafkaFeatureFlags.IsDispatcherOrderedExecutorMetricsEnabled(),
},
}

if destination.CACerts != nil {
Expand Down Expand Up @@ -371,6 +377,16 @@ func (r *Reconciler) reconcileTriggerEgress(ctx context.Context, broker *eventin
egress.DeliveryOrder = deliveryOrder
}

if r.KafkaFeatureFlags.IsDispatcherRateLimiterEnabled() {
triggerVReplicasAnnotationValue, ok := trigger.Annotations[triggerVReplicasAnnotation]
if ok {
vReplicas, err := strconv.Atoi(triggerVReplicasAnnotationValue)
if err == nil {
egress.VReplicas = int32(vReplicas)
}
}
Comment on lines +381 to +387
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think using this annotation is the correct direction to go for manually controlling the scale of triggers. There has been discussion about implementing the scale subresource for the triggers and then using that here to set the vreplicas for the egress.

Any thoughts @pierDipi @creydr ?

Copy link
Author

@lamluongbinh lamluongbinh Jan 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your reply, @Cali0707. Do we have any documentation about the scale subresource that you mentioned above? Is it k8s scale subresource?

I added the vreplicas annotation here since it’s easier to manage via annotations. Making changes directly in the CRDs would be riskier and require additional modifications.

If we don’t use annotations, I’m curious if there’s a manual way to control the vreplica/rate for each trigger. This would be especially useful since each trigger sends messages to different services, each with unique needs, SLOs, and allocated resources.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The scale subresource is documented here: https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#scale-subresource

It provides a consistent API for anything wanting to scale a resource in the k8s ecosystem. With this approach, your trigger spec would look something like:

apiVersion: eventing.knative.dev/v1
kind: Trigger
metadata: ...
spec:
  scale: 10
  # other spec stuff here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Kubernetes, the scale subresource primarily facilitates scaling out physical dispatcher pods, which can enhance processing capacity and accelerate resource handling. However, simply increasing the number of pods does not directly address rate limiting. In fact, scaling out could unintentionally disrupt the rate limiting logic, as adding more pods would result in a higher overall processing rate.

The current rate limiting formula is:

Rate Limit = MaxPollRecords × vReplicas × Number of Dispatcher Pods

Scaling pods out just make us have higher rate limit for all triggers.

Current Approach
In our current implementation, rate limits can be controlled by adjusting the number of vReplicas and dispatcher pods. To support this, I have introduced an annotation that enables dynamic rate limiting by modifying the vReplicas value.

Future Considerations
If we decide to utilize the scale subresource to improve processing speed in the future, additional logic will be required to ensure rate limits remain consistent. This could be achieved by introducing new annotations or configuration options that control rate limits independently. A potential solution would be dynamically adjusting rate limits within the trigger configuration, such as:

Example 1: Defining rate limits in the spec

apiVersion: eventing.knative.dev/v1
kind: Trigger
metadata: ...
spec:
  maxRatePerPod: 30

Example 2: Using annotations for rate limits

apiVersion: eventing.knative.dev/v1
kind: Trigger
metadata:
  annotations:
    kafka.eventing.knative.dev/maxRatePerPod: "30"

Ultimately, achieving a balance between scaling and rate limiting is crucial to maintaining system performance while ensuring that processing limits are not exceeded.

Any thoughts on your end?

}

return egress, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ public OrderedConsumerVerticle(final ConsumerVerticleContext context, final Init
final var vReplicas = Math.max(1, context.getEgress().getVReplicas());
final var tokens = context.getMaxPollRecords() * vReplicas;
if (context.getEgress().getFeatureFlags().getEnableRateLimiter()) {
// using intervally will be more effective for precise rate limiting.
this.bucket = new LocalBucketBuilder()
.addLimit(Bandwidth.classic(tokens, Refill.greedy(tokens, Duration.ofSeconds(1))))
.addLimit(Bandwidth.classic(tokens, Refill.intervally(tokens, Duration.ofSeconds(1))))
.withSynchronizationStrategy(SynchronizationStrategy.SYNCHRONIZED)
.withMillisecondPrecision()
.build();
Expand Down Expand Up @@ -181,7 +182,9 @@ private void recordsHandler(ConsumerRecords<Object, CloudEvent> records) {

if (bucket != null) {
// Once we have new records, we force add them to internal per-partition queues.
bucket.forceAddTokens(records.count());
// bucket.forceAddTokens(records.count());
// I think there are some mistake here since we need to consume message, not add more tokens: https://github.com/bucket4j/bucket4j
bucket.tryConsume(records.count());
}

// Put records in internal per-partition queues.
Expand Down