summaryrefslogtreecommitdiff
path: root/txnsync/incomingMsgQ.go
blob: aecb673bdd3490d8fae24b7d42a7f30e1e58a5e5 (plain)
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// Copyright (C) 2019-2021 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand 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.
//
// go-algorand 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 go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package txnsync

import (
	"sync"

	"github.com/algorand/go-deadlock"
)

// queuedMsgEntry used as a helper struct to manage the manipulation of incoming
// message queue.
type queuedMsgEntry struct {
	msg  incomingMessage
	next *queuedMsgEntry
	prev *queuedMsgEntry
}

type queuedMsgList struct {
	head *queuedMsgEntry
}

// incomingMessageQueue manages the global incoming message queue across all the incoming peers.
type incomingMessageQueue struct {
	outboundPeerCh    chan incomingMessage
	enqueuedPeersMap  map[*Peer]*queuedMsgEntry
	messages          queuedMsgList
	freelist          queuedMsgList
	enqueuedPeersMu   deadlock.Mutex
	enqueuedPeersCond *sync.Cond
	shutdownRequest   chan struct{}
	shutdownConfirmed chan struct{}
	deletePeersCh     chan interface{}
	peerlessCount     int
}

// maxPeersCount defines the maximum number of supported peers that can have their messages waiting
// in the incoming message queue at the same time. This number can be lower then the actual number of
// connected peers, as it's used only for pending messages.
const maxPeersCount = 2048

// maxPeerlessCount is the number of messages that we've received that doesn't have a Peer object allocated
// for them ( yet )
const maxPeerlessCount = 512

// makeIncomingMessageQueue creates an incomingMessageQueue object and initializes all the internal variables.
func makeIncomingMessageQueue() *incomingMessageQueue {
	imq := &incomingMessageQueue{
		outboundPeerCh:    make(chan incomingMessage),
		enqueuedPeersMap:  make(map[*Peer]*queuedMsgEntry, maxPeersCount),
		shutdownRequest:   make(chan struct{}, 1),
		shutdownConfirmed: make(chan struct{}, 1),
		deletePeersCh:     make(chan interface{}),
	}
	imq.enqueuedPeersCond = sync.NewCond(&imq.enqueuedPeersMu)
	imq.freelist.initialize(maxPeersCount)
	go imq.messagePump()
	return imq
}

// dequeueHead removes the first head message from the linked list.
func (ml *queuedMsgList) dequeueHead() (out *queuedMsgEntry) {
	if ml.head == nil {
		return nil
	}
	entry := ml.head
	out = entry
	if entry.next == entry {
		ml.head = nil
		return
	}
	entry.next.prev = entry.prev
	entry.prev.next = entry.next
	ml.head = entry.next
	out.next = out
	out.prev = out
	return
}

// initialize initializes a list to have msgCount entries.
func (ml *queuedMsgList) initialize(msgCount int) {
	msgs := make([]queuedMsgEntry, msgCount)
	for i := 0; i < msgCount; i++ {
		msgs[i].next = &msgs[(i+1)%msgCount]
		msgs[i].prev = &msgs[(i+msgCount-1)%msgCount]
	}
	ml.head = &msgs[0]
}

// empty methods tests to see if the linked list is empty
func (ml *queuedMsgList) empty() bool {
	return ml.head == nil
}

// remove removes the given msg from the linked list. The method
// is written with the assumption that the given msg is known to be
// part of the linked list.
func (ml *queuedMsgList) remove(msg *queuedMsgEntry) {
	if msg.next == msg {
		ml.head = nil
		return
	}
	msg.prev.next = msg.next
	msg.next.prev = msg.prev
	if ml.head == msg {
		ml.head = msg.next
	}
	msg.prev = msg
	msg.next = msg
}

