// getTablets gets all available tablets from HealthCheck, // and selects the usable ones based several rules: // master - return one from any cells with latest reparent timestamp; // replica - return all from local cell. func (dg *discoveryGateway) getTablets(keyspace, shard string, tabletType topodatapb.TabletType) []*discovery.TabletStats { tsList := dg.hc.GetTabletStatsFromTarget(keyspace, shard, tabletType) // for master, use any cells and return the one with max reparent timestamp. if tabletType == topodatapb.TabletType_MASTER { var maxTimestamp int64 var t *discovery.TabletStats for _, ts := range tsList { if ts.LastError != nil || !ts.Serving { continue } if ts.TabletExternallyReparentedTimestamp >= maxTimestamp { maxTimestamp = ts.TabletExternallyReparentedTimestamp t = ts } } if t == nil { return nil } return []*discovery.TabletStats{t} } // for non-master, use only tablets from local cell and filter by replication lag. list := make([]*discovery.TabletStats, 0, len(tsList)) for _, ts := range tsList { if ts.LastError != nil || !ts.Serving { continue } if dg.localCell != ts.Tablet.Alias.Cell { continue } list = append(list, ts) } list = discovery.FilterByReplicationLag(list) return list }
// getEndPoints gets all available endpoints from HealthCheck, // and selects the usable ones based several rules: // master - return one from any cells with latest reparent timestamp; // replica - return all from local cell. // TODO(liang): select replica by replication lag. func (dg *discoveryGateway) getEndPoints(keyspace, shard string, tabletType pbt.TabletType) []*pbt.EndPoint { epsList := dg.hc.GetEndPointStatsFromTarget(keyspace, shard, tabletType) // for master, use any cells and return the one with max reparent timestamp. if tabletType == pbt.TabletType_MASTER { var maxTimestamp int64 var ep *pbt.EndPoint for _, eps := range epsList { if eps.LastError != nil || !eps.Serving { continue } if eps.TabletExternallyReparentedTimestamp >= maxTimestamp { maxTimestamp = eps.TabletExternallyReparentedTimestamp ep = eps.EndPoint } } if ep == nil { return nil } return []*pbt.EndPoint{ep} } // for non-master, use only endpoints from local cell and filter by replication lag. list := make([]*discovery.EndPointStats, 0, len(epsList)) for _, eps := range epsList { if eps.LastError != nil || !eps.Serving { continue } if dg.localCell != eps.Cell { continue } list = append(list, eps) } list = discovery.FilterByReplicationLag(list) epList := make([]*pbt.EndPoint, 0, len(list)) for _, eps := range list { epList = append(epList, eps.EndPoint) } return epList }