|
| 1 | +// Copyright 2022 The Cockroach Authors. |
| 2 | +// |
| 3 | +// Licensed as a CockroachDB Enterprise file under the Cockroach Community |
| 4 | +// License (the "License"); you may not use this file except in compliance with |
| 5 | +// the License. You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt |
| 8 | + |
| 9 | +package tenantcostclient_test |
| 10 | + |
| 11 | +import ( |
| 12 | + "context" |
| 13 | + "fmt" |
| 14 | + "strconv" |
| 15 | + "strings" |
| 16 | + "testing" |
| 17 | + "time" |
| 18 | + |
| 19 | + "github.com/cockroachdb/cockroach/pkg/base" |
| 20 | + _ "github.com/cockroachdb/cockroach/pkg/ccl" // ccl init hooks |
| 21 | + _ "github.com/cockroachdb/cockroach/pkg/ccl/kvccl/kvtenantccl" |
| 22 | + "github.com/cockroachdb/cockroach/pkg/ccl/multitenantccl/tenantcostclient" |
| 23 | + _ "github.com/cockroachdb/cockroach/pkg/ccl/multitenantccl/tenantcostserver" |
| 24 | + "github.com/cockroachdb/cockroach/pkg/roachpb" |
| 25 | + "github.com/cockroachdb/cockroach/pkg/settings/cluster" |
| 26 | + "github.com/cockroachdb/cockroach/pkg/sql/stats" |
| 27 | + "github.com/cockroachdb/cockroach/pkg/testutils/serverutils" |
| 28 | + "github.com/cockroachdb/cockroach/pkg/testutils/skip" |
| 29 | + "github.com/cockroachdb/cockroach/pkg/testutils/sqlutils" |
| 30 | + "github.com/cockroachdb/cockroach/pkg/util/leaktest" |
| 31 | + "github.com/cockroachdb/cockroach/pkg/util/log" |
| 32 | + "github.com/cockroachdb/cockroach/pkg/util/protoutil" |
| 33 | + "github.com/stretchr/testify/require" |
| 34 | +) |
| 35 | + |
| 36 | +// TestEstimateQueryRUConsumption is a sanity check for the RU estimates |
| 37 | +// produced for queries that are run by a tenant under EXPLAIN ANALYZE. The RU |
| 38 | +// consumption of a query is not deterministic, since it depends on inexact |
| 39 | +// quantities like the (already estimated) CPU usage. Therefore, the test runs |
| 40 | +// each query multiple times and then checks that the total estimated RU |
| 41 | +// consumption is within reasonable distance from the actual measured RUs for |
| 42 | +// the tenant. |
| 43 | +func TestEstimateQueryRUConsumption(t *testing.T) { |
| 44 | + defer leaktest.AfterTest(t)() |
| 45 | + defer log.Scope(t).Close(t) |
| 46 | + |
| 47 | + // This test becomes flaky when the machine/cluster is under significant |
| 48 | + // background load, so it should only be run manually. |
| 49 | + skip.IgnoreLint(t, "intended to be manually run as a sanity test") |
| 50 | + |
| 51 | + ctx := context.Background() |
| 52 | + |
| 53 | + st := cluster.MakeTestingClusterSettings() |
| 54 | + stats.AutomaticStatisticsClusterMode.Override(ctx, &st.SV, false) |
| 55 | + stats.UseStatisticsOnSystemTables.Override(ctx, &st.SV, false) |
| 56 | + stats.AutomaticStatisticsOnSystemTables.Override(ctx, &st.SV, false) |
| 57 | + |
| 58 | + // Lower the target duration for reporting tenant usage so that it can be |
| 59 | + // measured accurately. Avoid decreasing too far, since doing so can add |
| 60 | + // measurable overhead. |
| 61 | + tenantcostclient.TargetPeriodSetting.Override(ctx, &st.SV, time.Millisecond*500) |
| 62 | + |
| 63 | + params := base.TestServerArgs{ |
| 64 | + Settings: st, |
| 65 | + DisableDefaultTestTenant: true, |
| 66 | + } |
| 67 | + |
| 68 | + s, mainDB, _ := serverutils.StartServer(t, params) |
| 69 | + defer s.Stopper().Stop(ctx) |
| 70 | + sysDB := sqlutils.MakeSQLRunner(mainDB) |
| 71 | + |
| 72 | + tenantID := serverutils.TestTenantID() |
| 73 | + tenant1, tenantDB1 := serverutils.StartTenant(t, s, base.TestTenantArgs{ |
| 74 | + TenantID: tenantID, |
| 75 | + Settings: st, |
| 76 | + }) |
| 77 | + defer tenant1.Stopper().Stop(ctx) |
| 78 | + defer tenantDB1.Close() |
| 79 | + tdb := sqlutils.MakeSQLRunner(tenantDB1) |
| 80 | + tdb.Exec(t, "SET CLUSTER SETTING sql.stats.automatic_collection.enabled=false") |
| 81 | + tdb.Exec(t, "CREATE TABLE abcd (a INT, b INT, c INT, d INT, INDEX (a, b, c))") |
| 82 | + |
| 83 | + type testCase struct { |
| 84 | + sql string |
| 85 | + count int |
| 86 | + } |
| 87 | + testCases := []testCase{ |
| 88 | + { // Insert statement |
| 89 | + sql: "INSERT INTO abcd (SELECT t%2, t%3, t, -t FROM generate_series(1,50000) g(t))", |
| 90 | + count: 1, |
| 91 | + }, |
| 92 | + { // Point query |
| 93 | + sql: "SELECT a FROM abcd WHERE (a, b) = (1, 1)", |
| 94 | + count: 10, |
| 95 | + }, |
| 96 | + { // Range query |
| 97 | + sql: "SELECT a FROM abcd WHERE (a, b) = (1, 1) AND c > 0 AND c < 10000", |
| 98 | + count: 10, |
| 99 | + }, |
| 100 | + { // Aggregate |
| 101 | + sql: "SELECT count(*) FROM abcd", |
| 102 | + count: 10, |
| 103 | + }, |
| 104 | + { // Distinct |
| 105 | + sql: "SELECT DISTINCT ON (a, b) * FROM abcd", |
| 106 | + count: 10, |
| 107 | + }, |
| 108 | + { // Full table scan |
| 109 | + sql: "SELECT a FROM abcd", |
| 110 | + count: 10, |
| 111 | + }, |
| 112 | + { // Lookup join |
| 113 | + sql: "SELECT a FROM (VALUES (1, 1), (0, 2)) v(x, y) INNER LOOKUP JOIN abcd ON (a, b) = (x, y)", |
| 114 | + count: 10, |
| 115 | + }, |
| 116 | + { // Index join |
| 117 | + sql: "SELECT * FROM abcd WHERE (a, b) = (0, 0)", |
| 118 | + count: 10, |
| 119 | + }, |
| 120 | + { // No kv IO, lots of network egress. |
| 121 | + sql: "SELECT 'deadbeef' FROM generate_series(1, 50000)", |
| 122 | + count: 10, |
| 123 | + }, |
| 124 | + } |
| 125 | + |
| 126 | + var err error |
| 127 | + var tenantEstimatedRUs int |
| 128 | + for _, tc := range testCases { |
| 129 | + for i := 0; i < tc.count; i++ { |
| 130 | + output := tdb.QueryStr(t, "EXPLAIN ANALYZE "+tc.sql) |
| 131 | + var estimatedRU int |
| 132 | + for _, row := range output { |
| 133 | + if len(row) != 1 { |
| 134 | + t.Fatalf("expected one column") |
| 135 | + } |
| 136 | + val := row[0] |
| 137 | + if strings.Contains(val, "estimated RUs consumed") { |
| 138 | + substr := strings.Split(val, " ") |
| 139 | + require.Equalf(t, 4, len(substr), "expected RU consumption message to have four words") |
| 140 | + ruCountStr := strings.Replace(strings.TrimSpace(substr[3]), ",", "", -1) |
| 141 | + estimatedRU, err = strconv.Atoi(ruCountStr) |
| 142 | + require.NoError(t, err, "failed to retrieve estimated RUs") |
| 143 | + break |
| 144 | + } |
| 145 | + } |
| 146 | + tenantEstimatedRUs += estimatedRU |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + getTenantRUs := func() float64 { |
| 151 | + // Sleep to ensure the measured RU consumption gets recorded in the |
| 152 | + // tenant_usage table. |
| 153 | + time.Sleep(time.Second) |
| 154 | + var consumptionBytes []byte |
| 155 | + var consumption roachpb.TenantConsumption |
| 156 | + var tenantRUs float64 |
| 157 | + rows := sysDB.Query(t, |
| 158 | + fmt.Sprintf( |
| 159 | + "SELECT total_consumption FROM system.tenant_usage WHERE tenant_id = %d AND instance_id = 0", |
| 160 | + tenantID.ToUint64(), |
| 161 | + ), |
| 162 | + ) |
| 163 | + for rows.Next() { |
| 164 | + require.NoError(t, rows.Scan(&consumptionBytes)) |
| 165 | + if len(consumptionBytes) == 0 { |
| 166 | + continue |
| 167 | + } |
| 168 | + require.NoError(t, protoutil.Unmarshal(consumptionBytes, &consumption)) |
| 169 | + tenantRUs += consumption.RU |
| 170 | + } |
| 171 | + return tenantRUs |
| 172 | + } |
| 173 | + tenantStartRUs := getTenantRUs() |
| 174 | + |
| 175 | + var tenantMeasuredRUs float64 |
| 176 | + for _, tc := range testCases { |
| 177 | + for i := 0; i < tc.count; i++ { |
| 178 | + tdb.QueryStr(t, tc.sql) |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + // Check the estimated RU aggregate for all the queries against the actual |
| 183 | + // measured RU consumption for the tenant. |
| 184 | + tenantMeasuredRUs = getTenantRUs() - tenantStartRUs |
| 185 | + const deltaFraction = 0.25 |
| 186 | + allowedDelta := tenantMeasuredRUs * deltaFraction |
| 187 | + require.InDeltaf(t, tenantMeasuredRUs, tenantEstimatedRUs, allowedDelta, |
| 188 | + "estimated RUs (%d) were not within %f RUs of the expected value (%f)", |
| 189 | + tenantEstimatedRUs, |
| 190 | + allowedDelta, |
| 191 | + tenantMeasuredRUs, |
| 192 | + ) |
| 193 | +} |
0 commit comments