-
Notifications
You must be signed in to change notification settings - Fork 351
/
ms_client.go
327 lines (306 loc) · 10.1 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
package hive
import (
"context"
"crypto/tls"
"errors"
"fmt"
"github.com/apache/thrift/lib/go/thrift"
"github.com/treeverse/lakefs/pkg/catalog"
"github.com/treeverse/lakefs/pkg/metastore"
"github.com/treeverse/lakefs/pkg/metastore/hive/gen-go/hive_metastore"
)
type ThriftHiveMetastoreClient interface {
CreateTable(ctx context.Context, tbl *hive_metastore.Table) (err error)
GetTable(ctx context.Context, dbname string, tableName string) (r *hive_metastore.Table, err error)
AlterTable(ctx context.Context, dbname string, tableName string, newTable *hive_metastore.Table) (err error)
AddPartitions(ctx context.Context, newParts []*hive_metastore.Partition) (r int32, err error)
GetPartitions(ctx context.Context, dbName string, tableName string, maxPartitions int16) (r []*hive_metastore.Partition, err error)
GetPartition(ctx context.Context, dbName string, tableName string, values []string) (r *hive_metastore.Partition, err error)
AlterPartitions(ctx context.Context, dbName string, tableName string, newPartitions []*hive_metastore.Partition) (err error)
AlterPartition(ctx context.Context, dbName string, tableName string, values *hive_metastore.Partition) (err error)
AddPartition(ctx context.Context, newPartition *hive_metastore.Partition) (r *hive_metastore.Partition, err error)
DropPartition(ctx context.Context, dbName string, tableName string, values []string, deleteData bool) (r bool, err error)
GetDatabase(ctx context.Context, name string) (r *hive_metastore.Database, err error)
GetDatabases(ctx context.Context, pattern string) (r []string, err error)
GetAllDatabases(ctx context.Context) (r []string, err error)
CreateDatabase(ctx context.Context, database *hive_metastore.Database) (err error)
GetTables(ctx context.Context, dbName string, pattern string) (r []string, err error)
}
type MSClient struct {
ctx context.Context
client ThriftHiveMetastoreClient
transport thrift.TTransport
}
func NewMSClient(ctx context.Context, addr string, secure bool) (*MSClient, error) {
msClient := &MSClient{
ctx: ctx,
}
err := msClient.open(addr, secure)
if err != nil {
return nil, err
}
return msClient, nil
}
func CopyOrMergeAll(ctx context.Context, fromClient, toClient *MSClient, schemaFilter, tableFilter, toBranch string) error {
databases, err := fromClient.client.GetDatabases(ctx, schemaFilter)
if err != nil {
return err
}
for _, dbName := range databases {
database, err := fromClient.client.GetDatabase(ctx, dbName)
if err != nil {
return err
}
err = toClient.client.CreateDatabase(ctx, database)
var alreadyExistsErr *hive_metastore.AlreadyExistsException
if err != nil && !errors.As(err, &alreadyExistsErr) {
return err
}
tablesNames, err := fromClient.client.GetTables(ctx, dbName, tableFilter)
if err != nil {
return err
}
for _, tableName := range tablesNames {
fmt.Printf("copy table %s.%s\n", dbName, tableName)
err = fromClient.CopyOrMergeTo(dbName, tableName, dbName, tableName, toBranch, tableName, nil, toClient.client)
if err != nil {
return err
}
}
}
return nil
}
func (c *MSClient) open(addr string, secure bool) error {
var err error
cfg := &thrift.TConfiguration{}
if secure {
cfg.TLSConfig = &tls.Config{
//nolint:gosec
InsecureSkipVerify: true,
}
c.transport, err = thrift.NewTSSLSocketConf(addr, cfg)
} else {
c.transport, err = thrift.NewTSocketConf(addr, cfg)
}
if err != nil {
return err
}
err = c.transport.Open()
if err != nil {
return err
}
protocolFactory := thrift.NewTBinaryProtocolFactoryConf(cfg)
iprot := protocolFactory.GetProtocol(c.transport)
oprot := protocolFactory.GetProtocol(c.transport)
c.client = hive_metastore.NewThriftHiveMetastoreClient(thrift.NewTStandardClient(iprot, oprot))
return nil
}
func (c *MSClient) Close() error {
if c.transport != nil {
return c.transport.Close()
}
return nil
}
func (c *MSClient) CopyOrMerge(fromDB, fromTable, toDB, toTable, toBranch, serde string, partition []string) error {
return c.CopyOrMergeTo(fromDB, fromTable, toDB, toTable, toBranch, serde, partition, c.client)
}
func (c *MSClient) CopyOrMergeTo(fromDB, fromTable, toDB, toTable, toBranch, serde string, partition []string, toClient ThriftHiveMetastoreClient) error {
if len(partition) > 0 {
return c.CopyPartition(fromDB, fromTable, toDB, toTable, toBranch, serde, partition)
}
table, err := toClient.GetTable(c.ctx, toDB, toTable)
var noSuchObjectErr *hive_metastore.NoSuchObjectException
if err != nil && !errors.As(err, &noSuchObjectErr) {
return err
}
if table == nil {
return c.Copy(fromDB, fromTable, toDB, toTable, toBranch, serde, toClient)
}
return c.Merge(fromDB, fromTable, toDB, toTable, toBranch, serde, toClient)
}
func (c *MSClient) Copy(fromDB, fromTable, toDB, toTable, toBranch, serde string, toClient ThriftHiveMetastoreClient) error {
table, err := c.client.GetTable(c.ctx, fromDB, fromTable)
if err != nil {
return err
}
table.DbName = toDB
table.TableName = toTable
if table.Sd != nil {
if table.Sd.SerdeInfo != nil {
table.Sd.SerdeInfo.Name = serde
}
table.Sd.Location, err = metastore.ReplaceBranchName(table.Sd.Location, toBranch)
if err != nil {
return err
}
}
partitions, err := c.client.GetPartitions(c.ctx, fromDB, fromTable, -1)
if err != nil {
return err
}
for _, partition := range partitions {
partition.DbName = toDB
partition.TableName = toTable
if partition.Sd != nil {
if partition.Sd.SerdeInfo != nil {
partition.Sd.SerdeInfo.Name = serde
}
partition.Sd.Location, err = metastore.ReplaceBranchName(partition.Sd.Location, toBranch)
if err != nil {
return err
}
}
}
err = toClient.CreateTable(c.ctx, table)
if err != nil {
return err
}
_, err = toClient.AddPartitions(c.ctx, partitions)
return err
}
func (c *MSClient) Merge(fromDB, fromTable, toDB, toTable, toBranch, serde string, toClient ThriftHiveMetastoreClient) error {
table, err := c.client.GetTable(c.ctx, fromDB, fromTable)
if err != nil {
return err
}
table.DbName = toDB
table.TableName = toTable
if table.Sd != nil {
if table.Sd.SerdeInfo != nil {
table.Sd.SerdeInfo.Name = serde
}
table.Sd.Location, err = metastore.ReplaceBranchName(table.Sd.Location, toBranch)
}
if err != nil {
return err
}
partitions, err := c.client.GetPartitions(c.ctx, fromDB, fromTable, -1)
if err != nil {
return err
}
toPartitions, err := toClient.GetPartitions(c.ctx, toDB, toTable, -1)
if err != nil {
return err
}
partitionIter := NewPartitionCollection(partitions)
toPartitionIter := NewPartitionCollection(toPartitions)
var addPartitions, removePartitions, alterPartitions []*hive_metastore.Partition
err = metastore.DiffIterable(partitionIter, toPartitionIter, func(difference catalog.DifferenceType, value interface{}, _ string) error {
partition, ok := value.(*hive_metastore.Partition)
if !ok {
return fmt.Errorf("%w in diffIterable call. expected to get *hive_metastore. Partition, but got: %T", ErrExpectedType, value)
}
partition.DbName = toDB
partition.TableName = toTable
if partition.Sd != nil {
if partition.Sd.SerdeInfo != nil {
partition.Sd.SerdeInfo.Name = toTable
}
partition.Sd.Location, err = metastore.ReplaceBranchName(partition.Sd.Location, toBranch)
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(c.ctx, toDB, toTable, table)
if err != nil {
return err
}
_, err = toClient.AddPartitions(c.ctx, addPartitions)
if err != nil {
return err
}
err = toClient.AlterPartitions(c.ctx, toDB, toTable, alterPartitions)
if err != nil {
return err
}
// drop one by one
for _, partition := range removePartitions {
_, err = toClient.DropPartition(c.ctx, toDB, toTable, partition.Values, true)
if err != nil {
return err
}
}
return nil
}
func (c *MSClient) CopyPartition(fromDB, fromTable, toDB, toTable, toBranch, serde string, partition []string) error {
p1, err := c.client.GetPartition(c.ctx, fromDB, fromTable, partition)
if err != nil {
return err
}
p2, err := c.client.GetPartition(c.ctx, toDB, toTable, partition)
if err != nil {
if _, ok := err.(*hive_metastore.NoSuchObjectException); !ok {
return err
}
}
if p1.Sd != nil {
if p1.Sd.SerdeInfo != nil {
p1.Sd.SerdeInfo.Name = serde
}
p1.DbName = toDB
p1.TableName = toTable
p1.Sd.Location, err = metastore.ReplaceBranchName(p1.Sd.Location, toBranch)
}
if err != nil {
return err
}
if p2 == nil {
_, err = c.client.AddPartition(c.ctx, p1)
} else {
err = c.client.AlterPartition(c.ctx, toDB, toTable, p1)
}
return err
}
func (c *MSClient) Diff(fromDB, fromTable, toDB, toTable string) (*metastore.MetaDiff, error) {
diffColumns, err := c.getColumnDiff(fromDB, fromTable, toDB, toTable)
if err != nil {
return nil, err
}
partitionDiff, err := c.getPartitionsDiff(fromDB, fromTable, toDB, toTable)
if err != nil {
return nil, err
}
return &metastore.MetaDiff{
PartitionDiff: partitionDiff,
ColumnsDiff: diffColumns,
}, nil
}
func (c *MSClient) getPartitionsDiff(fromDB string, fromTable string, toDB string, toTable string) (catalog.Differences, error) {
partitions, err := c.client.GetPartitions(c.ctx, fromDB, fromTable, -1)
if err != nil {
return nil, err
}
toPartitions, err := c.client.GetPartitions(c.ctx, toDB, toTable, -1)
if err != nil {
return nil, err
}
partitionIter := NewPartitionCollection(partitions)
toPartitionIter := NewPartitionCollection(toPartitions)
return metastore.Diff(partitionIter, toPartitionIter)
}
func (c *MSClient) getColumnDiff(fromDB, fromTable, toDB, toTable string) (catalog.Differences, error) {
tableFrom, err := c.client.GetTable(c.ctx, fromDB, fromTable)
if err != nil {
return nil, err
}
tableTo, err := c.client.GetTable(c.ctx, toDB, toTable)
if err != nil {
return nil, err
}
colsIter := NewFSCollection(tableFrom.GetSd().GetCols())
colsToIter := NewFSCollection(tableTo.GetSd().GetCols())
return metastore.Diff(colsIter, colsToIter)
}