-
Notifications
You must be signed in to change notification settings - Fork 271
/
service_commands.go
184 lines (165 loc) · 5.47 KB
/
service_commands.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
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright 2021 - 2022 Matrix Origin
//
// 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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package logservice
import (
"context"
"fmt"
"reflect"
"time"
"go.uber.org/zap"
"github.com/matrixorigin/matrixone/pkg/hakeeper"
pb "github.com/matrixorigin/matrixone/pkg/pb/logservice"
v2 "github.com/matrixorigin/matrixone/pkg/util/metric/v2"
"github.com/matrixorigin/matrixone/pkg/util/trace"
)
func (s *Service) handleCommands(cmds []pb.ScheduleCommand) {
for _, cmd := range cmds {
s.runtime.Logger().Info(fmt.Sprintf("%s applying cmd: %s", s.ID(), cmd.LogString()))
if cmd.GetConfigChange() != nil {
s.runtime.Logger().Debug("applying schedule command:", zap.String("command", cmd.LogString()))
switch cmd.ConfigChange.ChangeType {
case pb.AddReplica:
s.handleAddReplica(cmd)
case pb.RemoveReplica:
s.handleRemoveReplica(cmd)
case pb.StartReplica:
s.handleStartReplica(cmd)
case pb.StopReplica:
s.handleStopReplica(cmd)
case pb.KillZombie:
s.handleKillZombie(cmd)
default:
panic("unknown config change cmd type")
}
} else if cmd.GetShutdownStore() != nil {
s.handleShutdownStore(cmd)
} else if cmd.GetCreateTaskService() != nil {
s.createTaskService(cmd.CreateTaskService)
s.createSQLLogger(cmd.CreateTaskService)
} else {
panic("unknown schedule command type")
}
}
}
func (s *Service) handleAddReplica(cmd pb.ScheduleCommand) {
shardID := cmd.ConfigChange.Replica.ShardID
replicaID := cmd.ConfigChange.Replica.ReplicaID
epoch := cmd.ConfigChange.Replica.Epoch
target := cmd.ConfigChange.Replica.UUID
if err := s.store.addReplica(shardID, replicaID, target, epoch); err != nil {
s.runtime.Logger().Error("failed to add replica", zap.Error(err))
}
}
func (s *Service) handleRemoveReplica(cmd pb.ScheduleCommand) {
shardID := cmd.ConfigChange.Replica.ShardID
replicaID := cmd.ConfigChange.Replica.ReplicaID
epoch := cmd.ConfigChange.Replica.Epoch
if err := s.store.removeReplica(shardID, replicaID, epoch); err != nil {
s.runtime.Logger().Error("failed to remove replica", zap.Error(err))
}
}
func (s *Service) handleStartReplica(cmd pb.ScheduleCommand) {
shardID := cmd.ConfigChange.Replica.ShardID
replicaID := cmd.ConfigChange.Replica.ReplicaID
join := len(cmd.ConfigChange.InitialMembers) == 0
if shardID == hakeeper.DefaultHAKeeperShardID {
if err := s.store.startHAKeeperReplica(replicaID,
cmd.ConfigChange.InitialMembers, join); err != nil {
s.runtime.Logger().Error("failed to start HAKeeper replica", zap.Error(err))
}
} else {
if err := s.store.startReplica(shardID,
replicaID, cmd.ConfigChange.InitialMembers, join); err != nil {
s.runtime.Logger().Error("failed to start log replica", zap.Error(err))
}
}
}
func (s *Service) handleStopReplica(cmd pb.ScheduleCommand) {
shardID := cmd.ConfigChange.Replica.ShardID
replicaID := cmd.ConfigChange.Replica.ReplicaID
if err := s.store.stopReplica(shardID, replicaID); err != nil {
s.runtime.Logger().Error("failed to stop replica", zap.Error(err))
}
}
func (s *Service) handleKillZombie(cmd pb.ScheduleCommand) {
shardID := cmd.ConfigChange.Replica.ShardID
replicaID := cmd.ConfigChange.Replica.ReplicaID
s.handleStopReplica(cmd)
s.store.removeMetadata(shardID, replicaID)
}
func (s *Service) handleShutdownStore(_ pb.ScheduleCommand) {
// notify main routine that have received shutdown cmd
select {
case s.shutdownC <- struct{}{}:
default:
}
}
func (s *Service) heartbeatWorker(ctx context.Context) {
// TODO: check tick interval
if s.cfg.HeartbeatInterval.Duration == 0 {
panic("invalid heartbeat interval")
}
defer func() {
s.runtime.Logger().Info("heartbeat worker stopped")
}()
ticker := time.NewTicker(s.cfg.HeartbeatInterval.Duration)
defer ticker.Stop()
ctx, span := trace.Start(ctx, "heartbeatWorker")
defer span.End()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
s.heartbeat(ctx)
// I'd call this an ugly hack to just workaround select's
// policy of randomly picking a ready channel from the case list.
select {
case <-ctx.Done():
return
default:
}
}
}
}
func (s *Service) heartbeat(ctx context.Context) {
start := time.Now()
defer func() {
v2.LogHeartbeatHistogram.Observe(time.Since(start).Seconds())
}()
ctx2, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
if s.haClient == nil {
if reflect.DeepEqual(s.cfg.HAKeeperClientConfig, HAKeeperClientConfig{}) {
panic("empty HAKeeper client config")
}
cc, err := NewLogHAKeeperClient(ctx2, s.cfg.GetHAKeeperClientConfig())
if err != nil {
s.runtime.Logger().Error("failed to create HAKeeper client", zap.Error(err))
return
}
s.haClient = cc
}
hb := s.store.getHeartbeatMessage()
hb.TaskServiceCreated = s.taskServiceCreated()
hb.ConfigData = s.config.GetData()
cb, err := s.haClient.SendLogHeartbeat(ctx2, hb)
if err != nil {
v2.LogHeartbeatFailureCounter.Inc()
s.runtime.Logger().Error("failed to send log service heartbeat", zap.Error(err))
return
}
s.config.DecrCount()
s.handleCommands(cb.Commands)
}