This repository has been archived by the owner on Nov 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 188
/
Copy pathpessimist.go
172 lines (143 loc) · 4.43 KB
/
pessimist.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
// Copyright 2020 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package shardddl
import (
"context"
"sync"
"go.etcd.io/etcd/clientv3"
"go.uber.org/zap"
"github.com/pingcap/dm/pkg/log"
"github.com/pingcap/dm/pkg/shardddl/pessimism"
"github.com/pingcap/dm/pkg/terror"
)
// Pessimist used to coordinate the shard DDL migration in pessimism mode.
type Pessimist struct {
mu sync.RWMutex
logger log.Logger
cli *clientv3.Client
task string
source string
// the shard DDL info which is pending to handle.
pendingInfo *pessimism.Info
// the shard DDL lock operation which is pending to handle.
pendingOp *pessimism.Operation
}
// NewPessimist creates a new Pessimist instance.
func NewPessimist(pLogger *log.Logger, cli *clientv3.Client, task, source string) *Pessimist {
return &Pessimist{
logger: pLogger.WithFields(zap.String("component", "shard DDL pessimist")),
cli: cli,
task: task,
source: source,
}
}
// Reset resets the internal state of the pessimist.
func (p *Pessimist) Reset() {
p.mu.Lock()
defer p.mu.Unlock()
p.pendingInfo = nil
p.pendingOp = nil
}
// ConstructInfo constructs a shard DDL info.
func (p *Pessimist) ConstructInfo(schema, table string, ddls []string) pessimism.Info {
return pessimism.NewInfo(p.task, p.source, schema, table, ddls)
}
// PutInfo puts the shard DDL info into etcd and returns the revision.
func (p *Pessimist) PutInfo(ctx context.Context, info pessimism.Info) (int64, error) {
// put info only no previous operation exists or not done.
rev, putted, err := pessimism.PutInfoIfOpNotDone(p.cli, info)
if err != nil {
return 0, err
} else if putted {
p.mu.Lock()
p.pendingInfo = &info
p.mu.Unlock()
return rev, nil
}
p.logger.Warn("the previous shard DDL operation still exists, waiting for it to be deleted", zap.Stringer("info", info))
// wait for the operation to be deleted.
ctx2, cancel2 := context.WithCancel(ctx)
defer cancel2()
ch := make(chan pessimism.Operation, 1)
errCh := make(chan error, 1)
go pessimism.WatchOperationDelete(ctx2, p.cli, info.Task, info.Source, rev, ch, errCh)
select {
case <-ch: // deleted.
case err = <-errCh:
return 0, err
case <-ctx.Done():
return 0, ctx.Err()
}
rev, err = pessimism.PutInfo(p.cli, info)
if err != nil {
return 0, err
}
p.mu.Lock()
p.pendingInfo = &info
p.mu.Unlock()
return rev, nil
}
// GetOperation gets the shard DDL lock operation relative to the shard DDL info.
func (p *Pessimist) GetOperation(ctx context.Context, info pessimism.Info, rev int64) (pessimism.Operation, error) {
ctx2, cancel2 := context.WithCancel(ctx)
defer cancel2()
ch := make(chan pessimism.Operation, 1)
errCh := make(chan error, 1)
go pessimism.WatchOperationPut(ctx2, p.cli, info.Task, info.Source, rev, ch, errCh)
select {
case op := <-ch:
p.mu.Lock()
p.pendingOp = &op
p.mu.Unlock()
return op, nil
case err := <-errCh:
return pessimism.Operation{}, err
case <-ctx.Done():
return pessimism.Operation{}, ctx.Err()
}
}
// DoneOperationDeleteInfo marks the shard DDL lock operation as done and delete the shard DDL info.
func (p *Pessimist) DoneOperationDeleteInfo(op pessimism.Operation, info pessimism.Info) error {
op.Done = true // mark the operation as `done`.
done, _, err := pessimism.PutOperationDeleteExistInfo(p.cli, op, info)
if err != nil {
return err
} else if !done {
return terror.ErrWorkerDDLLockInfoNotFound.Generatef("DDL info for (%s, %s) not found", info.Task, info.Source)
}
p.mu.Lock()
p.pendingInfo = nil
p.pendingOp = nil
p.mu.Unlock()
return nil
}
// PendingInfo returns the shard DDL info which is pending to handle.
func (p *Pessimist) PendingInfo() *pessimism.Info {
p.mu.RLock()
defer p.mu.RUnlock()
if p.pendingInfo == nil {
return nil
}
info := *p.pendingInfo
return &info
}
// PendingOperation returns the shard DDL lock operation which is pending to handle.
func (p *Pessimist) PendingOperation() *pessimism.Operation {
p.mu.RLock()
defer p.mu.RUnlock()
if p.pendingOp == nil {
return nil
}
op := *p.pendingOp
return &op
}