Skip to content

Commit

Permalink
fix: handle non-pointer values in the flag parser (#3720)
Browse files Browse the repository at this point in the history
  • Loading branch information
kolesnikovae authored Nov 27, 2024
1 parent a5ec059 commit fa69fe0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 57 deletions.
19 changes: 12 additions & 7 deletions pkg/experiment/compactor/compaction_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
Expand All @@ -19,6 +20,7 @@ import (
"github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
_ "go.uber.org/automaxprocs"
"golang.org/x/sync/errgroup"

metastorev1 "github.com/grafana/pyroscope/api/gen/proto/go/metastore/v1"
Expand Down Expand Up @@ -47,17 +49,17 @@ type Worker struct {
}

type Config struct {
JobConcurrency util.ConcurrencyLimit `yaml:"job_capacity"`
JobPollInterval time.Duration `yaml:"job_poll_interval"`
SmallObjectSize int `yaml:"small_object_size_bytes"`
TempDir string `yaml:"temp_dir"`
RequestTimeout time.Duration `yaml:"request_timeout"`
JobConcurrency int `yaml:"job_capacity"`
JobPollInterval time.Duration `yaml:"job_poll_interval"`
SmallObjectSize int `yaml:"small_object_size_bytes"`
TempDir string `yaml:"temp_dir"`
RequestTimeout time.Duration `yaml:"request_timeout"`
}

func (cfg *Config) RegisterFlags(f *flag.FlagSet) {
const prefix = "compaction-worker."
tempdir := filepath.Join(os.TempDir(), "pyroscope-compactor")
f.TextVar(&cfg.JobConcurrency, prefix+"job-concurrency", util.GoMaxProcsConcurrencyLimit(), "Number of concurrent jobs compaction worker will run. Defaults to the number of CPU cores.")
f.IntVar(&cfg.JobConcurrency, prefix+"job-concurrency", 0, "Number of concurrent jobs compaction worker will run. Defaults to the number of CPU cores.")
f.DurationVar(&cfg.JobPollInterval, prefix+"job-poll-interval", 5*time.Second, "Interval between job requests")
f.DurationVar(&cfg.RequestTimeout, prefix+"request-timeout", 5*time.Second, "Job request timeout.")
f.IntVar(&cfg.SmallObjectSize, prefix+"small-object-size-bytes", 8<<20, "Size of the object that can be loaded in memory.")
Expand Down Expand Up @@ -95,7 +97,10 @@ func New(
storage: storage,
metrics: newMetrics(reg),
}
w.threads = int(config.JobConcurrency)
w.threads = config.JobConcurrency
if w.threads < 1 {
w.threads = runtime.GOMAXPROCS(-1)
}
w.queue = make(chan *compactionJob, 2*w.threads)
w.jobs = make(map[string]*compactionJob, 2*w.threads)
w.capacity.Store(int32(w.threads))
Expand Down
48 changes: 0 additions & 48 deletions pkg/util/concurrency.go

This file was deleted.

13 changes: 11 additions & 2 deletions tools/doc-generator/parse/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strings"
"time"
"unicode"
"unsafe"

"github.com/go-kit/log"
"github.com/grafana/dskit/flagext"
Expand Down Expand Up @@ -115,8 +116,16 @@ func Flags(cfg flagext.Registerer, logger log.Logger) map[uintptr]*flag.Flag {
return
}

ptr := reflect.ValueOf(f.Value).Pointer()
flags[ptr] = f
val := reflect.ValueOf(f.Value)
if val.Kind() == reflect.Ptr {
flags[val.Pointer()] = f
} else if val.CanAddr() {
flags[val.UnsafeAddr()] = f
} else if val.CanInterface() {
flags[uintptr(unsafe.Pointer(&f.Value))] = f
} else {
panic("cannot get pointer to flag value")
}
})

return flags
Expand Down

0 comments on commit fa69fe0

Please sign in to comment.