@@ -18,7 +18,9 @@ import (
1818 "math"
1919 "strconv"
2020 "sync"
21+ "sync/atomic"
2122 "time"
23+ "unsafe"
2224
2325 "github.com/coreos/etcd/clientv3"
2426 "github.com/coreos/etcd/clientv3/concurrency"
@@ -88,7 +90,7 @@ type SchemaSyncer interface {
8890type schemaVersionSyncer struct {
8991 selfSchemaVerPath string
9092 etcdCli * clientv3.Client
91- session * concurrency. Session
93+ session unsafe. Pointer
9294 mu struct {
9395 sync.RWMutex
9496 globalVerCh clientv3.WatchChan
@@ -143,23 +145,32 @@ func (s *schemaVersionSyncer) Init(ctx context.Context) error {
143145 return errors .Trace (err )
144146 }
145147 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 )
147149 if err != nil {
148150 return errors .Trace (err )
149151 }
152+ s .storeSession (session )
150153
151154 s .mu .Lock ()
152155 s .mu .globalVerCh = s .etcdCli .Watch (ctx , DDLGlobalSchemaVersion )
153156 s .mu .Unlock ()
154157
155158 err = PutKVToEtcd (ctx , s .etcdCli , keyOpDefaultRetryCnt , s .selfSchemaVerPath , InitialVersion ,
156- clientv3 .WithLease (s .session .Lease ()))
159+ clientv3 .WithLease (s .loadSession () .Lease ()))
157160 return errors .Trace (err )
158161}
159162
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+
160171// Done implements SchemaSyncer.Done interface.
161172func (s * schemaVersionSyncer ) Done () <- chan struct {} {
162- return s .session .Done ()
173+ return s .loadSession () .Done ()
163174}
164175
165176// Restart implements SchemaSyncer.Restart interface.
@@ -176,12 +187,12 @@ func (s *schemaVersionSyncer) Restart(ctx context.Context) error {
176187 if err != nil {
177188 return errors .Trace (err )
178189 }
179- s .session = session
190+ s .storeSession ( session )
180191
181192 childCtx , cancel := context .WithTimeout (ctx , keyOpDefaultTimeout )
182193 defer cancel ()
183194 err = PutKVToEtcd (childCtx , s .etcdCli , putKeyRetryUnlimited , s .selfSchemaVerPath , InitialVersion ,
184- clientv3 .WithLease (s .session .Lease ()))
195+ clientv3 .WithLease (s .loadSession () .Lease ()))
185196
186197 return errors .Trace (err )
187198}
@@ -219,7 +230,7 @@ func (s *schemaVersionSyncer) UpdateSelfVersion(ctx context.Context, version int
219230 startTime := time .Now ()
220231 ver := strconv .FormatInt (version , 10 )
221232 err := PutKVToEtcd (ctx , s .etcdCli , putKeyNoRetry , s .selfSchemaVerPath , ver ,
222- clientv3 .WithLease (s .session .Lease ()))
233+ clientv3 .WithLease (s .loadSession () .Lease ()))
223234
224235 metrics .UpdateSelfVersionHistogram .WithLabelValues (metrics .RetLabel (err )).Observe (time .Since (startTime ).Seconds ())
225236 return errors .Trace (err )
0 commit comments