// filterRemove removes zero or more messages from the linked list, for which the given
// removeFunc returns true. The removed linked list entries are returned as a linked list.
func (ml *queuedMsgList) filterRemove(removeFunc func(*queuedMsgEntry) bool) *queuedMsgEntry {
	if ml.empty() {
		return nil
	}
	// do we have a single item ?
	if ml.head.next == ml.head {
		if removeFunc(ml.head) {
			out := ml.head
			ml.head = nil
			return out
		}
		return nil
	}
	current := ml.head
	last := ml.head.prev
	var letGo queuedMsgList
	for {
		next := current.next
		if removeFunc(current) {
			ml.remove(current)
			letGo.enqueueTail(current)
		}
		if current == last {
			break
		}
		current = next
	}
	return letGo.head
}

// enqueueTail adds to the current linked list another linked list whose head is msg.
func (ml *queuedMsgList) enqueueTail(msg *queuedMsgEntry) {
	if ml.head == nil {
		ml.head = msg
		return
	} else if msg == nil {
		return
	}
	lastEntryOld := ml.head.prev
	lastEntryNew := msg.prev
	lastEntryOld.next = msg
	ml.head.prev = lastEntryNew
	msg.prev = lastEntryOld
	lastEntryNew.next = ml.head
}

// shutdown signals to the message pump to shut down and waits until the message pump goroutine
// aborts.
func (imq *incomingMessageQueue) shutdown() {
	imq.enqueuedPeersMu.Lock()
	close(imq.shutdownRequest)
	imq.enqueuedPeersCond.Signal()
	imq.enqueuedPeersMu.Unlock()
	<-imq.shutdownConfirmed
}

// messagePump is the incoming message queue message pump. It takes messages from the messages list
// and attempt to write these to the outboundPeerCh.
func (imq *incomingMessageQueue) messagePump() {
	defer close(imq.shutdownConfirmed)
	imq.enqueuedPeersMu.Lock()
	defer imq.enqueuedPeersMu.Unlock()

	for {
		// check if we need to shutdown.
		select {
		case <-imq.shutdownRequest:
			return
		default:
		}

		// do we have any item to enqueue ?
		if !imq.messages.empty() {
			msgEntry := imq.messages.dequeueHead()
			msg := msgEntry.msg
			imq.freelist.enqueueTail(msgEntry)
			if msg.peer != nil {
				delete(imq.enqueuedPeersMap, msg.peer)
			} else {
				imq.peerlessCount--
			}
			imq.enqueuedPeersMu.Unlock()
		writeOutboundMessage:
			select {
			case imq.outboundPeerCh <- msg:
				imq.enqueuedPeersMu.Lock()
				continue
			case <-imq.shutdownRequest:
				imq.enqueuedPeersMu.Lock()
				return
			// see if this msg need to be delivered or not.
			case droppedPeer := <-imq.deletePeersCh:
				if msg.networkPeer == droppedPeer {
					// we want to skip this message.
					imq.enqueuedPeersMu.Lock()
					continue
				}
				goto writeOutboundMessage
			}
		}
		imq.enqueuedPeersCond.Wait()
	}
}

// getIncomingMessageChannel returns the incoming messages channel, which would contain entries once
// we have one ( or more ) pending incoming messages.
func (imq *incomingMessageQueue) getIncomingMessageChannel() <-chan incomingMessage {
	return imq.outboundPeerCh
}

// enqueue places the given message on the queue, if and only if it's associated peer doesn't
// appear on the incoming message queue already. In the case there is no peer, the message
// would be placed on the queue as is.
// The method returns false if the incoming message doesn't have it's peer on the queue and
// the method has failed to place the message on the queue. True is returned otherwise.
func (imq *incomingMessageQueue) enqueue(m incomingMessage) bool {
	imq.enqueuedPeersMu.Lock()
	defer imq.enqueuedPeersMu.Unlock()
	if m.peer != nil {
		if _, has := imq.enqueuedPeersMap[m.peer]; has {
			return true
		}
	} else {
		// do we have enough "room" for peerless messages ?
		if imq.peerlessCount >= maxPeerlessCount {
			return false
		}
	}
	// do we have enough room in the message queue for the new message ?
	if imq.freelist.empty() {
		// no - we don't have enough room in the circular buffer.
		return false
	}
	freeMsgEntry := imq.freelist.dequeueHead()
	freeMsgEntry.msg = m
	imq.messages.enqueueTail(freeMsgEntry)
	// if we successfully enqueued the message, set the enqueuedPeersMap so that we won't enqueue the same peer twice.
	if m.peer != nil {
		imq.enqueuedPeersMap[m.peer] = freeMsgEntry
	} else {
		imq.peerlessCount++
	}
	imq.enqueuedPeersCond.Signal()
	return true
}

