Skip to content

Commit fc51463

Browse files
authored
Cleans up pkg/storage/segment and global state (#183)
* cleans up segment and global state
1 parent 3628793 commit fc51463

File tree

13 files changed

+74
-113
lines changed

13 files changed

+74
-113
lines changed

.github/workflows/lint-go.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Go Linting
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
jobs:
10+
go-lint:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout
14+
uses: actions/checkout@v2
15+
- name: Run revive action
16+
uses: petethepig/revive-action@v5
17+
with:
18+
config: revive.toml
19+
exclude: "vendor/..."

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ ifeq ("$(shell go env GOARCH || true)", "arm64")
55
GODEBUG=asyncpreemptoff=1
66
endif
77

8+
ALL_SPIES = "ebpfspy,rbspy,pyspy,debugspy"
89
ifeq ("$(shell go env GOOS || true)", "linux")
910
ENABLED_SPIES ?= "ebpfspy,rbspy,pyspy"
1011
else
@@ -79,7 +80,7 @@ ensure-logrus-not-used:
7980

8081
.PHONY: unused
8182
unused:
82-
staticcheck -f stylish -unused.whole-program ./...
83+
staticcheck -f stylish -tags $(ALL_SPIES) -unused.whole-program ./...
8384

8485
.PHONY: install-dev-tools
8586
install-dev-tools:

cmd/pyroscope/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
func main() {
12-
cfg := config.New()
12+
cfg := &config.Config{}
1313
err := cli.Start(cfg)
1414
if err != nil {
1515
os.Stderr.Write([]byte(color.RedString("Error: ") + err.Error() + "\n\n"))

pkg/config/config.go

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,7 @@ type Server struct {
5050

5151
// TODO: I don't think a lot of people will change these values.
5252
// I think these should just be constants.
53-
Multiplier int `skip:"true" def:"10"`
54-
MinResolution time.Duration `skip:"true" def:"10s"`
55-
MaxResolution time.Duration `skip:"true" def:"8760h"` // 365 days
56-
StorageMaxDepth int `skip:"true"`
57-
BadgerNoTruncate bool `def:"false" desc:"indicates whether value log files should be truncated to delete corrupt data, if any"`
53+
BadgerNoTruncate bool `def:"false" desc:"indicates whether value log files should be truncated to delete corrupt data, if any"`
5854

5955
MaxNodesSerialization int `def:"2048" desc:"max number of nodes used when saving profiles to disk"`
6056
MaxNodesRender int `def:"8192" desc:"max number of nodes used to display data on the frontend"`
@@ -96,44 +92,3 @@ type Exec struct {
9692
GroupName string `def:"" desc:"starts process under specified group name"`
9793
PyspyBlocking bool `def:"false" desc:"enables blocking mode for pyspy"`
9894
}
99-
100-
func calculateMaxDepth(min, max time.Duration, multiplier int) int {
101-
depth := 0
102-
for min < max {
103-
min *= time.Duration(multiplier)
104-
depth++
105-
}
106-
return depth
107-
}
108-
109-
// TODO: remove these preset configs
110-
func New() *Config {
111-
return NewForTests("/tmp/")
112-
}
113-
114-
func NewForTests(path string) *Config {
115-
cfg := &Config{
116-
Server: Server{
117-
StoragePath: path,
118-
APIBindAddr: ":4040",
119-
120-
CacheSegmentSize: 10,
121-
CacheTreeSize: 10,
122-
CacheDictionarySize: 10,
123-
CacheDimensionSize: 10,
124-
125-
Multiplier: 10,
126-
MinResolution: 10 * time.Second,
127-
MaxResolution: time.Hour * 24 * 365 * 5,
128-
129-
MaxNodesSerialization: 2048,
130-
MaxNodesRender: 2048,
131-
132-
OutOfSpaceThreshold: 512 * 1024 * 1024, // bytes (default: 512MB)
133-
},
134-
}
135-
136-
cfg.Server.StorageMaxDepth = calculateMaxDepth(cfg.Server.MinResolution, cfg.Server.MaxResolution, cfg.Server.Multiplier)
137-
138-
return cfg
139-
}

pkg/storage/segment/constants.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package segment
2+
3+
import "time"
4+
5+
// TODO: at some point we should change it so that segments can support different
6+
// resolution and multiplier values. For now they are constants
7+
const (
8+
multiplier = 10
9+
resolution = 10 * time.Second
10+
)
11+
12+
var durations = []time.Duration{}
13+
14+
func init() {
15+
d := resolution
16+
// TODO: better upper boundary, currently 50 is a magic number
17+
for i := 0; i < 50; i++ {
18+
durations = append(durations, d)
19+
d *= time.Duration(multiplier)
20+
}
21+
}

pkg/storage/segment/fuzz_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (sm *storageMock) Get(st, et time.Time, cb func(depth int, samples, writes
5454

5555
// if you change something in this test make sure it doesn't change test coverage.
5656
func fuzzTest(testWrites bool, writeSize func() int) {
57-
s := New(10*time.Second, 10)
57+
s := New()
5858
m := newMock(10 * time.Second)
5959

6060
r := rand.New(rand.NewSource(1213))

pkg/storage/segment/segment.go

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ type streeNode struct {
1616
children []*streeNode
1717
}
1818

19-
var durations []time.Duration
20-
2119
func (parent *streeNode) replace(child *streeNode) {
2220
i := child.time.Sub(parent.time) / durations[child.depth]
2321
parent.children[i] = child
@@ -170,26 +168,12 @@ func newNode(t time.Time, depth, multiplier int) *streeNode {
170168
return sn
171169
}
172170

173-
// TODO: global state is not good
174-
func InitializeGlobalState(resolution time.Duration, multiplier int) {
175-
// this is here just to initialize global duration variable
176-
New(resolution, multiplier)
177-
}
178-
179-
func New(resolution time.Duration, multiplier int) *Segment {
171+
func New() *Segment {
180172
st := &Segment{
181173
resolution: resolution,
182174
multiplier: multiplier,
183-
durations: []time.Duration{},
184-
}
185-
186-
// TODO better upper boundary
187-
d := resolution
188-
for i := 0; i < 50; i++ {
189-
st.durations = append(st.durations, d)
190-
d *= time.Duration(multiplier)
175+
durations: durations,
191176
}
192-
durations = st.durations
193177

194178
return st
195179
}

pkg/storage/segment/segment_test.go

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,10 @@ func expectChildrenSamplesAddUpToParentSamples(tn *streeNode) {
4747
}
4848

4949
var _ = Describe("stree", func() {
50-
r := 10 * time.Second
51-
m := 10
52-
5350
Context("Get", func() {
5451
Context("When there's no root", func() {
5552
It("get doesn't fail", func() {
56-
s := New(r, m)
53+
s := New()
5754
Expect(doGet(s, testing.SimpleTime(0), testing.SimpleTime(39))).To(HaveLen(0))
5855
})
5956
})
@@ -64,7 +61,7 @@ var _ = Describe("stree", func() {
6461
Context("When second insert is far in the future", func() {
6562
It("sets root properly", func() {
6663
log.Println("---")
67-
s := New(r, m)
64+
s := New()
6865
s.Put(testing.SimpleTime(1330),
6966
testing.SimpleTime(1339), 1, func(de int, t time.Time, r *big.Rat, a []Addon) {})
7067
Expect(s.root).ToNot(BeNil())
@@ -77,7 +74,7 @@ var _ = Describe("stree", func() {
7774
Context("When second insert is far in the past", func() {
7875
It("sets root properly", func() {
7976
log.Println("---")
80-
s := New(r, m)
77+
s := New()
8178
s.Put(testing.SimpleTime(2030),
8279
testing.SimpleTime(2039), 1, func(de int, t time.Time, r *big.Rat, a []Addon) {})
8380
Expect(s.root).ToNot(BeNil())
@@ -91,31 +88,31 @@ var _ = Describe("stree", func() {
9188

9289
Context("When empty", func() {
9390
It("sets root properly", func() {
94-
s := New(r, m)
91+
s := New()
9592
s.Put(testing.SimpleTime(0),
9693
testing.SimpleTime(9), 1, func(de int, t time.Time, r *big.Rat, a []Addon) {})
9794
Expect(s.root).ToNot(BeNil())
9895
Expect(s.root.depth).To(Equal(0))
9996
})
10097

10198
It("sets root properly", func() {
102-
s := New(r, m)
99+
s := New()
103100
s.Put(testing.SimpleTime(0),
104101
testing.SimpleTime(49), 1, func(de int, t time.Time, r *big.Rat, a []Addon) {})
105102
Expect(s.root).ToNot(BeNil())
106103
Expect(s.root.depth).To(Equal(1))
107104
})
108105

109106
It("sets root properly", func() {
110-
s := New(r, m)
107+
s := New()
111108
s.Put(testing.SimpleTime(10),
112109
testing.SimpleTime(109), 1, func(de int, t time.Time, r *big.Rat, a []Addon) {})
113110
Expect(s.root).ToNot(BeNil())
114111
Expect(s.root.depth).To(Equal(2))
115112
})
116113

117114
It("sets root properly", func() {
118-
s := New(r, m)
115+
s := New()
119116
s.Put(testing.SimpleTime(10),
120117
testing.SimpleTime(19), 1, func(de int, t time.Time, r *big.Rat, a []Addon) {})
121118
Expect(s.root).ToNot(BeNil())
@@ -126,7 +123,7 @@ var _ = Describe("stree", func() {
126123
})
127124

128125
It("sets root properly", func() {
129-
s := New(r, m)
126+
s := New()
130127
s.Put(testing.SimpleTime(10),
131128
testing.SimpleTime(19), 1, func(de int, t time.Time, r *big.Rat, a []Addon) {})
132129
Expect(s.root).ToNot(BeNil())
@@ -139,7 +136,7 @@ var _ = Describe("stree", func() {
139136
})
140137

141138
It("sets root properly", func() {
142-
s := New(r, m)
139+
s := New()
143140
s.Put(testing.SimpleTime(10),
144141
testing.SimpleTime(19), 10, func(de int, t time.Time, r *big.Rat, a []Addon) {})
145142
Expect(s.root).ToNot(BeNil())
@@ -152,7 +149,7 @@ var _ = Describe("stree", func() {
152149
})
153150

154151
It("sets root properly", func() {
155-
s := New(r, m)
152+
s := New()
156153
s.Put(testing.SimpleTime(10),
157154
testing.SimpleTime(19), 1, func(de int, t time.Time, r *big.Rat, a []Addon) {})
158155
Expect(s.root).ToNot(BeNil())
@@ -171,7 +168,7 @@ var _ = Describe("stree", func() {
171168
})
172169

173170
It("sets root properly", func() {
174-
s := New(r, m)
171+
s := New()
175172
s.Put(testing.SimpleTime(30),
176173
testing.SimpleTime(39), 1, func(de int, t time.Time, r *big.Rat, a []Addon) {})
177174
Expect(s.root).ToNot(BeNil())
@@ -191,7 +188,7 @@ var _ = Describe("stree", func() {
191188
})
192189

193190
It("works with 3 mins", func() {
194-
s := New(r, m)
191+
s := New()
195192
s.Put(testing.SimpleTime(10),
196193
testing.SimpleTime(70), 1, func(de int, t time.Time, r *big.Rat, a []Addon) {})
197194
Expect(s.root).ToNot(BeNil())
@@ -200,7 +197,7 @@ var _ = Describe("stree", func() {
200197
})
201198

202199
It("sets trie properly, gets work", func() {
203-
s := New(r, m)
200+
s := New()
204201

205202
s.Put(testing.SimpleTime(0),
206203
testing.SimpleTime(9), 1, func(de int, t time.Time, r *big.Rat, a []Addon) {})

pkg/storage/segment/serialization.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ func (s *Segment) Serialize(w io.Writer) error {
7878
return nil
7979
}
8080

81-
func Deserialize(resolution time.Duration, multiplier int, r io.Reader) (*Segment, error) {
82-
s := New(resolution, multiplier)
81+
func Deserialize(r io.Reader) (*Segment, error) {
82+
s := New()
8383
br := bufio.NewReader(r) // TODO if it's already a bytereader skip
8484

8585
// reads serialization format version, see comment at the top
@@ -155,8 +155,8 @@ func (t *Segment) Bytes() []byte {
155155
return b.Bytes()
156156
}
157157

158-
func FromBytes(resolution time.Duration, multiplier int, p []byte) *Segment {
158+
func FromBytes(p []byte) *Segment {
159159
// TODO: handle error
160-
t, _ := Deserialize(resolution, multiplier, bytes.NewReader(p))
160+
t, _ := Deserialize(bytes.NewReader(p))
161161
return t
162162
}

pkg/storage/segment/serialization_test.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,9 @@ var serializedExampleV2 = "\x02={\"aggregationType\":\"\",\"sampleRate\":0,\"spy
2121
"\x00\x8a\x92\xb8Ø\xfe\xff\xff\xff\x01\x01\x01\x01\x00\x00\x94\x92\xb8Ø\xfe\xff\xff\xff\x01\x01\x01\x01\x00"
2222

2323
var _ = Describe("stree", func() {
24-
r := 10 * time.Second
25-
m := 10
26-
2724
Context("Serialize / Deserialize", func() {
2825
It("both functions work properly", func() {
29-
s := New(r, m)
26+
s := New()
3027
s.Put(testing.SimpleTime(0),
3128
testing.SimpleTime(9), 1, func(de int, t time.Time, r *big.Rat, a []Addon) {})
3229
s.Put(testing.SimpleTime(10),
@@ -39,7 +36,7 @@ var _ = Describe("stree", func() {
3936
serialized := buf.Bytes()
4037
log.Printf("%q", serialized)
4138

42-
s, err := Deserialize(r, m, bytes.NewReader(serialized))
39+
s, err := Deserialize(bytes.NewReader(serialized))
4340
Expect(err).ToNot(HaveOccurred())
4441
var buf2 bytes.Buffer
4542
s.Serialize(&buf2)
@@ -50,7 +47,7 @@ var _ = Describe("stree", func() {
5047

5148
Context("Serialize", func() {
5249
It("serializes segment tree properly", func() {
53-
s := New(r, m)
50+
s := New()
5451
s.Put(testing.SimpleTime(0),
5552
testing.SimpleTime(9), 1, func(de int, t time.Time, r *big.Rat, a []Addon) {})
5653
s.Put(testing.SimpleTime(10),
@@ -69,7 +66,7 @@ var _ = Describe("stree", func() {
6966
Context("Deserialize", func() {
7067
Context("v1", func() {
7168
It("deserializes v1 data", func() {
72-
s, err := Deserialize(r, m, bytes.NewReader([]byte(serializedExampleV1)))
69+
s, err := Deserialize(bytes.NewReader([]byte(serializedExampleV1)))
7370
Expect(err).ToNot(HaveOccurred())
7471
Expect(s.root.children[0]).ToNot(BeNil())
7572
Expect(s.root.children[1]).ToNot(BeNil())
@@ -79,7 +76,7 @@ var _ = Describe("stree", func() {
7976
})
8077
Context("v2", func() {
8178
It("deserializes v2 data", func() {
82-
s, err := Deserialize(r, m, bytes.NewReader([]byte(serializedExampleV2)))
79+
s, err := Deserialize(bytes.NewReader([]byte(serializedExampleV2)))
8380
Expect(err).ToNot(HaveOccurred())
8481
Expect(s.root.children[0]).ToNot(BeNil())
8582
Expect(s.root.children[1]).ToNot(BeNil())

0 commit comments

Comments
 (0)