Skip to content

Commit

Permalink
Merge pull request #4619 from HubSpot/cache_cell_regions
Browse files Browse the repository at this point in the history
healthcheck: Cache regions in tablet_stats_cache
  • Loading branch information
sougou authored Feb 15, 2019
2 parents 19f6421 + 64c7c61 commit dd8cb89
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
19 changes: 17 additions & 2 deletions go/vt/discovery/tablet_stats_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ type TabletStatsCache struct {
entries map[string]map[string]map[topodatapb.TabletType]*tabletStatsCacheEntry
// tsm is a helper to broadcast aggregate stats.
tsm srvtopo.TargetStatsMultiplexer
// cellRegions is a cache of cell regions
cellRegions map[string]string
}

// tabletStatsCacheEntry is the per keyspace/shard/tabletType
Expand Down Expand Up @@ -134,6 +136,7 @@ func newTabletStatsCache(hc HealthCheck, ts *topo.Server, cell string, setListen
aggregatesChan: make(chan []*srvtopo.TargetStatsEntry, 100),
entries: make(map[string]map[string]map[topodatapb.TabletType]*tabletStatsCacheEntry),
tsm: srvtopo.NewTargetStatsMultiplexer(),
cellRegions: make(map[string]string),
}

if setListener {
Expand Down Expand Up @@ -194,12 +197,24 @@ func (tc *TabletStatsCache) getOrCreateEntry(target *querypb.Target) *tabletStat
}

func (tc *TabletStatsCache) getRegionByCell(cell string) string {
return topo.GetRegionByCell(context.Background(), tc.ts, cell)
tc.mu.Lock()
defer tc.mu.Unlock()

if region, ok := tc.cellRegions[cell]; ok {
return region
}

region := topo.GetRegionByCell(context.Background(), tc.ts, cell)
tc.cellRegions[cell] = region

return region
}

// StatsUpdate is part of the HealthCheckStatsListener interface.
func (tc *TabletStatsCache) StatsUpdate(ts *TabletStats) {
if ts.Target.TabletType != topodatapb.TabletType_MASTER && ts.Tablet.Alias.Cell != tc.cell && tc.getRegionByCell(ts.Tablet.Alias.Cell) != tc.getRegionByCell(tc.cell) {
if ts.Target.TabletType != topodatapb.TabletType_MASTER &&
ts.Tablet.Alias.Cell != tc.cell &&
tc.getRegionByCell(ts.Tablet.Alias.Cell) != tc.getRegionByCell(tc.cell) {
// this is for a non-master tablet in a different cell and a different region, drop it
return
}
Expand Down
5 changes: 3 additions & 2 deletions go/vt/discovery/tablet_stats_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ func TestTabletStatsCache(t *testing.T) {
// HealthCheck object, so we can't call NewTabletStatsCache.
// So we just construct this object here.
tsc := &TabletStatsCache{
cell: "cell",
entries: make(map[string]map[string]map[topodatapb.TabletType]*tabletStatsCacheEntry),
cell: "cell",
entries: make(map[string]map[string]map[topodatapb.TabletType]*tabletStatsCacheEntry),
cellRegions: make(map[string]string),
}

// empty
Expand Down

0 comments on commit dd8cb89

Please sign in to comment.