forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clients-pool.go
292 lines (239 loc) · 7.98 KB
/
clients-pool.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
/*
* Copyright (c) 2018. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
package views
import (
"context"
"fmt"
"strings"
"sync"
"time"
"github.com/golang/protobuf/proto"
"github.com/micro/go-micro/client"
"go.uber.org/zap"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/config"
"github.com/pydio/cells/common/log"
"github.com/pydio/cells/common/micro"
"github.com/pydio/cells/common/proto/object"
"github.com/pydio/cells/common/proto/tree"
"github.com/pydio/cells/common/registry"
config2 "github.com/pydio/go-os/config"
)
type sourceAlias struct {
dataSource string
bucket string
}
// ClientsPool is responsible for discovering available datasources and
// keeping an up to date registry that is used by the routers.
type ClientsPool struct {
Sources map[string]LoadedSource
aliases map[string]sourceAlias
// Statically set for testing
TreeClient tree.NodeProviderClient
TreeClientWrite tree.NodeReceiverClient
genericClient client.Client
configMutex *sync.Mutex
watcher registry.Watcher
confWatcher config2.Watcher
}
// NewSource instantiates a LoadedSource with a minio client
func NewSource(data *object.DataSource) (LoadedSource, error) {
loaded := LoadedSource{}
loaded.DataSource = *data
var err error
loaded.Client, err = data.CreateClient()
return loaded, err
}
// NewClientsPool creates a client pool and initialises it by calling the registry.
func NewClientsPool(watchRegistry bool) (pool *ClientsPool) {
pool = &ClientsPool{
Sources: make(map[string]LoadedSource),
aliases: make(map[string]sourceAlias),
}
pool.configMutex = &sync.Mutex{}
if IsUnitTestEnv {
// Workaround the fact that no registry is present when doing unit tests
return pool
}
pool.listDatasources()
if watchRegistry {
go pool.watchRegistry()
go pool.watchConfigChanges()
}
return pool
}
// Close stops the underlying watcher if defined.
func (p *ClientsPool) Close() {
if p.watcher != nil {
p.watcher.Stop()
}
if p.confWatcher != nil {
p.confWatcher.Stop()
}
}
func (p *ClientsPool) GetTreeClient() tree.NodeProviderClient {
if p.TreeClient != nil {
return p.TreeClient
}
return tree.NewNodeProviderClient(common.SERVICE_GRPC_NAMESPACE_+common.SERVICE_TREE, defaults.NewClient())
}
func (p *ClientsPool) GetTreeClientWrite() tree.NodeReceiverClient {
if p.TreeClientWrite != nil {
return p.TreeClientWrite
}
return tree.NewNodeReceiverClient(common.SERVICE_GRPC_NAMESPACE_+common.SERVICE_TREE, defaults.NewClient())
}
func (p *ClientsPool) GetDataSourceInfo(dsName string, retries ...int) (LoadedSource, error) {
if dsName == "default" {
dsName = config.Get("defaults", "datasource").String("default")
}
if cl, ok := p.Sources[dsName]; ok {
return cl, nil
} else if alias, aOk := p.aliases[dsName]; aOk {
if dsi, e := p.GetDataSourceInfo(alias.dataSource); e != nil {
return dsi, e
} else {
ds := LoadedSource{}
ds.DataSource = *proto.Clone(&dsi.DataSource).(*object.DataSource)
ds.DataSource.ObjectsBucket = alias.bucket
ds.Client = dsi.Client
return ds, nil
}
} else if len(retries) == 0 || retries[0] <= 5 {
var retry int
if len(retries) > 0 {
retry = retries[0]
}
delay := (retry + 1) * 2
log.Logger(context.Background()).Debug(fmt.Sprintf("[ClientsPool] cannot find datasource, retrying in %ds...", delay), zap.String("ds", dsName), zap.Any("retries", retry))
<-time.After(time.Duration(delay) * time.Second)
p.listDatasources()
return p.GetDataSourceInfo(dsName, retry+1)
} else {
e := fmt.Errorf("Could not find DataSource " + dsName)
var keys []string
for k, _ := range p.Sources {
keys = append(keys, k)
}
log.Logger(context.Background()).Error(e.Error(), zap.Strings("currentSources", keys))
return LoadedSource{}, e
}
}
func (p *ClientsPool) listDatasources() {
if IsUnitTestEnv {
// Workaround the fact that no registry is present when doing unit tests
return
}
otherServices, err := registry.ListRunningServices()
if err != nil {
return
}
indexServices := filterServices(otherServices, func(v string) bool {
return strings.Contains(v, common.SERVICE_GRPC_NAMESPACE_+common.SERVICE_DATA_SYNC_)
})
ctx := context.Background()
for _, indexService := range indexServices {
dataSourceName := strings.TrimPrefix(indexService, common.SERVICE_GRPC_NAMESPACE_+common.SERVICE_DATA_SYNC_)
if dataSourceName == "" {
continue
}
s3endpointClient := object.NewDataSourceEndpointClient(common.SERVICE_GRPC_NAMESPACE_+common.SERVICE_DATA_SYNC_+dataSourceName, defaults.NewClient(client.Retries(3), client.RequestTimeout(1*time.Second)))
response, err := s3endpointClient.GetDataSourceConfig(ctx, &object.GetDataSourceConfigRequest{})
if err == nil && response.DataSource != nil {
log.Logger(ctx).Debug("Creating client for datasource " + dataSourceName)
p.createClientsForDataSource(dataSourceName, response.DataSource)
} else {
log.Logger(context.Background()).Debug("no answer from endpoint, maybe not ready yet? "+common.SERVICE_GRPC_NAMESPACE_+common.SERVICE_DATA_SYNC_+dataSourceName, zap.Any("r", response), zap.Error(err))
}
}
p.registerAlternativeClient(common.PYDIO_THUMBSTORE_NAMESPACE)
p.registerAlternativeClient(common.PYDIO_DOCSTORE_BINARIES_NAMESPACE)
p.registerAlternativeClient(common.PYDIO_VERSIONS_NAMESPACE)
}
func (p *ClientsPool) registerAlternativeClient(namespace string) error {
dataSource, bucket, err := GetGenericStoreClientConfig(namespace)
if err != nil {
return err
}
p.configMutex.Lock()
defer p.configMutex.Unlock()
p.aliases[namespace] = sourceAlias{
dataSource: dataSource,
bucket: bucket,
}
return nil
}
func (p *ClientsPool) watchRegistry() {
watcher, err := registry.Watch()
p.watcher = watcher
if err != nil {
return
}
for {
result, err := watcher.Next()
if result != nil && err == nil {
srv := result.Service
if strings.Contains(srv.Name(), common.SERVICE_GRPC_NAMESPACE_+common.SERVICE_DATA_SYNC_) {
dsName := strings.TrimPrefix(srv.Name(), common.SERVICE_GRPC_NAMESPACE_+common.SERVICE_DATA_SYNC_)
log.Logger(context.Background()).Debug("[ClientsPool] Registry action", zap.String("action", result.Action), zap.Any("srv", srv.Name()))
if _, ok := p.Sources[dsName]; ok && result.Action == "stopped" {
// Reset list
p.configMutex.Lock()
delete(p.Sources, dsName)
p.configMutex.Unlock()
}
p.listDatasources()
}
}
}
}
func (p *ClientsPool) watchConfigChanges() {
watcher, err := config.Default().Watch("services", common.SERVICE_GRPC_NAMESPACE_+common.SERVICE_DATA_SYNC, "sources")
if err != nil {
return
}
p.confWatcher = watcher
for {
event, err := watcher.Next()
if event != nil && err == nil {
p.listDatasources()
}
}
}
func (p *ClientsPool) createClientsForDataSource(dataSourceName string, dataSource *object.DataSource, registerKey ...string) error {
log.Logger(context.Background()).Debug("Adding dataSource", zap.String("dsname", dataSourceName))
p.configMutex.Lock()
defer p.configMutex.Unlock()
loaded, err := NewSource(dataSource)
if err != nil {
return err
}
p.Sources[dataSourceName] = loaded
return nil
}
func filterServices(vs []registry.Service, f func(string) bool) []string {
vsf := make([]string, 0)
for _, v := range vs {
if f(v.Name()) {
vsf = append(vsf, v.Name())
}
}
return vsf
}