@@ -335,7 +335,7 @@ func TestVStreamMulti(t *testing.T) {
335335 }
336336}
337337
338- func TestVStreamsCreatedAndLagMetrics (t * testing.T ) {
338+ func TestVStreamsMetrics (t * testing.T ) {
339339 ctx , cancel := context .WithCancel (context .Background ())
340340 defer cancel ()
341341 cell := "aa"
@@ -346,9 +346,11 @@ func TestVStreamsCreatedAndLagMetrics(t *testing.T) {
346346 vsm := newTestVStreamManager (ctx , hc , st , cell )
347347 vsm .vstreamsCreated .ResetAll ()
348348 vsm .vstreamsLag .ResetAll ()
349- sbc0 := hc .AddTestTablet (cell , "1.1.1.1" , 1001 , ks , "-20" , topodatapb .TabletType_PRIMARY , true , 1 , nil )
349+ hostname1 := "host1"
350+ hostname2 := "host2"
351+ sbc0 := hc .AddTestTablet (cell , hostname1 , 1001 , ks , "-20" , topodatapb .TabletType_PRIMARY , true , 1 , nil )
350352 addTabletToSandboxTopo (t , ctx , st , ks , "-20" , sbc0 .Tablet ())
351- sbc1 := hc .AddTestTablet (cell , "1.1.1.1" , 1002 , ks , "20-40" , topodatapb .TabletType_PRIMARY , true , 1 , nil )
353+ sbc1 := hc .AddTestTablet (cell , hostname2 , 1002 , ks , "20-40" , topodatapb .TabletType_PRIMARY , true , 1 , nil )
352354 addTabletToSandboxTopo (t , ctx , st , ks , "20-40" , sbc1 .Tablet ())
353355
354356 send0 := []* binlogdatapb.VEvent {
@@ -377,15 +379,96 @@ func TestVStreamsCreatedAndLagMetrics(t *testing.T) {
377379 ch := startVStream (ctx , t , vsm , vgtid , nil )
378380 <- ch
379381 <- ch
382+ expectedLabels1Prefix := "TestVStream.-20.PRIMARY"
383+ expectedLabels2Prefix := "TestVStream.20-40.PRIMARY"
384+ expectedLabels1 := expectedLabels1Prefix + "." + hostname1
385+ expectedLabels2 := expectedLabels2Prefix + "." + hostname2
380386 wantVStreamsCreated := make (map [string ]int64 )
381- wantVStreamsCreated ["TestVStream.-20.PRIMARY" ] = 1
382- wantVStreamsCreated ["TestVStream.20-40.PRIMARY" ] = 1
387+ wantVStreamsCreated [expectedLabels1 ] = 1
388+ wantVStreamsCreated [expectedLabels2 ] = 1
383389 assert .Equal (t , wantVStreamsCreated , vsm .vstreamsCreated .Counts (), "vstreamsCreated matches" )
384390
385391 wantVStreamsLag := make (map [string ]int64 )
386- wantVStreamsLag ["TestVStream.-20.PRIMARY" ] = 5
387- wantVStreamsLag ["TestVStream.20-40.PRIMARY" ] = 7
392+ wantVStreamsLag [expectedLabels1 ] = 5
393+ wantVStreamsLag [expectedLabels2 ] = 7
388394 assert .Equal (t , wantVStreamsLag , vsm .vstreamsLag .Counts (), "vstreamsLag matches" )
395+
396+ wantVStreamsCount := make (map [string ]int64 )
397+ wantVStreamsCount [expectedLabels1 ] = 1
398+ wantVStreamsCount [expectedLabels2 ] = 1
399+ assert .Equal (t , wantVStreamsCount , vsm .vstreamsCount .Counts (), "vstreamsCount matches" )
400+
401+ wantVStreamsEventsStreamed := make (map [string ]int64 )
402+ wantVStreamsEventsStreamed [expectedLabels1 ] = 2
403+ wantVStreamsEventsStreamed [expectedLabels2 ] = 2
404+ assert .Equal (t , wantVStreamsEventsStreamed , vsm .vstreamsEventsStreamed .Counts (), "vstreamsEventsStreamed matches" )
405+
406+ wantVStreamsEndedWithErrors := make (map [string ]int64 )
407+ wantVStreamsEndedWithErrors [expectedLabels1Prefix ] = 0
408+ wantVStreamsEndedWithErrors [expectedLabels2Prefix ] = 0
409+ assert .Equal (t , wantVStreamsEndedWithErrors , vsm .vstreamsEndedWithErrors .Counts (), "vstreamsEndedWithErrors matches" )
410+ }
411+
412+ func TestVStreamsMetricsErrors (t * testing.T ) {
413+ ctx , cancel := context .WithCancel (context .Background ())
414+ defer cancel ()
415+ cell := "aa"
416+ ks := "TestVStream"
417+ _ = createSandbox (ks )
418+ hc := discovery .NewFakeHealthCheck (nil )
419+ st := getSandboxTopo (ctx , cell , ks , []string {"-20" , "20-40" })
420+ vsm := newTestVStreamManager (ctx , hc , st , cell )
421+ vsm .vstreamsCreated .ResetAll ()
422+ vsm .vstreamsLag .ResetAll ()
423+ hostname1 := "host1"
424+ hostname2 := "host2"
425+ sbc0 := hc .AddTestTablet (cell , hostname1 , 1001 , ks , "-20" , topodatapb .TabletType_PRIMARY , true , 1 , nil )
426+ addTabletToSandboxTopo (t , ctx , st , ks , "-20" , sbc0 .Tablet ())
427+ sbc1 := hc .AddTestTablet (cell , hostname2 , 1002 , ks , "20-40" , topodatapb .TabletType_PRIMARY , true , 1 , nil )
428+ addTabletToSandboxTopo (t , ctx , st , ks , "20-40" , sbc1 .Tablet ())
429+
430+ const wantErr = "Invalid arg message"
431+ sbc0 .AddVStreamEvents (nil , vterrors .Errorf (vtrpcpb .Code_INVALID_ARGUMENT , wantErr ))
432+
433+ send1 := []* binlogdatapb.VEvent {
434+ {Type : binlogdatapb .VEventType_GTID , Gtid : "gtid02" },
435+ {Type : binlogdatapb .VEventType_COMMIT , Timestamp : 10 , CurrentTime : 17 * 1e9 },
436+ }
437+ sbc1 .AddVStreamEvents (send1 , nil )
438+
439+ vgtid := & binlogdatapb.VGtid {
440+ ShardGtids : []* binlogdatapb.ShardGtid {{
441+ Keyspace : ks ,
442+ Shard : "-20" ,
443+ Gtid : "pos" ,
444+ }, {
445+ Keyspace : ks ,
446+ Shard : "20-40" ,
447+ Gtid : "pos" ,
448+ }},
449+ }
450+ ch := make (chan * binlogdatapb.VStreamResponse )
451+ done := make (chan struct {})
452+ go func () {
453+ err := vsm .VStream (ctx , topodatapb .TabletType_PRIMARY , vgtid , nil , & vtgatepb.VStreamFlags {}, func (events []* binlogdatapb.VEvent ) error {
454+ ch <- & binlogdatapb.VStreamResponse {Events : events }
455+ return nil
456+ })
457+
458+ if err == nil || ! strings .Contains (err .Error (), wantErr ) {
459+ t .Errorf ("vstream end: %v, must contain %v" , err .Error (), wantErr )
460+ }
461+ close (done )
462+ }()
463+ <- done
464+
465+ expectedLabels1 := "TestVStream.-20.PRIMARY"
466+ expectedLabels2 := "TestVStream.20-40.PRIMARY"
467+
468+ wantVStreamsEndedWithErrors := make (map [string ]int64 )
469+ wantVStreamsEndedWithErrors [expectedLabels1 ] = 1
470+ wantVStreamsEndedWithErrors [expectedLabels2 ] = 1
471+ assert .Equal (t , wantVStreamsEndedWithErrors , vsm .vstreamsEndedWithErrors .Counts (), "vstreamsEndedWithErrors matches" )
389472}
390473
391474func TestVStreamRetriableErrors (t * testing.T ) {
0 commit comments