-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
migrate_span_configs_test.go
157 lines (139 loc) · 5.01 KB
/
migrate_span_configs_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright 2021 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package migrations_test
import (
"context"
"fmt"
"testing"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/clusterversion"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/server"
"github.com/cockroachdb/cockroach/pkg/spanconfig"
"github.com/cockroachdb/cockroach/pkg/testutils"
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
"github.com/cockroachdb/cockroach/pkg/testutils/testcluster"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/stretchr/testify/require"
)
// TestEnsureSpanConfigReconciliation verifies that the migration waits for a
// span config reconciliation attempt, blocking until it occurs.
func TestEnsureSpanConfigReconciliation(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
blockReconcilerCh := make(chan struct{})
tc := testcluster.StartTestCluster(t, 1, base.TestClusterArgs{
ServerArgs: base.TestServerArgs{
EnableSpanConfigs: true,
Knobs: base.TestingKnobs{
Server: &server.TestingKnobs{
DisableAutomaticVersionUpgrade: 1,
BinaryVersionOverride: clusterversion.ByKey(
clusterversion.EnsureSpanConfigReconciliation - 1,
),
},
SpanConfig: &spanconfig.TestingKnobs{
ReconcilerInitialInterceptor: func() {
<-blockReconcilerCh
},
},
},
},
})
defer tc.Stopper().Stop(ctx)
ts := tc.Server(0)
tdb := sqlutils.MakeSQLRunner(tc.ServerConn(0))
scKVAccessor := ts.SpanConfigKVAccessor().(spanconfig.KVAccessor)
scReconciler := ts.SpanConfigReconciler().(spanconfig.Reconciler)
{ // Ensure that no span config entries are to be found
entries, err := scKVAccessor.GetSpanConfigEntriesFor(ctx, []roachpb.Span{
keys.EverythingSpan,
})
require.NoError(t, err)
require.Empty(t, entries)
}
// Ensure that upgrade attempts without having reconciled simply fail.
tdb.Exec(t, "SET statement_timeout='500ms'")
tdb.ExpectErr(t, "query execution canceled due to statement timeout",
"SET CLUSTER SETTING version = $1",
clusterversion.ByKey(clusterversion.EnsureSpanConfigReconciliation).String(),
)
// Kicking off the reconciliation process allows the upgrade to proceed.
close(blockReconcilerCh)
tdb.ExecSucceedsSoon(t,
"SET CLUSTER SETTING version = $1",
clusterversion.ByKey(clusterversion.EnsureSpanConfigReconciliation).String(),
)
require.False(t, scReconciler.Checkpoint().IsEmpty())
{ // Ensure that the host tenant's span configs are installed.
entries, err := scKVAccessor.GetSpanConfigEntriesFor(ctx, []roachpb.Span{
keys.EverythingSpan,
})
require.NoError(t, err)
require.NotEmpty(t, entries)
}
}
// TestEnsureSpanConfigSubscription verifies that the migration waits for all
// stores to have observed a reconciliation state.
func TestEnsureSpanConfigSubscription(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
blockSubscriberCh := make(chan struct{})
tc := testcluster.StartTestCluster(t, 1, base.TestClusterArgs{
ServerArgs: base.TestServerArgs{
EnableSpanConfigs: true,
Knobs: base.TestingKnobs{
Server: &server.TestingKnobs{
DisableAutomaticVersionUpgrade: 1,
BinaryVersionOverride: clusterversion.ByKey(
clusterversion.EnsureSpanConfigSubscription - 1,
),
},
SpanConfig: &spanconfig.TestingKnobs{
KVSubscriberPostRangefeedStartInterceptor: func() {
<-blockSubscriberCh
},
},
},
},
})
defer tc.Stopper().Stop(ctx)
ts := tc.Server(0)
tdb := sqlutils.MakeSQLRunner(tc.ServerConn(0))
scKVAccessor := ts.SpanConfigKVAccessor().(spanconfig.KVAccessor)
scKVSubscriber := ts.SpanConfigKVSubscriber().(spanconfig.KVSubscriber)
testutils.SucceedsSoon(t, func() error {
entries, err := scKVAccessor.GetSpanConfigEntriesFor(ctx, []roachpb.Span{
keys.EverythingSpan,
})
require.NoError(t, err)
if len(entries) == 0 {
return fmt.Errorf("empty global span configuration state")
}
return nil
})
// Ensure that upgrade attempts without having subscribed simply fail.
tdb.Exec(t, "SET statement_timeout='500ms'")
tdb.ExpectErr(t, "query execution canceled due to statement timeout",
"SET CLUSTER SETTING version = $1",
clusterversion.ByKey(clusterversion.EnsureSpanConfigSubscription).String(),
)
// Unblocking the subscription process allows the upgrade to proceed.
close(blockSubscriberCh)
tdb.ExecSucceedsSoon(t,
"SET CLUSTER SETTING version = $1",
clusterversion.ByKey(clusterversion.EnsureSpanConfigSubscription).String(),
)
require.False(t, scKVSubscriber.LastUpdated().IsEmpty())
}