Skip to content

Commit cd0adbb

Browse files
authored
Add flag to divert V2 traffic to new backend (#1734)
This is to help understand latency (especially website) on dev(/tbd new test instance) Currently only set to divert Node and Observation, since those are the only ones with feature parity at the moment Set to always use Spanner for local
1 parent 5cb23d4 commit cd0adbb

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

deploy/featureflags/local.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ flags:
33
V3MirrorFraction: 1.0
44
UseSpannerGraph: true
55
UseStaleReads: true
6+
V2DivertFraction: 1.0

internal/featureflags/featureflags.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ type Flags struct {
3838
UseStaleReads bool `yaml:"UseStaleReads"`
3939
// Whether to enable the embeddings resolver.
4040
EnableEmbeddingsResolver bool `yaml:"EnableEmbeddingsResolver"`
41+
// Fraction of V2 API requests to divert to the new dispatcher backend. Value from 0 to 1.0.
42+
V2DivertFraction float64 `yaml:"V2DivertFraction"`
4143
}
4244

4345
// setDefaultValues creates a new Flags struct with default values.
@@ -49,6 +51,7 @@ func setDefaultValues() *Flags {
4951
SpannerGraphDatabase: "",
5052
UseStaleReads: false,
5153
EnableEmbeddingsResolver: false,
54+
V2DivertFraction: 0.0,
5255
}
5356
}
5457

@@ -66,6 +69,12 @@ func (f *Flags) validateFlagValues() error {
6669
if f.UseStaleReads && !f.UseSpannerGraph {
6770
return fmt.Errorf("UseStaleReads requires UseSpannerGraph to be true")
6871
}
72+
if f.V2DivertFraction < 0 || f.V2DivertFraction > 1.0 {
73+
return fmt.Errorf("V2DivertFraction must be between 0 and 1.0, got %f", f.V2DivertFraction)
74+
}
75+
if f.V2DivertFraction > 0 && !f.EnableV3 {
76+
return fmt.Errorf("V2DivertFraction > 0 requires EnableV3 to be true")
77+
}
6978
return nil
7079
}
7180

internal/server/handler_v2.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ package server
1818
import (
1919
"context"
2020
"log/slog"
21+
"math/rand"
2122
"time"
2223

2324
"github.com/datacommonsorg/mixer/internal/log"
2425
"github.com/datacommonsorg/mixer/internal/merger"
2526
pb "github.com/datacommonsorg/mixer/internal/proto"
2627
pbv2 "github.com/datacommonsorg/mixer/internal/proto/v2"
28+
"github.com/datacommonsorg/mixer/internal/server/datasources"
2729
"github.com/datacommonsorg/mixer/internal/server/pagination"
2830
"github.com/datacommonsorg/mixer/internal/server/statvar/search"
2931
"github.com/datacommonsorg/mixer/internal/server/translator"
@@ -105,6 +107,11 @@ func (s *Server) V2Resolve(
105107
func (s *Server) V2Node(ctx context.Context, in *pbv2.NodeRequest) (
106108
*pbv2.NodeResponse, error,
107109
) {
110+
if rand.Float64() < s.flags.V2DivertFraction {
111+
slog.Info("V2Node request diverted to dispatcher backend", "request", in)
112+
return s.dispatcher.Node(ctx, in, datasources.DefaultPageSize)
113+
}
114+
108115
v2StartTime := time.Now()
109116
errGroup, errCtx := errgroup.WithContext(ctx)
110117
localRespChan := make(chan *pbv2.NodeResponse, 1)
@@ -260,6 +267,11 @@ func (s *Server) V2Event(
260267
func (s *Server) V2Observation(
261268
ctx context.Context, in *pbv2.ObservationRequest,
262269
) (*pbv2.ObservationResponse, error) {
270+
if rand.Float64() < s.flags.V2DivertFraction {
271+
slog.Info("V2Observation request diverted to dispatcher backend", "request", in)
272+
return s.dispatcher.Observation(ctx, in)
273+
}
274+
263275
v2StartTime := time.Now()
264276

265277
surface, toRemote := util.GetMetadata(ctx)

0 commit comments

Comments
 (0)