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

orca: server side custom metrics implementation #5531

Merged
merged 27 commits into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
f7b002f
orca: server side custom metrics implementation
easwars Jun 13, 2022
415534c
make vet happy
easwars Jul 21, 2022
3f895cb
add comment about map copy
easwars Jul 27, 2022
f3faeec
combine two APIs into one for enabling call metrics
easwars Jul 27, 2022
8d20f18
move out-of-band metrics to service implementation
easwars Jul 27, 2022
1480966
fix typo
easwars Jul 28, 2022
dd2be28
grab read lock where appropriate
easwars Aug 2, 2022
3895f17
rename service implementation files
easwars Aug 2, 2022
abda20e
rename Server to Service
easwars Aug 2, 2022
58f5ebf
delete the NewService function
easwars Aug 2, 2022
87beb81
remove local var
easwars Aug 2, 2022
5000e59
embed a metricRecorded in the Service type
easwars Aug 2, 2022
ceea6a3
review comments
easwars Aug 16, 2022
709bf80
review comments pass 2
easwars Aug 17, 2022
828fe66
review comments pass 3
easwars Aug 18, 2022
95df238
allow any min interval
easwars Aug 19, 2022
f00c3f2
simplify checks for determining reporting interval
easwars Aug 22, 2022
7843f63
rename multiServerOption to joinServerOption
easwars Aug 24, 2022
6dbce85
get rid of the interfaces in favor of concrete types
easwars Aug 24, 2022
e8ae32d
lazy allocation of the metric recorder for per-call metrics
easwars Aug 24, 2022
83b68fd
delete unwanted method
easwars Aug 24, 2022
e91a4cc
add missing comment
easwars Aug 25, 2022
ac4ec4a
fold oob metric reporting functionality into Service impl
easwars Aug 25, 2022
f4eb507
review comments
easwars Aug 26, 2022
8665e34
delete Delete{CPU/Memory} APIs
easwars Aug 26, 2022
e68810e
accept a grpc.Server in Register
easwars Aug 30, 2022
d1972f8
allocate and use the recorderWrapper in the interceptor
easwars Aug 31, 2022
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
3 changes: 3 additions & 0 deletions internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ var (
// ClearExtraDialOptions clears the array of extra DialOption. This
// method is useful in testing and benchmarking.
ClearExtraDialOptions func()
// JoinServerOptions combines the server options passed as arguments into a
// single server option.
JoinServerOptions interface{} // func(...grpc.ServerOption) grpc.ServerOption

// NewXDSResolverWithConfigForTesting creates a new xds resolver builder using
// the provided xds bootstrap config instead of the global configuration from
Expand Down
130 changes: 130 additions & 0 deletions orca/call_metric_recorder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
*
* Copyright 2022 gRPC 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 orca

import (
"context"
"sync"
"sync/atomic"

v3orcapb "github.com/cncf/xds/go/xds/data/orca/v3"
)

// CallMetricRecorder provides functionality to record per-RPC custom backend
// metrics. See CallMetricsServerOption() for more details.
//
// Safe for concurrent use.
type CallMetricRecorder struct {
cpu atomic.Value // float64
memory atomic.Value // float64

mu sync.RWMutex
requestCost map[string]float64
utilization map[string]float64
}

func newCallMetricRecorder() *CallMetricRecorder {
return &CallMetricRecorder{
requestCost: make(map[string]float64),
utilization: make(map[string]float64),
}
}

// SetCPUUtilization records a measurement for the CPU utilization metric.
func (c *CallMetricRecorder) SetCPUUtilization(val float64) {
c.cpu.Store(val)
}

// SetMemoryUtilization records a measurement for the memory utilization metric.
func (c *CallMetricRecorder) SetMemoryUtilization(val float64) {
c.memory.Store(val)
}

// SetRequestCost records a measurement for a request cost metric,
// uniquely identifiable by name.
func (c *CallMetricRecorder) SetRequestCost(name string, val float64) {
c.mu.Lock()
c.requestCost[name] = val
c.mu.Unlock()
}

// SetUtilization records a measurement for a utilization metric uniquely
// identifiable by name.
func (c *CallMetricRecorder) SetUtilization(name string, val float64) {
c.mu.Lock()
c.utilization[name] = val
c.mu.Unlock()
}

// toLoadReportProto dumps the recorded measurements as an OrcaLoadReport proto.
func (c *CallMetricRecorder) toLoadReportProto() *v3orcapb.OrcaLoadReport {
c.mu.RLock()
defer c.mu.RUnlock()

cost := make(map[string]float64, len(c.requestCost))
for k, v := range c.requestCost {
cost[k] = v
}
util := make(map[string]float64, len(c.utilization))
for k, v := range c.utilization {
util[k] = v
}
cpu, _ := c.cpu.Load().(float64)
mem, _ := c.memory.Load().(float64)
return &v3orcapb.OrcaLoadReport{
CpuUtilization: cpu,
MemUtilization: mem,
RequestCost: cost,
Utilization: util,
}
}

type callMetricRecorderCtxKey struct{}

// CallMetricRecorderFromContext returns the RPC specific custom metrics
// recorder [CallMetricRecorder] embedded in the provided RPC context.
//
// Returns nil if no custom metrics recorder is found in the provided context,
// which will be the case when custom metrics reporting is not enabled.
func CallMetricRecorderFromContext(ctx context.Context) *CallMetricRecorder {
rw, ok := ctx.Value(callMetricRecorderCtxKey{}).(*recorderWrapper)
if !ok {
return nil
}
return rw.recorder()
}

func newContextWithRecorderWrapper(ctx context.Context, r *recorderWrapper) context.Context {
return context.WithValue(ctx, callMetricRecorderCtxKey{}, r)
}

// recorderWrapper is a wrapper around a CallMetricRecorder to ensures that
// concurrent calls to CallMetricRecorderFromContext() results in only one
// allocation of the underlying metric recorder.
type recorderWrapper struct {
once sync.Once
r *CallMetricRecorder
}

func (rw *recorderWrapper) recorder() *CallMetricRecorder {
rw.once.Do(func() {
rw.r = newCallMetricRecorder()
})
return rw.r
}
Loading