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

nsqd: add pool to minimize buffer allocation on message send #1292

Merged
merged 1 commit into from
Nov 12, 2020
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions nsqd/buffer_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ func bufferPoolGet() *bytes.Buffer {
}

func bufferPoolPut(b *bytes.Buffer) {
b.Reset()
imxyb marked this conversation as resolved.
Show resolved Hide resolved
bp.Put(b)
}
14 changes: 5 additions & 9 deletions nsqd/channel.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package nsqd

import (
"bytes"
"container/heap"
"errors"
"math"
Expand All @@ -11,6 +10,7 @@ import (
"time"

"github.com/nsqio/go-diskqueue"

"github.com/nsqio/nsq/internal/lg"
"github.com/nsqio/nsq/internal/pqueue"
"github.com/nsqio/nsq/internal/quantile"
Expand Down Expand Up @@ -211,8 +211,6 @@ finish:
// flush persists all the messages in internal memory buffers to the backend
// it does not drain inflight/deferred because it is only called in Close()
func (c *Channel) flush() error {
var msgBuf bytes.Buffer

if len(c.memoryMsgChan) > 0 || len(c.inFlightMessages) > 0 || len(c.deferredMessages) > 0 {
c.ctx.nsqd.logf(LOG_INFO, "CHANNEL(%s): flushing %d memory %d in-flight %d deferred messages to backend",
c.name, len(c.memoryMsgChan), len(c.inFlightMessages), len(c.deferredMessages))
Expand All @@ -221,7 +219,7 @@ func (c *Channel) flush() error {
for {
select {
case msg := <-c.memoryMsgChan:
err := writeMessageToBackend(&msgBuf, msg, c.backend)
err := writeMessageToBackend(msg, c.backend)
if err != nil {
c.ctx.nsqd.logf(LOG_ERROR, "failed to write message to backend - %s", err)
}
Expand All @@ -233,7 +231,7 @@ func (c *Channel) flush() error {
finish:
c.inFlightMutex.Lock()
for _, msg := range c.inFlightMessages {
err := writeMessageToBackend(&msgBuf, msg, c.backend)
err := writeMessageToBackend(msg, c.backend)
if err != nil {
c.ctx.nsqd.logf(LOG_ERROR, "failed to write message to backend - %s", err)
}
Expand All @@ -243,7 +241,7 @@ finish:
c.deferredMutex.Lock()
for _, item := range c.deferredMessages {
msg := item.Value.(*Message)
err := writeMessageToBackend(&msgBuf, msg, c.backend)
err := writeMessageToBackend(msg, c.backend)
if err != nil {
c.ctx.nsqd.logf(LOG_ERROR, "failed to write message to backend - %s", err)
}
Expand Down Expand Up @@ -307,9 +305,7 @@ func (c *Channel) put(m *Message) error {
select {
case c.memoryMsgChan <- m:
default:
b := bufferPoolGet()
err := writeMessageToBackend(b, m, c.backend)
bufferPoolPut(b)
err := writeMessageToBackend(m, c.backend)
c.ctx.nsqd.SetHealth(err)
if err != nil {
c.ctx.nsqd.logf(LOG_ERROR, "CHANNEL(%s): failed to write message to backend - %s",
Expand Down
6 changes: 3 additions & 3 deletions nsqd/message.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package nsqd

import (
"bytes"
"encoding/binary"
"fmt"
"io"
Expand Down Expand Up @@ -90,8 +89,9 @@ func decodeMessage(b []byte) (*Message, error) {
return &msg, nil
}

func writeMessageToBackend(buf *bytes.Buffer, msg *Message, bq BackendQueue) error {
buf.Reset()
func writeMessageToBackend(msg *Message, bq BackendQueue) error {
buf := bufferPoolGet()
defer bufferPoolPut(buf)
_, err := msg.WriteTo(buf)
if err != nil {
return err
Expand Down
4 changes: 3 additions & 1 deletion nsqd/protocol_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ func (p *protocolV2) IOLoop(conn net.Conn) error {

func (p *protocolV2) SendMessage(client *clientV2, msg *Message) error {
p.ctx.nsqd.logf(LOG_DEBUG, "PROTOCOL(V2): writing msg(%s) to client(%s) - %s", msg.ID, client, msg.Body)
var buf = &bytes.Buffer{}

buf := bufferPoolGet()
defer bufferPoolPut(buf)
imxyb marked this conversation as resolved.
Show resolved Hide resolved

_, err := msg.WriteTo(buf)
if err != nil {
Expand Down
9 changes: 2 additions & 7 deletions nsqd/topic.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package nsqd

import (
"bytes"
"errors"
"strings"
"sync"
Expand Down Expand Up @@ -221,9 +220,7 @@ func (t *Topic) put(m *Message) error {
select {
case t.memoryMsgChan <- m:
default:
b := bufferPoolGet()
err := writeMessageToBackend(b, m, t.backend)
bufferPoolPut(b)
err := writeMessageToBackend(m, t.backend)
t.ctx.nsqd.SetHealth(err)
if err != nil {
t.ctx.nsqd.logf(LOG_ERROR,
Expand Down Expand Up @@ -409,8 +406,6 @@ finish:
}

func (t *Topic) flush() error {
var msgBuf bytes.Buffer

if len(t.memoryMsgChan) > 0 {
t.ctx.nsqd.logf(LOG_INFO,
"TOPIC(%s): flushing %d memory messages to backend",
Expand All @@ -420,7 +415,7 @@ func (t *Topic) flush() error {
for {
select {
case msg := <-t.memoryMsgChan:
err := writeMessageToBackend(&msgBuf, msg, t.backend)
err := writeMessageToBackend(msg, t.backend)
if err != nil {
t.ctx.nsqd.logf(LOG_ERROR,
"ERROR: failed to write message to backend - %s", err)
Expand Down