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

healthcheck: Cache regions in tablet_stats_cache #4619

Merged
merged 3 commits into from
Feb 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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