Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejava
titleClient Quota Callback
/**
 * Quota callback interface for brokers that enables customization of client quota computation.
 */
public interface ClientQuotaCallback extends Configurable {

    /**
     * Quota callback invoked to determine the quota limitmetric tags to be applied for a request.
     * Quota limits are associated with quota metrics and all clients which use the same
     * metric tags share the quota limit.
     *
     * @param principal The user principal of the connection for which quota is requested
     * @param clientId  The client id associated with the request
     * @param quotaType Type of quota requested
     * @return the quota including the limit and metricmetric tags that indicate which other clients share this quota
     */
    Map<String, ClientQuotaString> quotaquotaMetricTags(KafkaPrincipal principal, String clientId, ClientQuotaType quotaType);

    /**
     * Returns the quota limit associated with the provided metric tags. These tags were returned from
     * a previous call to {@link #quota#quotaMetricTags(KafkaPrincipal, String, ClientQuotaType)}. This method is invoked
     * invoked by quota managers to obtain the current quota limit applied to a metric afterwhen athe quotafirst update orrequest
     * clusterusing metadatathese change.tags Ifis theprocessed. tagsIt areis noalso longerinvoked inafter usea afterquota the update,  or cluster metadata change.
     * If the tags are no longer in use after the update, (e.g. this is a
     * {user, client-id} quota metric
     * and the quota now in use is a {user} quota), null is returned.
     *
     * @param metricTags Metric tags for a quota metric of type `quotaType`
     * @param quotaType  Type of quota requested
     * @return the quota limit for the provided metric tags or null if the metric tags are no longer in use
     */
    Double quotaLimit(Map<String, String> metricTags, ClientQuotaType quotaType);

    /**
     * Metadata update callback that is invoked whenever UpdateMetadata request is received from
     * the controller. This is useful if quota computation takes partitions into account.
     * Topics that are being deleted will not be included in `cluster`.
     *
     * @param cluster Cluster metadata including partitions and their leaders if known
     * @return true if quotas have changed and metric configs may need to be updated
     */
    boolean updateClusterMetadata(Cluster cluster);

    /**
     * Quota configuration update callback that is invoked when quota configuration for an entity is
     * updated in ZooKeeper. This is useful to track configured quotas if built-in quota configuration
     * tools are used for quota management.
     *
     * @param quotaEntity The quota entity for which quota is being updated
     * @param quotaType   Type of quota being updated
     * @param newValue    The new quota value
     * @return true if quotas have changed and metric configs may need to be updated
     */
    boolean updateQuota(ClientQuotaEntity quotaEntity, ClientQuotaType quotaType, double newValue);

    /**
     * Quota configuration removal callback that is invoked when quota configuration for an entity is
     * removed in ZooKeeper. This is useful to track configured quotas if built-in quota configuration
     * tools are used for quota management.
     *
     * @param quotaEntity The quota entity for which quota is being updated.
     * @param quotaType   Type of quota being updated.
     * @return true if quotas have changed and metric configs may need to be updated
     */
    boolean removeQuota(ClientQuotaEntity quotaEntity, ClientQuotaType quotaType);

    /**
     * Closes this instance.
     */
    void close();
}

 

The quota returned by the callback should include callback is invoked to obtain the quota limit as well the metric tags to be used. These metric tags determine which entities share the quota.

By default the tags "user" and "client-id" will be used for all quota metrics. When <user, client-id> quota config is used, user tag is set to user principal of the session and client-id tag is set to the client-id of the request. If <user> quota config is used, user tag is set to user principal of the session and client-id tag is set to empty string. Similarly, if <client-id> quota config is used, the user tag is set to empty string. This ensures that the same quota sensors and metrics are shared by all requests that match each quota config.

Code Block
languagejava
titleClientQuota
/**
 * Client quota returned by {@link ClientQuotaCallback} that includes the quota bound
 * as well as the metric tags that indicate which other clients share this quota.
 */
public class ClientQuota {
    private final double quotaLimit;
    private final Map<String, String> metricTags;

    /**
     * Constructs an instance of ClientQuota.
     *
     * @param quotaLimit The quota bound to be applied
     * @param metricTags The tags to be added to the quota metric for this request. All
     *                   entities which have the same `metricTags` share the `quotaLimit`
     */
    public ClientQuota(double quotaLimit, Map<String, String> metricTags) {
        this.quotaLimit = quotaLimit;
        this.metricTags = metricTags;
    }

    /**
     * Returns the quota bound.
     */
    public double quotaLimit() {
        return quotaLimit;
    }

    /**
     * Returns the tags to be added to the quota metric for this request. All
     * entities which have the same `metricTags` share a quota.
     */
    public Map<String, String> metricTags() {
        return metricTags;
    }
}

 

When quota configuration is updated in ZooKeeper, quota callbacks are notified of configuration changes. Quota configuration entities can be combined to define quotas at different levels.

...