-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathtopology.go
214 lines (179 loc) · 5.57 KB
/
topology.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
package topology
import (
"database/sql"
"encoding/json"
"errors"
"os"
"os/signal"
"strconv"
"syscall"
"time"
"github.com/golang/protobuf/ptypes/any"
"github.com/uber-go/tally/v4"
"go.uber.org/zap"
"golang.org/x/net/context"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/types/known/structpb"
topologyv1cfg "github.com/lyft/clutch/backend/api/config/service/topology/v1"
topologyv1 "github.com/lyft/clutch/backend/api/topology/v1"
"github.com/lyft/clutch/backend/service"
pgservice "github.com/lyft/clutch/backend/service/db/postgres"
)
const Name = "clutch.service.topology"
type Service interface {
GetTopology(ctx context.Context) error
Search(ctx context.Context, search *topologyv1.SearchRequest) ([]*topologyv1.Resource, string, error)
Autocomplete(ctx context.Context, typeURL, search string, limit uint64, caseSensitive bool) ([]*topologyv1.Resource, error)
}
type client struct {
config *topologyv1cfg.Config
db *sql.DB
log *zap.Logger
scope tally.Scope
cacheTTL time.Duration
batchInsertSize int
batchInsertFlush time.Duration
}
// CacheableTopology is implemented by a service that wishes to enable the topology API feature set
//
// By implementing this interface the topology service will automatically setup all services which implement it.
// Automatically ingesting Resource objects via the `GetTopologyObjectChannel()` function.
// This enables users to make use of the Topology APIs with these new Topology Resources.
type CacheableTopology interface {
CacheEnabled() bool
// Notably the cache TTL is provided, this information can be used to ensure
// all active resources are added to the cache more often than the cache TTL.
StartTopologyCaching(ctx context.Context, ttl time.Duration) (<-chan *topologyv1.UpdateCacheRequest, error)
}
func New(cfg *any.Any, logger *zap.Logger, scope tally.Scope) (service.Service, error) {
topologyConfig := &topologyv1cfg.Config{}
err := cfg.UnmarshalTo(topologyConfig)
if err != nil {
return nil, err
}
p, ok := service.Registry[pgservice.Name]
if !ok {
return nil, errors.New("Please config the datastore [clutch.service.db.postgres] to use the topology service")
}
dbClient, ok := p.(pgservice.Client)
if !ok {
return nil, errors.New("Unable to get the datastore client")
}
c := &client{
config: topologyConfig,
db: dbClient.DB(),
log: logger,
scope: scope,
}
if topologyConfig.Cache == nil {
c.log.Info("Topology caching is disabled")
return c, nil
}
c.batchInsertSize = int(c.config.Cache.BatchInsertSize)
// If TTL is not set default to two hours
c.cacheTTL = time.Hour * 2
if c.config.Cache.Ttl != nil {
c.cacheTTL = c.config.Cache.Ttl.AsDuration()
}
// Default flush to 10 seconds
c.batchInsertFlush = time.Second * 10
if c.config.Cache.BatchInsertFlush != nil {
c.batchInsertFlush = c.config.Cache.BatchInsertFlush.AsDuration()
}
ctx, ctxCancelFunc := context.WithCancel(context.Background())
sigc := make(chan os.Signal, 1)
signal.Notify(
sigc,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGQUIT,
syscall.SIGTERM,
)
go func() {
<-sigc
c.log.Info("Caught shutdown signal, shutting down topology caching and releasing advisory lock")
ctxCancelFunc()
signal.Stop(sigc)
}()
c.log.Info("Topology caching is enabled")
go c.acquireTopologyCacheLock(ctx)
return c, nil
}
func (c *client) GetTopology(ctx context.Context) error {
return nil
}
func (c *client) Search(ctx context.Context, req *topologyv1.SearchRequest) ([]*topologyv1.Resource, string, error) {
query, nextPageToken, err := paginatedQueryBuilder(
req.Filter,
req.Sort,
req.PageToken,
req.Limit,
)
if err != nil {
return nil, "", err
}
rows, err := query.RunWith(c.db).Query()
if err != nil {
return nil, "", err
}
defer rows.Close()
results := []*topologyv1.Resource{}
for rows.Next() {
var id string
var data []byte
var metadata []byte
if err := rows.Scan(&id, &data, &metadata); err != nil {
c.log.Error("Error scanning row", zap.Error(err))
return nil, "", err
}
var dataAny any.Any
if err := protojson.Unmarshal(data, &dataAny); err != nil {
c.log.Error("Error unmarshaling data field", zap.Error(err))
return nil, "", err
}
var metadataMap map[string]*structpb.Value
if err := json.Unmarshal(metadata, &metadataMap); err != nil {
c.log.Error("Error unmarshaling metadata", zap.Error(err))
return nil, "", err
}
results = append(results, &topologyv1.Resource{
Id: id,
Pb: &dataAny,
Metadata: metadataMap,
})
}
// Rows.Err will report the last error encountered by Rows.Scan.
if err := rows.Err(); err != nil {
c.log.Error("Error processing rows for topology search query", zap.Error(err))
return nil, "", err
}
// if their are no results then set the next page to nil
nextPageTokenStr := ""
if len(results) > 0 {
nextPageTokenStr = strconv.FormatUint(nextPageToken, 10)
}
return results, nextPageTokenStr, nil
}
func (c *client) Autocomplete(ctx context.Context, typeURL, search string, limit uint64, caseSensitive bool) ([]*topologyv1.Resource, error) {
searchRequest := &topologyv1.SearchRequest{
PageToken: "0",
Limit: limit,
Sort: &topologyv1.SearchRequest_Sort{
Direction: topologyv1.SearchRequest_Sort_ASCENDING,
Field: "column.id",
},
Filter: &topologyv1.SearchRequest_Filter{
TypeUrl: typeURL,
Search: &topologyv1.SearchRequest_Filter_Search{
Field: "column.id",
Text: search,
},
CaseSensitive: caseSensitive,
},
}
results, _, err := c.Search(ctx, searchRequest)
if err != nil {
return nil, err
}
return results, err
}