OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package coordinator | 5 package coordinator |
6 | 6 |
7 import ( | 7 import ( |
8 "github.com/luci/luci-go/appengine/logdog/coordinator/config" | 8 "github.com/luci/luci-go/appengine/logdog/coordinator/config" |
9 "github.com/luci/luci-go/common/gcloud/gs" | 9 "github.com/luci/luci-go/common/gcloud/gs" |
10 log "github.com/luci/luci-go/common/logging" | 10 log "github.com/luci/luci-go/common/logging" |
11 "github.com/luci/luci-go/server/logdog/storage" | 11 "github.com/luci/luci-go/server/logdog/storage" |
| 12 "github.com/luci/luci-go/server/logdog/storage/archive" |
12 "golang.org/x/net/context" | 13 "golang.org/x/net/context" |
13 ) | 14 ) |
14 | 15 |
15 // Service is the base service container for LogDog handlers and endpoints. It | 16 // Service is the base service container for LogDog handlers and endpoints. It |
16 // is primarily usable as a means of consistently stubbing out various external | 17 // is primarily usable as a means of consistently stubbing out various external |
17 // components. | 18 // components. |
18 type Service struct { | 19 type Service struct { |
19 » // StorageFunc is a function that generates an intermediate Storage inst
ance | 20 » // IntermediateStorageFunc is a function that generates an intermediate
Storage instance |
20 // for use by this service. If nil, the production intermediate Storage | 21 // for use by this service. If nil, the production intermediate Storage |
21 // instance will be used. | 22 // instance will be used. |
22 // | 23 // |
23 // This is provided for testing purposes. | 24 // This is provided for testing purposes. |
24 » StorageFunc func(context.Context) (storage.Storage, error) | 25 » IntermediateStorageFunc func(context.Context) (storage.Storage, error) |
| 26 |
| 27 » // ArchiveStorageFunc returns Archive storage for the specified log stre
am. |
| 28 » ArchiveStorageFunc func(context.Context, *LogStream) (storage.Storage, e
rror) |
25 | 29 |
26 // GSClientFunc is a function that generates a Google Storage client ins
tance | 30 // GSClientFunc is a function that generates a Google Storage client ins
tance |
27 // for use by this service. If nil, the production Google Storage Client
will | 31 // for use by this service. If nil, the production Google Storage Client
will |
28 // be used. | 32 // be used. |
29 GSClientFunc func(context.Context) (gs.Client, error) | 33 GSClientFunc func(context.Context) (gs.Client, error) |
30 } | 34 } |
31 | 35 |
32 // Storage retrieves the configured Storage instance. | 36 // StorageForLogStream returns the Storage instance to use for a given |
33 func (s *Service) Storage(c context.Context) (storage.Storage, error) { | 37 // LogStream. |
34 » sf := s.StorageFunc | 38 // |
35 » if sf == nil { | 39 // The caller is responsible for calling Close on the returned Storage instance |
36 » » // Production: use BigTable storage. | 40 // when finished. |
37 » » sf = config.GetStorage | 41 func (s *Service) StorageForLogStream(c context.Context, ls *LogStream) (storage
.Storage, error) { |
| 42 » if !ls.Archived() { |
| 43 » » log.Debugf(c, "Log is not archived. Fetching from intermediate s
torage.") |
| 44 |
| 45 » » // Logs are not archived. Fetch from intermediate storage. |
| 46 » » return s.IntermediateStorage(c) |
38 } | 47 } |
39 | 48 |
40 » st, err := sf(c) | 49 » log.Debugf(c, "Log is archived. Fetching from archive storage.") |
| 50 » return s.ArchiveStorage(c, ls) |
| 51 } |
| 52 |
| 53 // IntermediateStorage retrieves the configured intermediate Storage instance. |
| 54 // The caller is responsible for calling Close on the returned Storage instance |
| 55 // when finished. |
| 56 func (s *Service) IntermediateStorage(c context.Context) (storage.Storage, error
) { |
| 57 » if sf := s.IntermediateStorageFunc; sf != nil { |
| 58 » » return sf(c) |
| 59 » } |
| 60 |
| 61 » // Production: use BigTable storage. |
| 62 » st, err := config.GetStorage(c) |
41 if err != nil { | 63 if err != nil { |
42 » » log.Errorf(log.SetError(c, err), "Failed to get Storage instance
.") | 64 » » log.Errorf(log.SetError(c, err), "Failed to get BigTable Storage
instance.") |
43 return nil, err | 65 return nil, err |
44 } | 66 } |
45 return st, nil | 67 return st, nil |
46 } | 68 } |
47 | 69 |
48 // GSClient instantiates a Google Storage client. | 70 // ArchiveStorage retrieves the configured archive Storage instance. |
49 func (s *Service) GSClient(c context.Context) (gs.Client, error) { | 71 // The caller is responsible for calling Close on the returned Storage instance |
50 » f := s.GSClientFunc | 72 // when finished. |
51 » if f == nil { | 73 func (s *Service) ArchiveStorage(c context.Context, ls *LogStream) (storage.Stor
age, error) { |
52 » » f = gs.NewProdClient | 74 » if sf := s.ArchiveStorageFunc; sf != nil { |
| 75 » » return sf(c, ls) |
53 } | 76 } |
54 | 77 |
55 » gsc, err := f(c) | 78 » gs, err := s.GSClient(c) |
56 if err != nil { | 79 if err != nil { |
57 » » log.Errorf(log.SetError(c, err), "Failed to get Google Storage c
lient.") | 80 » » log.WithError(err).Errorf(c, "Failed to create Google Storage cl
ient.") |
| 81 » » return nil, err |
| 82 » } |
| 83 |
| 84 » st, err := archive.New(c, archive.Options{ |
| 85 » » IndexURL: ls.ArchiveIndexURL, |
| 86 » » StreamURL: ls.ArchiveStreamURL, |
| 87 » » Client: gs, |
| 88 » }) |
| 89 » if err != nil { |
| 90 » » log.WithError(err).Errorf(c, "Failed to create Google Storage st
orage instance.") |
| 91 » » return nil, err |
| 92 » } |
| 93 » return st, nil |
| 94 } |
| 95 |
| 96 // GSClient instantiates a Google Storage client. The caller is responsible |
| 97 // for closing the client. |
| 98 func (s *Service) GSClient(c context.Context) (gs.Client, error) { |
| 99 » if f := s.GSClientFunc; f != nil { |
| 100 » » return f(c) |
| 101 » } |
| 102 |
| 103 » gsc, err := gs.NewProdClient(c) |
| 104 » if err != nil { |
| 105 » » log.Errorf(log.SetError(c, err), "Failed to get production Googl
e Storage client.") |
58 return nil, err | 106 return nil, err |
59 } | 107 } |
60 return gsc, nil | 108 return gsc, nil |
61 } | 109 } |
OLD | NEW |