This repository has been archived by the owner on Aug 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
routing_table.go
151 lines (121 loc) · 2.55 KB
/
routing_table.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package routing
import (
"context"
"sort"
"sync"
rpc "code.cloudfoundry.org/log-cache/pkg/rpc/logcache_v1"
)
// RoutingTable makes decisions for where a item should be routed.
type RoutingTable struct {
mu sync.RWMutex
addrs map[string]int
h func(string) uint64
latestTerm uint64
table []rangeInfo
}
// NewRoutingTable returns a new RoutingTable.
func NewRoutingTable(addrs []string, hasher func(string) uint64) *RoutingTable {
a := make(map[string]int)
for i, addr := range addrs {
a[addr] = i
}
return &RoutingTable{
addrs: a,
h: hasher,
}
}
// Lookup takes a item, hash it and determine what node it should be
// routed to.
func (t *RoutingTable) Lookup(item string) []int {
h := t.h(item)
t.mu.RLock()
defer t.mu.RUnlock()
var result []int
for _, r := range t.table {
if h < r.r.Start || h > r.r.End {
// Outside of range
continue
}
result = append(result, r.idx)
}
return result
}
// LookupAll returns every index that has a range where the item would
// fall under.
func (t *RoutingTable) LookupAll(item string) []int {
h := t.h(item)
t.mu.RLock()
defer t.mu.RUnlock()
var result []int
ranges := t.table
for {
i := t.findRange(h, ranges)
if i < 0 {
break
}
result = append(result, ranges[i].idx)
ranges = ranges[i+1:]
}
return result
}
// SetRanges sets the routing table.
func (t *RoutingTable) SetRanges(ctx context.Context, in *rpc.SetRangesRequest) (*rpc.SetRangesResponse, error) {
t.mu.Lock()
defer t.mu.Unlock()
t.table = nil
for addr, ranges := range in.Ranges {
for _, r := range ranges.Ranges {
var sr Range
sr.CloneRpcRange(r)
t.table = append(t.table, rangeInfo{
idx: t.addrs[addr],
r: sr,
})
}
}
sort.Sort(rangeInfos(t.table))
return &rpc.SetRangesResponse{}, nil
}
func (t *RoutingTable) findRange(h uint64, rs []rangeInfo) int {
for i, r := range rs {
if h < r.r.Start || h > r.r.End {
// Outside of range
continue
}
return i
}
return -1
}
type Range struct {
Start uint64
End uint64
}
func (sr *Range) CloneRpcRange(r *rpc.Range) {
sr.Start = r.Start
sr.End = r.End
}
func (sr *Range) ToRpcRange() *rpc.Range {
return &rpc.Range{
Start: sr.Start,
End: sr.End,
}
}
type rangeInfo struct {
r Range
idx int
}
type rangeInfos []rangeInfo
func (r rangeInfos) Len() int {
return len(r)
}
func (r rangeInfos) Less(i, j int) bool {
if r[i].r.Start == r[j].r.Start {
return r[i].idx > r[j].idx
}
return r[i].r.Start < r[j].r.Start
}
func (r rangeInfos) Swap(i, j int) {
tmp := r[i]
r[i] = r[j]
r[j] = tmp
}