Skip to content
This repository was archived by the owner on Mar 5, 2024. It is now read-only.

Commit e18c85e

Browse files
committed
Rebase master onto this branch
1 parent eaf6f9c commit e18c85e

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

serve.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,12 @@ func (vs *version) serveError(w http.ResponseWriter, key string, err error) {
120120
w.WriteHeader(http.StatusInternalServerError)
121121
}
122122

123-
func shuffle(vs []string) []string {
123+
func shuffle(vs []string, disappeared []string) []string {
124124
shuffled := make([]string, len(vs))
125125
perm := rand.Perm(len(vs))
126126
for i, v := range perm {
127127
shuffled[v] = vs[i]
128128
}
129129

130-
return shuffled
130+
return append(shuffled, disappeared...)
131131
}

sharding/partitions.go

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type Partitions struct {
3333
selected map[int]bool
3434
local map[int]bool
3535
remote map[int][]string
36+
disappeared map[int][]string
3637
numMissing int
3738
readyClosed bool
3839
shouldAdvertise bool
@@ -55,6 +56,7 @@ func WatchPartitions(zkWatcher *zk.Watcher, peers *Peers, db, version string, nu
5556
replication: replication,
5657
local: make(map[int]bool),
5758
remote: make(map[int][]string),
59+
disappeared: make(map[int][]string, 1024),
5860
}
5961

6062
p.pickLocal()
@@ -69,6 +71,19 @@ func WatchPartitions(zkWatcher *zk.Watcher, peers *Peers, db, version string, nu
6971
return p
7072
}
7173

74+
// Dedupelicates elements in a slice of strings.
75+
func dedupe(nodes []string) []string {
76+
found := map[string]bool{}
77+
dedupedNodes := make([]string, 0, len(nodes))
78+
for _, node := range nodes {
79+
if !found[node] {
80+
found[node] = true
81+
dedupedNodes = append(dedupedNodes, node)
82+
}
83+
}
84+
return dedupedNodes
85+
}
86+
7287
// pickLocal selects which partitions are local by iterating through
7388
// them all, and checking the hashring to see if this peer is one of the
7489
// replicas.
@@ -107,17 +122,20 @@ func (p *Partitions) sync(updates chan []string) {
107122
}
108123

109124
// FindPeers returns the list of peers who have the given partition available.
110-
func (p *Partitions) FindPeers(partition int) []string {
111-
if p.peers == nil {
112-
return nil
113-
}
114-
125+
func (p *Partitions) FindPeers(partition int) ([]string, []string) {
115126
p.lock.RLock()
116127
defer p.lock.RUnlock()
117128

129+
disappearedPeers := make([]string, 1024)
130+
copy(disappearedPeers, p.disappeared[partition])
131+
132+
if p.peers == nil {
133+
return nil, disappearedPeers
134+
}
135+
118136
peers := make([]string, len(p.remote[partition]))
119137
copy(peers, p.remote[partition])
120-
return peers
138+
return peers, disappearedPeers
121139
}
122140

123141
// Update updates the list of local partitions to the given list.
@@ -228,6 +246,25 @@ func (p *Partitions) updateRemote(nodes []string) {
228246
}
229247
}
230248

249+
for partitionId, partition := range p.remote {
250+
disappearedPeers := make([]string, len(partition))
251+
for _, oldPeer := range partition {
252+
found := false
253+
for _, newPeer := range remote[partitionId] {
254+
if newPeer == oldPeer {
255+
found = true
256+
}
257+
}
258+
if !found {
259+
disappearedPeers = append(disappearedPeers, oldPeer)
260+
}
261+
}
262+
p.disappeared[partitionId] = dedupe(append(disappearedPeers, p.disappeared[partitionId]...))
263+
if len(p.disappeared[partitionId]) >= 1024 {
264+
p.disappeared[partitionId] = p.disappeared[partitionId][:1024]
265+
}
266+
}
267+
231268
p.remote = remote
232269
p.updateMissing()
233270
}

0 commit comments

Comments
 (0)