Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make messageQueue.msgAndCtxs a circular buffer #2433

Merged
merged 5 commits into from
Dec 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 15 additions & 20 deletions snow/networking/handler/message_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/snow/networking/tracker"
"github.com/ava-labs/avalanchego/snow/validators"
"github.com/ava-labs/avalanchego/utils/buffer"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
)

Expand Down Expand Up @@ -69,7 +70,7 @@ type messageQueue struct {
// Node ID --> Messages this node has in [msgs]
nodeToUnprocessedMsgs map[ids.NodeID]int
// Unprocessed messages
msgAndCtxs []*msgAndContext
msgAndCtxs buffer.Deque[*msgAndContext]
}

func NewMessageQueue(
Expand All @@ -85,6 +86,7 @@ func NewMessageQueue(
cpuTracker: cpuTracker,
cond: sync.NewCond(&sync.Mutex{}),
nodeToUnprocessedMsgs: make(map[ids.NodeID]int),
msgAndCtxs: buffer.NewUnboundedDeque[*msgAndContext](1 /*=initSize*/),
Copy link
Contributor

@joshua-kim joshua-kim Dec 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do this in our codebase but I don't understand why... IDEs will usually display the name of the parameter and in this case it's really not needed since we only have one possible parameter

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vscode does not put in the name of the parameter (which most of the team uses)

}
return m, m.metrics.initialize(metricsNamespace, ctx.Registerer, ops)
}
Expand All @@ -99,7 +101,7 @@ func (m *messageQueue) Push(ctx context.Context, msg Message) {
}

// Add the message to the queue
m.msgAndCtxs = append(m.msgAndCtxs, &msgAndContext{
m.msgAndCtxs.PushRight(&msgAndContext{
msg: msg,
ctx: ctx,
})
Expand All @@ -124,13 +126,13 @@ func (m *messageQueue) Pop() (context.Context, Message, bool) {
if m.closed {
return nil, Message{}, false
}
if len(m.msgAndCtxs) != 0 {
if m.msgAndCtxs.Len() != 0 {
break
}
m.cond.Wait()
}

n := len(m.msgAndCtxs)
n := m.msgAndCtxs.Len() // note that n > 0
i := 0
for {
if i == n {
Expand All @@ -140,20 +142,14 @@ func (m *messageQueue) Pop() (context.Context, Message, bool) {
}

var (
msgAndCtx = m.msgAndCtxs[0]
msg = msgAndCtx.msg
ctx = msgAndCtx.ctx
nodeID = msg.NodeID()
msgAndCtx, _ = m.msgAndCtxs.PopLeft()
msg = msgAndCtx.msg
ctx = msgAndCtx.ctx
nodeID = msg.NodeID()
)
m.msgAndCtxs[0] = nil

// See if it's OK to process [msg] next
if m.canPop(msg) || i == n { // i should never == n but handle anyway as a fail-safe
if cap(m.msgAndCtxs) == 1 {
m.msgAndCtxs = nil // Give back memory if possible
} else {
m.msgAndCtxs = m.msgAndCtxs[1:]
}
m.nodeToUnprocessedMsgs[nodeID]--
if m.nodeToUnprocessedMsgs[nodeID] == 0 {
delete(m.nodeToUnprocessedMsgs, nodeID)
Expand All @@ -165,8 +161,7 @@ func (m *messageQueue) Pop() (context.Context, Message, bool) {
}
// [msg.nodeID] is causing excessive CPU usage.
// Push [msg] to back of [m.msgs] and handle it later.
m.msgAndCtxs = append(m.msgAndCtxs, msgAndCtx)
m.msgAndCtxs = m.msgAndCtxs[1:]
m.msgAndCtxs.PushRight(msgAndCtx)
i++
m.metrics.numExcessiveCPU.Inc()
}
Expand All @@ -176,18 +171,18 @@ func (m *messageQueue) Len() int {
m.cond.L.Lock()
defer m.cond.L.Unlock()

return len(m.msgAndCtxs)
return m.msgAndCtxs.Len()
}

func (m *messageQueue) Shutdown() {
m.cond.L.Lock()
defer m.cond.L.Unlock()

// Remove all the current messages from the queue
for _, msg := range m.msgAndCtxs {
msg.msg.OnFinishedHandling()
for m.msgAndCtxs.Len() > 0 {
msgAndCtx, _ := m.msgAndCtxs.PopLeft()
msgAndCtx.msg.OnFinishedHandling()
}
m.msgAndCtxs = nil
m.nodeToUnprocessedMsgs = nil

// Update metrics
Expand Down
Loading