How to Reset Quota, when we are defining quota in KVM Map

Not applicable

We have implemented quota per tenant/client on our on-premise gateway, by saving the quota in KVMMap. We are keeping a mapping of tenant/client ID to backend server in KVM Map and on this map we are also saving minute/day quota for each tenant.

We can update quota for a tenant by making a REST call. Is there a way quota can be reset the next day automatically, to the original value, by updating value in KVM map or do we need to write some custom code to do that?

0 3 425
3 REPLIES 3

Sure.

First, you may be aware but I'll state it anyway: there are different kinds of quota. You can read about them in the Quota docs.

The default quota (don't use a type= attribute on the toplevel element) is designed to reset at midnight (GMT) every day, if you use "day" as the time unit.

You can also use the calendar type and specify a particular time for reset. Again, use "day" as the time unit.

This should do exactly what you want, I guess.

Our issue is different. Say, the quota for a tenant is 100/minute (stored in KVM Map). During some activity, we realize that we need to increase it, temporarily, for a day. We update quota by updating the KVMMap, using REST call, to say 1000/min

Our expectation is that after a day, the quota should be reset back to 100/min automatically, at midnight. Is this possible, or we need to develop some custom service for this.

The quota limits are dynamic, specified in a variable in the policy configuration. You can choose the limit, by setting a value into the variable, and specifying that variable.

<Quota name='Q-1'>
    <Interval>1</Interval>
    <TimeUnit>minute</TimeUnit>
    <Allow countRef='my_custom_variable'/>
    <Identifier ref='tenant_id'/>
    <Distributed>true</Distributed>
    <Synchronous>false</Synchronous>
    <PreciseAtSecondsLevel>false</PreciseAtSecondsLevel>
</Quota>

Presumably you will read "my_custom_variable" from the KVM. It will normally hold the "standard" value, whatever it is (maybe 100). At some point you can update that value, and when the KVM cache expires and the value is re-read from persisted store, you will get that updated value (maybe 1000) into the variable.

At that point the Quota will enforce on the new, updated limit.

I am not clear on what will happen, in the case that you update the value of the limit, during a counting interval. (During a particular minute). I suppose that if the quota bucket is partially full, for example maybe the count is at 68, the limit _was_ 100, and then you then raise the limit to 1000 via the KVM update I just described. I suppose in this case the quota would just work the way you want for the duration of the minute. I've never tested this, so it's worth testing yourself. If it doesn't work this way out of the box you may need to execute a "ResetQuota" policy when you update the KVM.

There is also the case in which the quota has already been exceeded for the tenant, for a particular time bucket - for example the quota count is 102, and the limit is 100, and the client has already received several 429 status codes. If you then raise the limit to 1000, I am not sure of the behavior here, either. Mayb e it will just work the way you want, or maybe you will need ResetQuota.

Just test it and either way, you know what to do.

The 2nd part of your ask is to reset the quota back to "normal", automatically.

I think if you want automatic behavior you will need to use a 2nd KVM, to flag that. Then daisy chain several KVM reads with conditions, to accomplish what you want. Eg

  1. Read KVM entry for "specialquota" for a particular tenant
  2. if there is a special quota setting
    1. check "expiry" time on special quota (this is a 2nd data item stored in the KVM entry)
    2. if the expiry has not passed, then set my_quota_limit to the limit specified in the specialquota
  3. if there is no special quota
    1. read from "standardquota" KVM element.
    2. set my_quota_limit to that value
  4. enforce Quota on my_quota_limit