-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathslinstance_test.go
143 lines (118 loc) · 4.53 KB
/
slinstance_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
// Copyright 2020 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 slinstance_test
import (
"context"
"testing"
"time"
"github.com/cockroachdb/cockroach/pkg/clusterversion"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql/enum"
"github.com/cockroachdb/cockroach/pkg/sql/sqlliveness"
"github.com/cockroachdb/cockroach/pkg/sql/sqlliveness/slinstance"
"github.com/cockroachdb/cockroach/pkg/sql/sqlliveness/slstorage"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/stop"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/stretchr/testify/require"
)
func TestSQLInstance(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx, stopper := context.Background(), stop.NewStopper()
defer stopper.Stop(ctx)
var ambientCtx log.AmbientContext
clock := hlc.NewClockForTesting(timeutil.NewManualTime(timeutil.Unix(0, 42)))
settings := cluster.MakeTestingClusterSettingsWithVersions(
clusterversion.TestingBinaryVersion,
clusterversion.TestingBinaryMinSupportedVersion,
true /* initializeVersion */)
slinstance.DefaultTTL.Override(ctx, &settings.SV, 20*time.Millisecond)
slinstance.DefaultHeartBeat.Override(ctx, &settings.SV, 10*time.Millisecond)
fakeStorage := slstorage.NewFakeStorage()
sqlInstance := slinstance.NewSQLInstance(ambientCtx, stopper, clock, fakeStorage, settings, nil, nil)
sqlInstance.Start(ctx, nil)
// Add one more instance to introduce concurrent access to storage.
dummy := slinstance.NewSQLInstance(ambientCtx, stopper, clock, fakeStorage, settings, nil, nil)
dummy.Start(ctx, nil)
s1, err := sqlInstance.Session(ctx)
require.NoError(t, err)
a, err := fakeStorage.IsAlive(ctx, s1.ID())
require.NoError(t, err)
require.True(t, a)
region, id, err := slstorage.UnsafeDecodeSessionID(s1.ID())
require.NoError(t, err)
require.Equal(t, enum.One, region)
require.NotNil(t, id)
s2, err := sqlInstance.Session(ctx)
require.NoError(t, err)
require.Equal(t, s1.ID(), s2.ID())
_ = fakeStorage.Delete(ctx, s2.ID())
t.Logf("deleted session %s", s2.ID())
a, err = fakeStorage.IsAlive(ctx, s2.ID())
require.NoError(t, err)
require.False(t, a)
var s3 sqlliveness.Session
require.Eventually(
t,
func() bool {
s3, err = sqlInstance.Session(ctx)
if err != nil {
t.Fatal(err)
}
return s3.ID().String() != s2.ID().String()
},
time.Second, 10*time.Millisecond,
)
a, err = fakeStorage.IsAlive(ctx, s3.ID())
require.NoError(t, err)
require.True(t, a)
require.NotEqual(t, s2.ID(), s3.ID())
// Stop the stopper and check that the heartbeat loop terminates
// and causes further Session() calls to fail.
stopper.Stop(ctx)
_, err = sqlInstance.Session(ctx)
require.Error(t, err)
}
func TestSQLInstanceRelease(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx, stopper := context.Background(), stop.NewStopper()
defer stopper.Stop(ctx)
clock := hlc.NewClockForTesting(timeutil.NewManualTime(timeutil.Unix(0, 42)))
settings := cluster.MakeTestingClusterSettingsWithVersions(
clusterversion.TestingBinaryVersion,
clusterversion.TestingBinaryMinSupportedVersion,
true /* initializeVersion */)
slinstance.DefaultTTL.Override(ctx, &settings.SV, 20*time.Millisecond)
slinstance.DefaultHeartBeat.Override(ctx, &settings.SV, 10*time.Millisecond)
fakeStorage := slstorage.NewFakeStorage()
var ambientCtx log.AmbientContext
sqlInstance := slinstance.NewSQLInstance(ambientCtx, stopper, clock, fakeStorage, settings, nil, nil)
sqlInstance.Start(ctx, enum.One)
activeSession, err := sqlInstance.Session(ctx)
require.NoError(t, err)
activeSessionID := activeSession.ID()
a, err := fakeStorage.IsAlive(ctx, activeSessionID)
require.NoError(t, err)
require.True(t, a)
require.NotEqual(t, 0, stopper.NumTasks())
// Make sure release is idempotent
for i := 0; i < 5; i++ {
finalSession, err := sqlInstance.Release(ctx)
require.NoError(t, err)
// Release should always return the last active session
require.Equal(t, activeSessionID, finalSession)
// Release should tear down the background heartbeat
require.Equal(t, 0, stopper.NumTasks())
}
}