measure remote grain latency
All checks were successful
Build and Publish / BuildAndDeploy (push) Successful in 1m51s
All checks were successful
Build and Publish / BuildAndDeploy (push) Successful in 1m51s
This commit is contained in:
31
rpc-pool.go
31
rpc-pool.go
@@ -4,6 +4,10 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||||
)
|
)
|
||||||
|
|
||||||
type RemoteGrainPool struct {
|
type RemoteGrainPool struct {
|
||||||
@@ -41,13 +45,36 @@ func NewRemoteGrain(id CartId, host string) (*RemoteGrain, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
remoteCartLatency = promauto.NewCounter(prometheus.CounterOpts{
|
||||||
|
Name: "cart_remote_grain_calls_total_latency",
|
||||||
|
Help: "The total latency of remote grains",
|
||||||
|
})
|
||||||
|
remoteCartCallsTotal = promauto.NewCounter(prometheus.CounterOpts{
|
||||||
|
Name: "cart_remote_grain_calls_total",
|
||||||
|
Help: "The total number of calls to remote grains",
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
func MeasureLatency(fn func() ([]byte, error)) ([]byte, error) {
|
||||||
|
start := time.Now()
|
||||||
|
data, err := fn()
|
||||||
|
if err != nil {
|
||||||
|
return data, err
|
||||||
|
}
|
||||||
|
elapsed := time.Since(start).Milliseconds()
|
||||||
|
remoteCartLatency.Add(float64(elapsed))
|
||||||
|
remoteCartCallsTotal.Inc()
|
||||||
|
return data, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (g *RemoteGrain) HandleMessage(message *Message, isReplay bool) ([]byte, error) {
|
func (g *RemoteGrain) HandleMessage(message *Message, isReplay bool) ([]byte, error) {
|
||||||
|
|
||||||
data, err := GetData(message.Write)
|
data, err := GetData(message.Write)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
reply, err := g.Call(RemoteHandleMutation, g.Id, RemoteHandleMutationReply, data)
|
reply, err := MeasureLatency(func() ([]byte, error) { return g.Call(RemoteHandleMutation, g.Id, RemoteHandleMutationReply, data) })
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -61,7 +88,7 @@ func (g *RemoteGrain) GetId() CartId {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (g *RemoteGrain) GetCurrentState() ([]byte, error) {
|
func (g *RemoteGrain) GetCurrentState() ([]byte, error) {
|
||||||
return g.Call(RemoteGetState, g.Id, RemoteGetStateReply, []byte{})
|
return MeasureLatency(func() ([]byte, error) { return g.Call(RemoteGetState, g.Id, RemoteGetStateReply, []byte{}) })
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRemoteGrainPool(addr string) *RemoteGrainPool {
|
func NewRemoteGrainPool(addr string) *RemoteGrainPool {
|
||||||
|
|||||||
@@ -41,7 +41,9 @@ func (h *GrainHandler) RemoteHandleMessageHandler(id CartId, data []byte) (uint3
|
|||||||
fmt.Println("Error reading message:", err)
|
fmt.Println("Error reading message:", err)
|
||||||
return RemoteHandleMutationReply, nil, err
|
return RemoteHandleMutationReply, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
replyData, err := h.pool.Process(id, msg)
|
replyData, err := h.pool.Process(id, msg)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error handling message:", err)
|
fmt.Println("Error handling message:", err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user