forked from nsqio/nsq
/
lookup_protocol_v1.go
261 lines (223 loc) · 7.86 KB
/
lookup_protocol_v1.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
package main
import (
"bufio"
"encoding/binary"
"encoding/json"
"fmt"
"github.com/bitly/go-nsq"
"github.com/bitly/nsq/util"
"io"
"log"
"net"
"os"
"strings"
"time"
)
type LookupProtocolV1 struct {
context *Context
}
func (p *LookupProtocolV1) IOLoop(conn net.Conn) error {
var err error
var line string
client := NewClientV1(conn)
err = nil
reader := bufio.NewReader(client)
for {
line, err = reader.ReadString('\n')
if err != nil {
break
}
line = strings.TrimSpace(line)
params := strings.Split(line, " ")
response, err := p.Exec(client, reader, params)
if err != nil {
context := ""
if parentErr := err.(util.ChildErr).Parent(); parentErr != nil {
context = " - " + parentErr.Error()
}
log.Printf("ERROR: [%s] - %s%s", client, err.Error(), context)
_, err = util.SendResponse(client, []byte(err.Error()))
if err != nil {
break
}
// errors of type FatalClientErr should forceably close the connection
if _, ok := err.(*util.FatalClientErr); ok {
break
}
continue
}
if response != nil {
_, err = util.SendResponse(client, response)
if err != nil {
break
}
}
}
log.Printf("CLIENT(%s): closing", client)
if client.peerInfo != nil {
registrations := p.context.nsqlookupd.DB.LookupRegistrations(client.peerInfo.id)
for _, r := range registrations {
if removed, _ := p.context.nsqlookupd.DB.RemoveProducer(r, client.peerInfo.id); removed {
log.Printf("DB: client(%s) UNREGISTER category:%s key:%s subkey:%s",
client, r.Category, r.Key, r.SubKey)
}
}
}
return err
}
func (p *LookupProtocolV1) Exec(client *ClientV1, reader *bufio.Reader, params []string) ([]byte, error) {
switch params[0] {
case "PING":
return p.PING(client, params)
case "IDENTIFY":
return p.IDENTIFY(client, reader, params[1:])
case "REGISTER":
return p.REGISTER(client, reader, params[1:])
case "UNREGISTER":
return p.UNREGISTER(client, reader, params[1:])
}
return nil, util.NewFatalClientErr(nil, "E_INVALID", fmt.Sprintf("invalid command %s", params[0]))
}
func getTopicChan(command string, params []string) (string, string, error) {
if len(params) == 0 {
return "", "", util.NewFatalClientErr(nil, "E_INVALID", fmt.Sprintf("%s insufficient number of params", command))
}
topicName := params[0]
var channelName string
if len(params) >= 2 {
channelName = params[1]
}
if !nsq.IsValidTopicName(topicName) {
return "", "", util.NewFatalClientErr(nil, "E_BAD_TOPIC", fmt.Sprintf("%s topic name '%s' is not valid", command, topicName))
}
if channelName != "" && !nsq.IsValidChannelName(channelName) {
return "", "", util.NewFatalClientErr(nil, "E_BAD_CHANNEL", fmt.Sprintf("%s channel name '%s' is not valid", command, channelName))
}
return topicName, channelName, nil
}
func (p *LookupProtocolV1) REGISTER(client *ClientV1, reader *bufio.Reader, params []string) ([]byte, error) {
if client.peerInfo == nil {
return nil, util.NewFatalClientErr(nil, "E_INVALID", "client must IDENTIFY")
}
topic, channel, err := getTopicChan("REGISTER", params)
if err != nil {
return nil, err
}
if channel != "" {
key := Registration{"channel", topic, channel}
if p.context.nsqlookupd.DB.AddProducer(key, &Producer{peerInfo: client.peerInfo}) {
log.Printf("DB: client(%s) REGISTER category:%s key:%s subkey:%s",
client, "channel", topic, channel)
}
}
key := Registration{"topic", topic, ""}
if p.context.nsqlookupd.DB.AddProducer(key, &Producer{peerInfo: client.peerInfo}) {
log.Printf("DB: client(%s) REGISTER category:%s key:%s subkey:%s",
client, "topic", topic, "")
}
return []byte("OK"), nil
}
func (p *LookupProtocolV1) UNREGISTER(client *ClientV1, reader *bufio.Reader, params []string) ([]byte, error) {
if client.peerInfo == nil {
return nil, util.NewFatalClientErr(nil, "E_INVALID", "client must IDENTIFY")
}
topic, channel, err := getTopicChan("UNREGISTER", params)
if err != nil {
return nil, err
}
if channel != "" {
key := Registration{"channel", topic, channel}
removed, left := p.context.nsqlookupd.DB.RemoveProducer(key, client.peerInfo.id)
if removed {
log.Printf("DB: client(%s) UNREGISTER category:%s key:%s subkey:%s",
client, "channel", topic, channel)
}
// for ephemeral channels, remove the channel as well if it has no producers
if left == 0 && strings.HasSuffix(channel, "#ephemeral") {
p.context.nsqlookupd.DB.RemoveRegistration(key)
}
} else {
// no channel was specified so this is a topic unregistration
// remove all of the channel registrations...
// normally this shouldn't happen which is why we print a warning message
// if anything is actually removed
registrations := p.context.nsqlookupd.DB.FindRegistrations("channel", topic, "*")
for _, r := range registrations {
if removed, _ := p.context.nsqlookupd.DB.RemoveProducer(r, client.peerInfo.id); removed {
log.Printf("WARNING: client(%s) unexpected UNREGISTER category:%s key:%s subkey:%s",
client, "channel", topic, r.SubKey)
}
}
key := Registration{"topic", topic, ""}
if removed, _ := p.context.nsqlookupd.DB.RemoveProducer(key, client.peerInfo.id); removed {
log.Printf("DB: client(%s) UNREGISTER category:%s key:%s subkey:%s",
client, "topic", topic, "")
}
}
return []byte("OK"), nil
}
func (p *LookupProtocolV1) IDENTIFY(client *ClientV1, reader *bufio.Reader, params []string) ([]byte, error) {
var err error
if client.peerInfo != nil {
return nil, util.NewFatalClientErr(err, "E_INVALID", "cannot IDENTIFY again")
}
var bodyLen int32
err = binary.Read(reader, binary.BigEndian, &bodyLen)
if err != nil {
return nil, util.NewFatalClientErr(err, "E_BAD_BODY", "IDENTIFY failed to read body size")
}
body := make([]byte, bodyLen)
_, err = io.ReadFull(reader, body)
if err != nil {
return nil, util.NewFatalClientErr(err, "E_BAD_BODY", "IDENTIFY failed to read body")
}
// body is a json structure with producer information
peerInfo := PeerInfo{id: client.RemoteAddr().String()}
err = json.Unmarshal(body, &peerInfo)
if err != nil {
return nil, util.NewFatalClientErr(err, "E_BAD_BODY", "IDENTIFY failed to decode JSON body")
}
peerInfo.RemoteAddress = client.RemoteAddr().String()
//TODO: remove this check for 1.0
if peerInfo.BroadcastAddress == "" {
peerInfo.BroadcastAddress = peerInfo.Address
}
// require all fields
if peerInfo.BroadcastAddress == "" || peerInfo.TcpPort == 0 || peerInfo.HttpPort == 0 || peerInfo.Version == "" {
return nil, util.NewFatalClientErr(nil, "E_BAD_BODY", "IDENTIFY missing fields")
}
peerInfo.lastUpdate = time.Now()
log.Printf("CLIENT(%s): IDENTIFY Address:%s TCP:%d HTTP:%d Version:%s",
client, peerInfo.BroadcastAddress, peerInfo.TcpPort, peerInfo.HttpPort, peerInfo.Version)
client.peerInfo = &peerInfo
if p.context.nsqlookupd.DB.AddProducer(Registration{"client", "", ""}, &Producer{peerInfo: client.peerInfo}) {
log.Printf("DB: client(%s) REGISTER category:%s key:%s subkey:%s", client, "client", "", "")
}
// build a response
data := make(map[string]interface{})
data["tcp_port"] = p.context.nsqlookupd.tcpAddr.Port
data["http_port"] = p.context.nsqlookupd.httpAddr.Port
data["version"] = util.BINARY_VERSION
hostname, err := os.Hostname()
if err != nil {
log.Fatalf("ERROR: unable to get hostname %s", err.Error())
}
data["address"] = hostname //TODO: remove for 1.0
data["broadcast_address"] = p.context.nsqlookupd.broadcastAddress
data["hostname"] = hostname
response, err := json.Marshal(data)
if err != nil {
log.Printf("ERROR: marshaling %v", data)
return []byte("OK"), nil
}
return response, nil
}
func (p *LookupProtocolV1) PING(client *ClientV1, params []string) ([]byte, error) {
if client.peerInfo != nil {
// we could get a PING before other commands on the same client connection
now := time.Now()
log.Printf("CLIENT(%s): pinged (last ping %s)", client.peerInfo.id, now.Sub(client.peerInfo.lastUpdate))
client.peerInfo.lastUpdate = now
}
return []byte("OK"), nil
}