-
Notifications
You must be signed in to change notification settings - Fork 2.9k
nsqd: support draining messages / removing nsqd from rotation #1305
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
Open
jehiah
wants to merge
3
commits into
nsqio:master
Choose a base branch
from
jehiah:drain_msgs_1305
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package test | ||
|
||
import ( | ||
"sync/atomic" | ||
) | ||
|
||
// GUIDFactory is an atomic sequence that can be used for MessageID's for benchmarks | ||
// to avoid ErrSequenceExpired when creating a large number of messages | ||
type GUIDFactory struct { | ||
n int64 | ||
} | ||
|
||
func (gf *GUIDFactory) NextMessageID() int64 { | ||
return atomic.AddInt64(&gf.n, 1) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,6 +54,7 @@ type Channel struct { | |
// state tracking | ||
clients map[int64]Consumer | ||
paused int32 | ||
isDraining int32 | ||
ephemeral bool | ||
deleteCallback func(*Channel) | ||
deleter sync.Once | ||
|
@@ -122,6 +123,20 @@ func NewChannel(topicName string, channelName string, nsqd *NSQD, | |
return c | ||
} | ||
|
||
// InFlightCount returns the number of messages that have been sent to a client, but not yet FIN or REQUEUE'd | ||
func (c *Channel) InFlightCount() int64 { | ||
c.inFlightMutex.Lock() | ||
defer c.inFlightMutex.Unlock() | ||
return int64(len(c.inFlightMessages)) | ||
} | ||
|
||
// DeferredCount returns the number of messages that are queued in-memory for future delivery to a client | ||
func (c *Channel) DeferredCount() int64 { | ||
c.deferredMutex.Lock() | ||
defer c.deferredMutex.Unlock() | ||
return int64(len(c.deferredMessages)) | ||
} | ||
|
||
func (c *Channel) initPQ() { | ||
pqSize := int(math.Max(1, float64(c.nsqd.getOpts().MemQueueSize)/10)) | ||
|
||
|
@@ -136,6 +151,23 @@ func (c *Channel) initPQ() { | |
c.deferredMutex.Unlock() | ||
} | ||
|
||
// StartDraining starts draining a channel | ||
// | ||
// if there are no outstanding messages the channel is deleted immediately | ||
// if there are messages outstanding it's deleted by FinishMessage | ||
func (c *Channel) StartDraining() { | ||
if !atomic.CompareAndSwapInt32(&c.isDraining, 0, 1) { | ||
return | ||
} | ||
depth, inFlight, deferred := c.Depth(), c.InFlightCount(), c.DeferredCount() | ||
c.nsqd.logf(LOG_INFO, "CHANNEL(%s): draining. depth:%d inFlight:%d deferred:%d", c.name, depth, inFlight, deferred) | ||
// if we are empty delete | ||
if depth+inFlight+deferred == 0 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a race condition b/w this line and line 162 (a concurrent pub that had already passed the |
||
go c.deleter.Do(func() { c.deleteCallback(c) }) | ||
} | ||
// else cleanup happens on last FinishMessage | ||
} | ||
|
||
// Exiting returns a boolean indicating if this channel is closed/exiting | ||
func (c *Channel) Exiting() bool { | ||
return atomic.LoadInt32(&c.exitFlag) == 1 | ||
|
@@ -187,6 +219,9 @@ func (c *Channel) exit(deleted bool) error { | |
return c.backend.Close() | ||
} | ||
|
||
// Empty drains the channel of messages. | ||
// | ||
// If the channel is draining this will delete the channel | ||
func (c *Channel) Empty() error { | ||
c.Lock() | ||
defer c.Unlock() | ||
|
@@ -196,16 +231,27 @@ func (c *Channel) Empty() error { | |
client.Empty() | ||
} | ||
|
||
MemoryDrain: | ||
for { | ||
select { | ||
case <-c.memoryMsgChan: | ||
default: | ||
goto finish | ||
break MemoryDrain | ||
} | ||
} | ||
|
||
finish: | ||
return c.backend.Empty() | ||
err := c.backend.Empty() | ||
|
||
// `backend.Empty` always results in an internal empty state (even if on-disk state might differ) | ||
// so we want to logically continue to finish draining if applicable. | ||
if atomic.LoadInt32(&c.isDraining) == 1 { | ||
go c.deleter.Do(func() { c.deleteCallback(c) }) | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// flush persists all the messages in internal memory buffers to the backend | ||
|
@@ -346,6 +392,8 @@ func (c *Channel) TouchMessage(clientID int64, id MessageID, clientMsgTimeout ti | |
} | ||
|
||
// FinishMessage successfully discards an in-flight message | ||
// | ||
// if this channel is draining and this is the last message this will initiate a channel deletion | ||
func (c *Channel) FinishMessage(clientID int64, id MessageID) error { | ||
msg, err := c.popInFlightMessage(clientID, id) | ||
if err != nil { | ||
|
@@ -355,6 +403,15 @@ func (c *Channel) FinishMessage(clientID int64, id MessageID) error { | |
if c.e2eProcessingLatencyStream != nil { | ||
c.e2eProcessingLatencyStream.Insert(msg.Timestamp) | ||
} | ||
|
||
if atomic.LoadInt32(&c.isDraining) == 1 { | ||
// if last msg, delete | ||
depth, inFlight, deferred := c.Depth(), c.InFlightCount(), c.DeferredCount() | ||
if depth+inFlight+deferred == 0 { | ||
c.nsqd.logf(LOG_INFO, "CHANNEL(%s): draining complete", c.name) | ||
go c.deleter.Do(func() { c.deleteCallback(c) }) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.