refactor: replace experimental `maps` and `slices` with stdlib (#15051)

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
pull/14544/head
Eng Zer Jun 1 year ago committed by GitHub
parent 215f994d0e
commit 062d1e9ca4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 3
      clients/pkg/promtail/client/batch.go
  2. 3
      clients/pkg/promtail/targets/cloudflare/fields.go
  3. 2
      go.mod
  4. 2
      integration/loki_micro_services_test.go
  5. 2
      pkg/bloombuild/builder/batch.go
  6. 2
      pkg/bloomgateway/client.go
  7. 2
      pkg/ingester/recalculate_owned_streams.go
  8. 15
      pkg/logql/accumulator.go
  9. 7
      pkg/logqlmodel/metadata/context.go
  10. 2
      pkg/pattern/drain/drain_test.go
  11. 3
      pkg/push/types.go
  12. 2
      pkg/querier/ingester_querier.go
  13. 2
      pkg/querier/querier.go
  14. 6
      pkg/querier/queryrange/codec.go
  15. 7
      pkg/querier/queryrange/queryrangebase/query_range.go
  16. 2
      pkg/storage/chunk/cache/resultscache/cache_test.go
  17. 2
      pkg/storage/chunk/fetcher/fetcher_test.go
  18. 2
      pkg/storage/stores/shipper/bloomshipper/store.go
  19. 5
      pkg/storage/stores/shipper/indexshipper/downloads/table.go
  20. 5
      pkg/storage/stores/shipper/indexshipper/downloads/table_manager.go
  21. 2
      pkg/util/entry_size.go
  22. 2
      tools/doc-generator/main.go
  23. 2
      tools/doc-generator/parse/root_blocks.go
  24. 3
      vendor/github.com/grafana/loki/pkg/push/types.go
  25. 94
      vendor/golang.org/x/exp/maps/maps.go
  26. 1
      vendor/modules.txt

@ -2,15 +2,14 @@ package client
import (
"fmt"
"slices"
"strconv"
"strings"
"time"
"github.com/gogo/protobuf/proto"
"github.com/golang/snappy"
"github.com/prometheus/common/model"
"golang.org/x/exp/slices"
"github.com/grafana/loki/v3/clients/pkg/promtail/api"

@ -2,8 +2,7 @@ package cloudflare
import (
"fmt"
"golang.org/x/exp/slices"
"slices"
)
type FieldsType string

@ -144,7 +144,6 @@ require (
github.com/willf/bloom v2.0.3+incompatible
go.opentelemetry.io/collector/pdata v1.20.0
go4.org/netipx v0.0.0-20230125063823-8449b0a6169f
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8
golang.org/x/oauth2 v0.24.0
golang.org/x/text v0.20.0
google.golang.org/protobuf v1.35.2
@ -189,6 +188,7 @@ require (
go.opentelemetry.io/contrib/detectors/gcp v1.29.0 // indirect
go.opentelemetry.io/otel/sdk v1.29.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.29.0 // indirect
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 // indirect
google.golang.org/grpc/stats/opentelemetry v0.0.0-20240907200651-3ffb98b2c93a // indirect
)

@ -5,6 +5,7 @@ package integration
import (
"context"
"encoding/json"
"slices"
"strings"
"sync"
"testing"
@ -15,7 +16,6 @@ import (
"github.com/prometheus/prometheus/model/labels"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/exp/slices"
"google.golang.org/protobuf/proto"
"github.com/grafana/loki/v3/integration/client"

@ -4,10 +4,10 @@ import (
"context"
"io"
"math"
"slices"
"time"
"github.com/grafana/dskit/multierror"
"golang.org/x/exp/slices"
"github.com/grafana/loki/v3/pkg/chunkenc"
iter "github.com/grafana/loki/v3/pkg/iter/v2"

@ -4,6 +4,7 @@ import (
"context"
"flag"
"io"
"slices"
"sort"
"github.com/go-kit/log"
@ -14,7 +15,6 @@ import (
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/atomic"
"golang.org/x/exp/slices"
"google.golang.org/grpc"
"google.golang.org/grpc/health/grpc_health_v1"

@ -3,6 +3,7 @@ package ingester
import (
"context"
"fmt"
"slices"
"sync"
"time"
@ -10,7 +11,6 @@ import (
"github.com/go-kit/log/level"
"github.com/grafana/dskit/ring"
"github.com/grafana/dskit/services"
"golang.org/x/exp/slices"
lokiring "github.com/grafana/loki/v3/pkg/util/ring"
)

@ -4,11 +4,11 @@ import (
"container/heap"
"context"
"fmt"
"maps"
"slices"
"sort"
"time"
"golang.org/x/exp/maps"
"github.com/grafana/loki/v3/pkg/logproto"
"github.com/grafana/loki/v3/pkg/logqlmodel"
"github.com/grafana/loki/v3/pkg/logqlmodel/metadata"
@ -102,8 +102,7 @@ func (a *QuantileSketchAccumulator) Result() []logqlmodel.Result {
)
}
warnings := maps.Keys(a.warnings)
sort.Strings(warnings)
warnings := slices.Sorted(maps.Keys(a.warnings))
return []logqlmodel.Result{
{
@ -178,8 +177,7 @@ func (a *CountMinSketchAccumulator) Result() []logqlmodel.Result {
)
}
warnings := maps.Keys(a.warnings)
sort.Strings(warnings)
warnings := slices.Sorted(maps.Keys(a.warnings))
return []logqlmodel.Result{
{
@ -476,10 +474,7 @@ func (acc *AccumulatedStreams) Result() []logqlmodel.Result {
)
}
warnings := maps.Keys(acc.warnings)
sort.Strings(warnings)
res.Warnings = warnings
res.Warnings = slices.Sorted(maps.Keys(acc.warnings))
return []logqlmodel.Result{res}
}

@ -7,11 +7,11 @@ package metadata
import (
"context"
"errors"
"maps"
"slices"
"sort"
"sync"
"golang.org/x/exp/maps"
"github.com/grafana/loki/v3/pkg/querier/queryrange/queryrangebase/definitions"
)
@ -87,8 +87,7 @@ func (c *Context) Warnings() []string {
c.mtx.Lock()
defer c.mtx.Unlock()
warnings := maps.Keys(c.warnings)
sort.Strings(warnings)
warnings := slices.Sorted(maps.Keys(c.warnings))
return warnings
}

@ -4,12 +4,12 @@ import (
"bufio"
"fmt"
"os"
"slices"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
"golang.org/x/exp/slices"
"github.com/grafana/loki/v3/pkg/logql/log/pattern"
)

@ -4,11 +4,10 @@ import (
"encoding/json"
"fmt"
"io"
"slices"
"strings"
"time"
"unsafe"
"golang.org/x/exp/slices"
)
// Stream contains a unique labels set as a string and a set of entries for it.

@ -3,6 +3,7 @@ package querier
import (
"context"
"net/http"
"slices"
"strings"
"time"
@ -10,7 +11,6 @@ import (
"github.com/go-kit/log/level"
"github.com/grafana/dskit/concurrency"
"github.com/grafana/dskit/user"
"golang.org/x/exp/slices"
"github.com/grafana/loki/v3/pkg/storage/stores/index/seriesvolume"

@ -4,6 +4,7 @@ import (
"context"
"flag"
"net/http"
"slices"
"sort"
"strconv"
"time"
@ -20,7 +21,6 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
"golang.org/x/exp/slices"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc/health/grpc_health_v1"

@ -7,9 +7,11 @@ import (
"encoding/json"
"fmt"
"io"
"maps"
"net/http"
"net/url"
"regexp"
"slices"
"sort"
"strings"
"time"
@ -21,7 +23,6 @@ import (
"github.com/pkg/errors"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/timestamp"
"golang.org/x/exp/maps"
"github.com/grafana/loki/v3/pkg/loghttp"
"github.com/grafana/loki/v3/pkg/logproto"
@ -2194,8 +2195,7 @@ func mergeLokiResponse(responses ...queryrangebase.Response) *LokiResponse {
}
}
warnings := maps.Keys(uniqueWarnings)
sort.Strings(warnings)
warnings := slices.Sorted(maps.Keys(uniqueWarnings))
if len(warnings) == 0 {
// When there are no warnings, keep it nil so it can be compared against

@ -5,14 +5,14 @@ import (
"context"
"fmt"
"io"
"maps"
"math"
"net/http"
"slices"
"sort"
"strconv"
"time"
"golang.org/x/exp/maps"
"github.com/gogo/status"
"github.com/grafana/dskit/httpgrpc"
jsoniter "github.com/json-iterator/go"
@ -173,8 +173,7 @@ func (p prometheusCodec) MergeResponse(responses ...Response) (Response, error)
}
}
warnings := maps.Keys(uniqueWarnings)
sort.Strings(warnings)
warnings := slices.Sorted(maps.Keys(uniqueWarnings))
if len(warnings) == 0 {
// When there are no warnings, keep it nil so it can be compared against

@ -2,6 +2,7 @@ package resultscache
import (
"context"
"slices"
"strconv"
"testing"
"time"
@ -12,7 +13,6 @@ import (
"github.com/grafana/dskit/user"
"github.com/prometheus/common/model"
"github.com/stretchr/testify/require"
"golang.org/x/exp/slices"
"github.com/grafana/loki/v3/pkg/logqlmodel/stats"
"github.com/grafana/loki/v3/pkg/storage/chunk/cache"

@ -3,6 +3,7 @@ package fetcher
import (
"context"
"fmt"
"slices"
"strconv"
"testing"
"time"
@ -10,7 +11,6 @@ import (
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
"github.com/stretchr/testify/assert"
"golang.org/x/exp/slices"
"github.com/grafana/loki/v3/pkg/chunkenc"
"github.com/grafana/loki/v3/pkg/compression"

@ -4,6 +4,7 @@ import (
"context"
"fmt"
"path"
"slices"
"sort"
"time"
@ -12,7 +13,6 @@ import (
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model"
"golang.org/x/exp/slices"
"github.com/grafana/loki/v3/pkg/storage"
v1 "github.com/grafana/loki/v3/pkg/storage/bloom/v1"

@ -3,9 +3,11 @@ package downloads
import (
"context"
"fmt"
"maps"
"os"
"path/filepath"
"runtime"
"slices"
"sync"
"time"
@ -13,7 +15,6 @@ import (
"github.com/go-kit/log/level"
"github.com/grafana/dskit/concurrency"
"github.com/pkg/errors"
"golang.org/x/exp/maps"
"golang.org/x/sync/errgroup"
"github.com/grafana/loki/v3/pkg/storage/chunk/client/util"
@ -272,7 +273,7 @@ func (t *table) Sync(ctx context.Context) error {
level.Debug(t.logger).Log("msg", fmt.Sprintf("syncing files for table %s", t.name))
t.indexSetsMtx.RLock()
users := maps.Keys(t.indexSets)
users := slices.Collect(maps.Keys(t.indexSets))
t.indexSetsMtx.RUnlock()
for _, userID := range users {

@ -3,8 +3,10 @@ package downloads
import (
"context"
"fmt"
"maps"
"os"
"path/filepath"
"slices"
"strings"
"sync"
"time"
@ -14,7 +16,6 @@ import (
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model"
"golang.org/x/exp/maps"
"github.com/grafana/loki/v3/pkg/compactor/deletion"
"github.com/grafana/loki/v3/pkg/storage/chunk/client/util"
@ -219,7 +220,7 @@ func (tm *tableManager) getOrCreateTable(tableName string) (Table, error) {
func (tm *tableManager) syncTables(ctx context.Context) error {
tm.tablesMtx.RLock()
tables := maps.Keys(tm.tables)
tables := slices.Collect(maps.Keys(tm.tables))
tm.tablesMtx.RUnlock()
start := time.Now()

@ -1,7 +1,7 @@
package util
import (
"golang.org/x/exp/slices"
"slices"
"github.com/grafana/loki/pkg/push"

@ -10,12 +10,12 @@ import (
"fmt"
"os"
"path/filepath"
"slices"
"strings"
"text/template"
"github.com/grafana/loki/v3/pkg/loki"
"github.com/grafana/loki/v3/tools/doc-generator/parse"
"golang.org/x/exp/slices"
)
const (

@ -4,6 +4,7 @@ package parse
import (
"reflect"
"slices"
"github.com/grafana/dskit/crypto/tls"
"github.com/grafana/dskit/grpcclient"
@ -12,7 +13,6 @@ import (
"github.com/grafana/dskit/kv/memberlist"
"github.com/grafana/dskit/runtimeconfig"
"github.com/grafana/dskit/server"
"golang.org/x/exp/slices"
"github.com/grafana/loki/v3/pkg/analytics"
"github.com/grafana/loki/v3/pkg/bloombuild"

@ -4,11 +4,10 @@ import (
"encoding/json"
"fmt"
"io"
"slices"
"strings"
"time"
"unsafe"
"golang.org/x/exp/slices"
)
// Stream contains a unique labels set as a string and a set of entries for it.

@ -1,94 +0,0 @@
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package maps defines various functions useful with maps of any type.
package maps
// Keys returns the keys of the map m.
// The keys will be in an indeterminate order.
func Keys[M ~map[K]V, K comparable, V any](m M) []K {
r := make([]K, 0, len(m))
for k := range m {
r = append(r, k)
}
return r
}
// Values returns the values of the map m.
// The values will be in an indeterminate order.
func Values[M ~map[K]V, K comparable, V any](m M) []V {
r := make([]V, 0, len(m))
for _, v := range m {
r = append(r, v)
}
return r
}
// Equal reports whether two maps contain the same key/value pairs.
// Values are compared using ==.
func Equal[M1, M2 ~map[K]V, K, V comparable](m1 M1, m2 M2) bool {
if len(m1) != len(m2) {
return false
}
for k, v1 := range m1 {
if v2, ok := m2[k]; !ok || v1 != v2 {
return false
}
}
return true
}
// EqualFunc is like Equal, but compares values using eq.
// Keys are still compared with ==.
func EqualFunc[M1 ~map[K]V1, M2 ~map[K]V2, K comparable, V1, V2 any](m1 M1, m2 M2, eq func(V1, V2) bool) bool {
if len(m1) != len(m2) {
return false
}
for k, v1 := range m1 {
if v2, ok := m2[k]; !ok || !eq(v1, v2) {
return false
}
}
return true
}
// Clear removes all entries from m, leaving it empty.
func Clear[M ~map[K]V, K comparable, V any](m M) {
for k := range m {
delete(m, k)
}
}
// Clone returns a copy of m. This is a shallow clone:
// the new keys and values are set using ordinary assignment.
func Clone[M ~map[K]V, K comparable, V any](m M) M {
// Preserve nil in case it matters.
if m == nil {
return nil
}
r := make(M, len(m))
for k, v := range m {
r[k] = v
}
return r
}
// Copy copies all key/value pairs in src adding them to dst.
// When a key in src is already present in dst,
// the value in dst will be overwritten by the value associated
// with the key in src.
func Copy[M1 ~map[K]V, M2 ~map[K]V, K comparable, V any](dst M1, src M2) {
for k, v := range src {
dst[k] = v
}
}
// DeleteFunc deletes any key/value pairs from m for which del returns true.
func DeleteFunc[M ~map[K]V, K comparable, V any](m M, del func(K, V) bool) {
for k, v := range m {
if del(k, v) {
delete(m, k)
}
}
}

@ -1831,7 +1831,6 @@ golang.org/x/crypto/sha3
# golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8
## explicit; go 1.20
golang.org/x/exp/constraints
golang.org/x/exp/maps
golang.org/x/exp/slices
# golang.org/x/mod v0.19.0
## explicit; go 1.18

Loading…
Cancel
Save