-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathcmd_update_read.go
87 lines (78 loc) · 2.75 KB
/
cmd_update_read.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
// Copyright 2014 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package batcheval
import (
"context"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/storage/batcheval/result"
"github.com/cockroachdb/cockroach/pkg/storage/engine"
"github.com/pkg/errors"
)
func init() {
RegisterCommand(roachpb.UpdateRead, DefaultDeclareKeys, UpdateRead)
}
// UpdateRead scans the key range specified by start key through end
// key, and returns an error on any keys written more recently than
// the txn's original timestamp and less recently than the txn's
// current timestamp.
func UpdateRead(
ctx context.Context, batch engine.ReadWriter, cArgs CommandArgs, resp roachpb.Response,
) (result.Result, error) {
args := cArgs.Args.(*roachpb.UpdateReadRequest)
h := cArgs.Header
if h.Txn != nil {
return result.Result{}, ErrTransactionUnsupported
}
// Use a time-bounded iterator to avoid unnecessarily iterating over
// older data.
iter := batch.NewTimeBoundIterator(h.Txn.OrigTimestamp, h.Timestamp.Next())
defer iter.Close()
// Iterate over values until we discover any value written after the
// original timestamp. Note that we do not use the current txn and
// the iteration is done with consistent=false. This reads only
// committed values and returns skipped intents.
intents, err := engine.MVCCIterateUsingIter(
ctx,
batch,
args.Key,
args.EndKey,
h.Timestamp,
false, /* consistent */
nil, /* txn */
false, /* reverse */
iter,
func(kv roachpb.KeyValue) (bool, error) {
if ts := kv.Value.Timestamp; h.Txn.OrigTimestamp.Less(ts) {
return true, errors.Errorf("encountered recently written key %s @%s", kv.Key, ts)
}
return false, nil
},
)
if err != nil {
return result.Result{}, err
}
// Now, check intents slice for any which were written earlier than
// the command's timestamp, not owned by this transaction.
for _, i := range intents {
// Ignore our own intents.
if i.Txn.ID == h.Txn.ID {
continue
}
// Return an error if a new intent was written to the span.
if ts := i.Txn.Timestamp; h.Txn.OrigTimestamp.Less(ts) {
return result.Result{}, errors.Errorf("encountered recently written intent %s @%s", i.Span.Key, ts)
}
}
return result.Result{}, nil
}