vitess.io/vitess@v0.16.2/go/vt/srvtopo/query_srvkeyspacenames.go (about) 1 /* 2 Copyright 2021 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package srvtopo 18 19 import ( 20 "context" 21 "time" 22 23 "vitess.io/vitess/go/stats" 24 "vitess.io/vitess/go/vt/topo" 25 ) 26 27 type SrvKeyspaceNamesQuery struct { 28 rq *resilientQuery 29 } 30 31 func NewSrvKeyspaceNamesQuery(topoServer *topo.Server, counts *stats.CountersWithSingleLabel, cacheRefresh, cacheTTL time.Duration) *SrvKeyspaceNamesQuery { 32 query := func(ctx context.Context, entry *queryEntry) (any, error) { 33 cell := entry.key.(cellName) 34 return topoServer.GetSrvKeyspaceNames(ctx, string(cell)) 35 } 36 37 rq := &resilientQuery{ 38 query: query, 39 counts: counts, 40 cacheRefreshInterval: cacheRefresh, 41 cacheTTL: cacheTTL, 42 entries: make(map[string]*queryEntry), 43 } 44 45 return &SrvKeyspaceNamesQuery{rq} 46 } 47 48 func (q *SrvKeyspaceNamesQuery) GetSrvKeyspaceNames(ctx context.Context, cell string, staleOK bool) ([]string, error) { 49 v, err := q.rq.getCurrentValue(ctx, cellName(cell), staleOK) 50 names, _ := v.([]string) 51 return names, err 52 } 53 54 func (q *SrvKeyspaceNamesQuery) srvKeyspaceNamesCacheStatus() (result []*SrvKeyspaceNamesCacheStatus) { 55 q.rq.mutex.Lock() 56 defer q.rq.mutex.Unlock() 57 58 for _, entry := range q.rq.entries { 59 entry.mutex.Lock() 60 value, _ := entry.value.([]string) 61 result = append(result, &SrvKeyspaceNamesCacheStatus{ 62 Cell: entry.key.String(), 63 Value: value, 64 ExpirationTime: entry.insertionTime.Add(q.rq.cacheTTL), 65 LastQueryTime: entry.lastQueryTime, 66 LastError: entry.lastError, 67 }) 68 entry.mutex.Unlock() 69 } 70 return 71 }