mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
fix iszero
This commit is contained in:
parent
3f6d6cf4c6
commit
c5a6f9bb6b
@ -54,8 +54,8 @@ func TestHeapUpdate(t *testing.T) {
|
|||||||
// Path 5
|
// Path 5
|
||||||
kvs[5].ExpireTime = time.Now().Add(time.Second * 12)
|
kvs[5].ExpireTime = time.Now().Add(time.Second * 12)
|
||||||
|
|
||||||
h.Update(kvs[3])
|
h.update(kvs[3])
|
||||||
h.Update(kvs[5])
|
h.update(kvs[5])
|
||||||
|
|
||||||
min := time.Now()
|
min := time.Now()
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package store
|
package store
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"container/heap"
|
||||||
"path"
|
"path"
|
||||||
"sort"
|
"sort"
|
||||||
"sync"
|
"sync"
|
||||||
@ -95,7 +96,7 @@ func (n *Node) IsHidden() bool {
|
|||||||
|
|
||||||
// IsPermanent function checks if the node is a permanent one.
|
// IsPermanent function checks if the node is a permanent one.
|
||||||
func (n *Node) IsPermanent() bool {
|
func (n *Node) IsPermanent() bool {
|
||||||
return !n.ExpireTime.IsZero()
|
return n.ExpireTime.IsZero()
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsExpired function checks if the node has been expired.
|
// IsExpired function checks if the node has been expired.
|
||||||
@ -144,7 +145,7 @@ func (n *Node) Write(value string, index uint64, term uint64) *etcdErr.Error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (n *Node) ExpirationAndTTL() (*time.Time, int64) {
|
func (n *Node) ExpirationAndTTL() (*time.Time, int64) {
|
||||||
if n.IsPermanent() {
|
if !n.IsPermanent() {
|
||||||
return &n.ExpireTime, int64(n.ExpireTime.Sub(time.Now())/time.Second) + 1
|
return &n.ExpireTime, int64(n.ExpireTime.Sub(time.Now())/time.Second) + 1
|
||||||
}
|
}
|
||||||
return nil, 0
|
return nil, 0
|
||||||
@ -239,6 +240,10 @@ func (n *Node) internalRemove(recursive bool, callback func(path string)) {
|
|||||||
callback(n.Path)
|
callback(n.Path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !n.IsPermanent() {
|
||||||
|
n.store.TTLKeyHeap.remove(n)
|
||||||
|
}
|
||||||
|
|
||||||
// the stop channel has a buffer. just send to it!
|
// the stop channel has a buffer. just send to it!
|
||||||
n.stopExpire <- true
|
n.stopExpire <- true
|
||||||
return
|
return
|
||||||
@ -257,6 +262,10 @@ func (n *Node) internalRemove(recursive bool, callback func(path string)) {
|
|||||||
callback(n.Path)
|
callback(n.Path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !n.IsPermanent() {
|
||||||
|
n.store.TTLKeyHeap.remove(n)
|
||||||
|
}
|
||||||
|
|
||||||
n.stopExpire <- true
|
n.stopExpire <- true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -362,6 +371,26 @@ func (n *Node) Pair(recurisive, sorted bool) KeyValuePair {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (n *Node) UpdateTTL(expireTime time.Time) {
|
func (n *Node) UpdateTTL(expireTime time.Time) {
|
||||||
|
|
||||||
|
if !n.IsPermanent() {
|
||||||
|
if expireTime.IsZero() {
|
||||||
|
// from ttl to permanent
|
||||||
|
// remove from ttl heap
|
||||||
|
n.store.TTLKeyHeap.remove(n)
|
||||||
|
} else {
|
||||||
|
// update ttl
|
||||||
|
// update ttl heap
|
||||||
|
n.store.TTLKeyHeap.update(n)
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
if !expireTime.IsZero() {
|
||||||
|
// from permanent to ttl
|
||||||
|
// push into ttl heap
|
||||||
|
heap.Push(n.store.TTLKeyHeap, n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if !n.IsPermanent() {
|
if !n.IsPermanent() {
|
||||||
// check if the node has been expired
|
// check if the node has been expired
|
||||||
// if the node is not expired, we need to stop the go routine associated with
|
// if the node is not expired, we need to stop the go routine associated with
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package store
|
package store
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"container/heap"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"path"
|
"path"
|
||||||
@ -393,6 +394,8 @@ func (s *store) internalCreate(nodePath string, value string, unique bool, repla
|
|||||||
|
|
||||||
// Node with TTL
|
// Node with TTL
|
||||||
if !n.IsPermanent() {
|
if !n.IsPermanent() {
|
||||||
|
heap.Push(s.TTLKeyHeap, n)
|
||||||
|
|
||||||
n.Expire()
|
n.Expire()
|
||||||
e.Expiration, e.TTL = n.ExpirationAndTTL()
|
e.Expiration, e.TTL = n.ExpirationAndTTL()
|
||||||
}
|
}
|
||||||
|
@ -48,8 +48,13 @@ func (h *TTLKeyHeap) Pop() interface{} {
|
|||||||
return x
|
return x
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *TTLKeyHeap) Update(n *Node) {
|
func (h *TTLKeyHeap) update(n *Node) {
|
||||||
index := h.Map[n]
|
index := h.Map[n]
|
||||||
heap.Remove(h, index)
|
heap.Remove(h, index)
|
||||||
heap.Push(h, n)
|
heap.Push(h, n)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *TTLKeyHeap) remove(n *Node) {
|
||||||
|
index := h.Map[n]
|
||||||
|
heap.Remove(h, index)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user