-
Notifications
You must be signed in to change notification settings - Fork 172
/
storage.go
250 lines (222 loc) · 6.58 KB
/
storage.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
/*
* Copyright (c) 2019-2022. 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 service
import (
"context"
"fmt"
"github.com/pkg/errors"
"go.uber.org/zap"
"strings"
"github.com/pydio/cells/v4/common/config"
"github.com/pydio/cells/v4/common/dao"
"github.com/pydio/cells/v4/common/log"
"github.com/pydio/cells/v4/common/registry"
servicecontext "github.com/pydio/cells/v4/common/service/context"
json "github.com/pydio/cells/v4/common/utils/jsonx"
)
type StorageOptions struct {
StorageKey string
SupportedDrivers []string
DefaultDriver dao.DriverProviderFunc
Migrator dao.MigratorFunc
prefix interface{}
jsonMeta string
}
func (o *StorageOptions) Prefix(options *ServiceOptions) string {
val := ""
if o.prefix == nil {
return val
}
switch v := o.prefix.(type) {
case func(*ServiceOptions) string:
val = v(options)
case func(*StorageOptions) string:
val = v(o)
case string:
val = v
}
return val
}
func (o *StorageOptions) ToMeta() string {
if o.jsonMeta == "" {
m := make(map[string]interface{})
m["supportedDrivers"] = o.SupportedDrivers
if o.Migrator != nil {
m["hasMigrator"] = true
}
d, _ := json.Marshal(m)
o.jsonMeta = string(d)
}
return o.jsonMeta
}
type StorageOption func(options *StorageOptions)
// WithStoragePrefix sets a prefix to be used differently depending on driver name
func WithStoragePrefix(i interface{}) StorageOption {
return func(options *StorageOptions) {
options.prefix = i
}
}
// WithStorageSupport declares wich drivers can be supported by this service
func WithStorageSupport(dd ...string) StorageOption {
return func(options *StorageOptions) {
options.SupportedDrivers = append(options.SupportedDrivers, dd...)
}
}
// WithStorageDefaultDriver provides a default driver/dsn couple if not set in the configuration
func WithStorageDefaultDriver(d func() (string, string)) StorageOption {
return func(options *StorageOptions) {
options.DefaultDriver = d
}
}
// WithStorageMigrator provides a Migrate function from one DAO to another
func WithStorageMigrator(d dao.MigratorFunc) StorageOption {
return func(options *StorageOptions) {
options.Migrator = d
}
}
func daoFromOptions(ctx context.Context, o *ServiceOptions, fd dao.DaoWrapperFunc, indexer bool, opts *StorageOptions) (dao.DAO, error) {
prefix := opts.Prefix(o)
cfgKey := "storage"
if indexer {
cfgKey = "indexer"
}
driver, dsn, defined := config.GetStorageDriver(cfgKey, o.Name)
if !defined && opts.DefaultDriver != nil {
driver, dsn = opts.DefaultDriver()
}
var c dao.DAO
var e error
cfg := config.Get("services", o.Name)
// Setting a lock on the registry in case of multiple services starting up at the same time
reg := servicecontext.GetRegistry(ctx)
if lock := reg.NewLocker("dao-init-" + o.Name); lock != nil {
lock.Lock()
defer lock.Unlock()
}
if indexer {
c, e = dao.InitIndexer(ctx, driver, dsn, prefix, fd, cfg)
} else {
c, e = dao.InitDAO(ctx, driver, dsn, prefix, fd, cfg)
}
if e != nil {
return nil, errors.Wrap(e, "dao.Initialization "+driver)
}
return c, nil
}
func isLocalDao(o *ServiceOptions, indexer bool, opts *StorageOptions) bool {
cfgKey := "storage"
if indexer {
cfgKey = "indexer"
}
driver, _, defined := config.GetStorageDriver(cfgKey, o.Name)
if !defined && opts.DefaultDriver != nil {
driver, _ = opts.DefaultDriver()
}
s, e := dao.IsShared(driver)
if e != nil {
if driver != "" {
fmt.Println("cannot check if driver " + driver + " is shared:" + e.Error())
}
return false
}
return !s
}
// WithStorage adds a storage handler to the current service
func WithStorage(fd dao.DaoWrapperFunc, opts ...StorageOption) ServiceOption {
return makeStorageServiceOption(false, fd, opts...)
}
// WithIndexer adds an indexer handler to the current service
func WithIndexer(fd dao.DaoWrapperFunc, opts ...StorageOption) ServiceOption {
return makeStorageServiceOption(true, fd, opts...)
}
func makeStorageServiceOption(indexer bool, fd dao.DaoWrapperFunc, opts ...StorageOption) ServiceOption {
return func(o *ServiceOptions) {
storageKey := "storage"
if indexer {
storageKey = "indexer"
}
sOpts := &StorageOptions{
StorageKey: storageKey,
}
for _, op := range opts {
op(sOpts)
}
o.Storages = append(o.Storages, sOpts)
// Pre-check DAO config and add flag Unique if necessary
if isLocalDao(o, indexer, sOpts) {
o.Unique = true
o.BeforeStop = append(o.BeforeStop, func(ctx context.Context) error {
var stopDao dao.DAO
if indexer {
stopDao = servicecontext.GetIndexer(ctx)
} else {
stopDao = servicecontext.GetDAO(ctx)
}
if stopDao == nil {
return nil
}
log.Logger(ctx).Info("Closing DAO connection now")
return stopDao.CloseConn(ctx)
})
}
o.BeforeStart = append(o.BeforeStart, func(ctx context.Context) (context.Context, error) {
d, err := daoFromOptions(ctx, o, fd, indexer, sOpts)
if err != nil {
return ctx, err
}
if indexer {
ctx = servicecontext.WithIndexer(ctx, d)
} else {
ctx = servicecontext.WithDAO(ctx, d)
}
// Now register DAO
reg := servicecontext.GetRegistry(ctx)
if reg == nil {
return ctx, nil
}
var regItem registry.Dao
if !d.As(®Item) {
return ctx, nil
}
// Build Edge Metadata
mm := map[string]string{
"SupportedDrivers": strings.Join(sOpts.SupportedDrivers, ","),
}
prefix := sOpts.Prefix(o)
if prefix != "" {
mm["Prefix"] = prefix
}
if sOpts.Migrator != nil {
mm["SupportedMigration"] = "true"
}
options := []registry.RegisterOption{
registry.WithEdgeTo(o.ID, "DAO", mm),
}
var regStatus registry.StatusReporter
if d.As(®Status) {
options = append(options, registry.WithWatch(regStatus))
}
if er := reg.Register(regItem, options...); er != nil {
log.Logger(ctx).Error(" -- Cannot register DAO: "+er.Error(), zap.Error(er))
}
return ctx, nil
})
}
}