forked from opensearch-project/observability
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pkg/agent: Add batching support for profile write requests
Fixes opensearch-project#14 Signed-off-by: Sumera Priyadarsini <sylphrenadin@gmail.com>
- Loading branch information
Showing
6 changed files
with
284 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package agent | ||
|
||
import ( | ||
"context" | ||
|
||
"sync" | ||
"time" | ||
|
||
"github.com/cespare/xxhash/v2" | ||
"github.com/go-kit/log" | ||
"github.com/go-kit/log/level" | ||
profilestorepb "github.com/parca-dev/parca/gen/proto/go/parca/profilestore/v1alpha1" | ||
) | ||
|
||
type Batcher struct { | ||
series map[uint64]*profilestorepb.RawProfileSeries | ||
writeClient profilestorepb.ProfileStoreServiceClient | ||
logger log.Logger | ||
|
||
mtx sync.RWMutex | ||
lastProfileTakenAt time.Time | ||
lastError error | ||
} | ||
|
||
func NewBatcher(wc profilestorepb.ProfileStoreServiceClient) *Batcher { | ||
return &Batcher{ | ||
series: make(map[uint64]*profilestorepb.RawProfileSeries), | ||
writeClient: wc, | ||
} | ||
} | ||
|
||
func (b *Batcher) loopReport(lastProfileTakenAt time.Time, lastError error) { | ||
b.mtx.Lock() | ||
defer b.mtx.Unlock() | ||
b.lastProfileTakenAt = lastProfileTakenAt | ||
b.lastError = lastError | ||
} | ||
|
||
func (b *Batcher) Run(ctx context.Context) error { | ||
// TODO(Sylfrena): Make ticker duration configurable | ||
const tickerDuration = 10000000000 | ||
|
||
ticker := time.NewTicker(tickerDuration) | ||
defer ticker.Stop() | ||
|
||
var err error | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
case <-ticker.C: | ||
} | ||
|
||
err := b.batchLoop(ctx) | ||
b.loopReport(time.Now(), err) | ||
} | ||
b.series = make(map[uint64]*profilestorepb.RawProfileSeries) | ||
return err | ||
} | ||
|
||
func (batcher *Batcher) batchLoop(ctx context.Context) error { | ||
|
||
batcher.mtx.Lock() | ||
defer batcher.mtx.Unlock() | ||
|
||
var profileSeries []*profilestorepb.RawProfileSeries | ||
|
||
for _, value := range batcher.series { | ||
profileSeries = append(profileSeries, &profilestorepb.RawProfileSeries{ | ||
Labels: value.Labels, | ||
Samples: value.Samples, | ||
}) | ||
|
||
} | ||
|
||
_, err := batcher.writeClient.WriteRaw(ctx, | ||
&profilestorepb.WriteRawRequest{Series: profileSeries}) | ||
|
||
if err != nil { | ||
level.Error(batcher.logger).Log("msg", "Writeclient failed to send profiles", "err", err) | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (batcher *Batcher) Scheduler(profileSeries profilestorepb.RawProfileSeries) { | ||
batcher.mtx.Lock() | ||
defer batcher.mtx.Unlock() | ||
|
||
labelsetHash := Hash(*profileSeries.Labels) | ||
|
||
existing_sample, ok := batcher.series[labelsetHash] | ||
if ok { | ||
batcher.series[labelsetHash].Samples = append(existing_sample.Samples, profileSeries.Samples...) | ||
} else { | ||
batcher.series[labelsetHash] = &profilestorepb.RawProfileSeries{} | ||
batcher.series[labelsetHash].Samples = profileSeries.Samples | ||
} | ||
} | ||
|
||
func Hash(ls profilestorepb.LabelSet) uint64 { | ||
var seps = []byte{'\xff'} | ||
b := make([]byte, 0, 1024) | ||
for _, v := range ls.Labels { | ||
if len(b)+len(v.Name)+len(v.Value)+2 >= cap(b) { | ||
// If labels entry is 1KB+ do not allocate whole entry. | ||
h := xxhash.New() | ||
_, _ = h.Write(b) | ||
_, _ = h.WriteString(v.Name) | ||
_, _ = h.Write(seps) | ||
_, _ = h.WriteString(v.Value) | ||
_, _ = h.Write(seps) | ||
return h.Sum64() | ||
} | ||
|
||
b = append(b, v.Name...) | ||
b = append(b, seps[0]) | ||
b = append(b, v.Value...) | ||
b = append(b, seps[0]) | ||
} | ||
return xxhash.Sum64(b) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
// Copyright 2021 The Parca Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package agent | ||
|
||
import ( | ||
"testing" | ||
|
||
profilestorepb "github.com/parca-dev/parca/gen/proto/go/parca/profilestore/v1alpha1" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestScheduler(t *testing.T) { | ||
wc := NewNoopProfileStoreClient() | ||
batcher := NewBatcher(wc) | ||
|
||
labelset1 := profilestorepb.LabelSet{ | ||
Labels: []*profilestorepb.Label{{ | ||
Name: "n1", | ||
Value: "v1", | ||
}}, | ||
} | ||
labelset2 := profilestorepb.LabelSet{ | ||
Labels: []*profilestorepb.Label{{ | ||
Name: "n2", | ||
Value: "v2", | ||
}}, | ||
} | ||
|
||
labelsetHash1 := Hash(labelset1) | ||
labelsetHash2 := Hash(labelset2) | ||
|
||
samples1 := []*profilestorepb.RawSample{{RawProfile: []byte{11, 04, 96}}} | ||
samples2 := []*profilestorepb.RawSample{{RawProfile: []byte{15, 11, 95}}} | ||
|
||
t.Run("insertFirstProfile", func(t *testing.T) { | ||
|
||
batcher.Scheduler(profilestorepb.RawProfileSeries{ | ||
Labels: &labelset1, | ||
Samples: samples1, | ||
}) | ||
|
||
series := map[uint64]*profilestorepb.RawProfileSeries{ | ||
labelsetHash1: &profilestorepb.RawProfileSeries{ | ||
Labels: &labelset1, | ||
Samples: samples1, | ||
}, | ||
} | ||
|
||
require.Equal(t, series[labelsetHash1].Samples, | ||
batcher.series[labelsetHash1].Samples) | ||
}) | ||
|
||
t.Run("insertSecondProfile", func(t *testing.T) { | ||
|
||
batcher.Scheduler(profilestorepb.RawProfileSeries{ | ||
Labels: &labelset2, | ||
Samples: samples2, | ||
}) | ||
|
||
series := map[uint64]*profilestorepb.RawProfileSeries{ | ||
labelsetHash1: &profilestorepb.RawProfileSeries{ | ||
Labels: &labelset1, | ||
Samples: samples1, | ||
}, | ||
labelsetHash2: &profilestorepb.RawProfileSeries{ | ||
Labels: &labelset2, | ||
Samples: samples2, | ||
}, | ||
} | ||
|
||
require.Equal(t, series[labelsetHash1].Samples, | ||
batcher.series[labelsetHash1].Samples) | ||
|
||
require.Equal(t, series[labelsetHash2].Samples, | ||
batcher.series[labelsetHash2].Samples) | ||
}) | ||
|
||
t.Run("appendProfile", func(t *testing.T) { | ||
|
||
batcher.Scheduler(profilestorepb.RawProfileSeries{ | ||
Labels: &labelset1, | ||
Samples: samples2, | ||
}) | ||
|
||
series := map[uint64]*profilestorepb.RawProfileSeries{ | ||
labelsetHash1: &profilestorepb.RawProfileSeries{ | ||
Labels: &labelset1, | ||
Samples: append(samples1, samples2...), | ||
}, | ||
labelsetHash2: &profilestorepb.RawProfileSeries{ | ||
Labels: &labelset2, | ||
Samples: samples2, | ||
}, | ||
} | ||
|
||
require.Equal(t, series[labelsetHash1].Samples, | ||
batcher.series[labelsetHash1].Samples) | ||
|
||
require.Equal(t, series[labelsetHash2].Samples, | ||
batcher.series[labelsetHash2].Samples) | ||
|
||
}) | ||
|
||
t.Run("hash", func(t *testing.T) { | ||
|
||
labelset := profilestorepb.LabelSet{ | ||
Labels: []*profilestorepb.Label{{ | ||
Name: "n1", | ||
Value: "v1", | ||
}}, | ||
} | ||
|
||
labelsetHash := Hash(labelset) | ||
|
||
require.Equal(t, uint64(0xa3b730de852c2e2c), labelsetHash) | ||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.