Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change dedup to use one key and do auto-cleanup of the dedup kv store #1168

Merged
merged 1 commit into from
Jul 18, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions manager/dedup.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ const (
// listRetry is the interval on which we retry listing a data path
listRetry = 10 * time.Second

// templateDataFlag is added as a flag to the shared data values
// so that we can use it as a sanity check
templateDataFlag = 0x22b9a127a2c03520
templateNoDataStr = "__NO_DATA__"
)

// templateData is GOB encoded share the dependency values
Expand All @@ -46,6 +44,10 @@ type templateData struct {
Data map[string]interface{}
}

func templateNoData() []byte {
return []byte(templateNoDataStr)
}

// DedupManager is used to de-duplicate which instance of Consul-Template
// is handling each template. For each template, a lock path is determined
// using the MD5 of the template. This path is used to elect a "leader"
Expand Down Expand Up @@ -148,9 +150,10 @@ START:
sessionCh := make(chan struct{})
ttl := fmt.Sprintf("%.6fs", float64(*d.config.TTL)/float64(time.Second))
se := &consulapi.SessionEntry{
Name: "Consul-Template de-duplication",
Behavior: "delete",
TTL: ttl,
Name: "Consul-Template de-duplication",
Behavior: "delete",
TTL: ttl,
LockDelay: 1 * time.Millisecond,
}
id, _, err := session.Create(se, nil)
if err != nil {
Expand Down Expand Up @@ -251,7 +254,7 @@ func (d *DedupManager) UpdateDeps(t *template.Template, deps []dep.Dependency) e
kvPair := consulapi.KVPair{
Key: dataPath,
Value: buf.Bytes(),
Flags: templateDataFlag,
Flags: consulapi.LockFlagValue,
}
client := d.clients.Consul()
if _, err := client.KV().Put(&kvPair, nil); err != nil {
Expand Down Expand Up @@ -403,7 +406,7 @@ START:
}

// Parse the data file
if pair != nil && pair.Flags == templateDataFlag {
if pair != nil && pair.Flags == consulapi.LockFlagValue && !bytes.Equal(pair.Value, templateNoData()) {
d.parseData(pair.Key, pair.Value)
}
goto START
Expand Down Expand Up @@ -450,7 +453,8 @@ func (d *DedupManager) attemptLock(client *consulapi.Client, session string, ses
log.Printf("[INFO] (dedup) attempting lock for template hash %s", t.ID())
basePath := path.Join(*d.config.Prefix, t.ID())
lopts := &consulapi.LockOptions{
Key: path.Join(basePath, "lock"),
Key: path.Join(basePath, "data"),
Value: templateNoData(),
Session: session,
MonitorRetries: 3,
MonitorRetryTime: 3 * time.Second,
Expand Down Expand Up @@ -484,11 +488,17 @@ func (d *DedupManager) attemptLock(client *consulapi.Client, session string, ses
case <-sessionCh:
log.Printf("[INFO] (dedup) releasing session '%s'", lopts.Key)
d.setLeader(t, nil)
lock.Unlock()
_, err = client.Session().Destroy(session, nil)
if err != nil {
log.Printf("[ERROR] (dedup) failed destroying session '%s', %s", session, err)
}
return
case <-d.stopCh:
log.Printf("[INFO] (dedup) releasing lock '%s'", lopts.Key)
lock.Unlock()
_, err = client.Session().Destroy(session, nil)
if err != nil {
log.Printf("[ERROR] (dedup) failed destroying session '%s', %s", session, err)
}
return
}
}
Expand Down