k8s.io/apiserver@v0.29.3/pkg/storage/etcd3/latency_tracker.go (about) 1 /* 2 Copyright 2022 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 etcd3 18 19 import ( 20 "context" 21 "time" 22 23 clientv3 "go.etcd.io/etcd/client/v3" 24 endpointsrequest "k8s.io/apiserver/pkg/endpoints/request" 25 ) 26 27 // NewETCDLatencyTracker returns an implementation of 28 // clientv3.KV that times the calls from the specified 29 // 'delegate' KV instance in order to track latency incurred. 30 func NewETCDLatencyTracker(delegate clientv3.KV) clientv3.KV { 31 return &clientV3KVLatencyTracker{KV: delegate} 32 } 33 34 // clientV3KVLatencyTracker decorates a clientv3.KV instance and times 35 // each call so we can track the latency an API request incurs in etcd 36 // round trips (the time it takes to send data to etcd and get the 37 // complete response back) 38 // 39 // If an API request involves N (N>=1) round trips to etcd, then we will sum 40 // up the latenciy incurred in each roundtrip. 41 42 // It uses the context associated with the request in flight, so there 43 // are no states shared among the requests in flight, and so there is no 44 // concurrency overhead. 45 // If the goroutine executing the request handler makes concurrent calls 46 // to the underlying storage layer, that is protected since the latency 47 // tracking function TrackStorageLatency is thread safe. 48 // 49 // NOTE: Compact is an asynchronous process and is not associated with 50 // any request, so we will not be tracking its latency. 51 type clientV3KVLatencyTracker struct { 52 clientv3.KV 53 } 54 55 func (c *clientV3KVLatencyTracker) Put(ctx context.Context, key, val string, opts ...clientv3.OpOption) (*clientv3.PutResponse, error) { 56 startedAt := time.Now() 57 defer func() { 58 endpointsrequest.TrackStorageLatency(ctx, time.Since(startedAt)) 59 }() 60 61 return c.KV.Put(ctx, key, val, opts...) 62 } 63 64 func (c *clientV3KVLatencyTracker) Get(ctx context.Context, key string, opts ...clientv3.OpOption) (*clientv3.GetResponse, error) { 65 startedAt := time.Now() 66 defer func() { 67 endpointsrequest.TrackStorageLatency(ctx, time.Since(startedAt)) 68 }() 69 70 return c.KV.Get(ctx, key, opts...) 71 } 72 73 func (c *clientV3KVLatencyTracker) Delete(ctx context.Context, key string, opts ...clientv3.OpOption) (*clientv3.DeleteResponse, error) { 74 startedAt := time.Now() 75 defer func() { 76 endpointsrequest.TrackStorageLatency(ctx, time.Since(startedAt)) 77 }() 78 79 return c.KV.Delete(ctx, key, opts...) 80 } 81 82 func (c *clientV3KVLatencyTracker) Do(ctx context.Context, op clientv3.Op) (clientv3.OpResponse, error) { 83 startedAt := time.Now() 84 defer func() { 85 endpointsrequest.TrackStorageLatency(ctx, time.Since(startedAt)) 86 }() 87 88 return c.KV.Do(ctx, op) 89 } 90 91 func (c *clientV3KVLatencyTracker) Txn(ctx context.Context) clientv3.Txn { 92 return &clientV3TxnTracker{ctx: ctx, Txn: c.KV.Txn(ctx)} 93 } 94 95 type clientV3TxnTracker struct { 96 ctx context.Context 97 clientv3.Txn 98 } 99 100 func (t *clientV3TxnTracker) Commit() (*clientv3.TxnResponse, error) { 101 startedAt := time.Now() 102 defer func() { 103 endpointsrequest.TrackStorageLatency(t.ctx, time.Since(startedAt)) 104 }() 105 106 return t.Txn.Commit() 107 }