Skip to content
This repository was archived by the owner on Sep 28, 2022. It is now read-only.

Commit 8e2f334

Browse files
committed
use shardnode cluster change detector w/ cleanup
1 parent a930004 commit 8e2f334

File tree

3 files changed

+83
-32
lines changed

3 files changed

+83
-32
lines changed

Diff for: client.go

+17-31
Original file line numberDiff line numberDiff line change
@@ -90,37 +90,11 @@ type Client struct {
9090
importLogEncoder encoder
9191
logLock sync.Mutex
9292

93-
// TODO shardNodes needs to be invalidated/updated when cluster topology changes.
9493
shardNodes shardNodes
9594
tick *time.Ticker
95+
done chan struct{}
9696
}
9797

98-
// func (c *Client) translateCol(index, key string) (uint64, bool) {
99-
// c.tlock.RLock()
100-
// v, b := c.translator.GetCol(index, key)
101-
// c.tlock.RUnlock()
102-
// return v, b
103-
// }
104-
105-
// func (c *Client) translateRow(index, field, key string) (uint64, bool) {
106-
// c.tlock.RLock()
107-
// v, b := c.translator.GetRow(index, field, key)
108-
// c.tlock.RUnlock()
109-
// return v, b
110-
// }
111-
112-
// func (c *Client) addTranslateCol(index, key string, value uint64) {
113-
// c.tlock.Lock()
114-
// c.translator.AddCol(index, key, value)
115-
// c.tlock.Unlock()
116-
// }
117-
118-
// func (c *Client) addTranslateRow(index, field, key string, value uint64) {
119-
// c.tlock.Lock()
120-
// c.translator.AddRow(index, field, key, value)
121-
// c.tlock.Unlock()
122-
// }
123-
12498
func (c *Client) getURIsForShard(index string, shard uint64) ([]*URI, error) {
12599
uris, ok := c.shardNodes.Get(index, shard)
126100
if ok {
@@ -139,13 +113,22 @@ func (c *Client) getURIsForShard(index string, shard uint64) ([]*URI, error) {
139113
}
140114

141115
func (c *Client) runChangeDetection() {
142-
c.tick = time.NewTicker(time.Minute)
143-
144-
for range c.tick.C {
145-
c.detectClusterChanges()
116+
for {
117+
select {
118+
case <-c.tick.C:
119+
c.detectClusterChanges()
120+
case <-c.done:
121+
return
122+
}
146123
}
147124
}
148125

126+
func (c *Client) Close() error {
127+
c.tick.Stop()
128+
close(c.done)
129+
return nil
130+
}
131+
149132
// detectClusterChanges chooses a random index and shard from the
150133
// shardNodes cache and deletes it. It then looks it up from Pilosa to
151134
// see if it still matches, and if not it drops the whole cache.
@@ -232,6 +215,8 @@ func newClientWithOptions(options *ClientOptions) *Client {
232215
coordinatorLock: &sync.RWMutex{},
233216

234217
shardNodes: newShardNodes(),
218+
tick: time.NewTicker(time.Minute),
219+
done: make(chan struct{}, 0),
235220
}
236221
if options.importLogWriter != nil {
237222
c.importLogEncoder = newImportLogEncoder(options.importLogWriter)
@@ -245,6 +230,7 @@ func newClientWithOptions(options *ClientOptions) *Client {
245230
c.minRetrySleepTime = 1 * time.Second
246231
c.maxRetrySleepTime = 2 * time.Minute
247232
c.importManager = newRecordImportManager(c)
233+
go c.runChangeDetection()
248234
return c
249235

250236
}

Diff for: client_internal_it_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func TestImportWithReplayErrors(t *testing.T) {
174174

175175
func TestDetectClusterChanges(t *testing.T) {
176176
c := getClient()
177-
177+
defer c.Close()
178178
c.shardNodes.data["blah"] = make(map[uint64][]*URI)
179179
c.shardNodes.data["blah"][1] = []*URI{{scheme: "zzz"}}
180180

0 commit comments

Comments
 (0)