// erase removes all the entries associated with the given network peer.
// this method isn't very efficient, and should be used only in cases where
// we disconnect from a peer and want to cleanup all the pending tasks associated
// with that peer.
func (imq *incomingMessageQueue) erase(peer *Peer, networkPeer interface{}) {
	imq.enqueuedPeersMu.Lock()

	var peerMsgEntry *queuedMsgEntry
	if peer == nil {
		// lookup for a Peer object.
		for peer, peerMsgEntry = range imq.enqueuedPeersMap {
			if peer.networkPeer != networkPeer {
				continue
			}
			break
		}
	} else {
		var has bool
		if peerMsgEntry, has = imq.enqueuedPeersMap[peer]; !has {
			// the peer object is not in the map.
			peer = nil
		}
	}

	if peer != nil {
		delete(imq.enqueuedPeersMap, peer)
		imq.messages.remove(peerMsgEntry)
		imq.freelist.enqueueTail(peerMsgEntry)
		imq.enqueuedPeersMu.Unlock()
		select {
		case imq.deletePeersCh <- networkPeer:
		default:
		}
		return
	}

	imq.removeMessageByNetworkPeer(networkPeer)
	imq.enqueuedPeersMu.Unlock()
	select {
	case imq.deletePeersCh <- networkPeer:
	default:
	}
}

// removeMessageByNetworkPeer removes the messages associated with the given network peer from the
// queue.
// note : the method expect that the enqueuedPeersMu lock would be taken.
func (imq *incomingMessageQueue) removeMessageByNetworkPeer(networkPeer interface{}) {
	peerlessCount := 0
	removeByNetworkPeer := func(msg *queuedMsgEntry) bool {
		if msg.msg.networkPeer == networkPeer {
			if msg.msg.peer == nil {
				peerlessCount++
			}
			return true
		}
		return false
	}
	removeList := imq.messages.filterRemove(removeByNetworkPeer)
	imq.freelist.enqueueTail(removeList)
	imq.peerlessCount -= peerlessCount
}

// prunePeers removes from the enqueuedMessages queue all the entries that are not provided in the
// given activePeers slice.
func (imq *incomingMessageQueue) prunePeers(activePeers []PeerInfo) (peerRemoved bool) {
	activePeersMap := make(map[*Peer]bool)
	activeNetworkPeersMap := make(map[interface{}]bool)
	for _, activePeer := range activePeers {
		if activePeer.TxnSyncPeer != nil {
			activePeersMap[activePeer.TxnSyncPeer] = true
		}
		if activePeer.NetworkPeer != nil {
			activeNetworkPeersMap[activePeer.NetworkPeer] = true
		}
	}
	imq.enqueuedPeersMu.Lock()
	defer imq.enqueuedPeersMu.Unlock()
	peerlessCount := 0
	isPeerMissing := func(msg *queuedMsgEntry) bool {
		if msg.msg.peer != nil {
			if !activePeersMap[msg.msg.peer] {
				return true
			}
		}
		if !activeNetworkPeersMap[msg.msg.networkPeer] {
			if msg.msg.peer == nil {
				peerlessCount++
			}
			return true
		}
		return false
	}
	removeList := imq.messages.filterRemove(isPeerMissing)
	peerRemoved = removeList != nil
	imq.freelist.enqueueTail(removeList)
	imq.peerlessCount -= peerlessCount
	return
}