vitess.io/vitess@v0.16.2/go/vt/vtgate/buffer/variables.go (about) 1 /* 2 Copyright 2019 The Vitess 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 buffer 18 19 import ( 20 "vitess.io/vitess/go/stats" 21 ) 22 23 // This file contains all status variables which can be used to monitor the 24 // buffer. 25 26 var ( 27 // starts counts how often we started buffering (including dry-run bufferings). 28 starts = stats.NewCountersWithMultiLabels( 29 "BufferStarts", 30 "Buffering operation starts, including dry-run", 31 []string{"Keyspace", "ShardName"}) 32 // stops counts how often we triggered the stop of a buffering, including 33 // dry-run bufferings. 34 // See the type "stopReason" below for all possible values of "Reason". 35 stops = stats.NewCountersWithMultiLabels( 36 "BufferStops", 37 "Buffering operation stops, including dry-runs", 38 []string{"Keyspace", "ShardName", "Reason"}) 39 40 // failoverDurationSumMs is the cumulative sum of all failover durations. 41 // In connection with "starts" it can be used to calculate a moving average. 42 failoverDurationSumMs = stats.NewCountersWithMultiLabels( 43 "BufferFailoverDurationSumMs", 44 "Total buffering failover duration", 45 []string{"Keyspace", "ShardName"}) 46 47 // utilizationSum is the cumulative sum of the maximum buffer utilization 48 // (in percentage) during each failover. 49 // Utilization = maximum number of requests buffered / buffer size. 50 // In connection with "starts" it can be used to calculate a moving average. 51 // TODO(mberlin): Replace this with a MultiHistogram once it's available. 52 utilizationSum = stats.NewGaugesWithMultiLabels( 53 "BufferUtilizationSum", 54 "Cumulative buffer utilization (in %) during failover", 55 []string{"Keyspace", "ShardName"}) 56 // utilizationDryRunSum is the cumulative sum of the maximum *theoretical* 57 // buffer utilization (in percentage) during each failover. 58 // Utilization = maximum number of requests buffered seen / buffer size. 59 // In connection with "starts" it can be used to calculate a moving average. 60 // Example: Buffer size = 10. Two consecutive failovers with maximum values of 61 // 15 and 5 seen requests each add up to a value of 200% (150% + 50% 62 // utilization). The moving average would be 100% because there were two 63 // failovers in that period. 64 // TODO(mberlin): Replace this with a MultiHistogram once it's available. 65 utilizationDryRunSum = stats.NewCountersWithMultiLabels( 66 "BufferUtilizationDryRunSum", 67 "Cumulative buffer utilization % during failover (dry-run)", 68 []string{"Keyspace", "ShardName"}) 69 70 // requestsBuffered tracks how many requests were added to the buffer. 71 // NOTE: The two counters "Buffered" and "Skipped" should cover all requests 72 // which passed through the buffer. 73 requestsBuffered = stats.NewCountersWithMultiLabels( 74 "BufferRequestsBuffered", 75 "Buffered requests", 76 []string{"Keyspace", "ShardName"}) 77 // requestsBufferedDryRun tracks how many requests would have been added to 78 // the buffer (dry-run mode). 79 requestsBufferedDryRun = stats.NewCountersWithMultiLabels( 80 "BufferRequestsBufferedDryRun", 81 "Buffered requests (dry-run)", 82 []string{"Keyspace", "ShardName"}) 83 // requestsBuffered tracks how many requests were drained from the buffer. 84 // NOTE: The sum of the two counters "Drained" and "Evicted" should be 85 // identical to the "Buffered" counter value. 86 requestsDrained = stats.NewCountersWithMultiLabels( 87 "BufferRequestsDrained", 88 "Drained buffered requests", 89 []string{"Keyspace", "ShardName"}) 90 // requestsEvicted tracks how many requests were evicted early from the buffer. 91 // See the type "evictedReason" below for all possible values of "Reason". 92 requestsEvicted = stats.NewCountersWithMultiLabels( 93 "BufferRequestsEvicted", 94 "Evicted buffered requests", 95 []string{"Keyspace", "ShardName", "Reason"}) 96 // requestsSkipped tracks how many requests would have been buffered but 97 // eventually were not (includes dry-run bufferings). 98 // See the type "skippedReason" below for all possible values of "Reason". 99 requestsSkipped = stats.NewCountersWithMultiLabels( 100 "BufferRequestsSkipped", 101 "Skipped buffering requests (incl. dry-run)", 102 []string{"Keyspace", "ShardName", "Reason"}) 103 ) 104 105 // stopReason is used in "stopsByReason" as "Reason" label. 106 type stopReason string 107 108 var stopReasons = []stopReason{stopShardMissing, stopFailoverEndDetected, stopMaxFailoverDurationExceeded, stopShutdown} 109 110 const ( 111 stopShardMissing stopReason = "ReshardingComplete" 112 stopFailoverEndDetected stopReason = "NewPrimarySeen" 113 stopMaxFailoverDurationExceeded stopReason = "MaxDurationExceeded" 114 stopShutdown stopReason = "Shutdown" 115 ) 116 117 // evictedReason is used in "requestsEvicted" as "Reason" label. 118 type evictedReason string 119 120 var evictReasons = []evictedReason{evictedContextDone, evictedBufferFull, evictedWindowExceeded} 121 122 const ( 123 evictedContextDone evictedReason = "ContextDone" 124 //lint: ignore SA9004 ok not to use explicit type here because implicit type string is correct 125 evictedBufferFull = "BufferFull" 126 evictedWindowExceeded = "WindowExceeded" 127 ) 128 129 // skippedReason is used in "requestsSkipped" as "Reason" label. 130 type skippedReason string 131 132 var skippedReasons = []skippedReason{skippedBufferFull, skippedDisabled, skippedShutdown, skippedLastReparentTooRecent, skippedLastFailoverTooRecent} 133 134 const ( 135 // skippedBufferFull occurs when all slots in the buffer are occupied by one 136 // or more concurrent failovers. Unlike "evictedBufferFull", no request could 137 // be evicted and therefore we had to skip this request. 138 skippedBufferFull skippedReason = "BufferFull" 139 // skippedDisabled is used when the buffer was disabled for that particular 140 // keyspace/shard. 141 skippedDisabled = "Disabled" 142 skippedShutdown = "Shutdown" 143 skippedLastReparentTooRecent = "LastReparentTooRecent" 144 skippedLastFailoverTooRecent = "LastFailoverTooRecent" 145 ) 146 147 // initVariablesForShard is used to initialize all shard variables to 0. 148 // If we don't do this, monitoring frameworks may not correctly calculate rates 149 // for the first failover of the shard because they see a transition from 150 // "no value for this label set (NaN)" to "a value". 151 // "statsKey" should have two members for keyspace and shard. 152 func initVariablesForShard(statsKey []string) { 153 starts.Reset(statsKey) 154 for _, reason := range stopReasons { 155 key := append(statsKey, string(reason)) 156 stops.Reset(key) 157 } 158 159 failoverDurationSumMs.Reset(statsKey) 160 161 utilizationSum.Set(statsKey, 0) 162 utilizationDryRunSum.Reset(statsKey) 163 164 requestsBuffered.Reset(statsKey) 165 requestsBufferedDryRun.Reset(statsKey) 166 requestsDrained.Reset(statsKey) 167 for _, reason := range evictReasons { 168 key := append(statsKey, string(reason)) 169 requestsEvicted.Reset(key) 170 } 171 for _, reason := range skippedReasons { 172 key := append(statsKey, string(reason)) 173 requestsSkipped.Reset(key) 174 } 175 } 176 177 // TODO(mberlin): Remove the gauge values below once we store them 178 // internally and have a /bufferz page where we can show this. 179 var ( 180 // bufferSizeStat publishes the configured per vtgate buffer size. It can be used 181 // to calculate the utilization of the buffer. 182 bufferSizeStat = stats.NewGauge("BufferSize", "The configured per vtgate buffer size") 183 lastFailoverDurationMs = stats.NewGaugesWithMultiLabels( 184 "BufferLastFailoverDurationMs", 185 "Buffered requests during the last failover. The value for a given shard will be reset at the next failover.", 186 []string{"Keyspace", "ShardName"}) 187 lastRequestsInFlightMax = stats.NewGaugesWithMultiLabels( 188 "BufferLastRequestsInFlightMax", 189 "The max value of buffered requests in flight of the last failover. The value for a given shard will be reset at the next failover.", 190 []string{"Keyspace", "ShardName"}) 191 // lastRequestsDryRunMax has the maximum number of requests which were seen during 192 // a dry-run buffering of the last failover. 193 // The value for a given shard will be reset at the next failover. 194 lastRequestsDryRunMax = stats.NewGaugesWithMultiLabels( 195 "BufferLastRequestsDryRunMax", 196 "Max # of requests which were seen during a dry-run buffering of the last failover", 197 []string{"Keyspace", "ShardName"}) 198 )