mirrored from https://chromium.googlesource.com/infra/luci/luci-go
-
Notifications
You must be signed in to change notification settings - Fork 43
/
read.go
227 lines (204 loc) · 6.34 KB
/
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
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
// Copyright 2020 The LUCI 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 invocations
import (
"context"
"cloud.google.com/go/spanner"
"github.com/golang/protobuf/proto"
"google.golang.org/grpc/codes"
"go.chromium.org/luci/common/errors"
"go.chromium.org/luci/common/trace"
"go.chromium.org/luci/grpc/appstatus"
"go.chromium.org/luci/server/span"
"go.chromium.org/luci/resultdb/internal/spanutil"
"go.chromium.org/luci/resultdb/pbutil"
pb "go.chromium.org/luci/resultdb/proto/v1"
)
// ReadColumns reads the specified columns from an invocation Spanner row.
// If the invocation does not exist, the returned error is annotated with
// NotFound GRPC code.
// For ptrMap see ReadRow comment in span/util.go.
func ReadColumns(ctx context.Context, id ID, ptrMap map[string]interface{}) error {
if id == "" {
return errors.Reason("id is unspecified").Err()
}
err := spanutil.ReadRow(ctx, "Invocations", id.Key(), ptrMap)
switch {
case spanner.ErrCode(err) == codes.NotFound:
return appstatus.Attachf(err, codes.NotFound, "%s not found", id.Name())
case err != nil:
return errors.Annotate(err, "failed to fetch %s", id.Name()).Err()
default:
return nil
}
}
func readMulti(ctx context.Context, ids IDSet, f func(id ID, inv *pb.Invocation) error) error {
if len(ids) == 0 {
return nil
}
st := spanner.NewStatement(`
SELECT
i.InvocationId,
i.State,
i.CreatedBy,
i.CreateTime,
i.FinalizeTime,
i.Deadline,
i.Tags,
i.BigQueryExports,
ARRAY(SELECT IncludedInvocationId FROM IncludedInvocations incl WHERE incl.InvocationID = i.InvocationId),
i.ProducerResource,
i.Realm,
FROM Invocations i
WHERE i.InvocationID IN UNNEST(@invIDs)
`)
st.Params = spanutil.ToSpannerMap(map[string]interface{}{
"invIDs": ids,
})
var b spanutil.Buffer
return spanutil.Query(ctx, st, func(row *spanner.Row) error {
var id ID
included := IDSet{}
var bqExports [][]byte
inv := &pb.Invocation{}
var createdBy spanner.NullString
var producerResource spanner.NullString
var realm spanner.NullString
err := b.FromSpanner(row, &id,
&inv.State,
&createdBy,
&inv.CreateTime,
&inv.FinalizeTime,
&inv.Deadline,
&inv.Tags,
&bqExports,
&included,
&producerResource,
&realm)
if err != nil {
return err
}
inv.Name = pbutil.InvocationName(string(id))
inv.IncludedInvocations = included.Names()
inv.CreatedBy = createdBy.StringVal
inv.ProducerResource = producerResource.StringVal
inv.Realm = realm.StringVal
if len(bqExports) > 0 {
inv.BigqueryExports = make([]*pb.BigQueryExport, len(bqExports))
for i, buf := range bqExports {
inv.BigqueryExports[i] = &pb.BigQueryExport{}
if err := proto.Unmarshal(buf, inv.BigqueryExports[i]); err != nil {
return errors.Annotate(err, "%s: failed to unmarshal BigQuery export", inv.Name).Err()
}
}
}
return f(id, inv)
})
}
// Read reads one invocation from Spanner.
// If the invocation does not exist, the returned error is annotated with
// NotFound GRPC code.
func Read(ctx context.Context, id ID) (*pb.Invocation, error) {
var ret *pb.Invocation
err := readMulti(ctx, NewIDSet(id), func(id ID, inv *pb.Invocation) error {
ret = inv
return nil
})
switch {
case err != nil:
return nil, err
case ret == nil:
return nil, appstatus.Errorf(codes.NotFound, "%s not found", id.Name())
default:
return ret, nil
}
}
// ReadBatch reads multiple invocations from Spanner.
// If any of them are not found, returns an error.
func ReadBatch(ctx context.Context, ids IDSet) (map[ID]*pb.Invocation, error) {
ret := make(map[ID]*pb.Invocation, len(ids))
err := readMulti(ctx, ids, func(id ID, inv *pb.Invocation) error {
if _, ok := ret[id]; ok {
panic("query is incorrect; it returned duplicated invocation IDs")
}
ret[id] = inv
return nil
})
if err != nil {
return nil, err
}
for id := range ids {
if _, ok := ret[id]; !ok {
return nil, appstatus.Errorf(codes.NotFound, "%s not found", id.Name())
}
}
return ret, nil
}
// ReadState returns the invocation's state.
func ReadState(ctx context.Context, id ID) (pb.Invocation_State, error) {
var state pb.Invocation_State
err := ReadColumns(ctx, id, map[string]interface{}{"State": &state})
return state, err
}
// ReadStateBatch reads the states of multiple invocations.
func ReadStateBatch(ctx context.Context, ids IDSet) (map[ID]pb.Invocation_State, error) {
ret := make(map[ID]pb.Invocation_State)
err := span.Read(ctx, "Invocations", ids.Keys(), []string{"InvocationID", "State"}).Do(func(r *spanner.Row) error {
var id ID
var s pb.Invocation_State
if err := spanutil.FromSpanner(r, &id, &s); err != nil {
return errors.Annotate(err, "failed to fetch %s", ids).Err()
}
ret[id] = s
return nil
})
if err != nil {
return nil, err
}
return ret, nil
}
// ReadRealm returns the invocation's realm.
func ReadRealm(ctx context.Context, id ID) (string, error) {
var realm string
err := ReadColumns(ctx, id, map[string]interface{}{"Realm": &realm})
return realm, err
}
// ReadRealms returns the invocations' realms.
// Makes a single RPC.
func ReadRealms(ctx context.Context, ids IDSet) (realms map[ID]string, err error) {
ctx, ts := trace.StartSpan(ctx, "resultdb.invocations.ReadRealms")
ts.Attribute("cr.dev/count", len(ids))
defer func() { ts.End(err) }()
realms = make(map[ID]string, len(ids))
var b spanutil.Buffer
err = span.Read(ctx, "Invocations", ids.Keys(), []string{"InvocationId", "Realm"}).Do(func(row *spanner.Row) error {
var id ID
var realm spanner.NullString
if err := b.FromSpanner(row, &id, &realm); err != nil {
return err
}
realms[id] = realm.StringVal
return nil
})
if err != nil {
return nil, err
}
// Return a NotFound error if ret is missing a requested invocation.
for id := range ids {
if _, ok := realms[id]; !ok {
return nil, appstatus.Errorf(codes.NotFound, "%s not found", id.Name())
}
}
return realms, nil
}