Skip to content

Use batch logic to push logs#129

Merged
mimir-d merged 1 commit intolinuxboot:mainfrom
mahmednabil109:add_batch_log_push
Aug 16, 2022
Merged

Use batch logic to push logs#129
mimir-d merged 1 commit intolinuxboot:mainfrom
mahmednabil109:add_batch_log_push

Conversation

@mahmednabil109
Copy link
Copy Markdown
Contributor

Signed-off-by: Mohamed Abokammer mahmednabil109@gmail.com

@mahmednabil109 mahmednabil109 marked this pull request as draft August 15, 2022 10:32
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Aug 15, 2022

Codecov Report

Merging #129 (5b83b81) into main (2ba91cd) will decrease coverage by 0.23%.
The diff coverage is 0.00%.

❗ Current head 5b83b81 differs from pull request most recent head 5119b40. Consider uploading reports for the commit 5119b40 to get more accurate results

@@            Coverage Diff             @@
##             main     #129      +/-   ##
==========================================
- Coverage   63.85%   63.62%   -0.24%     
==========================================
  Files         164      164              
  Lines       10312    10347      +35     
==========================================
- Hits         6585     6583       -2     
- Misses       3009     3040      +31     
- Partials      718      724       +6     
Flag Coverage Δ
e2e 49.46% <0.00%> (-0.27%) ⬇️
integration 55.04% <0.00%> (-0.23%) ⬇️
unittests 49.39% <ø> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
cmds/admin_server/server/server.go 0.00% <0.00%> (ø)
cmds/admin_server/storage/mongo/mongo.go 8.19% <0.00%> (+0.34%) ⬆️
pkg/loggerhook/httphook.go 0.00% <0.00%> (ø)
pkg/jobmanager/jobmanager.go 76.92% <0.00%> (-1.10%) ⬇️
pkg/runner/step_runner.go 88.84% <0.00%> (-0.40%) ⬇️
plugins/teststeps/waitport/waitport.go 68.93% <0.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@mahmednabil109 mahmednabil109 force-pushed the add_batch_log_push branch 2 times, most recently from 76852c9 to be60ffb Compare August 15, 2022 13:10
@mahmednabil109 mahmednabil109 marked this pull request as ready for review August 15, 2022 13:33
@mimir-d mimir-d requested a review from rihter007 August 15, 2022 13:57
Comment thread cmds/admin_server/storage/storage.go Outdated

type Storage interface {
StoreLog(ctx xcontext.Context, entry Log) error
StoreLogs(ctx xcontext.Context, batch LogBatch) error
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why do we need a LogBatch type? Why not to just []Log?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

to make it easy to add any metadata -in the futuer- along with the batch.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

valid point, but what kind of metadata do you expect?

Comment thread cmds/admin_server/server/server.go Outdated
Comment on lines +60 to +66
func (lb *LogBatch) ToStorageBatch() storage.LogBatch {
var logBatch storage.LogBatch
for _, log := range lb.Logs {
logBatch.Logs = append(logBatch.Logs, log.ToStorageLog())
}
return logBatch
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Quick question: should this function be thread-safe?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I can't see why, the logBatch will not be shared across threads.

Comment thread cmds/admin_server/server/server.go Outdated
@@ -169,6 +209,7 @@ func initRouter(ctx xcontext.Context, rh RouteHandler, middlewares []gin.Handler

r.GET("/status", rh.status)
r.POST("/log", rh.addLog)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I also expected that batching will apply to /log API, so the local server will hold sending the logs further. @mimir-d what do you think?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I can merge the two endpoints, by always sending an array of logs.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It is ok to have two separate, but inside use batch for everything

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

it's fine to change the endpoint now, but keep in mind that this is a breaking change and wont work after this starts to be used

Comment thread pkg/loggerhook/httphook.go Outdated
MaxBatchSize = 500000 // size in bytes
MaxBatchLength = 100
BatchRetries = 4
BatchRetryDelay = 100 * time.Millisecond
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Having good defaults is great, but I would also add overriding with command line parameters

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yeah, i guess it will be better to add it in another PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

will be added, #130

Comment thread pkg/loggerhook/httphook.go Outdated

func (hh *HttpHook) postBatch() error {
logs := hh.logBatch
hh.logBatch = make([]server.Log, 0)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

in context with retry hh.logBatch will be 0 after first try and if it failed, they will be lost by the time of the second try

Comment thread pkg/loggerhook/httphook.go Outdated
fmt.Fprintf(os.Stderr, "Http Logger Err: %v", err)
hh.batchMux.Unlock()
case <-hh.sendTicker.C:
hh.batchMux.Lock()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

As far as I understand logHandler works in a single goroutine, so if batch variables are not used elsewhere, having mutexes is not needed.

Also block everything during retries (that have Sleep) is not great, probably a good way is to either launch a goroutine that will try to post or to add retries in the current loop. Probably retrying at the next tick is ok.

@mahmednabil109 mahmednabil109 force-pushed the add_batch_log_push branch 2 times, most recently from 6a97c5d to 7f9fa92 Compare August 15, 2022 15:53
Comment thread pkg/loggerhook/httphook.go Outdated
if len(hh.logBatch) > MaxBatchLength || hh.batchSize > uint64(MaxBatchSize) {
err := hh.postBatch()
if err != nil {
fmt.Fprintf(os.Stderr, "Send Batch faild: %v", err)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit

"... failEd: %v"

Comment thread pkg/loggerhook/httphook.go Outdated
break
}
// if the batch is sent
hh.logBatch = make([]server.Log, 0)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit

Just hh.logBatch = nil

Comment thread pkg/loggerhook/httphook.go
Comment thread pkg/loggerhook/httphook.go Outdated
rihter007
rihter007 previously approved these changes Aug 15, 2022
Copy link
Copy Markdown
Contributor

@rihter007 rihter007 left a comment

Choose a reason for hiding this comment

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

Overall looks good, just left some final comments

rihter007
rihter007 previously approved these changes Aug 15, 2022
Copy link
Copy Markdown
Contributor

@rihter007 rihter007 left a comment

Choose a reason for hiding this comment

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

`LGTM!

Copy link
Copy Markdown
Member

@mimir-d mimir-d left a comment

Choose a reason for hiding this comment

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

the mandatory change is to address non-empty current batch when closing; rest are recommendations

Comment thread cmds/admin_server/storage/mongo/mongo.go Outdated
Comment thread cmds/admin_server/storage/mongo/mongo.go Outdated
Comment thread pkg/loggerhook/httphook.go Outdated
Comment thread pkg/loggerhook/httphook.go Outdated
Comment thread pkg/loggerhook/httphook.go Outdated
Comment thread pkg/loggerhook/httphook.go Outdated
Comment thread pkg/loggerhook/httphook.go
Comment thread pkg/loggerhook/httphook.go Outdated
Comment thread pkg/loggerhook/httphook.go
Comment thread pkg/loggerhook/httphook.go Outdated
Signed-off-by: Mohamed Abokammer <mahmednabil109@gmail.com>

// Batch defines a log batch that handles the size in bytes of the logs
type Batch struct {
addr string
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this isnt realy a property of the batch, but i can ignore this; you shouldve had it as a param in send

@mimir-d mimir-d merged commit a3f975d into linuxboot:main Aug 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants