-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathtable_history.go
294 lines (269 loc) · 8.53 KB
/
table_history.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Copyright 2018 The Cockroach Authors.
//
// Licensed as a CockroachDB Enterprise file under the Cockroach Community
// License (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt
package changefeedccl
import (
"context"
"sort"
"time"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/util/encoding"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/ccl/storageccl/engineccl"
"github.com/cockroachdb/cockroach/pkg/internal/client"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/storage/engine"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/pkg/errors"
)
type tableHistoryWaiter struct {
ts hlc.Timestamp
errCh chan error
}
// tableHistory tracks that a some invariants hold over a set of tables as time
// advances.
//
// Internally, two timestamps are tracked. The high-water is the highest
// timestamp such that every version of a TableDescriptor has met a provided
// invariant (via `validateFn`). An error timestamp is also kept, which is the
// lowest timestamp where at least one table doesn't meet the invariant.
//
// The `ValidateThroughTS` method allows a user to block until some given
// timestamp is greater (or equal) to either the high-water or the error
// timestamp. In the latter case, it returns the error.
type tableHistory struct {
validateFn func(*sqlbase.TableDescriptor) error
mu struct {
syncutil.Mutex
// the highest known valid timestamp
highWater hlc.Timestamp
// the lowest known invalid timestamp
errTS hlc.Timestamp
// the error associated with errTS
err error
// callers waiting on a timestamp to be resolved as valid or invalid
waiters []tableHistoryWaiter
}
}
// makeTableHistory creates tableHistory with the given initial high-water and
// invariant check function. It is expected that `validateFn` is deterministic.
func makeTableHistory(
validateFn func(*sqlbase.TableDescriptor) error, initialHighWater hlc.Timestamp,
) *tableHistory {
m := &tableHistory{validateFn: validateFn}
m.mu.highWater = initialHighWater
return m
}
// HighWater returns the current high-water timestamp.
func (m *tableHistory) HighWater() hlc.Timestamp {
m.mu.Lock()
highWater := m.mu.highWater
m.mu.Unlock()
return highWater
}
// WaitForTS blocks until the given timestamp is greater or equal to the
// high-water or error timestamp. In the latter case, the error is returned.
//
// If called twice with the same timestamp, two different errors may be returned
// (since the error timestamp can recede). However, the return for a given
// timestamp will never switch from nil to an error or vice-versa (assuming that
// `validateFn` is deterministic and the ingested descriptors are read
// transactionally).
func (m *tableHistory) WaitForTS(ctx context.Context, ts hlc.Timestamp) error {
var errCh chan error
m.mu.Lock()
highWater := m.mu.highWater
var err error
if m.mu.errTS != (hlc.Timestamp{}) && !ts.Less(m.mu.errTS) {
err = m.mu.err
}
fastPath := err != nil || !highWater.Less(ts)
if !fastPath {
errCh = make(chan error, 1)
m.mu.waiters = append(m.mu.waiters, tableHistoryWaiter{ts: ts, errCh: errCh})
}
m.mu.Unlock()
if fastPath {
if log.V(1) {
log.Infof(ctx, "fastpath for %s: %v", ts, err)
}
return err
}
if log.V(1) {
log.Infof(ctx, "waiting for %s highwater", ts)
}
start := timeutil.Now()
select {
case <-ctx.Done():
return ctx.Err()
case err := <-errCh:
if log.V(1) {
log.Infof(ctx, "waited %s for %s highwater: %v", timeutil.Since(start), ts, err)
}
return err
}
}
// IngestDescriptors checks the given descriptors against the invariant check
// function and adjusts the high-water or error timestamp appropriately. It is
// required that the descriptors represent a transactional kv read between the
// two given timestamps.
func (m *tableHistory) IngestDescriptors(
startTS, endTS hlc.Timestamp, descs []*sqlbase.TableDescriptor,
) error {
sort.Slice(descs, func(i, j int) bool {
return descs[i].ModificationTime.Less(descs[j].ModificationTime)
})
var validateErr error
for _, desc := range descs {
if err := m.validateFn(desc); validateErr == nil {
validateErr = err
}
}
if validateErr != nil {
m.mu.Lock()
defer m.mu.Unlock()
// don't care about startTS in the invalid case
if m.mu.errTS == (hlc.Timestamp{}) || endTS.Less(m.mu.errTS) {
m.mu.errTS = endTS
m.mu.err = validateErr
newWaiters := make([]tableHistoryWaiter, 0, len(m.mu.waiters))
for _, w := range m.mu.waiters {
if w.ts.Less(m.mu.errTS) {
newWaiters = append(newWaiters, w)
continue
}
w.errCh <- validateErr
}
m.mu.waiters = newWaiters
}
return validateErr
}
m.mu.Lock()
defer m.mu.Unlock()
if m.mu.highWater.Less(startTS) {
return errors.Errorf(`gap between %s and %s`, m.mu.highWater, startTS)
}
if m.mu.highWater.Less(endTS) {
m.mu.highWater = endTS
newWaiters := make([]tableHistoryWaiter, 0, len(m.mu.waiters))
for _, w := range m.mu.waiters {
if m.mu.highWater.Less(w.ts) {
newWaiters = append(newWaiters, w)
continue
}
w.errCh <- nil
}
m.mu.waiters = newWaiters
}
return nil
}
type tableHistoryUpdater struct {
settings *cluster.Settings
db *client.DB
targets map[sqlbase.ID]string
m *tableHistory
}
func (u *tableHistoryUpdater) PollTableDescs(ctx context.Context) error {
// TODO(dan): Replace this with a RangeFeed once it stabilizes.
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(changefeedPollInterval.Get(&u.settings.SV)):
}
startTS, endTS := u.m.HighWater(), u.db.Clock().Now()
if !startTS.Less(endTS) {
continue
}
descs, err := fetchTableDescriptorVersions(ctx, u.db, startTS, endTS, u.targets)
if err != nil {
return err
}
if err := u.m.IngestDescriptors(startTS, endTS, descs); err != nil {
return err
}
}
}
func fetchTableDescriptorVersions(
ctx context.Context, db *client.DB, startTS, endTS hlc.Timestamp, targets map[sqlbase.ID]string,
) ([]*sqlbase.TableDescriptor, error) {
if log.V(2) {
log.Infof(ctx, `fetching table descs [%s,%s)`, startTS, endTS)
}
start := timeutil.Now()
span := roachpb.Span{Key: keys.MakeTablePrefix(keys.DescriptorTableID)}
span.EndKey = span.Key.PrefixEnd()
header := roachpb.Header{Timestamp: endTS}
req := &roachpb.ExportRequest{
RequestHeader: roachpb.RequestHeaderFromSpan(span),
StartTime: startTS,
MVCCFilter: roachpb.MVCCFilter_All,
ReturnSST: true,
}
res, pErr := client.SendWrappedWith(ctx, db.NonTransactionalSender(), header, req)
if log.V(2) {
log.Infof(ctx, `fetched table descs [%s,%s) took %s`, startTS, endTS, timeutil.Since(start))
}
if pErr != nil {
return nil, errors.Wrapf(
pErr.GoError(), `fetching changes for [%s,%s)`, span.Key, span.EndKey)
}
var tableDescs []*sqlbase.TableDescriptor
for _, file := range res.(*roachpb.ExportResponse).Files {
if err := func() error {
it, err := engineccl.NewMemSSTIterator(file.SST, false /* verify */)
if err != nil {
return err
}
defer it.Close()
for it.Seek(engine.NilKey); ; it.Next() {
if ok, err := it.Valid(); err != nil {
return err
} else if !ok {
return nil
}
remaining, _, _, err := sqlbase.DecodeTableIDIndexID(it.UnsafeKey().Key)
if err != nil {
return err
}
_, tableID, err := encoding.DecodeUvarintAscending(remaining)
if err != nil {
return err
}
// WIP: I think targets currently doesn't contain interleaved
// parents if they are not watched by the changefeed, but this
// seems wrong.
origName, ok := targets[sqlbase.ID(tableID)]
if !ok {
// Uninteresting table.
continue
}
unsafeValue := it.UnsafeValue()
if unsafeValue == nil {
return errors.Errorf(`"%s" was dropped or truncated`, origName)
}
value := roachpb.Value{RawBytes: unsafeValue}
var desc sqlbase.Descriptor
if err := value.GetProto(&desc); err != nil {
return err
}
if tableDesc := desc.GetTable(); tableDesc != nil {
// WIP
log.Infof(ctx, "%s %d %s", desc.GetName(), tableDesc.Version, it.UnsafeKey().Timestamp)
tableDescs = append(tableDescs, tableDesc)
}
}
}(); err != nil {
return nil, err
}
}
return tableDescs, nil
}