forked from GoogleCloudPlatform/compute-image-windows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwsfc.go
277 lines (233 loc) · 6.92 KB
/
wsfc.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Copyright 2017 Google Inc. All Rights Reserved.
//
// 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 main
import (
"net"
"strconv"
"strings"
"sync"
"time"
"github.com/GoogleCloudPlatform/compute-image-windows/logger"
)
const wsfcDefaultAgentPort = "59998"
type agentState int
// Enum for agentState
const (
running agentState = iota
stopped
)
var (
once sync.Once
agentInstance *wsfcAgent
)
type wsfcManager struct {
agentNewState agentState
agentNewPort string
agent healthAgent
}
// Create new wsfcManager based on metadata agent request state will be set to
// running if one of the following is true:
// - EnableWSFC is set
// - WSFCAddresses is set (As an advanced setting, it will always override EnableWSFC flag)
func newWsfcManager() *wsfcManager {
newState := stopped
enabled, err := config.Section("wsfc").Key("enabled").Bool()
if (err == nil && enabled) || len(config.Section("wsfc").Key("addresses").String()) > 0 {
newState = running
} else if err != nil {
enabled, err = strconv.ParseBool(newMetadata.Instance.Attributes.EnableWSFC)
if (err == nil && enabled) || len(newMetadata.Instance.Attributes.WSFCAddresses) > 0 {
newState = running
} else if err != nil {
enabled, err = strconv.ParseBool(newMetadata.Project.Attributes.EnableWSFC)
if (err == nil && enabled) || len(newMetadata.Project.Attributes.WSFCAddresses) > 0 {
newState = running
}
}
}
newPort := wsfcDefaultAgentPort
port := config.Section("wsfc").Key("port").String()
if len(port) > 0 {
newPort = port
} else if len(newMetadata.Instance.Attributes.WSFCAgentPort) > 0 {
newPort = newMetadata.Instance.Attributes.WSFCAgentPort
} else if len(newMetadata.Project.Attributes.WSFCAgentPort) > 0 {
newPort = newMetadata.Instance.Attributes.WSFCAgentPort
}
return &wsfcManager{agentNewState: newState, agentNewPort: newPort, agent: getWsfcAgentInstance()}
}
// Implement manager.diff()
func (m *wsfcManager) diff() bool {
return m.agentNewState != m.agent.getState() || m.agentNewPort != m.agent.getPort()
}
// Implement manager.disabled().
// wsfc manager is always enabled. The manager is just a broker which manages the state of wsfcAgent. User
// can disable the wsfc feature by setting the metadata. If the manager is disabled, the agent will stop.
func (m *wsfcManager) disabled() bool {
return false
}
func (m *wsfcManager) timeout() bool {
return false
}
// Diff will always be called before set. So in set, only two cases are possible:
// - state changed: start or stop the wsfc agent accordingly
// - port changed: restart the agent if it is running
func (m *wsfcManager) set() error {
m.agent.setPort(m.agentNewPort)
// if state changes
if m.agentNewState != m.agent.getState() {
if m.agentNewState == running {
return m.agent.run()
}
return m.agent.stop()
}
// If port changed
if m.agent.getState() == running {
if err := m.agent.stop(); err != nil {
return err
}
return m.agent.run()
}
return nil
}
// interface for agent answering health check ping
type healthAgent interface {
getState() agentState
getPort() string
setPort(string)
run() error
stop() error
}
// Windows failover cluster agent, implements healthAgent interface
type wsfcAgent struct {
port string
waitGroup *sync.WaitGroup
listener *net.TCPListener
}
// Start agent and taking tcp request
func (a *wsfcAgent) run() error {
if a.getState() == running {
logger.Infoln("wsfc agent is already running")
return nil
}
logger.Info("Starting wsfc agent...")
listenerAddr, err := net.ResolveTCPAddr("tcp", ":"+a.port)
if err != nil {
return err
}
listener, err := net.ListenTCP("tcp", listenerAddr)
if err != nil {
return err
}
// goroutine for handling request
go func() {
for {
conn, err := listener.Accept()
if err != nil {
// if err is not due to listener closed, return
if opErr, ok := err.(*net.OpError); ok && strings.Contains(opErr.Error(), "closed") {
logger.Info("wsfc agent - tcp listener closed.")
return
}
logger.Errorln("wsfc agent - error on accepting request: ", err)
continue
}
a.waitGroup.Add(1)
go a.handleHealthCheckRequest(conn)
}
}()
logger.Infoln("wsfc agent stared. Listening on port:", a.port)
a.listener = listener
return nil
}
// Handle health check request.
// The request payload is WSFC ip address.
// Sendback 1 if ipaddress is found locally and 0 otherwise.
func (a *wsfcAgent) handleHealthCheckRequest(conn net.Conn) {
defer conn.Close()
defer a.waitGroup.Done()
conn.SetDeadline(time.Now().Add(time.Second))
buf := make([]byte, 1024)
// Read the incoming connection into the buffer.
reqLen, err := conn.Read(buf)
if err != nil {
logger.Errorln("wsfc - error on processing request:", err)
return
}
wsfcIP := strings.TrimSpace(string(buf[:reqLen]))
reply, err := checkIPExist(wsfcIP)
if err != nil {
logger.Errorln("wsfc - error on checking local ip:", err)
}
conn.Write([]byte(reply))
}
// Stop agent. Will wait for all existing request to be completed.
func (a *wsfcAgent) stop() error {
if a.getState() == stopped {
logger.Info("wsfc agent already stopped.")
return nil
}
logger.Info("Stopping wsfc agent...")
// close listener first to avoid taking additional request
err := a.listener.Close()
// wait for exiting request to finish
a.waitGroup.Wait()
a.listener = nil
logger.Info("wsfc agent stopped.")
return err
}
// Get the current state of the agent. If there is a valid listener,
// return state running and if listener is nil, return stopped
func (a *wsfcAgent) getState() agentState {
if a.listener != nil {
return running
}
return stopped
}
func (a *wsfcAgent) getPort() string {
return a.port
}
func (a *wsfcAgent) setPort(newPort string) {
if newPort != a.port {
logger.Infof("update wsfc agent from port %v to %v", a.port, newPort)
a.port = newPort
}
}
// Create wsfc agent only once
func getWsfcAgentInstance() *wsfcAgent {
once.Do(func() {
agentInstance = &wsfcAgent{
port: wsfcDefaultAgentPort,
waitGroup: &sync.WaitGroup{},
listener: nil,
}
})
return agentInstance
}
// help func to check whether the ip exists on local host.
func checkIPExist(ip string) (string, error) {
addrs, err := net.InterfaceAddrs()
if err != nil {
return "0", err
}
for _, address := range addrs {
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
ipString := ipnet.IP.To4().String()
if ip == ipString {
return "1", nil
}
}
}
return "0", nil
}