/
client.go
275 lines (251 loc) · 6.81 KB
/
client.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
// Copyright 2018 The Chubao Authors.
//
// 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 master
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
"sync"
"time"
"github.com/chubaofs/chubaofs/proto"
"github.com/chubaofs/chubaofs/util/log"
)
const (
requestTimeout = 30 * time.Second
)
var (
ErrNoValidMaster = errors.New("no valid master")
)
type MasterClient struct {
sync.RWMutex
masters []string
useSSL bool
leaderAddr string
timeout time.Duration
adminAPI *AdminAPI
clientAPI *ClientAPI
nodeAPI *NodeAPI
userAPI *UserAPI
}
// AddNode add the given address as the master address.
func (c *MasterClient) AddNode(address string) {
c.Lock()
c.updateMaster(address)
c.Unlock()
}
// Leader returns the current leader address.
func (c *MasterClient) Leader() (addr string) {
c.RLock()
addr = c.leaderAddr
c.RUnlock()
return
}
func (c *MasterClient) AdminAPI() *AdminAPI {
return c.adminAPI
}
func (c *MasterClient) ClientAPI() *ClientAPI {
return c.clientAPI
}
func (c *MasterClient) NodeAPI() *NodeAPI {
return c.nodeAPI
}
func (c *MasterClient) UserAPI() *UserAPI {
return c.userAPI
}
// Change the leader address.
func (c *MasterClient) setLeader(addr string) {
c.Lock()
c.leaderAddr = addr
c.Unlock()
}
// Change the request timeout
func (c *MasterClient) SetTimeout(timeout uint16) {
c.Lock()
c.timeout = time.Duration(timeout) * time.Second
c.Unlock()
}
func (c *MasterClient) serveRequest(r *request) (repsData []byte, err error) {
leaderAddr, nodes := c.prepareRequest()
host := leaderAddr
for i := -1; i < len(nodes); i++ {
if i == -1 {
if host == "" {
continue
}
} else {
host = nodes[i]
}
var resp *http.Response
var schema string
if c.useSSL {
schema = "https"
} else {
schema = "http"
}
var url = fmt.Sprintf("%s://%s%s", schema, host,
r.path)
resp, err = c.httpRequest(r.method, url, r.params, r.header, r.body)
if err != nil {
log.LogErrorf("serveRequest: send http request fail: method(%v) url(%v) err(%v)", r.method, url, err)
continue
}
stateCode := resp.StatusCode
repsData, err = ioutil.ReadAll(resp.Body)
_ = resp.Body.Close()
if err != nil {
log.LogErrorf("serveRequest: read http response body fail: err(%v)", err)
continue
}
switch stateCode {
case http.StatusForbidden:
curMasterAddr := strings.TrimSpace(string(repsData))
curMasterAddr = strings.Replace(curMasterAddr, "\n", "", -1)
if len(curMasterAddr) == 0 {
log.LogWarnf("serveRequest: server response status 403: request(%s) status"+
"(403), body is empty", host)
err = ErrNoValidMaster
return
}
repsData, err = c.serveRequest(r)
return
case http.StatusOK:
if leaderAddr != host {
c.setLeader(host)
}
var body = &struct {
Code int32 `json:"code"`
Msg string `json:"msg"`
Data json.RawMessage `json:"data"`
}{}
if err := json.Unmarshal(repsData, body); err != nil {
log.LogErrorf("unmarshal response body err:%v", err)
return nil, fmt.Errorf("unmarshal response body err:%v", err)
}
// o represent proto.ErrCodeSuccess
if body.Code != 0 {
log.LogWarnf("serveRequest: code[%v], msg[%v], data[%v] ", body.Code, body.Msg, body.Data)
return nil, proto.ParseErrorCode(body.Code)
}
return []byte(body.Data), nil
default:
log.LogErrorf("serveRequest: unknown status: host(%v) uri(%v) status(%v) body(%s).",
resp.Request.URL.String(), host, stateCode, strings.Replace(string(repsData), "\n", "", -1))
continue
}
}
err = ErrNoValidMaster
return
}
// Nodes returns all master addresses.
func (c *MasterClient) Nodes() (nodes []string) {
c.RLock()
nodes = c.masters
c.RUnlock()
return
}
// prepareRequest returns the leader address and all master addresses.
func (c *MasterClient) prepareRequest() (addr string, nodes []string) {
c.RLock()
addr = c.leaderAddr
nodes = c.masters
c.RUnlock()
return
}
func (c *MasterClient) httpRequest(method, url string, param, header map[string]string, reqData []byte) (resp *http.Response, err error) {
client := http.DefaultClient
reader := bytes.NewReader(reqData)
if header["isTimeOut"] != "" {
var isTimeOut bool
if isTimeOut, err = strconv.ParseBool(header["isTimeOut"]); err != nil {
return
}
if isTimeOut {
client.Timeout = c.timeout
}
} else {
client.Timeout = c.timeout
}
var req *http.Request
fullUrl := c.mergeRequestUrl(url, param)
log.LogDebugf("httpRequest: merge request url: method(%v) url(%v) bodyLength[%v].", method, fullUrl, len(reqData))
if req, err = http.NewRequest(method, fullUrl, reader); err != nil {
return
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Connection", "close")
for k, v := range header {
req.Header.Set(k, v)
}
resp, err = client.Do(req)
return
}
func (c *MasterClient) updateMaster(address string) {
contains := false
for _, master := range c.masters {
if master == address {
contains = true
break
}
}
if !contains {
c.masters = append(c.masters, address)
}
c.leaderAddr = address
}
func (c *MasterClient) mergeRequestUrl(url string, params map[string]string) string {
if params != nil && len(params) > 0 {
buff := bytes.NewBuffer([]byte(url))
isFirstParam := true
for k, v := range params {
if isFirstParam {
buff.WriteString("?")
isFirstParam = false
} else {
buff.WriteString("&")
}
buff.WriteString(k)
buff.WriteString("=")
buff.WriteString(v)
}
return buff.String()
}
return url
}
// NewMasterHelper returns a new MasterClient instance.
func NewMasterClient(masters []string, useSSL bool) *MasterClient {
var mc = &MasterClient{masters: masters, useSSL: useSSL, timeout: requestTimeout}
mc.adminAPI = &AdminAPI{mc: mc}
mc.clientAPI = &ClientAPI{mc: mc}
mc.nodeAPI = &NodeAPI{mc: mc}
mc.userAPI = &UserAPI{mc: mc}
return mc
}
// NewMasterClientFromString parse raw master address configuration
// string and returns a new MasterClient instance.
// Notes that a valid format raw string must match: "{HOST}:{PORT},{HOST}:{PORT}"
func NewMasterClientFromString(masterAddr string, useSSL bool) *MasterClient {
var masters = make([]string, 0)
for _, master := range strings.Split(masterAddr, ",") {
master = strings.TrimSpace(master)
if master != "" {
masters = append(masters, master)
}
}
return NewMasterClient(masters, useSSL)
}