-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnamespace.go
359 lines (299 loc) · 9.29 KB
/
namespace.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package mapper
import (
"fmt"
"strconv"
"strings"
"io/ioutil"
"github.com/captaincodeman/datastore-locker"
"golang.org/x/net/context"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
apistorage "google.golang.org/api/storage/v1"
"google.golang.org/appengine"
"google.golang.org/appengine/datastore"
"google.golang.org/appengine/log"
"google.golang.org/appengine/taskqueue"
)
type (
// namespace processes a single namespace for a job
namespace struct {
locker.Lock
common `datastore:"-"`
// Namespace is the namespace to process
Namespace string `datastore:"namespace,noindex"`
// ShardsTotal is the total number of shards generated for this namespace
ShardsTotal int `datastore:"shards_total,noindex"`
// ShardsSuccessful is the number of shards completed successfully
ShardsSuccessful int `datastore:"shards_successful,noindex"`
// ShardsFailed is the number of shards failed
ShardsFailed int `datastore:"shards_failed,noindex"`
}
)
const (
namespaceKind = "namespace"
)
func (n *namespace) setJob(job *job) {
n.job = job
}
func (n *namespace) copyFrom(x namespace) {
n.Lock = x.Lock
n.common = x.common
n.Namespace = x.Namespace
n.ShardsTotal = x.ShardsTotal
n.ShardsSuccessful = x.ShardsSuccessful
n.ShardsFailed = x.ShardsFailed
}
func (n *namespace) jobID() string {
parts := strings.Split(n.id, "/")
return parts[0] + "/" + parts[1]
}
func (n *namespace) jobKey(c context.Context, config Config) *datastore.Key {
return datastore.NewKey(c, config.DatastorePrefix+jobKind, n.jobID(), 0, nil)
}
func (n *namespace) namespaceFilename() string {
parts := strings.Split(n.id, "/")
ns := parts[2]
if ns == "" {
ns = "~"
}
// mapper type / unique id / namespace
// TODO: set filename or at least extension
return parts[0] + "/" + parts[1] + "/" + ns + ".json"
}
func (n *namespace) shardFilename(shard int) string {
parts := strings.Split(n.id, "/")
ns := parts[2]
if ns == "" {
ns = "~"
}
// mapper type / unique id / namespace / shard
// TODO: set filename or at least extension
return parts[0] + "/" + parts[1] + "/" + ns + "/" + strconv.Itoa(shard) + ".json"
}
func (n *namespace) split(c context.Context, mapper *mapper) error {
queries, err := n.Query.split(c, n.job.Shards, mapper.config.Oversampling)
if err != nil {
return err
}
n.ShardsTotal = len(queries)
// from original source:
// Even though this method does not schedule shard task and save shard state
// transactionally, it's safe for taskqueue to retry this logic because
// the initial shard_state for each shard is the same from any retry.
// This is an important yet reasonable assumption on shard.
// I'm not so sure - it feels like the query splitting could easily return
// a different set of values unless the datastore is in read-only mode ...
// so maybe have one task to split the query, then another (that can be
// repeated) which does the shard scheduling ...
keys := make([]*datastore.Key, n.ShardsTotal)
for i := 0; i < n.ShardsTotal; i++ {
id := fmt.Sprintf("%s/%d", n.id, i)
key := datastore.NewKey(c, mapper.config.DatastorePrefix+shardKind, id, 0, nil)
keys[i] = key
}
shards := make([]*shard, n.ShardsTotal)
if err := datastore.GetMulti(c, keys, shards); err != nil {
if me, ok := err.(appengine.MultiError); ok {
for i, merr := range me {
if merr == datastore.ErrNoSuchEntity {
// keys[i] is missing, create the shard for it
k := keys[i]
q := queries[i].Namespace(n.Namespace)
s := new(shard)
s.start(q)
s.Shard = i
s.Namespace = n.Namespace
s.Description = q.String()
if err := mapper.locker.Schedule(c, k, s, mapper.config.Path+shardURL, nil); err != nil {
return err
}
}
}
} else {
// some other error
return err
}
}
return nil
}
func (n *namespace) update(c context.Context, mapper *mapper, key *datastore.Key) error {
queue, ok := locker.QueueFromContext(c)
if !ok {
queue = mapper.config.DefaultQueue
}
// update namespace status within a transaction
return datastore.RunInTransaction(c, func(tc context.Context) error {
fresh := new(namespace)
if err := datastore.Get(tc, key, fresh); err != nil {
return err
}
// shards can already be processing ahead of this total being written
fresh.ShardsTotal = n.ShardsTotal
// if all shards have completed, schedule namespace/completed to update job
if fresh.ShardsSuccessful == fresh.ShardsTotal {
t := mapper.locker.NewTask(key, fresh, mapper.config.Path+namespaceCompleteURL, nil)
if _, err := taskqueue.Add(tc, t, queue); err != nil {
log.Errorf(c, "add task %s", err.Error())
return err
}
}
if _, err := datastore.Put(tc, key, fresh); err != nil {
return err
}
return nil
}, &datastore.TransactionOptions{XG: true})
}
func (n *namespace) completed(c context.Context, mapper *mapper, key *datastore.Key) error {
n.complete()
// update namespace status and job within a transaction
queue, ok := locker.QueueFromContext(c)
if !ok {
queue = mapper.config.DefaultQueue
}
fresh := new(namespace)
jobKey := n.jobKey(c, *mapper.config)
job := new(job)
return datastore.RunInTransaction(c, func(tc context.Context) error {
keys := []*datastore.Key{key, jobKey}
vals := []interface{}{fresh, job}
if err := datastore.GetMulti(tc, keys, vals); err != nil {
return err
}
if job.Abort {
return nil
}
fresh.copyFrom(*n)
fresh.Lock.Complete()
job.NamespacesSuccessful++
job.common.rollup(n.common)
if job.NamespacesSuccessful == job.NamespacesTotal && !job.Iterating {
t := mapper.locker.NewTask(jobKey, job, mapper.config.Path+jobCompleteURL, nil)
if _, err := taskqueue.Add(tc, t, queue); err != nil {
return err
}
}
if _, err := datastore.PutMulti(tc, keys, vals); err != nil {
return err
}
return nil
}, &datastore.TransactionOptions{XG: true})
}
// rollup shards into single namespace file
func (n *namespace) rollup(c context.Context) error {
// nothing to do if no output writing
if n.job.Bucket == "" {
return nil
}
var service *apistorage.Service
if appengine.IsDevAppServer() {
jsonKey, err := ioutil.ReadFile("service-account.json")
if err != nil {
return err
}
conf, err := google.JWTConfigFromJSON(jsonKey, apistorage.DevstorageReadWriteScope)
if err != nil {
return err
}
client := conf.Client(c)
service, err = apistorage.New(client)
if err != nil {
return err
}
} else {
var err error
token := google.AppEngineTokenSource(c, apistorage.DevstorageReadWriteScope)
client := oauth2.NewClient(c, token)
service, err = apistorage.New(client)
if err != nil {
return err
}
}
target := n.namespaceFilename()
sources := make([]string, n.ShardsTotal)
for i := 0; i < n.ShardsTotal; i++ {
sources[i] = n.shardFilename(i)
}
return n.combineFiles(c, service, target, sources)
}
func (n *namespace) combineFiles(c context.Context, service *apistorage.Service, target string, sources []string) error {
if _, err := service.Objects.Get(n.job.Bucket, target).Context(c).Do(); err != nil {
count := len(sources)
switch {
case count == 1:
// copy single file
_, err := service.Objects.Copy(n.job.Bucket, sources[0], n.job.Bucket, target, nil).Context(c).Do()
return err
case count <= 32:
// combine source files to target
log.Debugf(c, "compose %s from %d sources", target, count)
req := &apistorage.ComposeRequest{
Destination: &apistorage.Object{Name: target},
SourceObjects: make([]*apistorage.ComposeRequestSourceObjects, count),
}
for i := 0; i < count; i++ {
log.Debugf(c, "source %d %s", i, sources[i])
req.SourceObjects[i] = &apistorage.ComposeRequestSourceObjects{Name: sources[i]}
}
res, err := service.Objects.Compose(n.job.Bucket, target, req).Context(c).Do()
if err != nil {
return err
}
log.Debugf(c, "created file %s gen %d %d bytes", res.Name, res.Generation, res.Size)
case count > 32:
// rollup to intermediate files
steps := ((count - 1) / 32) + 1
targets := make([]string, steps)
for i := 0; i < steps; i++ {
targets[i] = fmt.Sprintf("%s-%d", target, i)
start := i * 32
stop := i*32 + 32
if stop > count {
stop = count
}
if err := n.combineFiles(c, service, targets[i], sources[start:stop]); err != nil {
return err
}
}
if err := n.combineFiles(c, service, target, targets); err != nil {
return err
}
}
// delete source files
for i := 0; i < count; i++ {
log.Debugf(c, "delete %s", sources[i])
if err := service.Objects.Delete(n.job.Bucket, sources[i]).Context(c).Do(); err != nil {
// TODO: check for 404 error and ignore but return err for failures (to ensure cleanup)
return nil
}
}
}
return nil
}
// Load implements the datastore PropertyLoadSaver imterface
func (n *namespace) Load(props []datastore.Property) error {
datastore.LoadStruct(n, props)
n.common.Load(props)
for _, prop := range props {
switch prop.Name {
case "query":
n.Query = &Query{}
if err := n.Query.GobDecode(prop.Value.([]byte)); err != nil {
return err
}
}
}
return nil
}
// Save implements the datastore PropertyLoadSaver imterface
func (n *namespace) Save() ([]datastore.Property, error) {
props, err := datastore.SaveStruct(n)
if err != nil {
return nil, err
}
cprops, err := n.common.Save()
if err != nil {
return nil, err
}
props = append(props, cprops...)
return props, nil
}