-
Notifications
You must be signed in to change notification settings - Fork 342
/
ms_client.go
362 lines (337 loc) · 14.3 KB
/
ms_client.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package metastore
import (
"context"
"errors"
"fmt"
"strings"
"github.com/treeverse/lakefs/pkg/catalog"
"github.com/treeverse/lakefs/pkg/logging"
mserrors "github.com/treeverse/lakefs/pkg/metastore/errors"
)
const dbfsPrefix = "dbfs:/"
type ReadClient interface {
GetTable(ctx context.Context, dbName string, tableName string) (r *Table, err error)
HasTable(ctx context.Context, dbName string, tableName string) (hasTable bool, err error)
GetPartitions(ctx context.Context, dbName string, tableName string) (r []*Partition, err error)
GetPartition(ctx context.Context, dbName string, tableName string, values []string) (r *Partition, err error)
GetDatabase(ctx context.Context, name string) (r *Database, err error)
GetDatabases(ctx context.Context, pattern string) (databases []*Database, err error)
GetTables(ctx context.Context, dbName string, pattern string) (tables []*Table, err error)
}
type WriteClient interface {
CreateTable(ctx context.Context, tbl *Table) error
AlterTable(ctx context.Context, dbName string, tableName string, newTable *Table) error
AddPartitions(ctx context.Context, tableName string, dbName string, newParts []*Partition) error
AlterPartitions(ctx context.Context, dbName string, tableName string, newPartitions []*Partition) error
AlterPartition(ctx context.Context, dbName string, tableName string, partition *Partition) error
AddPartition(ctx context.Context, tableName string, dbName string, newPartition *Partition) error
DropPartition(ctx context.Context, dbName string, tableName string, values []string) error
CreateDatabase(ctx context.Context, database *Database) error
NormalizeDBName(name string) string // NormalizeDBName changes the db name to be a valid name for the client
GetDBLocation(dbName string) string // getDBLocation returns the expected locationURI of the database
}
type Client interface {
ReadClient
WriteClient
}
func CopyOrMerge(ctx context.Context, fromClient, toClient Client, fromDB, fromTable, toDB, toTable, toBranch, serde string, partition []string, fixSparkPlaceHolder bool, dbfsLocation string) error {
transformLocation := func(location string) (string, error) {
location = HandleDBFSLocation(ctx, location, dbfsLocation)
transformedLocation, err := ReplaceBranchName(location, toBranch)
if err != nil {
return "", fmt.Errorf("failed to replace branch name with location: '%s' and branch: '%s': %w", location, toBranch, err)
}
return transformedLocation, nil
}
return copyOrMergeWithTransformLocation(ctx, fromClient, toClient, fromDB, fromTable, toDB, toTable, serde, false, partition, transformLocation, fixSparkPlaceHolder)
}
func CopyDB(ctx context.Context, fromClient, toClient Client, fromDB, toDB, toBranch string, dbfsLocation string) error {
transformLocation := func(location string) (string, error) {
if location == "" {
return "", nil
}
location = HandleDBFSLocation(ctx, location, dbfsLocation)
transformedLocation, err := ReplaceBranchName(location, toBranch)
if err != nil {
return "", fmt.Errorf("failed to replace branch name with location: '%s' and branch: '%s': %w", location, toBranch, err)
}
return transformedLocation, nil
}
return copyDBWithTransformLocation(ctx, fromClient, toClient, fromDB, toDB, transformLocation)
}
func copyDBWithTransformLocation(ctx context.Context, fromClient, toClient Client, fromDB string, toDB string, transformLocation func(location string) (string, error)) error {
schema, err := fromClient.GetDatabase(ctx, fromDB)
if err != nil {
return fmt.Errorf("failed to get database on copy from '%s': %w", fromDB, err)
}
schema.Name = toDB
schema.LocationURI, err = transformLocation(schema.LocationURI)
if err != nil {
return err
}
err = toClient.CreateDatabase(ctx, schema)
if err != nil {
return fmt.Errorf("failed to create database with name '%s' and location '%s': %w", schema.Name, schema.LocationURI, err)
}
return nil
}
func copyOrMergeWithTransformLocation(ctx context.Context, fromClient, toClient Client, fromDB, fromTable, toDB, toTable, serde string, setSymlink bool, partition []string, transformLocation func(location string) (string, error), fixSparkPlaceHolder bool) error {
log := logging.FromContext(ctx).WithFields(logging.Fields{
"from_db": fromDB,
"from_table": fromTable,
"to_db": toDB,
"to_table": toTable,
"set_symlink": setSymlink,
"serde": serde,
"partition_len": len(partition),
})
if len(partition) > 0 {
log.Debug("CopyPartition")
return CopyPartition(ctx, fromClient, toClient, fromDB, fromTable, toDB, toTable, serde, setSymlink, partition, transformLocation, fixSparkPlaceHolder)
}
hasTable, err := toClient.HasTable(ctx, toDB, toTable)
if err != nil {
return err
}
if !hasTable {
log.Debug("Copy")
table, err := fromClient.GetTable(ctx, fromDB, fromTable)
if err != nil {
return err
}
partitions, err := fromClient.GetPartitions(ctx, fromDB, fromTable)
if err != nil {
return err
}
return Copy(ctx, table, partitions, toDB, toTable, serde, setSymlink, toClient, transformLocation, fixSparkPlaceHolder)
}
log.Debug("Merge")
table, err := fromClient.GetTable(ctx, fromDB, fromTable)
if err != nil {
return err
}
partitions, err := fromClient.GetPartitions(ctx, fromDB, fromTable)
if err != nil {
return err
}
partitionCollection := NewPartitionCollection(partitions)
return Merge(ctx, table, partitionCollection, toDB, toTable, serde, setSymlink, toClient, transformLocation, fixSparkPlaceHolder)
}
func CopyOrMergeFromValues(ctx context.Context, fromClient Client, fTable *Table, toClient Client, fromDB, fromTable, toDB, toTable, serde string, transformLocation func(location string) (string, error), fixSparkPlaceHolder bool) error {
hasTable, err := toClient.HasTable(ctx, toDB, toTable)
if err != nil {
return err
}
partitions, err := fromClient.GetPartitions(ctx, fromDB, fromTable)
if err != nil {
return err
}
if !hasTable {
return Copy(ctx, fTable, partitions, toDB, toTable, serde, false, toClient, transformLocation, fixSparkPlaceHolder)
}
partitionCollection := NewPartitionCollection(partitions)
return Merge(ctx, fTable, partitionCollection, toDB, toTable, serde, false, toClient, transformLocation, fixSparkPlaceHolder)
}
func CopyOrMergeAll(ctx context.Context, fromClient, toClient Client, schemaFilter, tableFilter, toBranch string, continueOnError, fixSparkPlaceHolder bool, dbfsLocation string) error {
databases, err := fromClient.GetDatabases(ctx, schemaFilter)
if err != nil {
return err
}
transformLocation := func(location string) (string, error) {
location = HandleDBFSLocation(ctx, location, dbfsLocation)
return ReplaceBranchName(location, toBranch)
}
return applyAll(ctx, fromClient, toClient, databases, tableFilter, transformLocation, fixSparkPlaceHolder, continueOnError)
}
// HandleDBFSLocation translates Data Bricks File system path to the S3 path using the dbfsLocation
func HandleDBFSLocation(ctx context.Context, location string, dbfsLocation string) string {
l := location
if dbfsLocation != "" && strings.HasPrefix(location, dbfsPrefix) {
l = strings.Replace(location, dbfsPrefix, dbfsLocation, 1)
}
logging.FromContext(ctx).WithFields(logging.Fields{"dbfsLocation": dbfsLocation, "location": location, "new_location": l}).Info("translate databricks file system path to s3 path")
return l
}
func ImportAll(ctx context.Context, fromClient, toClient Client, schemaFilter, tableFilter, repo, toBranch string, continueOnError, fixSparkPlaceHolder bool, dbfsLocation string) error {
databases, err := fromClient.GetDatabases(ctx, schemaFilter)
if err != nil {
return err
}
transformLocation := func(location string) (string, error) {
location = HandleDBFSLocation(ctx, location, dbfsLocation)
return ReplaceExternalToLakeFSImported(location, repo, toBranch)
}
return applyAll(ctx, fromClient, toClient, databases, tableFilter, transformLocation, fixSparkPlaceHolder, continueOnError)
}
func applyAll(ctx context.Context, fromClient Client, toClient Client, databases []*Database, tableFilter string, transformLocation func(location string) (string, error), fixSparkPlaceHolder bool, continueOnError bool) error {
for _, database := range databases {
fromDBName := database.Name
toDBName := toClient.NormalizeDBName(database.Name)
err := copyDBWithTransformLocation(ctx, fromClient, toClient, fromDBName, toDBName, transformLocation)
if err != nil && !errors.Is(err, mserrors.ErrSchemaExists) {
return err
}
tables, err := fromClient.GetTables(ctx, fromDBName, tableFilter)
if err != nil {
return err
}
for _, table := range tables {
tableName := table.TableName
fmt.Printf("table %s.%s -> %s.%s\n", fromDBName, tableName, toDBName, tableName)
err = CopyOrMergeFromValues(ctx, fromClient, table, toClient, fromDBName, tableName, toDBName, tableName, tableName, transformLocation, fixSparkPlaceHolder)
if err != nil {
if !continueOnError {
return err
}
fmt.Println(err)
}
}
}
return nil
}
func Copy(ctx context.Context, fromTable *Table, partitions []*Partition, toDB, toTable, serde string, setSymlink bool, toClient WriteClient, transformLocation func(location string) (string, error), fixSparkPlaceHolder bool) error {
isSparkSQLTable := fromTable.isSparkSQLTable()
err := fromTable.Update(ctx, toDB, toTable, serde, setSymlink, transformLocation, isSparkSQLTable, fixSparkPlaceHolder)
if err != nil {
return err
}
for _, partition := range partitions {
err := partition.Update(ctx, toDB, toTable, serde, setSymlink, transformLocation, isSparkSQLTable, fixSparkPlaceHolder)
if err != nil {
return err
}
}
err = toClient.CreateTable(ctx, fromTable)
if err != nil {
return err
}
err = toClient.AddPartitions(ctx, toTable, toDB, partitions)
return err
}
func Merge(ctx context.Context, table *Table, partitionIter Collection, toDB, toTable, serde string, setSymlink bool, toClient Client, transformLocation func(location string) (string, error), fixSparkPlaceHolder bool) error {
isSparkSQLTable := table.isSparkSQLTable()
err := table.Update(ctx, toDB, toTable, serde, setSymlink, transformLocation, isSparkSQLTable, fixSparkPlaceHolder)
if err != nil {
return err
}
toPartitions, err := toClient.GetPartitions(ctx, toDB, toTable)
if err != nil {
return err
}
toPartitionIter := NewPartitionCollection(toPartitions)
var addPartitions, removePartitions, alterPartitions []*Partition
err = DiffIterable(partitionIter, toPartitionIter, func(difference catalog.DifferenceType, value interface{}, _ string) error {
partition, ok := value.(*Partition)
if !ok {
return fmt.Errorf("%w at diffIterable, got %T while expected *Partition", mserrors.ErrExpectedType, value)
}
err = partition.Update(ctx, toDB, toTable, serde, setSymlink, transformLocation, isSparkSQLTable, fixSparkPlaceHolder)
if err != nil {
return err
}
switch difference {
case catalog.DifferenceTypeRemoved:
removePartitions = append(removePartitions, partition)
case catalog.DifferenceTypeAdded:
addPartitions = append(addPartitions, partition)
default:
alterPartitions = append(alterPartitions, partition)
}
return nil
})
if err != nil {
return err
}
err = toClient.AlterTable(ctx, toDB, toTable, table)
if err != nil {
return err
}
err = toClient.AddPartitions(ctx, toTable, toDB, addPartitions)
if err != nil {
return err
}
err = toClient.AlterPartitions(ctx, toDB, toTable, alterPartitions)
if err != nil {
return err
}
// drop one by one
for _, partition := range removePartitions {
values := partition.Values
err = toClient.DropPartition(ctx, toDB, toTable, values)
if err != nil {
return err
}
}
return nil
}
func CopyPartition(ctx context.Context, fromClient ReadClient, toClient Client, fromDB, fromTable, toDB, toTable, serde string, setSymlink bool, partition []string, transformLocation func(location string) (string, error), fixSparkPlaceHolder bool) error {
t1, err := fromClient.GetTable(ctx, fromDB, fromTable)
if err != nil {
return err
}
p1, err := fromClient.GetPartition(ctx, fromDB, fromTable, partition)
if err != nil {
return err
}
p2, err := toClient.GetPartition(ctx, toDB, toTable, partition)
if err != nil {
return err
}
err = p1.Update(ctx, toDB, toTable, serde, setSymlink, transformLocation, t1.isSparkSQLTable(), fixSparkPlaceHolder)
if err != nil {
return err
}
if p2 == nil {
err = toClient.AddPartition(ctx, "", "", p1)
} else {
err = toClient.AlterPartition(ctx, toDB, toTable, p1)
}
return err
}
func GetDiff(ctx context.Context, fromClient, toClient ReadClient, fromDB, fromTable, toDB, toTable string) (*MetaDiff, error) {
diffColumns, err := getColumnDiff(ctx, fromClient, toClient, fromDB, fromTable, toDB, toTable)
if err != nil {
return nil, err
}
partitionDiff, err := getPartitionsDiff(ctx, fromClient, toClient, fromDB, fromTable, toDB, toTable)
if err != nil {
return nil, err
}
return &MetaDiff{
PartitionDiff: partitionDiff,
ColumnsDiff: diffColumns,
}, nil
}
func getPartitionsDiff(ctx context.Context, fromClient, toClient ReadClient, fromDB string, fromTable string, toDB string, toTable string) (catalog.Differences, error) {
partitions, err := fromClient.GetPartitions(ctx, fromDB, fromTable)
if err != nil {
return nil, err
}
partitionIter := NewPartitionCollection(partitions)
toPartitions, err := toClient.GetPartitions(ctx, toDB, toTable)
if err != nil {
return nil, err
}
toPartitionIter := NewPartitionCollection(toPartitions)
return Diff(partitionIter, toPartitionIter)
}
func getColumnDiff(ctx context.Context, fromClient, toClient ReadClient, fromDB, fromTable, toDB, toTable string) (catalog.Differences, error) {
table, err := fromClient.GetTable(ctx, fromDB, fromTable)
if err != nil {
return nil, err
}
colsIter := NewColumnCollection(table.Sd.Cols)
toTbl, err := toClient.GetTable(ctx, toDB, toTable)
if err != nil {
return nil, err
}
toColumns := toTbl.Sd.Cols // TODO(Guys): change name
toColsIter := NewColumnCollection(toColumns)
return Diff(colsIter, toColsIter)
}
func CopyOrMergeToSymlink(ctx context.Context, fromClient, toClient Client, fromDB, fromTable, toDB, toTable, locationPrefix string, fixSparkPlaceHolder bool) error {
transformLocation := func(location string) (string, error) {
return GetSymlinkLocation(location, locationPrefix)
}
return copyOrMergeWithTransformLocation(ctx, fromClient, toClient, fromDB, fromTable, toDB, toTable, "", true, nil, transformLocation, fixSparkPlaceHolder)
}