-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiterator.go
243 lines (200 loc) · 5.98 KB
/
iterator.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
package mapper
import (
"fmt"
"time"
"github.com/captaincodeman/datastore-locker"
"golang.org/x/net/context"
"google.golang.org/appengine/datastore"
"google.golang.org/appengine/log"
"google.golang.org/appengine/taskqueue"
)
type (
// iterator processes namespaces for a job
iterator struct {
locker.Lock
common `datastore:"-"`
// Cursor is the datastore cursor to start from
Cursor string `datastore:"cursor,noindex"`
}
)
const (
iteratorKind = "iterator"
)
func (it *iterator) setJob(job *job) {
it.job = job
}
func (it *iterator) copyFrom(x iterator) {
it.Lock = x.Lock
it.common = x.common
it.Cursor = x.Cursor
}
func (it *iterator) jobKey(c context.Context, config Config) *datastore.Key {
return datastore.NewKey(c, config.DatastorePrefix+jobKind, it.id, 0, nil)
}
func (it *iterator) jobID() string {
return it.id
}
func (it *iterator) process(c context.Context, mapper *mapper, namespaceStr string) error {
// process the namespace - split the query and create shards
log.Debugf(c, "process %s", namespaceStr)
it.Count++
id := fmt.Sprintf("%s/%s", it.id, namespaceStr)
key := datastore.NewKey(c, mapper.config.DatastorePrefix+namespaceKind, id, 0, nil)
q := it.Query.Namespace(namespaceStr)
ns := new(namespace)
ns.start(q)
ns.Namespace = namespaceStr
// TODO: what if *this* task is restarted - ns tasks
// would be re-created and could alreadt be executing
return mapper.locker.Schedule(c, key, ns, mapper.config.Path+namespaceURL, nil)
}
func (it *iterator) createQuery(c context.Context) *datastore.Query {
q := datastore.NewQuery("__namespace__")
switch it.Query.selection {
case all:
// no filter
case empty:
q = q.Filter("__key__ =", datastore.NewKey(c, "__namespace__", "", 1, nil))
case named:
q = q.Filter("__key__ >", datastore.NewKey(c, "__namespace__", "", 1, nil))
}
q = q.Order("__key__")
q = q.KeysOnly()
return q
}
func (it *iterator) iterate(c context.Context, mapper *mapper) (bool, error) {
taskTimeout := time.After(mapper.config.TaskTimeout)
taskRunning := true
// if the query defines the specific namespaces to process
// then we can just process that list directly
if it.Query.selection == selected {
for _, namespace := range it.Query.namespaces {
it.process(c, mapper, namespace)
}
return true, nil
}
q := it.createQuery(c)
var cursor *datastore.Cursor
if it.Cursor != "" {
newCursor, err := datastore.DecodeCursor(it.Cursor)
if err != nil {
log.Errorf(c, "get start cursor error %s", err.Error())
return false, err
}
cursor = &newCursor
}
// main task loop to repeat datastore query with cursor
for taskRunning {
// if cursor is set, start the query at that point
if cursor != nil {
q = q.Start(*cursor)
}
// limit how long the cursor can run before we requery
cursorTimeout := time.After(mapper.config.CursorTimeout)
// datastore cursor context needs to run for the max allowed
cc, _ := context.WithTimeout(c, time.Duration(60)*time.Second)
t := q.Run(cc)
// item loop to iterate cursor
cursorLoop:
for {
key, err := t.Next(nil)
if err == datastore.Done {
// we reached the end
return true, nil
}
if err != nil {
log.Errorf(c, "error %s", err.Error())
return false, err
}
namespace := key.StringID()
if err := it.process(c, mapper, namespace); err != nil {
return false, err
}
select {
case <-taskTimeout:
// clearing the flag breaks us out of the task loop but also lets us update the
// cursor first when we break from the inner cursorLoop
taskRunning = false
break cursorLoop
default:
select {
case <-cursorTimeout:
// this forces a new cursor and query so we don't suffer from datastore timeouts
break cursorLoop
default:
// no timeout so carry on with the current cursor
continue cursorLoop
}
}
}
// we need to get the cursor for where we are upto whether we are requerying
// within this task or scheduling a new continuation slice
newCursor, err := t.Cursor()
if err != nil {
log.Errorf(c, "get next cursor error %s", err.Error())
return false, err
}
cursor = &newCursor
it.Cursor = cursor.String()
}
return false, nil
}
func (it *iterator) completed(c context.Context, mapper *mapper, key *datastore.Key) error {
// mark iterator as complete
it.complete()
// update iterator status and job within a transaction
queue, ok := locker.QueueFromContext(c)
if !ok {
queue = mapper.config.DefaultQueue
}
fresh := new(iterator)
jobKey := it.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(*it)
fresh.Lock.Complete()
job.NamespacesTotal += int(it.Count)
job.Iterating = false
// only the iterator walltime is rolled up into the job counts
job.WallTime += it.WallTime
// it's unlikely (but possible) that the shards and namespaces completed
// before this task so handle case that job is now also fully complete
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})
}
// Load implements the datastore PropertyLoadSaver imterface
func (it *iterator) Load(props []datastore.Property) error {
datastore.LoadStruct(it, props)
it.common.Load(props)
return nil
}
// Save implements the datastore PropertyLoadSaver imterface
func (it *iterator) Save() ([]datastore.Property, error) {
props, err := datastore.SaveStruct(it)
if err != nil {
return nil, err
}
cprops, err := it.common.Save()
if err != nil {
return nil, err
}
props = append(props, cprops...)
return props, nil
}