Iterators of various types are widely used throughout the Loki code base. With the recent code additions of the bloom filters, a new set of utility functions for iterators emerged in github.com/grafana/loki/v3/pkg/storage/bloom/v1. The package defines interfaces for various common types, but also provides implementations to create and compose new iterators that implement these interfaces. This new package uses Go Generics.
However, at the current state, there are multiple iterator interfaces and implementations, which adds cognitive overhead in understanding how they work and which ones should be used. The idea is to unify them into a single iterator lib for Loki.
As a first step towards a single iterator library for the Loki code base, this PR moves the utilities from the pkg/storage/bloom/v1 package to the pkg/iter/v2 package.
Second, it changes the existing EntryIterator and SampleIterator iterators to "inherit" v2.CloseIterator in order to expose the same function names.
And lastly, naming conventions of iterator interfaces and structs are unified.
The basic iterator interface (defined in pkg/iter/v2/interface.go) looks like so:
// Usage:
//
// for it.Next() {
// curr := it.At()
// // do something
// }
// if it.Err() != nil {
// // do something
// }
type Iterator[T any] interface {
Next() bool
Err() error
At() T
}
Discussion
I proposed to use the Err() function for getting the optional error, because it was used in the pkg/storage/bloom/v1 package. However, in existing iterators for entries and samples, this function was named Error().
I don't have strong arguments for or against one or the other name.
Next steps
This PR intentionally leaves the duplicate implementation of various iterator types, such a heap sort iterators, slice iterators, or peeking iterators. These should be consolidated in a follow up PR.
[x] Title matches the required conventional commits format, see here
Note that Promtail is considered to be feature complete, and future development for logs collection will be in Grafana Alloy. As such, feat PRs are unlikely to be accepted unless a case can be made for the feature actually being a bug fix to existing behavior.
[ ] Changes that require user attention or interaction to upgrade are documented in docs/sources/setup/upgrade/_index.md
[ ] For Helm chart changes bump the Helm chart version in production/helm/loki/Chart.yaml and update production/helm/loki/CHANGELOG.md and production/helm/loki/README.md. Example PR
[ ] If the change is deprecating or removing a configuration option, update the deprecated-config.yaml and deleted-config.yaml files respectively in the tools/deprecated-config-checker directory. Example PR
What this PR does / why we need it:
Iterators of various types are widely used throughout the Loki code base. With the recent code additions of the bloom filters, a new set of utility functions for iterators emerged in
github.com/grafana/loki/v3/pkg/storage/bloom/v1
. The package defines interfaces for various common types, but also provides implementations to create and compose new iterators that implement these interfaces. This new package uses Go Generics.pkg/storage/bloom/v1
package to thepkg/iter/v2
package.EntryIterator
andSampleIterator
iterators to "inherit"v2.CloseIterator
in order to expose the same function names.The basic iterator interface (defined in
pkg/iter/v2/interface.go
) looks like so:Discussion
I proposed to use the
Err()
function for getting the optional error, because it was used in thepkg/storage/bloom/v1
package. However, in existing iterators for entries and samples, this function was namedError()
. I don't have strong arguments for or against one or the other name.Next steps
This PR intentionally leaves the duplicate implementation of various iterator types, such a heap sort iterators, slice iterators, or peeking iterators. These should be consolidated in a follow up PR.
Checklist
CONTRIBUTING.md
guide (required)feat
PRs are unlikely to be accepted unless a case can be made for the feature actually being a bug fix to existing behavior.docs/sources/setup/upgrade/_index.md
production/helm/loki/Chart.yaml
and updateproduction/helm/loki/CHANGELOG.md
andproduction/helm/loki/README.md
. Example PRdeprecated-config.yaml
anddeleted-config.yaml
files respectively in thetools/deprecated-config-checker
directory. Example PR