|
| 1 | +/* |
| 2 | +Copyright 2021 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package coalescing |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "time" |
| 22 | + |
| 23 | + "github.com/go-logr/logr" |
| 24 | + "github.com/pkg/errors" |
| 25 | + "go.opentelemetry.io/otel/api/trace" |
| 26 | + "go.opentelemetry.io/otel/label" |
| 27 | + "sigs.k8s.io/cluster-api-provider-azure/util/cache/ttllru" |
| 28 | + "sigs.k8s.io/cluster-api-provider-azure/util/tele" |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 30 | +) |
| 31 | + |
| 32 | +type ( |
| 33 | + // ReconcileCache uses and underlying time to live last recently used cache to track high frequency requests. |
| 34 | + // A reconciler should call ShouldProcess to determine if the key has expired. If the key has expired, a zero value |
| 35 | + // time.Time and true is returned. If the key has not expired, the expiration and false is returned. Upon successful |
| 36 | + // reconciliation a reconciler should call Reconciled to update the cache expiry. |
| 37 | + ReconcileCache struct { |
| 38 | + lastSuccessfulReconciliationCache ttllru.PeekingCacher |
| 39 | + } |
| 40 | + |
| 41 | + // ReconcileCacher describes an interface for determining if a request should be reconciled through a call to |
| 42 | + // ShouldProcess and if ok, reset the cool down through a call to Reconciled |
| 43 | + ReconcileCacher interface { |
| 44 | + ShouldProcess(key string) (expiration time.Time, ok bool) |
| 45 | + Reconciled(key string) |
| 46 | + } |
| 47 | + |
| 48 | + // reconciler is the caching reconciler middleware that uses the cache or |
| 49 | + reconciler struct { |
| 50 | + upstream reconcile.Reconciler |
| 51 | + cache ReconcileCacher |
| 52 | + log logr.Logger |
| 53 | + } |
| 54 | +) |
| 55 | + |
| 56 | +// NewRequestCache creates a new instance of a ReconcileCache given a specified window of expiration |
| 57 | +func NewRequestCache(window time.Duration) (*ReconcileCache, error) { |
| 58 | + cache, err := ttllru.New(1024, window) |
| 59 | + if err != nil { |
| 60 | + return nil, errors.Wrap(err, "failed to build ttllru cache") |
| 61 | + } |
| 62 | + |
| 63 | + return &ReconcileCache{ |
| 64 | + lastSuccessfulReconciliationCache: cache, |
| 65 | + }, nil |
| 66 | +} |
| 67 | + |
| 68 | +// ShouldProcess determines if the key has expired. If the key has expired, a zero value |
| 69 | +// time.Time and true is returned. If the key has not expired, the expiration and false is returned. |
| 70 | +func (cache *ReconcileCache) ShouldProcess(key string) (time.Time, bool) { |
| 71 | + _, expiration, ok := cache.lastSuccessfulReconciliationCache.Peek(key) |
| 72 | + return expiration, !ok |
| 73 | +} |
| 74 | + |
| 75 | +// Reconciled updates the cache expiry for a given key |
| 76 | +func (cache *ReconcileCache) Reconciled(key string) { |
| 77 | + cache.lastSuccessfulReconciliationCache.Add(key, nil) |
| 78 | +} |
| 79 | + |
| 80 | +// NewReconciler returns a reconcile wrapper that will delay new reconcile.Requests |
| 81 | +// after the cache expiry of the request string key. |
| 82 | +// A successful reconciliation is defined as as one where no error is returned |
| 83 | +func NewReconciler(upstream reconcile.Reconciler, cache ReconcileCacher, log logr.Logger) reconcile.Reconciler { |
| 84 | + return &reconciler{ |
| 85 | + upstream: upstream, |
| 86 | + cache: cache, |
| 87 | + log: log.WithName("CoalescingReconciler"), |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +// Reconcile sends a request to the upstream reconciler if the request is outside of the debounce window |
| 92 | +func (rc *reconciler) Reconcile(ctx context.Context, r reconcile.Request) (reconcile.Result, error) { |
| 93 | + ctx, span := tele.Tracer().Start(ctx, "controllers.reconciler.Reconcile", |
| 94 | + trace.WithAttributes( |
| 95 | + label.String("namespace", r.Namespace), |
| 96 | + label.String("name", r.Name), |
| 97 | + )) |
| 98 | + defer span.End() |
| 99 | + |
| 100 | + log := rc.log.WithValues("request", r.String()) |
| 101 | + |
| 102 | + if expiration, ok := rc.cache.ShouldProcess(r.String()); !ok { |
| 103 | + log.V(4).Info("not processing", "expiration", expiration, "timeUntil", time.Until(expiration)) |
| 104 | + var requeueAfter = time.Until(expiration) |
| 105 | + if requeueAfter < 1*time.Second { |
| 106 | + requeueAfter = 1 * time.Second |
| 107 | + } |
| 108 | + return reconcile.Result{RequeueAfter: requeueAfter}, nil |
| 109 | + } |
| 110 | + |
| 111 | + log.V(4).Info("processing") |
| 112 | + result, err := rc.upstream.Reconcile(ctx, r) |
| 113 | + if err != nil { |
| 114 | + log.V(4).Info("not successful") |
| 115 | + return result, err |
| 116 | + } |
| 117 | + |
| 118 | + log.V(4).Info("successful") |
| 119 | + rc.cache.Reconciled(r.String()) |
| 120 | + return result, nil |
| 121 | +} |
0 commit comments