66package tabletservermock
77
88import (
9+ "sync"
10+
911 "github.com/youtube/vitess/go/vt/dbconfigs"
1012 "github.com/youtube/vitess/go/vt/mysqlctl"
1113 querypb "github.com/youtube/vitess/go/vt/proto/query"
@@ -38,6 +40,9 @@ type StateChange struct {
3840
3941// Controller is a mock tabletserver.Controller
4042type Controller struct {
43+ // mu protects the fields in this structure
44+ mu sync.Mutex
45+
4146 // CurrentTarget stores the last known target
4247 CurrentTarget querypb.Target
4348
@@ -84,12 +89,18 @@ func (tqsc *Controller) AddStatusPart() {
8489
8590// InitDBConfig is part of the tabletserver.Controller interface
8691func (tqsc * Controller ) InitDBConfig (target querypb.Target , dbConfigs dbconfigs.DBConfigs , schemaOverrides []tabletserver.SchemaOverride , mysqld mysqlctl.MysqlDaemon ) error {
92+ tqsc .mu .Lock ()
93+ defer tqsc .mu .Unlock ()
94+
8795 tqsc .CurrentTarget = target
8896 return nil
8997}
9098
9199// SetServingType is part of the tabletserver.Controller interface
92100func (tqsc * Controller ) SetServingType (tabletType topodatapb.TabletType , serving bool , alsoAllow []topodatapb.TabletType ) (bool , error ) {
101+ tqsc .mu .Lock ()
102+ defer tqsc .mu .Unlock ()
103+
93104 stateChanged := false
94105 if tqsc .SetServingTypeError == nil {
95106 stateChanged = tqsc .QueryServiceEnabled != serving || tqsc .CurrentTarget .TabletType != tabletType
@@ -108,16 +119,25 @@ func (tqsc *Controller) SetServingType(tabletType topodatapb.TabletType, serving
108119
109120// IsServing is part of the tabletserver.Controller interface
110121func (tqsc * Controller ) IsServing () bool {
122+ tqsc .mu .Lock ()
123+ defer tqsc .mu .Unlock ()
124+
111125 return tqsc .QueryServiceEnabled
112126}
113127
114128// IsHealthy is part of the tabletserver.Controller interface
115129func (tqsc * Controller ) IsHealthy () error {
130+ tqsc .mu .Lock ()
131+ defer tqsc .mu .Unlock ()
132+
116133 return tqsc .IsHealthyError
117134}
118135
119136// ReloadSchema is part of the tabletserver.Controller interface
120137func (tqsc * Controller ) ReloadSchema () {
138+ tqsc .mu .Lock ()
139+ defer tqsc .mu .Unlock ()
140+
121141 tqsc .ReloadSchemaCount ++
122142}
123143
@@ -150,6 +170,9 @@ func (tqsc *Controller) QueryServiceStats() *tabletserver.QueryServiceStats {
150170
151171// BroadcastHealth is part of the tabletserver.Controller interface
152172func (tqsc * Controller ) BroadcastHealth (terTimestamp int64 , stats * querypb.RealtimeStats ) {
173+ tqsc .mu .Lock ()
174+ defer tqsc .mu .Unlock ()
175+
153176 tqsc .BroadcastData <- & BroadcastData {
154177 TERTimestamp : terTimestamp ,
155178 RealtimeStats : * stats ,
@@ -159,5 +182,8 @@ func (tqsc *Controller) BroadcastHealth(terTimestamp int64, stats *querypb.Realt
159182
160183// EnterLameduck implements tabletserver.Controller.
161184func (tqsc * Controller ) EnterLameduck () {
185+ tqsc .mu .Lock ()
186+ defer tqsc .mu .Unlock ()
187+
162188 tqsc .IsInLameduck = true
163189}
0 commit comments