This repository has been archived by the owner on Feb 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
source.go
502 lines (425 loc) · 14 KB
/
source.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
package gps
import (
"context"
"errors"
"fmt"
"sync"
"github.com/sdboyer/gps/pkgtree"
)
// sourceState represent the states that a source can be in, depending on how
// much search and discovery work ahs been done by a source's managing gateway.
//
// These are basically used to achieve a cheap approximation of a FSM.
type sourceState int32
const (
sourceIsSetUp sourceState = 1 << iota
sourceExistsUpstream
sourceExistsLocally
sourceHasLatestVersionList
sourceHasLatestLocally
)
type srcReturnChans struct {
ret chan *sourceGateway
err chan error
}
func (rc srcReturnChans) awaitReturn() (sg *sourceGateway, err error) {
select {
case sg = <-rc.ret:
case err = <-rc.err:
}
return
}
type sourceCoordinator struct {
supervisor *supervisor
srcmut sync.RWMutex // guards srcs and nameToURL maps
srcs map[string]*sourceGateway
nameToURL map[string]string
psrcmut sync.Mutex // guards protoSrcs map
protoSrcs map[string][]srcReturnChans
deducer deducer
cachedir string
}
func newSourceCoordinator(superv *supervisor, deducer deducer, cachedir string) *sourceCoordinator {
return &sourceCoordinator{
supervisor: superv,
deducer: deducer,
cachedir: cachedir,
srcs: make(map[string]*sourceGateway),
nameToURL: make(map[string]string),
protoSrcs: make(map[string][]srcReturnChans),
}
}
func (sc *sourceCoordinator) getSourceGatewayFor(ctx context.Context, id ProjectIdentifier) (*sourceGateway, error) {
if sc.supervisor.getLifetimeContext().Err() != nil {
return nil, errors.New("sourceCoordinator has been terminated")
}
normalizedName := id.normalizedSource()
sc.srcmut.RLock()
if url, has := sc.nameToURL[normalizedName]; has {
srcGate, has := sc.srcs[url]
sc.srcmut.RUnlock()
if has {
return srcGate, nil
}
panic(fmt.Sprintf("%q was URL for %q in nameToURL, but no corresponding srcGate in srcs map", url, normalizedName))
}
sc.srcmut.RUnlock()
// No gateway exists for this path yet; set up a proto, being careful to fold
// together simultaneous attempts on the same path.
rc := srcReturnChans{
ret: make(chan *sourceGateway),
err: make(chan error),
}
// The rest of the work needs its own goroutine, the results of which will
// be re-joined to this call via the return chans.
go sc.setUpSourceGateway(ctx, normalizedName, rc)
return rc.awaitReturn()
}
// Not intended to be called externally - call getSourceGatewayFor instead.
func (sc *sourceCoordinator) setUpSourceGateway(ctx context.Context, normalizedName string, rc srcReturnChans) {
sc.psrcmut.Lock()
if chans, has := sc.protoSrcs[normalizedName]; has {
// Another goroutine is already working on this normalizedName. Fold
// in with that work by attaching our return channels to the list.
sc.protoSrcs[normalizedName] = append(chans, rc)
sc.psrcmut.Unlock()
return
}
sc.protoSrcs[normalizedName] = []srcReturnChans{rc}
sc.psrcmut.Unlock()
doReturn := func(sg *sourceGateway, err error) {
sc.psrcmut.Lock()
if sg != nil {
for _, rc := range sc.protoSrcs[normalizedName] {
rc.ret <- sg
}
} else if err != nil {
for _, rc := range sc.protoSrcs[normalizedName] {
rc.err <- err
}
} else {
panic("sg and err both nil")
}
delete(sc.protoSrcs, normalizedName)
sc.psrcmut.Unlock()
}
pd, err := sc.deducer.deduceRootPath(ctx, normalizedName)
if err != nil {
// As in the deducer, don't cache errors so that externally-driven retry
// strategies can be constructed.
doReturn(nil, err)
return
}
// It'd be quite the feat - but not impossible - for a gateway
// corresponding to this normalizedName to have slid into the main
// sources map after the initial unlock, but before this goroutine got
// scheduled. Guard against that by checking the main sources map again
// and bailing out if we find an entry.
var srcGate *sourceGateway
sc.srcmut.RLock()
if url, has := sc.nameToURL[normalizedName]; has {
if srcGate, has := sc.srcs[url]; has {
sc.srcmut.RUnlock()
doReturn(srcGate, nil)
return
}
panic(fmt.Sprintf("%q was URL for %q in nameToURL, but no corresponding srcGate in srcs map", url, normalizedName))
}
sc.srcmut.RUnlock()
srcGate = newSourceGateway(pd.mb, sc.supervisor, sc.cachedir)
// The normalized name is usually different from the source URL- e.g.
// github.com/sdboyer/gps vs. https://github.com/sdboyer/gps. But it's
// possible to arrive here with a full URL as the normalized name - and
// both paths *must* lead to the same sourceGateway instance in order to
// ensure disk access is correctly managed.
//
// Therefore, we now must query the sourceGateway to get the actual
// sourceURL it's operating on, and ensure it's *also* registered at
// that path in the map. This will cause it to actually initiate the
// maybeSource.try() behavior in order to settle on a URL.
url, err := srcGate.sourceURL(ctx)
if err != nil {
doReturn(nil, err)
return
}
// We know we have a working srcGateway at this point, and need to
// integrate it back into the main map.
sc.srcmut.Lock()
defer sc.srcmut.Unlock()
// Record the name -> URL mapping, even if it's a self-mapping.
sc.nameToURL[normalizedName] = url
if sa, has := sc.srcs[url]; has {
// URL already had an entry in the main map; use that as the result.
doReturn(sa, nil)
return
}
sc.srcs[url] = srcGate
doReturn(srcGate, nil)
}
// sourceGateways manage all incoming calls for data from sources, serializing
// and caching them as needed.
type sourceGateway struct {
cachedir string
maybe maybeSource
srcState sourceState
src source
cache singleSourceCache
mu sync.Mutex // global lock, serializes all behaviors
suprvsr *supervisor
}
func newSourceGateway(maybe maybeSource, superv *supervisor, cachedir string) *sourceGateway {
sg := &sourceGateway{
maybe: maybe,
cachedir: cachedir,
suprvsr: superv,
}
sg.cache = sg.createSingleSourceCache()
return sg
}
func (sg *sourceGateway) syncLocal(ctx context.Context) error {
sg.mu.Lock()
defer sg.mu.Unlock()
_, err := sg.require(ctx, sourceIsSetUp|sourceExistsLocally|sourceHasLatestLocally)
return err
}
func (sg *sourceGateway) existsInCache(ctx context.Context) bool {
sg.mu.Lock()
defer sg.mu.Unlock()
_, err := sg.require(ctx, sourceIsSetUp|sourceExistsLocally)
if err != nil {
return false
}
return sg.srcState&sourceExistsLocally != 0
}
func (sg *sourceGateway) existsUpstream(ctx context.Context) bool {
sg.mu.Lock()
defer sg.mu.Unlock()
_, err := sg.require(ctx, sourceIsSetUp|sourceExistsUpstream)
if err != nil {
return false
}
return sg.srcState&sourceExistsUpstream != 0
}
func (sg *sourceGateway) exportVersionTo(ctx context.Context, v Version, to string) error {
sg.mu.Lock()
defer sg.mu.Unlock()
_, err := sg.require(ctx, sourceIsSetUp|sourceExistsLocally)
if err != nil {
return err
}
r, err := sg.convertToRevision(ctx, v)
if err != nil {
return err
}
return sg.suprvsr.do(ctx, sg.src.upstreamURL(), ctExportTree, func(ctx context.Context) error {
return sg.src.exportRevisionTo(ctx, r, to)
})
}
func (sg *sourceGateway) getManifestAndLock(ctx context.Context, pr ProjectRoot, v Version, an ProjectAnalyzer) (Manifest, Lock, error) {
sg.mu.Lock()
defer sg.mu.Unlock()
r, err := sg.convertToRevision(ctx, v)
if err != nil {
return nil, nil, err
}
m, l, has := sg.cache.getManifestAndLock(r, an)
if has {
return m, l, nil
}
_, err = sg.require(ctx, sourceIsSetUp|sourceExistsLocally)
if err != nil {
return nil, nil, err
}
name, vers := an.Info()
label := fmt.Sprintf("%s:%s.%v", sg.src.upstreamURL(), name, vers)
err = sg.suprvsr.do(ctx, label, ctGetManifestAndLock, func(ctx context.Context) error {
m, l, err = sg.src.getManifestAndLock(ctx, pr, r, an)
return err
})
if err != nil {
return nil, nil, err
}
sg.cache.setManifestAndLock(r, an, m, l)
return m, l, nil
}
// FIXME ProjectRoot input either needs to parameterize the cache, or be
// incorporated on the fly on egress...?
func (sg *sourceGateway) listPackages(ctx context.Context, pr ProjectRoot, v Version) (pkgtree.PackageTree, error) {
sg.mu.Lock()
defer sg.mu.Unlock()
r, err := sg.convertToRevision(ctx, v)
if err != nil {
return pkgtree.PackageTree{}, err
}
ptree, has := sg.cache.getPackageTree(r)
if has {
return ptree, nil
}
_, err = sg.require(ctx, sourceIsSetUp|sourceExistsLocally)
if err != nil {
return pkgtree.PackageTree{}, err
}
label := fmt.Sprintf("%s:%s", pr, sg.src.upstreamURL())
err = sg.suprvsr.do(ctx, label, ctListPackages, func(ctx context.Context) error {
ptree, err = sg.src.listPackages(ctx, pr, r)
return err
})
if err != nil {
return pkgtree.PackageTree{}, err
}
sg.cache.setPackageTree(r, ptree)
return ptree, nil
}
func (sg *sourceGateway) convertToRevision(ctx context.Context, v Version) (Revision, error) {
// When looking up by Version, there are four states that may have
// differing opinions about version->revision mappings:
//
// 1. The upstream source/repo (canonical)
// 2. The local source/repo
// 3. The local cache
// 4. The input (params to this method)
//
// If the input differs from any of the above, it's likely because some lock
// got written somewhere with a version/rev pair that has since changed or
// been removed. But correct operation dictates that such a mis-mapping be
// respected; if the mis-mapping is to be corrected, it has to be done
// intentionally by the caller, not automatically here.
r, has := sg.cache.toRevision(v)
if has {
return r, nil
}
if sg.srcState&sourceHasLatestVersionList != 0 {
// We have the latest version list already and didn't get a match, so
// this is definitely a failure case.
return "", fmt.Errorf("version %q does not exist in source", v)
}
// The version list is out of date; it's possible this version might
// show up after loading it.
_, err := sg.require(ctx, sourceIsSetUp|sourceHasLatestVersionList)
if err != nil {
return "", err
}
r, has = sg.cache.toRevision(v)
if !has {
return "", fmt.Errorf("version %q does not exist in source", v)
}
return r, nil
}
func (sg *sourceGateway) listVersions(ctx context.Context) ([]PairedVersion, error) {
sg.mu.Lock()
defer sg.mu.Unlock()
// TODO(sdboyer) The problem here is that sourceExistsUpstream may not be
// sufficient (e.g. bzr, hg), but we don't want to force local b/c git
// doesn't need it
_, err := sg.require(ctx, sourceIsSetUp|sourceExistsUpstream|sourceHasLatestVersionList)
if err != nil {
return nil, err
}
return sg.cache.getAllVersions(), nil
}
func (sg *sourceGateway) revisionPresentIn(ctx context.Context, r Revision) (bool, error) {
sg.mu.Lock()
defer sg.mu.Unlock()
_, err := sg.require(ctx, sourceIsSetUp|sourceExistsLocally)
if err != nil {
return false, err
}
if _, exists := sg.cache.getVersionsFor(r); exists {
return true, nil
}
present, err := sg.src.revisionPresentIn(r)
if err == nil && present {
sg.cache.markRevisionExists(r)
}
return present, err
}
func (sg *sourceGateway) sourceURL(ctx context.Context) (string, error) {
sg.mu.Lock()
defer sg.mu.Unlock()
_, err := sg.require(ctx, sourceIsSetUp)
if err != nil {
return "", err
}
return sg.src.upstreamURL(), nil
}
// createSingleSourceCache creates a singleSourceCache instance for use by
// the encapsulated source.
func (sg *sourceGateway) createSingleSourceCache() singleSourceCache {
// TODO(sdboyer) when persistent caching is ready, just drop in the creation
// of a source-specific handle here
return newMemoryCache()
}
func (sg *sourceGateway) require(ctx context.Context, wanted sourceState) (errState sourceState, err error) {
todo := (^sg.srcState) & wanted
var flag sourceState = 1
for todo != 0 {
if todo&flag != 0 {
// Assign the currently visited bit to errState so that we can
// return easily later.
//
// Also set up addlState so that individual ops can easily attach
// more states that were incidentally satisfied by the op.
errState = flag
var addlState sourceState
switch flag {
case sourceIsSetUp:
sg.src, addlState, err = sg.maybe.try(ctx, sg.cachedir, sg.cache, sg.suprvsr)
case sourceExistsUpstream:
err = sg.suprvsr.do(ctx, sg.src.sourceType(), ctSourcePing, func(ctx context.Context) error {
if !sg.src.existsUpstream(ctx) {
return fmt.Errorf("%s does not exist upstream", sg.src.upstreamURL())
}
return nil
})
case sourceExistsLocally:
if !sg.src.existsLocally(ctx) {
err = sg.suprvsr.do(ctx, sg.src.sourceType(), ctSourceInit, func(ctx context.Context) error {
return sg.src.initLocal(ctx)
})
if err == nil {
addlState |= sourceHasLatestLocally
} else {
err = fmt.Errorf("%s does not exist in the local cache and fetching failed: %s", sg.src.upstreamURL(), err)
}
}
case sourceHasLatestVersionList:
var pvl []PairedVersion
err = sg.suprvsr.do(ctx, sg.src.sourceType(), ctListVersions, func(ctx context.Context) error {
pvl, err = sg.src.listVersions(ctx)
return err
})
if err != nil {
sg.cache.storeVersionMap(pvl, true)
}
case sourceHasLatestLocally:
err = sg.suprvsr.do(ctx, sg.src.sourceType(), ctSourceFetch, func(ctx context.Context) error {
return sg.src.updateLocal(ctx)
})
}
if err != nil {
return
}
checked := flag | addlState
sg.srcState |= checked
todo &= ^checked
}
flag <<= 1
}
return 0, nil
}
// source is an abstraction around the different underlying types (git, bzr, hg,
// svn, maybe raw on-disk code, and maybe eventually a registry) that can
// provide versioned project source trees.
type source interface {
existsLocally(context.Context) bool
existsUpstream(context.Context) bool
upstreamURL() string
initLocal(context.Context) error
updateLocal(context.Context) error
listVersions(context.Context) ([]PairedVersion, error)
getManifestAndLock(context.Context, ProjectRoot, Revision, ProjectAnalyzer) (Manifest, Lock, error)
listPackages(context.Context, ProjectRoot, Revision) (pkgtree.PackageTree, error)
revisionPresentIn(Revision) (bool, error)
exportRevisionTo(context.Context, Revision, string) error
sourceType() string
}