forked from pingcap/dm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathddl_exec_info.go
159 lines (136 loc) · 3.52 KB
/
ddl_exec_info.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
// Copyright 2019 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 syncer
import (
"context"
"sync"
"time"
"github.com/siddontang/go/sync2"
"github.com/pingcap/dm/dm/pb"
"github.com/pingcap/dm/pkg/terror"
)
var (
ddlExecIdle = "idle"
ddlExecSending = "sending"
ddlExecClosed = "closed"
)
// DDLExecItem wraps request and response for a sharding DDL execution
type DDLExecItem struct {
req *pb.ExecDDLRequest
resp chan error
}
// newDDLExecItem creates a new DDLExecItem
func newDDLExecItem(req *pb.ExecDDLRequest) *DDLExecItem {
item := &DDLExecItem{
req: req,
resp: make(chan error, 1), // one elem buffered
}
return item
}
// DDLExecInfo used by syncer to execute or ignore sharding DDL
// it's specific to syncer, and can not be used by other process unit
type DDLExecInfo struct {
sync.RWMutex
status sync2.AtomicString
ch chan *DDLExecItem // item.req.Exec: true for exec, false for ignore
cancel chan struct{} // chan used to cancel sending
ddls []string // DDL which is blocking
}
// NewDDLExecInfo creates a new DDLExecInfo
func NewDDLExecInfo() *DDLExecInfo {
i := &DDLExecInfo{
ch: make(chan *DDLExecItem), // un-buffered
cancel: make(chan struct{}),
}
i.status.Set(ddlExecIdle)
return i
}
// Renew renews the chan
func (i *DDLExecInfo) Renew() {
i.Lock()
defer i.Unlock()
i.cancelAndWaitSending()
if i.status.Get() != ddlExecClosed {
close(i.ch)
}
i.ch = make(chan *DDLExecItem)
i.cancel = make(chan struct{})
i.ddls = nil
i.status.Set(ddlExecIdle)
}
// Close closes the chan
func (i *DDLExecInfo) Close() {
i.Lock()
defer i.Unlock()
if i.status.CompareAndSwap(ddlExecClosed, ddlExecClosed) {
return
}
i.cancelAndWaitSending()
close(i.ch)
i.ddls = nil
i.status.Set(ddlExecClosed)
}
// NOTE: in caller, should do lock
func (i *DDLExecInfo) cancelAndWaitSending() {
// close the un-closed cancel chan
select {
case <-i.cancel:
default:
close(i.cancel)
}
// wait Send to return
timer := time.NewTicker(1 * time.Millisecond)
defer timer.Stop()
for range timer.C {
if !i.status.CompareAndSwap(ddlExecSending, ddlExecSending) {
return // return from select and for
}
}
}
// Send sends an item (with request) to the chan
func (i *DDLExecInfo) Send(ctx context.Context, item *DDLExecItem) error {
i.RLock()
if !i.status.CompareAndSwap(ddlExecIdle, ddlExecSending) {
i.RUnlock()
return terror.ErrSyncerUnitDDLExecChanCloseOrBusy.Generate()
}
i.RUnlock()
defer i.status.Set(ddlExecIdle)
select {
case <-ctx.Done():
return terror.ErrSyncerUnitDDLChanDone.Generate()
case i.ch <- item:
return nil
case <-i.cancel:
return terror.ErrSyncerUnitDDLChanCanceled.Generate()
}
}
// Chan returns a receive only DDLExecItem chan
func (i *DDLExecInfo) Chan(ddls []string) <-chan *DDLExecItem {
i.Lock()
i.ddls = ddls
i.Unlock()
return i.ch
}
// BlockingDDLs returns current blocking DDL
func (i *DDLExecInfo) BlockingDDLs() []string {
i.RLock()
defer i.RUnlock()
return i.ddls
}
// ClearBlockingDDL clears current blocking DDL
func (i *DDLExecInfo) ClearBlockingDDL() {
i.Lock()
defer i.Unlock()
i.ddls = nil
}