CountTokens returns the number tokens within the range for each instance. In case of zone-awareness, this method takes into account only tokens of the same zone. More precisely, for each instance only the distance between its tokens and tokens of the instances from the same zone will be considered.
()
| 811 | // the same zone. More precisely, for each instance only the distance between |
| 812 | // its tokens and tokens of the instances from the same zone will be considered. |
| 813 | func (r *Desc) CountTokens() map[string]int64 { |
| 814 | var ( |
| 815 | owned = make(map[string]int64, len(r.Ingesters)) |
| 816 | ringTokensByZone = r.getTokensByZone() |
| 817 | ringInstanceByToken = r.getTokensInfo() |
| 818 | ) |
| 819 | |
| 820 | for _, ringTokens := range ringTokensByZone { |
| 821 | for i, token := range ringTokens { |
| 822 | var prevToken uint32 |
| 823 | |
| 824 | // Compute how many tokens are within the range. |
| 825 | if i == 0 { |
| 826 | prevToken = ringTokens[len(ringTokens)-1] |
| 827 | } else { |
| 828 | prevToken = ringTokens[i-1] |
| 829 | } |
| 830 | |
| 831 | diff := tokenDistance(prevToken, token) |
| 832 | info := ringInstanceByToken[token] |
| 833 | owned[info.InstanceID] = owned[info.InstanceID] + diff |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | // Set to 0 the number of owned tokens by instances which don't have tokens yet. |
| 838 | for id := range r.Ingesters { |
| 839 | if _, ok := owned[id]; !ok { |
| 840 | owned[id] = 0 |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | return owned |
| 845 | } |
| 846 | |
| 847 | func (r *Ring) updateRingZones(zones []string) { |
| 848 | r.ringZones = zones |