Skip to content

Commit

Permalink
Merge pull request #527 from parca-dev/move-rendering
Browse files Browse the repository at this point in the history
*: Move rendering query results out of storage package
  • Loading branch information
brancz authored Jan 6, 2022
2 parents 6e21eb3 + 961de1b commit 93b96f7
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 63 deletions.
15 changes: 15 additions & 0 deletions pkg/profile/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"time"

"github.com/google/pprof/profile"
"github.com/google/uuid"
"github.com/parca-dev/parca/pkg/metastore"
)

Expand Down Expand Up @@ -105,3 +106,17 @@ func (p *ScaledInstantProfile) Samples() map[string]*Sample {
}
return samples
}

// MakeSample creates a sample from a stack trace (list of locations) and a
// value. Mostly meant for testing.
func MakeSample(value int64, locationIds []uuid.UUID) *Sample {
s := &Sample{
Value: value,
}

for _, id := range locationIds {
s.Location = append(s.Location, &metastore.Location{ID: id})
}

return s
}
2 changes: 1 addition & 1 deletion pkg/storage/flamegraph.go → pkg/query/flamegraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package storage
package query

import (
"bytes"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package storage
package query

import (
"bytes"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package storage
package query

import (
"context"
Expand Down Expand Up @@ -153,9 +153,9 @@ func TestGenerateFlamegraphFlat(t *testing.T) {
l5.ID, err = uuid.FromBytes(l5ID)
require.NoError(t, err)

s0 := makeSample(2, []uuid.UUID{l2.ID, l1.ID})
s1 := makeSample(1, []uuid.UUID{l5.ID, l3.ID, l2.ID, l1.ID})
s2 := makeSample(3, []uuid.UUID{l4.ID, l3.ID, l2.ID, l1.ID})
s0 := parcaprofile.MakeSample(2, []uuid.UUID{l2.ID, l1.ID})
s1 := parcaprofile.MakeSample(1, []uuid.UUID{l5.ID, l3.ID, l2.ID, l1.ID})
s2 := parcaprofile.MakeSample(3, []uuid.UUID{l4.ID, l3.ID, l2.ID, l1.ID})

k0 := parcaprofile.MakeStacktraceKey(s0)
k1 := parcaprofile.MakeStacktraceKey(s1)
Expand Down Expand Up @@ -251,7 +251,7 @@ func TestGenerateFlamegraphFromFlatProfile(t *testing.T) {
func testGenerateFlamegraphFromFlatProfile(t *testing.T, l metastore.ProfileMetaStore) *pb.Flamegraph {
ctx := context.Background()

f, err := os.Open("testdata/profile1.pb.gz")
f, err := os.Open("../storage/testdata/profile1.pb.gz")
require.NoError(t, err)
p1, err := profile.Parse(f)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/storage/pprof.go → pkg/query/pprof.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package storage
package query

import (
"context"
Expand Down
4 changes: 2 additions & 2 deletions pkg/storage/pprof_test.go → pkg/query/pprof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package storage
package query

import (
"context"
Expand Down Expand Up @@ -147,7 +147,7 @@ func TestGeneratePprofNilMapping(t *testing.T) {
l2.ID, err = uuid.FromBytes(l2ID)
require.NoError(t, err)

sample := makeSample(2, []uuid.UUID{
sample := parcaprofile.MakeSample(2, []uuid.UUID{
l2.ID,
l1.ID,
})
Expand Down
4 changes: 2 additions & 2 deletions pkg/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ func (q *Query) selectProfileForDiff(ctx context.Context, s *pb.ProfileDiffSelec
func (q *Query) renderReport(ctx context.Context, p profile.InstantProfile, typ pb.QueryRequest_ReportType) (*pb.QueryResponse, error) {
switch typ {
case pb.QueryRequest_REPORT_TYPE_FLAMEGRAPH_UNSPECIFIED:
fg, err := storage.GenerateFlamegraphFlat(ctx, q.tracer, q.metaStore, p)
fg, err := GenerateFlamegraphFlat(ctx, q.tracer, q.metaStore, p)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to generate flamegraph: %v", err.Error())
}
Expand All @@ -299,7 +299,7 @@ func (q *Query) renderReport(ctx context.Context, p profile.InstantProfile, typ
},
}, nil
case pb.QueryRequest_REPORT_TYPE_PPROF_UNSPECIFIED:
pp, err := storage.GenerateFlatPprof(ctx, q.metaStore, p)
pp, err := GenerateFlatPprof(ctx, q.metaStore, p)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to generate pprof: %v", err.Error())
}
Expand Down
16 changes: 8 additions & 8 deletions pkg/storage/diff_flat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestDiffFlatProfileSimple(t *testing.T) {
uuid2 := uuid.MustParse("00000000-0000-0000-0000-000000000002")
uuid3 := uuid.MustParse("00000000-0000-0000-0000-000000000003")

s1 := makeSample(3, []uuid.UUID{uuid2, uuid1})
s1 := parcaprofile.MakeSample(3, []uuid.UUID{uuid2, uuid1})
k1 := uuid.MustParse("00000000-0000-0000-0000-0000000000e1")

p1 := &parcaprofile.FlatProfile{
Expand All @@ -50,7 +50,7 @@ func TestDiffFlatProfileSimple(t *testing.T) {
},
}

s2 := makeSample(1, []uuid.UUID{uuid3, uuid1})
s2 := parcaprofile.MakeSample(1, []uuid.UUID{uuid3, uuid1})
k2 := uuid.MustParse("00000000-0000-0000-0000-0000000000e2")

p2 := &parcaprofile.FlatProfile{
Expand Down Expand Up @@ -89,10 +89,10 @@ func TestDiffFlatProfileDeep(t *testing.T) {
uuid3 := uuid.MustParse("00000000-0000-0000-0000-000000000003")
uuid6 := uuid.MustParse("00000000-0000-0000-0000-000000000006")

s0 := makeSample(3, []uuid.UUID{uuid3, uuid3, uuid2})
s1 := makeSample(3, []uuid.UUID{uuid6, uuid2})
s2 := makeSample(3, []uuid.UUID{uuid2, uuid3})
s3 := makeSample(3, []uuid.UUID{uuid1, uuid3})
s0 := parcaprofile.MakeSample(3, []uuid.UUID{uuid3, uuid3, uuid2})
s1 := parcaprofile.MakeSample(3, []uuid.UUID{uuid6, uuid2})
s2 := parcaprofile.MakeSample(3, []uuid.UUID{uuid2, uuid3})
s3 := parcaprofile.MakeSample(3, []uuid.UUID{uuid1, uuid3})

k0 := uuid.MustParse("00000000-0000-0000-0000-0000000000e0")
k1 := uuid.MustParse("00000000-0000-0000-0000-0000000000e1")
Expand All @@ -115,8 +115,8 @@ func TestDiffFlatProfileDeep(t *testing.T) {
},
}

s4 := makeSample(3, []uuid.UUID{uuid3, uuid2, uuid2})
s5 := makeSample(5, []uuid.UUID{uuid2, uuid3})
s4 := parcaprofile.MakeSample(3, []uuid.UUID{uuid3, uuid2, uuid2})
s5 := parcaprofile.MakeSample(5, []uuid.UUID{uuid2, uuid3})

k4 := uuid.MustParse("00000000-0000-0000-0000-0000000000e4")

Expand Down
4 changes: 2 additions & 2 deletions pkg/storage/head_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestHead_MaxTime(t *testing.T) {
app, err := h.Appender(ctx, labels.FromStrings("foo", "bar"))
require.NoError(t, err)

s := makeSample(1, []uuid.UUID{
s := profile.MakeSample(1, []uuid.UUID{
uuid.MustParse("00000000-0000-0000-0000-000000000002"),
uuid.MustParse("00000000-0000-0000-0000-000000000001"),
})
Expand Down Expand Up @@ -128,7 +128,7 @@ func BenchmarkStripeSeries(b *testing.B) {
func TestHead_Truncate(t *testing.T) {
h := NewHead(prometheus.NewRegistry(), trace.NewNoopTracerProvider().Tracer(""), nil)

s := makeSample(1, []uuid.UUID{
s := profile.MakeSample(1, []uuid.UUID{
uuid.MustParse("00000000-0000-0000-0000-000000000002"),
uuid.MustParse("00000000-0000-0000-0000-000000000001"),
})
Expand Down
19 changes: 3 additions & 16 deletions pkg/storage/interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,21 @@ import (

"github.com/google/uuid"
"github.com/parca-dev/parca/pkg/profile"
"github.com/parca-dev/parca/pkg/metastore"
"github.com/stretchr/testify/require"
)

func makeSample(value int64, locationIds []uuid.UUID) *profile.Sample {
s := &profile.Sample{
Value: value,
}

for _, id := range locationIds {
s.Location = append(s.Location, &metastore.Location{ID: id})
}

return s
}

func TestScaledInstantProfile(t *testing.T) {
s1 := makeSample(2, []uuid.UUID{
s1 := profile.MakeSample(2, []uuid.UUID{
uuid.MustParse("00000000-0000-0000-0000-000000000002"),
uuid.MustParse("00000000-0000-0000-0000-000000000001"),
})
s2 := makeSample(1, []uuid.UUID{
s2 := profile.MakeSample(1, []uuid.UUID{
uuid.MustParse("00000000-0000-0000-0000-000000000005"),
uuid.MustParse("00000000-0000-0000-0000-000000000003"),
uuid.MustParse("00000000-0000-0000-0000-000000000002"),
uuid.MustParse("00000000-0000-0000-0000-000000000001"),
})
s3 := makeSample(3, []uuid.UUID{
s3 := profile.MakeSample(3, []uuid.UUID{
uuid.MustParse("00000000-0000-0000-0000-000000000004"),
uuid.MustParse("00000000-0000-0000-0000-000000000003"),
uuid.MustParse("00000000-0000-0000-0000-000000000002"),
Expand Down
28 changes: 14 additions & 14 deletions pkg/storage/merge_flat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestMergeFlatProfileSimple(t *testing.T) {
uuid2 := uuid.MustParse("00000000-0000-0000-0000-000000000002")
uuid3 := uuid.MustParse("00000000-0000-0000-0000-000000000003")

s1 := makeSample(2, []uuid.UUID{uuid2, uuid1})
s1 := parcaprofile.MakeSample(2, []uuid.UUID{uuid2, uuid1})
k1 := uuid.MustParse("00000000-0000-0000-0000-0000000000e1")

p1 := &parcaprofile.FlatProfile{
Expand All @@ -52,7 +52,7 @@ func TestMergeFlatProfileSimple(t *testing.T) {
},
}

s2 := makeSample(1, []uuid.UUID{uuid3, uuid1})
s2 := parcaprofile.MakeSample(1, []uuid.UUID{uuid3, uuid1})
k2 := uuid.MustParse("00000000-0000-0000-0000-0000000000e2")

p2 := &parcaprofile.FlatProfile{
Expand Down Expand Up @@ -97,10 +97,10 @@ func TestMergeFlatProfileDeep(t *testing.T) {
uuid3 := uuid.MustParse("00000000-0000-0000-0000-000000000003")
uuid6 := uuid.MustParse("00000000-0000-0000-0000-000000000006")

s1 := makeSample(3, []uuid.UUID{uuid1, uuid3})
s2 := makeSample(3, []uuid.UUID{uuid2, uuid3})
s3 := makeSample(3, []uuid.UUID{uuid3, uuid3, uuid2})
s4 := makeSample(3, []uuid.UUID{uuid6, uuid2})
s1 := parcaprofile.MakeSample(3, []uuid.UUID{uuid1, uuid3})
s2 := parcaprofile.MakeSample(3, []uuid.UUID{uuid2, uuid3})
s3 := parcaprofile.MakeSample(3, []uuid.UUID{uuid3, uuid3, uuid2})
s4 := parcaprofile.MakeSample(3, []uuid.UUID{uuid6, uuid2})

k1 := uuid.MustParse("00000000-0000-0000-0000-0000000000e1")
k2 := uuid.MustParse("00000000-0000-0000-0000-0000000000e2")
Expand All @@ -123,7 +123,7 @@ func TestMergeFlatProfileDeep(t *testing.T) {
},
}

s5 := makeSample(3, []uuid.UUID{uuid3, uuid2, uuid2})
s5 := parcaprofile.MakeSample(3, []uuid.UUID{uuid3, uuid2, uuid2})
k5 := uuid.MustParse("00000000-0000-0000-0000-0000000000e5")

p2 := &parcaprofile.FlatProfile{
Expand Down Expand Up @@ -183,11 +183,11 @@ func TestMergeFlatProfile(t *testing.T) {
uuid5 := uuid.MustParse("00000000-0000-0000-0000-000000000005")
uuid6 := uuid.MustParse("00000000-0000-0000-0000-000000000006")

s1 := makeSample(2, []uuid.UUID{uuid2, uuid1})
s2 := makeSample(1, []uuid.UUID{uuid6, uuid3, uuid2, uuid1})
s3 := makeSample(3, []uuid.UUID{uuid4, uuid3, uuid2, uuid1})
s4 := makeSample(3, []uuid.UUID{uuid3, uuid3, uuid2})
s5 := makeSample(3, []uuid.UUID{uuid6, uuid2})
s1 := parcaprofile.MakeSample(2, []uuid.UUID{uuid2, uuid1})
s2 := parcaprofile.MakeSample(1, []uuid.UUID{uuid6, uuid3, uuid2, uuid1})
s3 := parcaprofile.MakeSample(3, []uuid.UUID{uuid4, uuid3, uuid2, uuid1})
s4 := parcaprofile.MakeSample(3, []uuid.UUID{uuid3, uuid3, uuid2})
s5 := parcaprofile.MakeSample(3, []uuid.UUID{uuid6, uuid2})

k1 := uuid.MustParse("00000000-0000-0000-0000-0000000000e1")
k2 := uuid.MustParse("00000000-0000-0000-0000-0000000000e2")
Expand All @@ -212,8 +212,8 @@ func TestMergeFlatProfile(t *testing.T) {
},
}

s6 := makeSample(1, []uuid.UUID{uuid5, uuid3, uuid2, uuid1})
s7 := makeSample(3, []uuid.UUID{uuid3, uuid2, uuid2})
s6 := parcaprofile.MakeSample(1, []uuid.UUID{uuid5, uuid3, uuid2, uuid1})
s7 := parcaprofile.MakeSample(3, []uuid.UUID{uuid3, uuid2, uuid2})

k6 := uuid.MustParse("00000000-0000-0000-0000-0000000000e6")
k7 := uuid.MustParse("00000000-0000-0000-0000-0000000000e7")
Expand Down
2 changes: 1 addition & 1 deletion pkg/storage/series_iterator_range_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestMemRangeSeries_Iterator(t *testing.T) {
app, err := s.Appender()
require.NoError(t, err)

s1 := makeSample(2, []uuid.UUID{
s1 := profile.MakeSample(2, []uuid.UUID{
uuid.MustParse("00000000-0000-0000-0000-000000000002"),
uuid.MustParse("00000000-0000-0000-0000-000000000001"),
})
Expand Down
20 changes: 10 additions & 10 deletions pkg/storage/series_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func TestMemSeries(t *testing.T) {
uuid4 := uuid.MustParse("00000000-0000-0000-0000-000000000004")
uuid5 := uuid.MustParse("00000000-0000-0000-0000-000000000005")

s11 := makeSample(1, []uuid.UUID{uuid2, uuid1})
s12 := makeSample(2, []uuid.UUID{uuid4, uuid1})
s11 := profile.MakeSample(1, []uuid.UUID{uuid2, uuid1})
s12 := profile.MakeSample(2, []uuid.UUID{uuid4, uuid1})
s12.Label = label
s12.NumLabel = numLabel
s12.NumUnit = numUnit
Expand Down Expand Up @@ -77,7 +77,7 @@ func TestMemSeries(t *testing.T) {
require.Equal(t, chunkenc.FromValuesXOR(1), s.samples[string(k11[:])][0])
require.Equal(t, chunkenc.FromValuesXOR(2), s.samples[string(k12[:])][0])

s2 := makeSample(3, []uuid.UUID{uuid2, uuid1})
s2 := profile.MakeSample(3, []uuid.UUID{uuid2, uuid1})
fp2 := &profile.FlatProfile{
Meta: profile.InstantProfileMeta{
PeriodType: profile.ValueType{},
Expand All @@ -99,7 +99,7 @@ func TestMemSeries(t *testing.T) {
require.Equal(t, chunkenc.FromValuesXOR(2), s.samples[string(k12[:])][0]) // sparse - nothing added

// Add another sample with one new Location
s3 := makeSample(4, []uuid.UUID{uuid3, uuid1})
s3 := profile.MakeSample(4, []uuid.UUID{uuid3, uuid1})
k3 := uuid.MustParse("00000000-0000-0000-0000-0000000000e3")

fp3 := &profile.FlatProfile{
Expand All @@ -124,7 +124,7 @@ func TestMemSeries(t *testing.T) {
require.Equal(t, chunkenc.FromValuesXORAt(2, 4), s.samples[string(k3[:])][0])

// Merging another profileTree onto the existing one with one new Location
s4 := makeSample(6, []uuid.UUID{uuid5, uuid2, uuid1})
s4 := profile.MakeSample(6, []uuid.UUID{uuid5, uuid2, uuid1})
k4 := uuid.MustParse("00000000-0000-0000-0000-0000000000e4")

fp4 := &profile.FlatProfile{
Expand All @@ -150,7 +150,7 @@ func TestMemSeries(t *testing.T) {
require.Equal(t, chunkenc.FromValuesXORAt(3, 6), s.samples[string(k4[:])][0])

// Merging another profileTree onto the existing one with one new Location
s5 := makeSample(7, []uuid.UUID{uuid2, uuid1})
s5 := profile.MakeSample(7, []uuid.UUID{uuid2, uuid1})
fp5 := &profile.FlatProfile{
Meta: profile.InstantProfileMeta{
PeriodType: profile.ValueType{},
Expand Down Expand Up @@ -190,8 +190,8 @@ func TestMemSeriesMany(t *testing.T) {
uuid2 := uuid.MustParse("00000000-0000-0000-0000-000000000002")
uuid4 := uuid.MustParse("00000000-0000-0000-0000-000000000004")

s1 := makeSample(0, []uuid.UUID{uuid2, uuid1})
s2 := makeSample(0, []uuid.UUID{uuid4, uuid1})
s1 := profile.MakeSample(0, []uuid.UUID{uuid2, uuid1})
s2 := profile.MakeSample(0, []uuid.UUID{uuid4, uuid1})

k1 := uuid.MustParse("00000000-0000-0000-0000-0000000000e1")
k2 := uuid.MustParse("00000000-0000-0000-0000-0000000000e2")
Expand Down Expand Up @@ -293,7 +293,7 @@ func TestMemSeries_truncateFlatChunksBeforeConcurrent(t *testing.T) {
app, err := s.Appender()
require.NoError(t, err)

s1 := makeSample(1, []uuid.UUID{
s1 := profile.MakeSample(1, []uuid.UUID{
uuid.MustParse("00000000-0000-0000-0000-000000000002"),
uuid.MustParse("00000000-0000-0000-0000-000000000001"),
})
Expand Down Expand Up @@ -359,7 +359,7 @@ func BenchmarkMemSeries_truncateChunksBefore(b *testing.B) {
app, err := s.Appender()
require.NoError(b, err)

sample := makeSample(1, []uuid.UUID{
sample := profile.MakeSample(1, []uuid.UUID{
uuid.MustParse("00000000-0000-0000-0000-000000000002"),
uuid.MustParse("00000000-0000-0000-0000-000000000001"),
})
Expand Down

0 comments on commit 93b96f7

Please sign in to comment.