lib/logstorage: fix uniq_values merge in cluster queries#1384
lib/logstorage: fix uniq_values merge in cluster queries#1384cuongleqq wants to merge 5 commits into
Conversation
valyala
left a comment
There was a problem hiding this comment.
It would be great adding a test for this case. The best type of the test would be an integration test inside apptest folder for the VictoriaLogs cluster setup. You can add the needed logs to one vmstorage node, while skipping adding the logs to another vmstorage node, and then issue a select query and verify its results. Note that you can ingest logs individually per every vmstorage node in the same way like you do for a single-node VictoriaLogs according to these docs.
| } | ||
|
|
||
| src := sfp.(*statsUniqValuesProcessor) | ||
| if len(src.m) == 0 { |
There was a problem hiding this comment.
It is OK to skip this check - the loop below will work correctly and fast on empty map and on nil map.
There was a problem hiding this comment.
I kept the check because:
- It avoids allocating an empty map on a nil destination when the source is empty or nil.
- I am not entirely sure whether merging two nil maps should result in an empty map.
That said, the allocation is tiny and the case should not happen often, so I am fine with dropping the check if you prefer the simpler flow.
There is no need in this optimization here, since merging the state shouldn't be a performance bottleneck here. This optimization will make the code more fragile without achieving visible performance benefits. |
|
@cuongleqq , could you also check other LogsQL pipes for the same type of bug, and fix it in separate pull requests? |
@valyala it seems like only I checked other stats functions and LogsQL pipes with similar map-based merge paths. Other stats functions initialize the destination map before writing to it, either via helpers: VictoriaLogs/lib/logstorage/stats_count_uniq.go Lines 284 to 293 in 3e6e9ef or directly: VictoriaLogs/lib/logstorage/stats_histogram.go Lines 174 to 178 in 3e6e9ef Other pipes also have safe initialization patterns.
VictoriaLogs/lib/logstorage/pipe_field_names.go Lines 108 to 113 in 3e6e9ef Pipes that use VictoriaLogs/lib/logstorage/hits_map.go Lines 339 to 373 in 3e6e9ef So I do not see another similar nil map merge issue at the moment. |
Done. I have added |
Signed-off-by: Cuong Le <cuongleqq@gmail.com>
Fixes #1383
This PR fixed
uniq_values()state merging when the destination state has an empty map and the source state has values (See the issue for how to reproduce).I fixed this bug by simply create a new map if the destination map is currently
nil.I have a question though:
There is an optimization I think of when I fix this bug: In the
sup.m == nilcase, we could avoid allocation by movingsrc.mdirectly tosup.minstead of allocating a new map and copying keys.I kept the safer copy-based approach for now to avoid changing semantics. Would you prefer the map moving optimization here? If so, I can make anothe commit.
Thanks.