-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
cmd_get.go
81 lines (73 loc) · 2.62 KB
/
cmd_get.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
// Copyright 2014 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 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/cockroachdb/cockroach/pkg/util/log"
)
func init() {
RegisterCommand(roachpb.Get, DefaultDeclareKeys, Get)
}
// Get returns the value for a specified key.
func Get(
ctx context.Context, batch engine.ReadWriter, cArgs CommandArgs, resp roachpb.Response,
) (result.Result, error) {
args := cArgs.Args.(*roachpb.GetRequest)
h := cArgs.Header
reply := resp.(*roachpb.GetResponse)
val, intent, err := engine.MVCCGet(ctx, batch, args.Key, h.Timestamp, engine.MVCCGetOptions{
Inconsistent: h.ReadConsistency != roachpb.CONSISTENT,
IgnoreSequence: shouldIgnoreSequenceNums(),
Txn: h.Txn,
})
if err != nil {
return result.Result{}, err
}
var intents []roachpb.Intent
if intent != nil {
intents = append(intents, *intent)
}
reply.Value = val
if h.ReadConsistency == roachpb.READ_UNCOMMITTED {
var intentVals []roachpb.KeyValue
intentVals, err = CollectIntentRows(ctx, batch, cArgs, intents)
if err == nil {
switch len(intentVals) {
case 0:
case 1:
reply.IntentValue = &intentVals[0].Value
default:
log.Fatalf(ctx, "more than 1 intent on single key: %v", intentVals)
}
}
}
return result.FromIntents(intents, args), err
}
func shouldIgnoreSequenceNums() bool {
// NOTE: In version 19.1 and below this checked if a cluster version was
// active. This was because Versions 2.1 and below did not properly
// propagate sequence numbers to leaf TxnCoordSenders, which meant that we
// couldn't rely on sequence numbers being properly assigned by those nodes.
// Therefore, we gated the use of sequence numbers while scanning on the
// cluster version.
//
// Because we checked this during batcheval instead of when sending a
// get/scan request with an associated flag on the request itself, 19.2
// clients can't immediately start relying on the correct sequence number
// behavior. Instead, they must wait until a new cluster version (19.2.X) is
// active which proves that all nodes are running version 19.2.
//
// TODO(nvanbenschoten): Remove in 20.1. This serves only as documentation
// now.
return false
}