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

Commit 03bb861

Browse files
committed
Address most comments and don't shuffle disappered nodes.
1 parent 1f05169 commit 03bb861

File tree

2 files changed

+30
-29
lines changed

2 files changed

+30
-29
lines changed

partitions.go

+28-26
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type partitions struct {
2525
selected map[int]bool
2626
local map[int]bool
2727
remote map[int][]string
28-
old map[int][]string
28+
disappeared map[int][]string
2929
numMissing int
3030
ready chan bool
3131
readyClosed bool
@@ -45,7 +45,7 @@ func watchPartitions(zkWatcher *zkWatcher, peers *peers, db, version string, num
4545
replication: replication,
4646
local: make(map[int]bool),
4747
remote: make(map[int][]string),
48-
old: make(map[int][]string, 1024),
48+
disappeared: make(map[int][]string, 1024),
4949
ready: make(chan bool),
5050
}
5151

@@ -113,6 +113,19 @@ func (p *partitions) updateLocalPartitions(local map[int]bool) {
113113
}
114114
}
115115

116+
func (p *partitions) deDupe(nodes []string) []string {
117+
found := map[string]bool{}
118+
dedupedNodes := make([]string, len(nodes))
119+
for _, node := range nodes {
120+
if !found[node] {
121+
found[node] = true
122+
123+
dedupedNodes = append(dedupedNodes, node)
124+
}
125+
}
126+
return dedupedNodes
127+
}
128+
116129
func (p *partitions) updateRemotePartitions(nodes []string) {
117130
if p.peers == nil {
118131
return
@@ -131,23 +144,14 @@ func (p *partitions) updateRemotePartitions(nodes []string) {
131144
}
132145
}
133146

134-
// Keep track of old peers in case zookeeper goes away.
135147
for partitionId, partition := range remote {
136148
newPartition := make([]string, len(partition))
137149
copy(newPartition, partition)
138150

139-
unDedupedPartition := append(newPartition, p.old[partitionId]...)
140-
found := map[string]bool{}
141-
142-
// Shitty dedupe, iterate though the remote peers
143-
for _, node := range unDedupedPartition {
144-
if !found[node] {
145-
found[node] = true
146-
p.old[partitionId] = append([]string{node}, p.old[partitionId]...)
147-
}
148-
}
149-
if len(p.old[partitionId]) >= 1024 {
150-
p.old[partitionId] = p.old[partitionId][:1024]
151+
unDedupedPartition := append(newPartition, p.disappeared[partitionId]...)
152+
p.disappeared[partitionId] = p.deDupe(unDedupedPartition)
153+
if len(p.disappeared[partitionId]) >= 1024 {
154+
p.disappeared[partitionId] = p.disappeared[partitionId][:1024]
151155
}
152156
}
153157

@@ -243,23 +247,21 @@ func (p *partitions) partitionZKNode(partition int) string {
243247
}
244248

245249
// getPeers returns the list of peers who have the given partition available.
246-
func (p *partitions) getPeers(partition int) []string {
247-
if p.peers == nil {
248-
return nil
249-
}
250-
250+
func (p *partitions) getPeers(partition int) ([]string, []string) {
251251
p.lock.RLock()
252252
defer p.lock.RUnlock()
253253

254+
disappearedPeers := make([]string, 1024)
255+
copy(disappearedPeers, p.disappeared[partition])
256+
257+
if p.peers == nil {
258+
return nil, disappearedPeers
259+
}
260+
254261
peers := make([]string, len(p.remote[partition]))
255262
copy(peers, p.remote[partition])
256263

257-
oldPeers := make([]string, 1024)
258-
copy(oldPeers, p.old[partition])
259-
// Append old peers to peer list, in case of Zookeeper issues.
260-
peers = append(peers, oldPeers...)
261-
262-
return peers
264+
return peers, disappearedPeers
263265
}
264266

265267
// partitionId returns a string id for the given partition, to be used for the

serve.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,11 @@ 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
}
129-
130-
return shuffled
129+
return append(shuffled, disappeared...)
131130
}

0 commit comments

Comments
 (0)