/
survey.go
209 lines (178 loc) · 5.67 KB
/
survey.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
/**********************************************************************************
* Copyright (c) 2009-2020 Misakai Ltd.
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Affero General Public License as published by the Free Software
* Foundation, either version 3 of the License, or(at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License along
* with this program. If not, see<http://www.gnu.org/licenses/>.
************************************************************************************/
package survey
import (
"errors"
"fmt"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/emitter-io/emitter/internal/event"
"github.com/emitter-io/emitter/internal/message"
"github.com/emitter-io/emitter/internal/security"
"github.com/emitter-io/emitter/internal/service"
"github.com/weaveworks/mesh"
)
const (
idSystem = uint32(0)
idQuery = uint32(3939663052)
)
// Surveyee handles the surveys.
type Surveyee interface {
OnSurvey(queryType string, request []byte) (response []byte, ok bool)
}
type gossiper interface {
ID() uint64
NumPeers() int
SendTo(mesh.PeerName, *message.Message) error
}
// Surveyor represents a distributed surveyor.
type Surveyor struct {
pubsub service.PubSub // The pub/sub broker to use.
gossip gossiper // The cluster service to use.
luid security.ID // The locally unique id of the manager.
next uint32 // The next available query identifier.
awaiters *sync.Map // The map of the awaiters.
handlers []Surveyee // The handlers array.
}
// New creates a new distributed surveyor.
func New(p service.PubSub, g gossiper) *Surveyor {
return &Surveyor{
pubsub: p,
gossip: g,
luid: security.NewID(),
next: 0,
awaiters: new(sync.Map),
handlers: make([]Surveyee, 0),
}
}
// Start subscribes the manager to the query channel.
func (c *Surveyor) Start() {
ev := &event.Subscription{
Peer: c.gossip.ID(),
Conn: c.luid,
Ssid: message.Ssid{idSystem, idQuery},
}
c.pubsub.Subscribe(c, ev)
}
// HandleFunc adds a handler for a query.
func (c *Surveyor) HandleFunc(surveyees ...Surveyee) {
c.handlers = append(c.handlers, surveyees...)
}
// ID returns the unique identifier of the subsriber.
func (c *Surveyor) ID() string {
return c.luid.String()
}
// Type returns the type of the subscriber
func (c *Surveyor) Type() message.SubscriberType {
return message.SubscriberDirect
}
// Send occurs when we have received a message.
func (c *Surveyor) Send(m *message.Message) error {
ssid := m.Ssid()
if len(ssid) != 3 {
return errors.New("Invalid query received")
}
switch string(m.Channel) {
case "response":
// We received a response, find the awaiter and forward a message to it
return c.onResponse(ssid[2], m.Payload)
default:
// We received a request, need to handle that by calling the appropriate handler
return c.onRequest(ssid, string(m.Channel), m.Payload)
}
}
// onRequest handles an incoming request
func (c *Surveyor) onResponse(id uint32, payload []byte) error {
if awaiter, ok := c.awaiters.Load(id); ok {
awaiter.(*queryAwaiter).receive <- payload
}
return nil
}
// onRequest handles an incoming request
func (c *Surveyor) onRequest(ssid message.Ssid, channel string, payload []byte) error {
// Get the query and reply node
ch := strings.Split(channel, "/")
query := ch[0]
reply, err := strconv.ParseInt(ch[1], 10, 64)
if err != nil {
return err
}
// Do not answer our own requests
replyAddr := mesh.PeerName(reply)
if c.gossip.ID() == uint64(replyAddr) {
return nil
}
// Go through all the handlers and execute the first matching one
for _, surveyee := range c.handlers {
if response, ok := surveyee.OnSurvey(query, payload); ok {
return c.gossip.SendTo(replyAddr, message.New(ssid, []byte("response"), response))
}
}
return errors.New("no query handler found for " + channel)
}
// Query issues a cluster-wide request.
func (c *Surveyor) Query(query string, payload []byte) (message.Awaiter, error) {
// Create an awaiter
numPeers := c.gossip.NumPeers()
awaiter := &queryAwaiter{
id: atomic.AddUint32(&c.next, 1),
receive: make(chan []byte, numPeers),
maximum: numPeers,
manager: c,
}
// Store an awaiter
c.awaiters.Store(awaiter.id, awaiter)
// Prepare a channel with the reply-to address
channel := fmt.Sprintf("%v/%v", query, c.gossip.ID())
// Publish the query as a message
c.pubsub.Publish(message.New(
message.Ssid{idSystem, idQuery, awaiter.id},
[]byte(channel),
payload,
), nil)
return awaiter, nil
}
// queryAwaiter represents an asynchronously awaiting response channel.
type queryAwaiter struct {
id uint32 // The identifier of the query.
maximum int // The maximum number of responses to wait for.
receive chan []byte // The receive channel to use.
manager *Surveyor // The query manager used.
}
// Gather awaits for the responses to be received, blocking until we're done.
func (a *queryAwaiter) Gather(timeout time.Duration) (r [][]byte) {
defer func() { a.manager.awaiters.Delete(a.id) }()
r = make([][]byte, 0, 4)
t := time.After(timeout)
c := a.maximum
// If there's no peers, no need to receive anything
if c == 0 {
return
}
for {
select {
case msg := <-a.receive:
r = append(r, msg)
c-- // Decrement the counter
if c == 0 {
return // We got all the responses we needed
}
case <-t:
return // We timed out
}
}
}