-
Notifications
You must be signed in to change notification settings - Fork 332
/
service.go
220 lines (190 loc) · 5.85 KB
/
service.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
package tablediff
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"sync"
"time"
"github.com/mitchellh/go-homedir"
"github.com/treeverse/lakefs/pkg/config"
"github.com/treeverse/lakefs/pkg/logging"
"github.com/treeverse/lakefs/pkg/plugins"
"github.com/treeverse/lakefs/pkg/plugins/internal"
)
var (
ErrTableNotFound = errors.New("table not found")
ErrLoadingPlugin = errors.New("failed to load diff plugin")
ErrFailedDiff = errors.New("failed to run diff")
)
const (
DiffTypeChanged = "changed"
DiffTypeDropped = "dropped"
DiffTypeCreated = "created"
OpTypeCreate = "create"
OpTypeUpdate = "update"
OpTypeDelete = "delete"
OpTypeUnknown = "unknown"
)
func getOpType(operationType OperationType) string {
switch operationType {
case OperationType_CREATE:
return OpTypeCreate
case OperationType_DELETE:
return OpTypeDelete
case OperationType_UPDATE:
return OpTypeUpdate
default:
return OpTypeUnknown
}
}
func getDiffType(diffType DiffType) string {
switch diffType {
case DiffType_CREATED:
return DiffTypeCreated
case DiffType_DROPPED:
return DiffTypeDropped
default:
return DiffTypeChanged
}
}
type DiffEntry struct {
ID string
Timestamp time.Time
Operation string
OperationContent map[string]string
OperationType string
}
type Response struct {
DiffType string
Diffs []DiffEntry
}
type RefPath struct {
Ref string
Path string
}
type TablePaths struct {
Left RefPath
Right RefPath
Base RefPath
}
type S3Creds struct {
Key config.SecureString
Secret config.SecureString
Endpoint string
}
type Params struct {
TablePaths TablePaths
S3Creds S3Creds
Repo string
}
type Differ interface {
Diff(context.Context, Params) (Response, error)
}
// Service is responsible for registering new Differ plugins and executing them at will.
// After initializing a Service, the CloseClients method should be called at some point to close gracefully all
// remaining plugins.
type Service struct {
pluginHandler internal.Handler[Differ, internal.HCPluginProperties]
closeFunctions map[string]func()
l sync.Mutex
}
func (s *Service) RunDiff(ctx context.Context, diffType string, diffParams Params) (Response, error) {
d, closeClient, err := s.pluginHandler.LoadPluginClient(diffType)
if err != nil {
logging.FromContext(ctx).WithError(err).
WithField("type", diffType).
WithField("params", fmt.Sprintf("%+v", diffParams)).
Error("failed to load the plugin client")
return Response{}, ErrLoadingPlugin
}
if closeClient != nil {
s.appendClosingFunction(diffType, closeClient)
}
diffResponse, err := d.Diff(ctx, diffParams)
if err != nil {
logging.FromContext(ctx).WithError(err).
WithField("type", diffType).
WithField("params", fmt.Sprintf("%+v", diffParams)).
Error("failed to perform diff")
if errors.Is(err, ErrTableNotFound) {
return Response{}, err
}
return Response{}, ErrFailedDiff
}
return diffResponse, nil
}
// Close should be called upon the destruction of the Service.
func (s *Service) Close() {
for _, cf := range s.closeFunctions {
cf()
}
}
func (s *Service) registerDiffClient(diffType string, props internal.HCPluginProperties) {
s.pluginHandler.RegisterPlugin(diffType, props)
}
func (s *Service) appendClosingFunction(diffType string, f func()) {
s.l.Lock()
defer s.l.Unlock()
if _, ok := s.closeFunctions[diffType]; !ok {
s.closeFunctions[diffType] = f
}
}
func (s *Service) EnabledDiffs() []string {
return s.pluginHandler.Plugins()
}
// NewService is used to initialize a new Differ service. The returned function is a closing function for the service.
func NewService(diffProps config.DiffProps, pluginProps config.Plugins) (*Service, func()) {
service := &Service{
pluginHandler: internal.NewManager[Differ](),
closeFunctions: make(map[string]func()),
}
registerPlugins(service, diffProps, pluginProps)
return service, service.Close
}
func registerPlugins(service *Service, diffProps config.DiffProps, pluginProps config.Plugins) {
registerDefaultPlugins(service, pluginProps.DefaultPath)
if diffProps.Delta.PluginName != "" {
registerPlugin(service, pluginProps, RegisterDeltaLakeDiffPlugin, "delta", diffProps.Delta.PluginName)
}
}
func registerDefaultPlugins(service *Service, pluginsPath string) {
diffPluginsDir := diffPluginsDefaultPath(pluginsPath)
deltaPath := filepath.Join(diffPluginsDir, "delta")
_, err := os.Stat(deltaPath)
if err != nil {
if !os.IsNotExist(err) {
logging.ContextUnavailable().WithError(err).Error("failed to access delta lake diff plugin")
}
return
}
pid := plugins.PluginIdentity{ProtocolVersion: 1, ExecutableLocation: deltaPath}
pa := plugins.PluginHandshake{}
RegisterDeltaLakeDiffPlugin(service, pid, pa)
}
func registerPlugin(service *Service, pluginProps config.Plugins, rf registrationFunc, diffType, pluginName string) {
// If the requested plugin wasn't configured with a path, it will be defined under the default location
pluginPath := filepath.Join(diffPluginsDefaultPath(pluginProps.DefaultPath), pluginName)
pluginVersion := 1 // default version
if props, ok := pluginProps.Properties[pluginName]; ok {
pp, err := homedir.Expand(props.Path)
if err != nil {
logging.ContextUnavailable().Errorf("failed to register a plugin for an invalid path: '%s'", props.Path)
return
}
pluginPath = pp
if props.Version != 0 {
pluginVersion = props.Version
}
}
pid := plugins.PluginIdentity{ProtocolVersion: uint(pluginVersion), ExecutableLocation: pluginPath}
pa := plugins.PluginHandshake{}
rf(service, pid, pa)
logging.ContextUnavailable().Infof("successfully registered a plugin for diff type: '%s'", diffType)
}
type registrationFunc func(ds *Service, pid plugins.PluginIdentity, handshake plugins.PluginHandshake)
func diffPluginsDefaultPath(pluginsPath string) string {
pp, _ := homedir.Expand(pluginsPath)
return filepath.Join(pp, "diff")
}