Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize StagedMetadatas conversion #3330

Merged
merged 3 commits into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions src/metrics/aggregation/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,21 +141,13 @@ func (id *ID) UnmarshalYAML(unmarshal func(interface{}) error) error {
}

// ToProto converts the aggregation id to a protobuf message in place.
func (id ID) ToProto(pb *aggregationpb.AggregationID) error {
if IDLen != 1 {
return fmt.Errorf("id length %d cannot be represented by a single integer", IDLen)
}
func (id ID) ToProto(pb *aggregationpb.AggregationID) {
pb.Id = id[0]
return nil
}

// FromProto converts the protobuf message to an aggregation id in place.
func (id *ID) FromProto(pb aggregationpb.AggregationID) error {
if IDLen != 1 {
return fmt.Errorf("id length %d cannot be represented by a single integer", IDLen)
}
func (id *ID) FromProto(pb aggregationpb.AggregationID) {
(*id)[0] = pb.Id
return nil
}

// CompressTypes compresses a list of aggregation types to an ID.
Expand All @@ -172,3 +164,10 @@ func MustCompressTypes(aggTypes ...Type) ID {
}
return res
}

func init() {
if IDLen != 1 {
// changing this const requires extensive surgery
panic(fmt.Sprintf("id length %d cannot be represented by a single integer", IDLen))
}
}
8 changes: 4 additions & 4 deletions src/metrics/aggregation/id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ var (

func TestIDToProto(t *testing.T) {
var pb aggregationpb.AggregationID
require.NoError(t, testID.ToProto(&pb))
testID.ToProto(&pb)
require.Equal(t, testIDProto, pb)
}

func TestIDFromProto(t *testing.T) {
var res ID
require.NoError(t, res.FromProto(testIDProto))
res.FromProto(testIDProto)
require.Equal(t, testID, res)
}

Expand All @@ -54,8 +54,8 @@ func TestIDRoundTrip(t *testing.T) {
pb aggregationpb.AggregationID
res ID
)
require.NoError(t, testID.ToProto(&pb))
require.NoError(t, res.FromProto(pb))
testID.ToProto(&pb)
res.FromProto(pb)
require.Equal(t, testID, res)
}

Expand Down
30 changes: 15 additions & 15 deletions src/metrics/encoding/protobuf/unaggregated_encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ var (
}
testPassthroughMetadata1 = policy.NewStoragePolicy(time.Minute, xtime.Minute, 12*time.Hour)
testPassthroughMetadata2 = policy.NewStoragePolicy(10*time.Second, xtime.Second, 6*time.Hour)
testCounter1Proto = metricpb.Counter{
testCounter1Proto = metricpb.Counter{
Id: []byte("testCounter1"),
Value: 123,
}
Expand Down Expand Up @@ -673,11 +673,11 @@ func TestUnaggregatedEncoderEncodeBatchTimerWithMetadatas(t *testing.T) {
enc.(*unaggregatedEncoder).encodeMessageFn = func(pb metricpb.MetricWithMetadatas) error { pbRes = pb; return nil }
for i, input := range inputs {
require.NoError(t, enc.EncodeMessage(encoding.UnaggregatedMessageUnion{
Type: encoding.BatchTimerWithMetadatasType,
Type: encoding.BatchTimerWithMetadatasType,
BatchTimerWithMetadatas: input,
}))
expectedProto := metricpb.MetricWithMetadatas{
Type: metricpb.MetricWithMetadatas_BATCH_TIMER_WITH_METADATAS,
Type: metricpb.MetricWithMetadatas_BATCH_TIMER_WITH_METADATAS,
BatchTimerWithMetadatas: &expected[i],
}
expectedMsgSize := expectedProto.Size()
Expand Down Expand Up @@ -793,11 +793,11 @@ func TestUnaggregatedEncoderEncodeForwardedMetricWithMetadata(t *testing.T) {
enc.(*unaggregatedEncoder).encodeMessageFn = func(pb metricpb.MetricWithMetadatas) error { pbRes = pb; return nil }
for i, input := range inputs {
require.NoError(t, enc.EncodeMessage(encoding.UnaggregatedMessageUnion{
Type: encoding.ForwardedMetricWithMetadataType,
Type: encoding.ForwardedMetricWithMetadataType,
ForwardedMetricWithMetadata: input,
}))
expectedProto := metricpb.MetricWithMetadatas{
Type: metricpb.MetricWithMetadatas_FORWARDED_METRIC_WITH_METADATA,
Type: metricpb.MetricWithMetadatas_FORWARDED_METRIC_WITH_METADATA,
ForwardedMetricWithMetadata: &expected[i],
}
expectedMsgSize := expectedProto.Size()
Expand Down Expand Up @@ -853,11 +853,11 @@ func TestUnaggregatedEncoderEncodeTimedMetricWithMetadata(t *testing.T) {
enc.(*unaggregatedEncoder).encodeMessageFn = func(pb metricpb.MetricWithMetadatas) error { pbRes = pb; return nil }
for i, input := range inputs {
require.NoError(t, enc.EncodeMessage(encoding.UnaggregatedMessageUnion{
Type: encoding.TimedMetricWithMetadataType,
Type: encoding.TimedMetricWithMetadataType,
TimedMetricWithMetadata: input,
}))
expectedProto := metricpb.MetricWithMetadatas{
Type: metricpb.MetricWithMetadatas_TIMED_METRIC_WITH_METADATA,
Type: metricpb.MetricWithMetadatas_TIMED_METRIC_WITH_METADATA,
TimedMetricWithMetadata: &expected[i],
}
expectedMsgSize := expectedProto.Size()
Expand Down Expand Up @@ -1121,12 +1121,12 @@ func TestUnaggregatedEncoderStress(t *testing.T) {
}
case unaggregated.BatchTimerWithMetadatas:
msg = encoding.UnaggregatedMessageUnion{
Type: encoding.BatchTimerWithMetadatasType,
Type: encoding.BatchTimerWithMetadatasType,
BatchTimerWithMetadatas: input,
}
res := expected[i].(metricpb.BatchTimerWithMetadatas)
expectedProto = metricpb.MetricWithMetadatas{
Type: metricpb.MetricWithMetadatas_BATCH_TIMER_WITH_METADATAS,
Type: metricpb.MetricWithMetadatas_BATCH_TIMER_WITH_METADATAS,
BatchTimerWithMetadatas: &res,
}
case unaggregated.GaugeWithMetadatas:
Expand All @@ -1141,32 +1141,32 @@ func TestUnaggregatedEncoderStress(t *testing.T) {
}
case aggregated.ForwardedMetricWithMetadata:
msg = encoding.UnaggregatedMessageUnion{
Type: encoding.ForwardedMetricWithMetadataType,
Type: encoding.ForwardedMetricWithMetadataType,
ForwardedMetricWithMetadata: input,
}
res := expected[i].(metricpb.ForwardedMetricWithMetadata)
expectedProto = metricpb.MetricWithMetadatas{
Type: metricpb.MetricWithMetadatas_FORWARDED_METRIC_WITH_METADATA,
Type: metricpb.MetricWithMetadatas_FORWARDED_METRIC_WITH_METADATA,
ForwardedMetricWithMetadata: &res,
}
case aggregated.TimedMetricWithMetadata:
msg = encoding.UnaggregatedMessageUnion{
Type: encoding.TimedMetricWithMetadataType,
Type: encoding.TimedMetricWithMetadataType,
TimedMetricWithMetadata: input,
}
res := expected[i].(metricpb.TimedMetricWithMetadata)
expectedProto = metricpb.MetricWithMetadatas{
Type: metricpb.MetricWithMetadatas_TIMED_METRIC_WITH_METADATA,
Type: metricpb.MetricWithMetadatas_TIMED_METRIC_WITH_METADATA,
TimedMetricWithMetadata: &res,
}
case aggregated.PassthroughMetricWithMetadata:
msg = encoding.UnaggregatedMessageUnion{
Type: encoding.PassthroughMetricWithMetadataType,
Type: encoding.PassthroughMetricWithMetadataType,
PassthroughMetricWithMetadata: input,
}
res := expected[i].(metricpb.TimedMetricWithStoragePolicy)
expectedProto = metricpb.MetricWithMetadatas{
Type: metricpb.MetricWithMetadatas_TIMED_METRIC_WITH_STORAGE_POLICY,
Type: metricpb.MetricWithMetadatas_TIMED_METRIC_WITH_STORAGE_POLICY,
TimedMetricWithStoragePolicy: &res,
}
default:
Expand Down
97 changes: 79 additions & 18 deletions src/metrics/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,7 @@ func (m PipelineMetadata) Clone() PipelineMetadata {

// ToProto converts the pipeline metadata to a protobuf message in place.
func (m PipelineMetadata) ToProto(pb *metricpb.PipelineMetadata) error {
if err := m.AggregationID.ToProto(&pb.AggregationId); err != nil {
return err
}
m.AggregationID.ToProto(&pb.AggregationId)
if err := m.Pipeline.ToProto(&pb.Pipeline); err != nil {
return err
}
Expand All @@ -158,9 +156,7 @@ func (m PipelineMetadata) ToProto(pb *metricpb.PipelineMetadata) error {

// FromProto converts the protobuf message to a pipeline metadata in place.
func (m *PipelineMetadata) FromProto(pb metricpb.PipelineMetadata) error {
if err := m.AggregationID.FromProto(pb.AggregationId); err != nil {
return err
}
m.AggregationID.FromProto(pb.AggregationId)
if err := m.Pipeline.FromProto(pb.Pipeline); err != nil {
return err
}
Expand Down Expand Up @@ -358,9 +354,7 @@ type ForwardMetadata struct {

// ToProto converts the forward metadata to a protobuf message in place.
func (m ForwardMetadata) ToProto(pb *metricpb.ForwardMetadata) error {
if err := m.AggregationID.ToProto(&pb.AggregationId); err != nil {
return err
}
m.AggregationID.ToProto(&pb.AggregationId)
if err := m.StoragePolicy.ToProto(&pb.StoragePolicy); err != nil {
return err
}
Expand All @@ -374,9 +368,7 @@ func (m ForwardMetadata) ToProto(pb *metricpb.ForwardMetadata) error {

// FromProto converts the protobuf message to a forward metadata in place.
func (m *ForwardMetadata) FromProto(pb metricpb.ForwardMetadata) error {
if err := m.AggregationID.FromProto(pb.AggregationId); err != nil {
return err
}
m.AggregationID.FromProto(pb.AggregationId)
if err := m.StoragePolicy.FromProto(pb.StoragePolicy); err != nil {
return err
}
Expand Down Expand Up @@ -487,13 +479,86 @@ func (sms StagedMetadatas) ToProto(pb *metricpb.StagedMetadatas) error {
}

// FromProto converts the protobuf message to a staged metadatas in place.
// This is an optimized method that merges some nested steps.
func (sms *StagedMetadatas) FromProto(pb metricpb.StagedMetadatas) error {
numMetadatas := len(pb.Metadatas)
if cap(*sms) >= numMetadatas {
*sms = (*sms)[:numMetadatas]
} else {
*sms = make([]StagedMetadata, numMetadatas)
}

for i := 0; i < numMetadatas; i++ {
metadata := &(*sms)[i]
metadataPb := &pb.Metadatas[i]
numPipelines := len(metadataPb.Metadata.Pipelines)

metadata.CutoverNanos = metadataPb.CutoverNanos
metadata.Tombstoned = metadataPb.Tombstoned

if cap(metadata.Pipelines) >= numPipelines {
metadata.Pipelines = metadata.Pipelines[:numPipelines]
} else {
metadata.Pipelines = make(PipelineMetadatas, numPipelines)
}

for j := 0; j < numPipelines; j++ {
var (
pipelinePb = &metadataPb.Metadata.Pipelines[j]
pipeline = &metadata.Pipelines[j]
numStoragePolicies = len(pipelinePb.StoragePolicies)
numOps = len(pipelinePb.Pipeline.Ops)
err error
)

pipeline.AggregationID[0] = pipelinePb.AggregationId.Id
pipeline.DropPolicy = policy.DropPolicy(pipelinePb.DropPolicy)

if len(pipeline.Tags) > 0 {
pipeline.Tags = pipeline.Tags[:0]
}
if len(pipeline.GraphitePrefix) > 0 {
pipeline.GraphitePrefix = pipeline.GraphitePrefix[:0]
}

if cap(pipeline.StoragePolicies) >= numStoragePolicies {
pipeline.StoragePolicies = pipeline.StoragePolicies[:numStoragePolicies]
} else {
pipeline.StoragePolicies = make([]policy.StoragePolicy, numStoragePolicies)
}

if cap(pipeline.Pipeline.Operations) >= numOps {
pipeline.Pipeline.Operations = pipeline.Pipeline.Operations[:numOps]
} else {
pipeline.Pipeline.Operations = make([]applied.OpUnion, numOps)
}

if len(pipelinePb.Pipeline.Ops) > 0 {
err = applied.OperationsFromProto(pipelinePb.Pipeline.Ops, pipeline.Pipeline.Operations)
if err != nil {
return err
}
}
if len(pipelinePb.StoragePolicies) > 0 {
err = policy.StoragePoliciesFromProto(pipelinePb.StoragePolicies, pipeline.StoragePolicies)
if err != nil {
return err
}
}
}
}

return nil
}

// fromProto is a non-optimized in place protobuf conversion method, used as a reference for tests.
func (sms *StagedMetadatas) fromProto(pb metricpb.StagedMetadatas) error {
numMetadatas := len(pb.Metadatas)
if cap(*sms) >= numMetadatas {
*sms = (*sms)[:numMetadatas]
} else {
*sms = make([]StagedMetadata, numMetadatas)
}
for i := 0; i < numMetadatas; i++ {
if err := (*sms)[i].FromProto(pb.Metadatas[i]); err != nil {
return err
Expand All @@ -519,9 +584,7 @@ type TimedMetadata struct {

// ToProto converts the timed metadata to a protobuf message in place.
func (m TimedMetadata) ToProto(pb *metricpb.TimedMetadata) error {
if err := m.AggregationID.ToProto(&pb.AggregationId); err != nil {
return err
}
m.AggregationID.ToProto(&pb.AggregationId)
if err := m.StoragePolicy.ToProto(&pb.StoragePolicy); err != nil {
return err
}
Expand All @@ -530,9 +593,7 @@ func (m TimedMetadata) ToProto(pb *metricpb.TimedMetadata) error {

// FromProto converts the protobuf message to a timed metadata in place.
func (m *TimedMetadata) FromProto(pb metricpb.TimedMetadata) error {
if err := m.AggregationID.FromProto(pb.AggregationId); err != nil {
return err
}
m.AggregationID.FromProto(pb.AggregationId)
if err := m.StoragePolicy.FromProto(pb.StoragePolicy); err != nil {
return err
}
Expand Down
Loading