-
Notifications
You must be signed in to change notification settings - Fork 54
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
Refactor and enhance pipeline with batch processing #607
Open
zhuoyuan-liu
wants to merge
1
commit into
jmpsec:main
Choose a base branch
from
zhuoyuan-liu:split-backend
base: main
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
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 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 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 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,93 @@ | ||
package handlers | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/jmpsec/osctrl/pkg/nodes" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
// writeEvent represents a single update request. | ||
type writeEvent struct { | ||
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. Either we have a typed event for each update, or something generic enough that can be used for any updates |
||
NodeID uint | ||
IP string | ||
} | ||
|
||
// batchWriter encapsulates the batching logic. | ||
type batchWriter struct { | ||
events chan writeEvent | ||
batchSize int // minimum number of events before flushing | ||
timeout time.Duration // maximum wait time before flushing | ||
nodesRepo nodes.NodeManager | ||
} | ||
|
||
// newBatchWriter creates and starts a new batch writer. | ||
func newBatchWriter(batchSize int, timeout time.Duration, repo nodes.NodeManager) *batchWriter { | ||
bw := &batchWriter{ | ||
events: make(chan writeEvent, 2000), // buffer size as needed | ||
batchSize: batchSize, | ||
timeout: timeout, | ||
nodesRepo: repo, | ||
} | ||
go bw.run() | ||
return bw | ||
} | ||
|
||
// addEvent sends a new write event to the batch writer. | ||
func (bw *batchWriter) addEvent(ev writeEvent) { | ||
bw.events <- ev | ||
} | ||
|
||
// run is the background worker that collects and flushes events. | ||
func (bw *batchWriter) run() { | ||
batch := make(map[uint]writeEvent) | ||
timer := time.NewTimer(bw.timeout) | ||
defer timer.Stop() | ||
for { | ||
select { | ||
case ev, ok := <-bw.events: | ||
if !ok { | ||
// Channel closed: flush any remaining events. | ||
if len(batch) > 0 { | ||
bw.flush(batch) | ||
} | ||
return | ||
} | ||
// Overwrite any existing event for the same NodeID. | ||
batch[ev.NodeID] = ev | ||
|
||
// Flush if we have reached the batch size threshold. | ||
if len(batch) >= bw.batchSize { | ||
if !timer.Stop() { | ||
<-timer.C // drain the timer channel if necessary | ||
} | ||
bw.flush(batch) | ||
batch = make(map[uint]writeEvent) | ||
timer.Reset(bw.timeout) | ||
} | ||
case <-timer.C: | ||
if len(batch) > 0 { | ||
bw.flush(batch) | ||
batch = make(map[uint]writeEvent) | ||
} | ||
timer.Reset(bw.timeout) | ||
} | ||
} | ||
} | ||
|
||
// flush performs the bulk update for a batch of events. | ||
func (bw *batchWriter) flush(batch map[uint]writeEvent) { | ||
|
||
nodeIDs := make([]uint, 0, len(batch)) | ||
for _, ev := range batch { | ||
nodeIDs = append(nodeIDs, ev.NodeID) | ||
|
||
// TODO: Implement the actual update logic. | ||
// Update the node's IP address. | ||
// Since the IP address changes infrequently, no need to update in bulk. | ||
} | ||
log.Info().Int("count", len(batch)).Msg("flushed batch") | ||
if err := bw.nodesRepo.RefreshLastSeenBatch(nodeIDs); err != nil { | ||
log.Err(err).Msg("refreshing last seen batch failed") | ||
} | ||
} |
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] In the QueryReadHandler, utils.GetIP(r) is called a second time even though it was already obtained earlier. Consider capturing the IP address once and reusing it to avoid potential inconsistencies.
Copilot is powered by AI, so mistakes are possible. Review output carefully before use.