Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
L
Loki
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
TeDomum
Loki
Commits
42df286b
Commit
42df286b
authored
6 years ago
by
Tom Wilkie
Committed by
Tom Wilkie
6 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Factor out mostCommon function, test it, fix it.
Signed-off-by:
Tom Wilkie
<
tom.wilkie@gmail.com
>
parent
db36b3c7
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
pkg/iter/iterator.go
+27
-17
27 additions, 17 deletions
pkg/iter/iterator.go
pkg/iter/iterator_test.go
+28
-1
28 additions, 1 deletion
pkg/iter/iterator_test.go
with
55 additions
and
18 deletions
pkg/iter/iterator.go
+
27
−
17
View file @
42df286b
...
...
@@ -134,6 +134,11 @@ func (i *heapIterator) requeue(ei EntryIterator, advanced bool) {
helpers
.
LogError
(
"closing iterator"
,
ei
.
Close
)
}
type
tuple
struct
{
logproto
.
Entry
EntryIterator
}
func
(
i
*
heapIterator
)
Next
()
bool
{
if
i
.
heap
.
Len
()
==
0
{
return
false
...
...
@@ -143,10 +148,7 @@ func (i *heapIterator) Next() bool {
// preserve their original order. We look at all the top entries in the
// heap with the same timestamp, and pop the ones whose common value
// occurs most often.
type
tuple
struct
{
logproto
.
Entry
EntryIterator
}
tuples
:=
make
([]
tuple
,
0
,
i
.
heap
.
Len
())
for
i
.
heap
.
Len
()
>
0
{
next
:=
i
.
heap
.
Peek
()
...
...
@@ -164,30 +166,38 @@ func (i *heapIterator) Next() bool {
// Find in entry which occurs most often which, due to quorum based
// replication, is guaranteed to be the correct next entry.
i
.
currEntry
=
mostCommon
(
tuples
)
.
Entry
// Requeue the iterators, only advancing them if they were not the
// correct pick.
for
j
:=
range
tuples
{
i
.
requeue
(
tuples
[
j
]
.
EntryIterator
,
tuples
[
j
]
.
Line
!=
i
.
currEntry
.
Line
)
}
return
true
}
func
mostCommon
(
tuples
[]
tuple
)
tuple
{
sort
.
Slice
(
tuples
,
func
(
i
,
j
int
)
bool
{
return
tuples
[
i
]
.
Line
<
tuples
[
j
]
.
Line
})
i
.
currEntry
=
tuples
[
0
]
.
Entry
count
,
max
:=
1
,
1
for
j
:=
1
;
j
<
len
(
tuples
);
j
++
{
if
tuples
[
j
]
.
Equal
(
tuples
[
j
-
1
]
)
{
result
:
=
tuples
[
0
]
count
,
max
:=
0
,
0
for
i
:=
0
;
i
<
len
(
tuples
)
-
1
;
i
++
{
if
tuples
[
i
]
.
Equal
(
tuples
[
i
+
1
]
.
Entry
)
{
count
++
continue
}
if
count
>
max
{
i
.
currEntry
=
tuples
[
j
-
1
]
.
Entry
result
=
tuples
[
i
]
max
=
count
}
count
++
count
=
0
}
// Requeue the iterators, only advancing them if they were not the
// correct pick.
for
j
:=
range
tuples
{
i
.
requeue
(
tuples
[
j
]
.
EntryIterator
,
tuples
[
j
]
.
Line
!=
i
.
currEntry
.
Line
)
if
count
>
max
{
result
=
tuples
[
len
(
tuples
)
-
1
]
}
return
true
return
result
}
func
(
i
*
heapIterator
)
Entry
()
logproto
.
Entry
{
...
...
This diff is collapsed.
Click to expand it.
pkg/iter/iterator_test.go
+
28
−
1
View file @
42df286b
...
...
@@ -5,11 +5,13 @@ import (
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/grafana/loki/pkg/logproto"
"github.com/stretchr/testify/assert"
)
const
testSize
=
10
0
const
testSize
=
10
func
TestIterator
(
t
*
testing
.
T
)
{
for
i
,
tc
:=
range
[]
struct
{
...
...
@@ -116,3 +118,28 @@ func inverse(g generator) generator {
return
g
(
-
i
)
}
}
func
TestMostCommont
(
t
*
testing
.
T
)
{
// First is most common.
tuples
:=
[]
tuple
{
{
Entry
:
logproto
.
Entry
{
Line
:
"a"
}},
{
Entry
:
logproto
.
Entry
{
Line
:
"b"
}},
{
Entry
:
logproto
.
Entry
{
Line
:
"c"
}},
{
Entry
:
logproto
.
Entry
{
Line
:
"a"
}},
{
Entry
:
logproto
.
Entry
{
Line
:
"b"
}},
{
Entry
:
logproto
.
Entry
{
Line
:
"c"
}},
{
Entry
:
logproto
.
Entry
{
Line
:
"a"
}},
}
require
.
Equal
(
t
,
"a"
,
mostCommon
(
tuples
)
.
Entry
.
Line
)
// Last is most common
tuples
=
[]
tuple
{
{
Entry
:
logproto
.
Entry
{
Line
:
"a"
}},
{
Entry
:
logproto
.
Entry
{
Line
:
"b"
}},
{
Entry
:
logproto
.
Entry
{
Line
:
"c"
}},
{
Entry
:
logproto
.
Entry
{
Line
:
"b"
}},
{
Entry
:
logproto
.
Entry
{
Line
:
"c"
}},
{
Entry
:
logproto
.
Entry
{
Line
:
"c"
}},
}
require
.
Equal
(
t
,
"c"
,
mostCommon
(
tuples
)
.
Entry
.
Line
)
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment