@@ -18,7 +18,9 @@ import (
18
18
"math"
19
19
"strconv"
20
20
"sync"
21
+ "sync/atomic"
21
22
"time"
23
+ "unsafe"
22
24
23
25
"github.com/coreos/etcd/clientv3"
24
26
"github.com/coreos/etcd/clientv3/concurrency"
@@ -88,7 +90,7 @@ type SchemaSyncer interface {
88
90
type schemaVersionSyncer struct {
89
91
selfSchemaVerPath string
90
92
etcdCli * clientv3.Client
91
- session * concurrency. Session
93
+ session unsafe. Pointer
92
94
mu struct {
93
95
sync.RWMutex
94
96
globalVerCh clientv3.WatchChan
@@ -143,23 +145,32 @@ func (s *schemaVersionSyncer) Init(ctx context.Context) error {
143
145
return errors .Trace (err )
144
146
}
145
147
logPrefix := fmt .Sprintf ("[%s] %s" , ddlPrompt , s .selfSchemaVerPath )
146
- s . session , err = owner .NewSession (ctx , logPrefix , s .etcdCli , owner .NewSessionDefaultRetryCnt , SyncerSessionTTL )
148
+ session , err : = owner .NewSession (ctx , logPrefix , s .etcdCli , owner .NewSessionDefaultRetryCnt , SyncerSessionTTL )
147
149
if err != nil {
148
150
return errors .Trace (err )
149
151
}
152
+ s .storeSession (session )
150
153
151
154
s .mu .Lock ()
152
155
s .mu .globalVerCh = s .etcdCli .Watch (ctx , DDLGlobalSchemaVersion )
153
156
s .mu .Unlock ()
154
157
155
158
err = PutKVToEtcd (ctx , s .etcdCli , keyOpDefaultRetryCnt , s .selfSchemaVerPath , InitialVersion ,
156
- clientv3 .WithLease (s .session .Lease ()))
159
+ clientv3 .WithLease (s .loadSession () .Lease ()))
157
160
return errors .Trace (err )
158
161
}
159
162
163
+ func (s * schemaVersionSyncer ) loadSession () * concurrency.Session {
164
+ return (* concurrency .Session )(atomic .LoadPointer (& s .session ))
165
+ }
166
+
167
+ func (s * schemaVersionSyncer ) storeSession (session * concurrency.Session ) {
168
+ atomic .StorePointer (& s .session , (unsafe .Pointer )(session ))
169
+ }
170
+
160
171
// Done implements SchemaSyncer.Done interface.
161
172
func (s * schemaVersionSyncer ) Done () <- chan struct {} {
162
- return s .session .Done ()
173
+ return s .loadSession () .Done ()
163
174
}
164
175
165
176
// Restart implements SchemaSyncer.Restart interface.
@@ -176,12 +187,12 @@ func (s *schemaVersionSyncer) Restart(ctx context.Context) error {
176
187
if err != nil {
177
188
return errors .Trace (err )
178
189
}
179
- s .session = session
190
+ s .storeSession ( session )
180
191
181
192
childCtx , cancel := context .WithTimeout (ctx , keyOpDefaultTimeout )
182
193
defer cancel ()
183
194
err = PutKVToEtcd (childCtx , s .etcdCli , putKeyRetryUnlimited , s .selfSchemaVerPath , InitialVersion ,
184
- clientv3 .WithLease (s .session .Lease ()))
195
+ clientv3 .WithLease (s .loadSession () .Lease ()))
185
196
186
197
return errors .Trace (err )
187
198
}
@@ -219,7 +230,7 @@ func (s *schemaVersionSyncer) UpdateSelfVersion(ctx context.Context, version int
219
230
startTime := time .Now ()
220
231
ver := strconv .FormatInt (version , 10 )
221
232
err := PutKVToEtcd (ctx , s .etcdCli , putKeyNoRetry , s .selfSchemaVerPath , ver ,
222
- clientv3 .WithLease (s .session .Lease ()))
233
+ clientv3 .WithLease (s .loadSession () .Lease ()))
223
234
224
235
metrics .UpdateSelfVersionHistogram .WithLabelValues (metrics .RetLabel (err )).Observe (time .Since (startTime ).Seconds ())
225
236
return errors .Trace (err )
0 commit comments