etcd/store/store.go

770 lines
19 KiB
Go
Raw Normal View History

// Copyright 2015 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2013-10-07 20:44:51 +04:00
2013-09-29 03:26:19 +04:00
package store
2013-09-03 22:30:42 +04:00
import (
2013-09-14 01:10:40 +04:00
"encoding/json"
2013-09-05 23:38:22 +04:00
"fmt"
"path"
"strconv"
2013-09-03 22:30:42 +04:00
"strings"
2013-09-29 03:58:57 +04:00
"sync"
2013-09-03 22:30:42 +04:00
"time"
2013-09-29 03:26:19 +04:00
etcdErr "github.com/coreos/etcd/error"
"github.com/coreos/etcd/pkg/types"
2016-03-23 03:10:28 +03:00
"github.com/jonboulle/clockwork"
2013-09-03 22:30:42 +04:00
)
2013-10-27 22:47:00 +04:00
// The default version to set when the store is first initialized.
const defaultVersion = 2
2013-11-06 02:57:53 +04:00
var minExpireTime time.Time
func init() {
minExpireTime, _ = time.Parse(time.RFC3339, "2000-01-01T00:00:00Z")
}
2013-10-14 21:12:30 +04:00
type Store interface {
2013-10-27 22:47:00 +04:00
Version() int
Index() uint64
2013-12-06 02:10:37 +04:00
Get(nodePath string, recursive, sorted bool) (*Event, error)
Set(nodePath string, dir bool, value string, expireOpts TTLOptionSet) (*Event, error)
Update(nodePath string, newValue string, expireOpts TTLOptionSet) (*Event, error)
2013-12-06 02:10:37 +04:00
Create(nodePath string, dir bool, value string, unique bool,
expireOpts TTLOptionSet) (*Event, error)
2013-10-15 09:22:23 +04:00
CompareAndSwap(nodePath string, prevValue string, prevIndex uint64,
value string, expireOpts TTLOptionSet) (*Event, error)
2014-09-23 01:40:19 +04:00
Delete(nodePath string, dir, recursive bool) (*Event, error)
2013-12-20 01:10:22 +04:00
CompareAndDelete(nodePath string, prevValue string, prevIndex uint64) (*Event, error)
2013-12-26 18:06:15 +04:00
2014-09-09 03:56:10 +04:00
Watch(prefix string, recursive, stream bool, sinceIndex uint64) (Watcher, error)
2013-12-06 02:10:37 +04:00
2013-10-14 21:12:30 +04:00
Save() ([]byte, error)
Recovery(state []byte) error
2013-12-06 02:10:37 +04:00
Clone() Store
SaveNoCopy() ([]byte, error)
2013-10-14 21:12:30 +04:00
JsonStats() []byte
DeleteExpiredKeys(cutoff time.Time)
2013-10-14 21:12:30 +04:00
}
type TTLOptionSet struct {
ExpireTime time.Time
Refresh bool
}
2013-10-14 21:12:30 +04:00
type store struct {
Root *node
2013-10-31 04:36:15 +04:00
WatcherHub *watcherHub
CurrentIndex uint64
2013-10-31 04:36:15 +04:00
Stats *Stats
CurrentVersion int
2013-11-05 09:51:14 +04:00
ttlKeyHeap *ttlKeyHeap // need to recovery manually
2013-10-31 04:36:15 +04:00
worldLock sync.RWMutex // stop the world lock
2014-10-17 02:19:00 +04:00
clock clockwork.Clock
readonlySet types.Set
2013-09-03 22:30:42 +04:00
}
2016-02-21 16:05:03 +03:00
// New creates a store where the given namespaces will be created as initial directories.
func New(namespaces ...string) Store {
s := newStore(namespaces...)
2014-10-17 02:19:00 +04:00
s.clock = clockwork.NewRealClock()
return s
2013-10-14 21:12:30 +04:00
}
func newStore(namespaces ...string) *store {
2013-10-14 21:12:30 +04:00
s := new(store)
2013-10-27 22:47:00 +04:00
s.CurrentVersion = defaultVersion
2015-02-27 01:49:42 +03:00
s.Root = newDir(s, "/", s.CurrentIndex, nil, Permanent)
for _, namespace := range namespaces {
2015-02-27 01:49:42 +03:00
s.Root.Add(newDir(s, namespace, s.CurrentIndex, s.Root, Permanent))
}
2013-09-29 03:26:19 +04:00
s.Stats = newStats()
s.WatcherHub = newWatchHub(1000)
2013-11-05 09:51:14 +04:00
s.ttlKeyHeap = newTtlKeyHeap()
s.readonlySet = types.NewUnsafeSet(append(namespaces, "/")...)
2013-09-29 03:26:19 +04:00
return s
2013-09-03 22:30:42 +04:00
}
2013-10-27 22:47:00 +04:00
// Version retrieves current version of the store.
func (s *store) Version() int {
return s.CurrentVersion
}
2016-02-21 16:05:03 +03:00
// Index retrieves the current index of the store.
func (s *store) Index() uint64 {
2014-07-17 08:49:29 +04:00
s.worldLock.RLock()
defer s.worldLock.RUnlock()
return s.CurrentIndex
}
2014-02-04 05:19:33 +04:00
// Get returns a get event.
// If recursive is true, it will return all the content under the node path.
// If sorted is true, it will sort the content by keys.
func (s *store) Get(nodePath string, recursive, sorted bool) (*Event, error) {
var err *etcdErr.Error
2013-09-29 03:58:57 +04:00
s.worldLock.Lock()
defer s.worldLock.Unlock()
2013-09-14 23:13:33 +04:00
defer func() {
if err == nil {
s.Stats.Inc(GetSuccess)
if recursive {
reportReadSuccess(GetRecursive)
} else {
reportReadSuccess(Get)
}
return
}
2013-09-03 22:30:42 +04:00
2013-09-29 03:26:19 +04:00
s.Stats.Inc(GetFail)
2015-06-17 16:32:13 +03:00
if recursive {
reportReadFailure(GetRecursive)
} else {
reportReadFailure(Get)
}
}()
nodePath = path.Clean(path.Join("/", nodePath))
n, err := s.internalGet(nodePath)
if err != nil {
2013-09-04 05:27:46 +04:00
return nil, err
}
2013-09-03 22:30:42 +04:00
2013-11-28 08:04:52 +04:00
e := newEvent(Get, nodePath, n.ModifiedIndex, n.CreatedIndex)
e.EtcdIndex = s.CurrentIndex
2014-10-17 02:19:00 +04:00
e.Node.loadInternalNode(n, recursive, sorted, s.clock)
2013-09-14 23:13:33 +04:00
2013-09-04 05:27:46 +04:00
return e, nil
2013-09-03 22:30:42 +04:00
}
2014-02-04 05:19:33 +04:00
// Create creates the node at nodePath. Create will help to create intermediate directories with no ttl.
// If the node has already existed, create will fail.
// If any node on the path is a file, create will fail.
func (s *store) Create(nodePath string, dir bool, value string, unique bool, expireOpts TTLOptionSet) (*Event, error) {
var err *etcdErr.Error
2013-10-01 10:18:52 +04:00
s.worldLock.Lock()
defer s.worldLock.Unlock()
2013-10-16 10:18:03 +04:00
defer func() {
if err == nil {
s.Stats.Inc(CreateSuccess)
reportWriteSuccess(Create)
return
}
2013-10-16 10:18:03 +04:00
s.Stats.Inc(CreateFail)
2015-06-17 16:32:13 +03:00
reportWriteFailure(Create)
}()
e, err := s.internalCreate(nodePath, dir, value, unique, false, expireOpts.ExpireTime, Create)
if err != nil {
return nil, err
2013-10-16 10:18:03 +04:00
}
e.EtcdIndex = s.CurrentIndex
s.WatcherHub.notify(e)
return e, nil
2013-10-15 10:04:21 +04:00
}
2014-02-04 05:19:33 +04:00
// Set creates or replace the node at nodePath.
func (s *store) Set(nodePath string, dir bool, value string, expireOpts TTLOptionSet) (*Event, error) {
var err *etcdErr.Error
2013-10-15 10:04:21 +04:00
s.worldLock.Lock()
defer s.worldLock.Unlock()
defer func() {
if err == nil {
s.Stats.Inc(SetSuccess)
2015-06-17 16:32:13 +03:00
reportWriteSuccess(Set)
return
}
s.Stats.Inc(SetFail)
reportWriteFailure(Set)
}()
// Get prevNode value
n, getErr := s.internalGet(nodePath)
if getErr != nil && getErr.ErrorCode != etcdErr.EcodeKeyNotFound {
err = getErr
return nil, err
}
if expireOpts.Refresh {
if getErr != nil {
err = getErr
return nil, err
} else {
value = n.Value
}
}
// Set new value
e, err := s.internalCreate(nodePath, dir, value, false, true, expireOpts.ExpireTime, Set)
if err != nil {
return nil, err
}
e.EtcdIndex = s.CurrentIndex
2013-10-16 10:18:03 +04:00
// Put prevNode into event
if getErr == nil {
prev := newEvent(Get, nodePath, n.ModifiedIndex, n.CreatedIndex)
2014-10-17 02:19:00 +04:00
prev.Node.loadInternalNode(n, false, false, s.clock)
e.PrevNode = prev.Node
2013-10-16 10:18:03 +04:00
}
if !expireOpts.Refresh {
s.WatcherHub.notify(e)
}
return e, nil
2013-09-03 22:30:42 +04:00
}
// returns user-readable cause of failed comparison
func getCompareFailCause(n *node, which int, prevValue string, prevIndex uint64) string {
switch which {
case CompareIndexNotMatch:
return fmt.Sprintf("[%v != %v]", prevIndex, n.ModifiedIndex)
case CompareValueNotMatch:
return fmt.Sprintf("[%v != %v]", prevValue, n.Value)
default:
return fmt.Sprintf("[%v != %v] [%v != %v]", prevValue, n.Value, prevIndex, n.ModifiedIndex)
}
}
2013-10-15 09:22:23 +04:00
func (s *store) CompareAndSwap(nodePath string, prevValue string, prevIndex uint64,
value string, expireOpts TTLOptionSet) (*Event, error) {
var err *etcdErr.Error
s.worldLock.Lock()
defer s.worldLock.Unlock()
defer func() {
if err == nil {
s.Stats.Inc(CompareAndSwapSuccess)
reportWriteSuccess(CompareAndSwap)
return
}
s.Stats.Inc(CompareAndSwapFail)
reportWriteFailure(CompareAndSwap)
}()
2013-10-06 22:23:52 +04:00
nodePath = path.Clean(path.Join("/", nodePath))
2013-12-02 03:01:24 +04:00
// we do not allow the user to change "/"
if s.readonlySet.Contains(nodePath) {
2013-12-02 03:01:24 +04:00
return nil, etcdErr.NewError(etcdErr.EcodeRootROnly, "/", s.CurrentIndex)
}
2013-10-06 22:23:52 +04:00
n, err := s.internalGet(nodePath)
2013-09-05 23:38:22 +04:00
if err != nil {
return nil, err
}
2013-12-20 01:10:22 +04:00
if n.IsDir() { // can only compare and swap file
err = etcdErr.NewError(etcdErr.EcodeNotFile, nodePath, s.CurrentIndex)
return nil, err
2013-09-05 23:38:22 +04:00
}
2013-10-14 23:57:35 +04:00
// If both of the prevValue and prevIndex are given, we will test both of them.
// Command will be executed, only if both of the tests are successful.
if ok, which := n.Compare(prevValue, prevIndex); !ok {
cause := getCompareFailCause(n, which, prevValue, prevIndex)
err = etcdErr.NewError(etcdErr.EcodeTestFailed, cause, s.CurrentIndex)
return nil, err
}
// update etcd index
s.CurrentIndex++
2013-11-28 08:04:52 +04:00
e := newEvent(CompareAndSwap, nodePath, s.CurrentIndex, n.CreatedIndex)
e.EtcdIndex = s.CurrentIndex
2014-10-17 02:19:00 +04:00
e.PrevNode = n.Repr(false, false, s.clock)
eNode := e.Node
2013-10-09 09:10:18 +04:00
// if test succeed, write the value
n.Write(value, s.CurrentIndex)
n.UpdateTTL(expireOpts.ExpireTime)
2013-10-06 22:23:52 +04:00
// copy the value for safety
2014-08-14 00:59:54 +04:00
valueCopy := value
eNode.Value = &valueCopy
2014-10-17 02:19:00 +04:00
eNode.Expiration, eNode.TTL = n.expirationAndTTL(s.clock)
2013-09-05 23:38:22 +04:00
if !expireOpts.Refresh {
s.WatcherHub.notify(e)
}
return e, nil
2013-09-03 22:30:42 +04:00
}
2014-02-04 05:19:33 +04:00
// Delete deletes the node at the given path.
// If the node is a directory, recursive must be true to delete it.
2013-12-06 02:10:37 +04:00
func (s *store) Delete(nodePath string, dir, recursive bool) (*Event, error) {
var err *etcdErr.Error
s.worldLock.Lock()
defer s.worldLock.Unlock()
defer func() {
if err == nil {
s.Stats.Inc(DeleteSuccess)
reportWriteSuccess(Delete)
return
}
s.Stats.Inc(DeleteFail)
reportWriteFailure(Delete)
}()
2013-10-06 22:23:52 +04:00
nodePath = path.Clean(path.Join("/", nodePath))
2013-12-02 03:01:24 +04:00
// we do not allow the user to change "/"
if s.readonlySet.Contains(nodePath) {
2013-12-02 03:01:24 +04:00
return nil, etcdErr.NewError(etcdErr.EcodeRootROnly, "/", s.CurrentIndex)
}
2013-10-06 22:23:52 +04:00
2013-12-06 05:46:52 +04:00
// recursive implies dir
2016-04-08 08:09:25 +03:00
if recursive {
2013-12-06 05:46:52 +04:00
dir = true
}
n, err := s.internalGet(nodePath)
if err != nil { // if the node does not exist, return error
2013-09-04 06:35:25 +04:00
return nil, err
}
2013-12-20 01:10:22 +04:00
nextIndex := s.CurrentIndex + 1
2013-11-28 08:04:52 +04:00
e := newEvent(Delete, nodePath, nextIndex, n.CreatedIndex)
2014-09-25 01:07:52 +04:00
e.EtcdIndex = nextIndex
2014-10-17 02:19:00 +04:00
e.PrevNode = n.Repr(false, false, s.clock)
2013-11-28 08:04:52 +04:00
eNode := e.Node
2013-09-04 06:35:25 +04:00
if n.IsDir() {
2013-11-28 08:04:52 +04:00
eNode.Dir = true
2013-09-04 06:35:25 +04:00
}
callback := func(path string) { // notify function
// notify the watchers with deleted set true
2013-10-08 09:17:58 +04:00
s.WatcherHub.notifyWatchers(e, path, true)
2013-09-07 09:05:11 +04:00
}
2013-12-06 02:10:37 +04:00
err = n.Remove(dir, recursive, callback)
2013-09-07 09:05:11 +04:00
if err != nil {
return nil, err
}
// update etcd index
s.CurrentIndex++
s.WatcherHub.notify(e)
2013-09-04 06:35:25 +04:00
return e, nil
2013-09-03 22:30:42 +04:00
}
2013-12-20 01:10:22 +04:00
func (s *store) CompareAndDelete(nodePath string, prevValue string, prevIndex uint64) (*Event, error) {
var err *etcdErr.Error
s.worldLock.Lock()
defer s.worldLock.Unlock()
defer func() {
if err == nil {
s.Stats.Inc(CompareAndDeleteSuccess)
reportWriteSuccess(CompareAndDelete)
return
}
2013-11-30 21:05:48 +04:00
s.Stats.Inc(CompareAndDeleteFail)
2015-06-17 16:32:13 +03:00
reportWriteFailure(CompareAndDelete)
}()
nodePath = path.Clean(path.Join("/", nodePath))
n, err := s.internalGet(nodePath)
if err != nil { // if the node does not exist, return error
return nil, err
}
2013-12-20 01:10:22 +04:00
if n.IsDir() { // can only compare and delete file
return nil, etcdErr.NewError(etcdErr.EcodeNotFile, nodePath, s.CurrentIndex)
}
// If both of the prevValue and prevIndex are given, we will test both of them.
// Command will be executed, only if both of the tests are successful.
if ok, which := n.Compare(prevValue, prevIndex); !ok {
cause := getCompareFailCause(n, which, prevValue, prevIndex)
return nil, etcdErr.NewError(etcdErr.EcodeTestFailed, cause, s.CurrentIndex)
}
// update etcd index
s.CurrentIndex++
e := newEvent(CompareAndDelete, nodePath, s.CurrentIndex, n.CreatedIndex)
e.EtcdIndex = s.CurrentIndex
2014-10-17 02:19:00 +04:00
e.PrevNode = n.Repr(false, false, s.clock)
callback := func(path string) { // notify function
// notify the watchers with deleted set true
s.WatcherHub.notifyWatchers(e, path, true)
}
err = n.Remove(false, false, callback)
if err != nil {
return nil, err
}
s.WatcherHub.notify(e)
return e, nil
}
2014-09-09 03:56:10 +04:00
func (s *store) Watch(key string, recursive, stream bool, sinceIndex uint64) (Watcher, error) {
2013-09-29 03:58:57 +04:00
s.worldLock.RLock()
defer s.worldLock.RUnlock()
key = path.Clean(path.Join("/", key))
2013-09-14 23:13:33 +04:00
if sinceIndex == 0 {
sinceIndex = s.CurrentIndex + 1
2013-09-14 23:13:33 +04:00
}
2016-02-01 08:42:39 +03:00
// WatcherHub does not know about the current index, so we need to pass it in
w, err := s.WatcherHub.watch(key, recursive, stream, sinceIndex, s.CurrentIndex)
2013-10-06 22:23:52 +04:00
if err != nil {
return nil, err
2013-10-06 22:23:52 +04:00
}
2013-12-26 18:06:15 +04:00
return w, nil
2013-09-14 23:13:33 +04:00
}
2014-02-04 05:19:33 +04:00
// walk walks all the nodePath and apply the walkFunc on each directory
func (s *store) walk(nodePath string, walkFunc func(prev *node, component string) (*node, *etcdErr.Error)) (*node, *etcdErr.Error) {
components := strings.Split(nodePath, "/")
2013-09-03 22:30:42 +04:00
2013-09-29 03:26:19 +04:00
curr := s.Root
2013-10-06 22:23:52 +04:00
var err *etcdErr.Error
2013-09-03 22:30:42 +04:00
for i := 1; i < len(components); i++ {
if len(components[i]) == 0 { // ignore empty string
return curr, nil
}
curr, err = walkFunc(curr, components[i])
if err != nil {
return nil, err
}
}
return curr, nil
}
2014-02-04 05:19:33 +04:00
// Update updates the value/ttl of the node.
2013-10-15 09:22:23 +04:00
// If the node is a file, the value and the ttl can be updated.
// If the node is a directory, only the ttl can be updated.
func (s *store) Update(nodePath string, newValue string, expireOpts TTLOptionSet) (*Event, error) {
var err *etcdErr.Error
s.worldLock.Lock()
defer s.worldLock.Unlock()
defer func() {
if err == nil {
s.Stats.Inc(UpdateSuccess)
reportWriteSuccess(Update)
return
}
s.Stats.Inc(UpdateFail)
reportWriteFailure(Update)
}()
2013-12-02 03:01:24 +04:00
nodePath = path.Clean(path.Join("/", nodePath))
// we do not allow the user to change "/"
if s.readonlySet.Contains(nodePath) {
2013-12-02 03:01:24 +04:00
return nil, etcdErr.NewError(etcdErr.EcodeRootROnly, "/", s.CurrentIndex)
}
currIndex, nextIndex := s.CurrentIndex, s.CurrentIndex+1
n, err := s.internalGet(nodePath)
2013-10-15 09:22:23 +04:00
if err != nil { // if the node does not exist, return error
return nil, err
}
if n.IsDir() && len(newValue) != 0 {
// if the node is a directory, we cannot update value to non-empty
return nil, etcdErr.NewError(etcdErr.EcodeNotFile, nodePath, currIndex)
}
2013-10-15 09:22:23 +04:00
if expireOpts.Refresh {
newValue = n.Value
}
2013-11-28 08:04:52 +04:00
e := newEvent(Update, nodePath, nextIndex, n.CreatedIndex)
2014-09-25 01:07:52 +04:00
e.EtcdIndex = nextIndex
2014-10-17 02:19:00 +04:00
e.PrevNode = n.Repr(false, false, s.clock)
2013-11-28 08:04:52 +04:00
eNode := e.Node
2013-10-15 09:22:23 +04:00
n.Write(newValue, nextIndex)
if n.IsDir() {
eNode.Dir = true
} else {
// copy the value for safety
2014-08-14 00:59:54 +04:00
newValueCopy := newValue
eNode.Value = &newValueCopy
}
2013-10-15 09:22:23 +04:00
// update ttl
n.UpdateTTL(expireOpts.ExpireTime)
2013-10-15 09:22:23 +04:00
2014-10-17 02:19:00 +04:00
eNode.Expiration, eNode.TTL = n.expirationAndTTL(s.clock)
2013-10-15 09:22:23 +04:00
if !expireOpts.Refresh {
s.WatcherHub.notify(e)
}
2013-10-15 09:22:23 +04:00
s.CurrentIndex = nextIndex
2013-10-15 09:22:23 +04:00
return e, nil
}
2013-12-06 02:10:37 +04:00
func (s *store) internalCreate(nodePath string, dir bool, value string, unique, replace bool,
expireTime time.Time, action string) (*Event, *etcdErr.Error) {
2013-10-06 22:23:52 +04:00
currIndex, nextIndex := s.CurrentIndex, s.CurrentIndex+1
if unique { // append unique item under the node path
nodePath += "/" + fmt.Sprintf("%020s", strconv.FormatUint(nextIndex, 10))
}
2013-10-03 09:15:12 +04:00
nodePath = path.Clean(path.Join("/", nodePath))
2013-12-02 03:01:24 +04:00
// we do not allow the user to change "/"
if s.readonlySet.Contains(nodePath) {
2013-12-02 03:01:24 +04:00
return nil, etcdErr.NewError(etcdErr.EcodeRootROnly, "/", currIndex)
}
// Assume expire times that are way in the past are
// This can occur when the time is serialized to JS
if expireTime.Before(minExpireTime) {
expireTime = Permanent
}
2013-12-06 02:10:37 +04:00
dirName, nodeName := path.Split(nodePath)
2013-10-03 09:15:12 +04:00
// walk through the nodePath, create dirs and get the last directory node
2013-12-06 02:10:37 +04:00
d, err := s.walk(dirName, s.checkDir)
2013-10-03 09:15:12 +04:00
if err != nil {
s.Stats.Inc(SetFail)
2015-06-17 16:32:13 +03:00
reportWriteFailure(action)
err.Index = currIndex
2013-10-03 09:15:12 +04:00
return nil, err
}
2013-11-28 08:04:52 +04:00
e := newEvent(action, nodePath, nextIndex, nextIndex)
eNode := e.Node
2013-10-03 09:15:12 +04:00
2013-12-06 02:10:37 +04:00
n, _ := d.GetChild(nodeName)
// force will try to replace an existing file
if n != nil {
2013-10-15 10:15:31 +04:00
if replace {
2013-10-06 22:23:52 +04:00
if n.IsDir() {
return nil, etcdErr.NewError(etcdErr.EcodeNotFile, nodePath, currIndex)
2013-10-06 22:23:52 +04:00
}
2014-10-17 02:19:00 +04:00
e.PrevNode = n.Repr(false, false, s.clock)
2013-10-06 22:23:52 +04:00
2013-12-06 02:10:37 +04:00
n.Remove(false, false, nil)
} else {
return nil, etcdErr.NewError(etcdErr.EcodeNodeExist, nodePath, currIndex)
2013-10-06 22:23:52 +04:00
}
}
2013-12-06 02:10:37 +04:00
if !dir { // create file
// copy the value for safety
2014-08-14 00:59:54 +04:00
valueCopy := value
eNode.Value = &valueCopy
2013-10-03 09:15:12 +04:00
2015-02-27 01:49:42 +03:00
n = newKV(s, nodePath, value, nextIndex, d, expireTime)
2013-10-03 09:15:12 +04:00
} else { // create directory
2013-11-28 08:04:52 +04:00
eNode.Dir = true
2013-10-03 09:15:12 +04:00
2015-02-27 01:49:42 +03:00
n = newDir(s, nodePath, nextIndex, d, expireTime)
2013-10-03 09:15:12 +04:00
}
2013-10-16 10:18:03 +04:00
// we are sure d is a directory and does not have the children with name n.Name
d.Add(n)
2013-10-03 09:15:12 +04:00
// node with TTL
2013-11-05 08:56:41 +04:00
if !n.IsPermanent() {
2013-11-05 09:51:14 +04:00
s.ttlKeyHeap.push(n)
2013-11-05 09:22:22 +04:00
2014-10-17 02:19:00 +04:00
eNode.Expiration, eNode.TTL = n.expirationAndTTL(s.clock)
2013-10-03 09:15:12 +04:00
}
s.CurrentIndex = nextIndex
2013-10-03 09:15:12 +04:00
return e, nil
}
2014-02-04 05:19:33 +04:00
// InternalGet gets the node of the given nodePath.
func (s *store) internalGet(nodePath string) (*node, *etcdErr.Error) {
2013-09-14 23:13:33 +04:00
nodePath = path.Clean(path.Join("/", nodePath))
2013-09-04 05:27:46 +04:00
walkFunc := func(parent *node, name string) (*node, *etcdErr.Error) {
2013-09-05 23:38:22 +04:00
if !parent.IsDir() {
err := etcdErr.NewError(etcdErr.EcodeNotDir, parent.Path, s.CurrentIndex)
2013-10-06 22:23:52 +04:00
return nil, err
2013-09-05 23:38:22 +04:00
}
child, ok := parent.Children[name]
2013-09-04 05:27:46 +04:00
if ok {
return child, nil
}
return nil, etcdErr.NewError(etcdErr.EcodeKeyNotFound, path.Join(parent.Path, name), s.CurrentIndex)
2013-09-04 05:27:46 +04:00
}
2013-09-29 03:26:19 +04:00
f, err := s.walk(nodePath, walkFunc)
2013-09-04 05:27:46 +04:00
if err != nil {
return nil, err
}
return f, nil
}
2016-02-01 08:42:39 +03:00
// DeleteExpiredKeys will delete all expired keys
func (s *store) DeleteExpiredKeys(cutoff time.Time) {
2013-11-06 09:47:25 +04:00
s.worldLock.Lock()
defer s.worldLock.Unlock()
for {
node := s.ttlKeyHeap.top()
if node == nil || node.ExpireTime.After(cutoff) {
2013-11-07 09:27:39 +04:00
break
2013-11-06 09:47:25 +04:00
}
s.CurrentIndex++
e := newEvent(Expire, node.Path, s.CurrentIndex, node.CreatedIndex)
e.EtcdIndex = s.CurrentIndex
2014-10-17 02:19:00 +04:00
e.PrevNode = node.Repr(false, false, s.clock)
callback := func(path string) { // notify function
// notify the watchers with deleted set true
s.WatcherHub.notifyWatchers(e, path, true)
}
s.ttlKeyHeap.pop()
node.Remove(true, true, callback)
2015-06-17 16:32:13 +03:00
reportExpiredKey()
2013-11-06 09:56:21 +04:00
s.Stats.Inc(ExpireCount)
s.WatcherHub.notify(e)
2013-11-06 09:47:25 +04:00
}
2013-11-06 09:47:25 +04:00
}
2014-02-04 05:19:33 +04:00
// checkDir will check whether the component is a directory under parent node.
2013-09-03 22:30:42 +04:00
// If it is a directory, this function will return the pointer to that node.
// If it does not exist, this function will create a new directory and return the pointer to that node.
// If it is a file, this function will return error.
func (s *store) checkDir(parent *node, dirName string) (*node, *etcdErr.Error) {
node, ok := parent.Children[dirName]
2013-09-03 22:30:42 +04:00
if ok {
if node.IsDir() {
return node, nil
}
2013-12-12 22:12:33 +04:00
return nil, etcdErr.NewError(etcdErr.EcodeNotDir, node.Path, s.CurrentIndex)
2013-09-03 22:30:42 +04:00
}
2015-02-27 01:49:42 +03:00
n := newDir(s, path.Join(parent.Path, dirName), s.CurrentIndex+1, parent, Permanent)
2013-09-03 22:30:42 +04:00
parent.Children[dirName] = n
return n, nil
}
2013-09-14 01:10:40 +04:00
2014-02-04 05:19:33 +04:00
// Save saves the static state of the store system.
// It will not be able to save the state of watchers.
// It will not save the parent field of the node. Or there will
2013-09-14 01:10:40 +04:00
// be cyclic dependencies issue for the json package.
2013-10-14 21:12:30 +04:00
func (s *store) Save() ([]byte, error) {
b, err := json.Marshal(s.Clone())
if err != nil {
return nil, err
}
return b, nil
}
func (s *store) SaveNoCopy() ([]byte, error) {
b, err := json.Marshal(s)
if err != nil {
return nil, err
}
return b, nil
}
func (s *store) Clone() Store {
2013-09-29 03:58:57 +04:00
s.worldLock.Lock()
2013-10-14 21:12:30 +04:00
clonedStore := newStore()
clonedStore.CurrentIndex = s.CurrentIndex
2013-09-29 04:41:45 +04:00
clonedStore.Root = s.Root.Clone()
clonedStore.WatcherHub = s.WatcherHub.clone()
2013-09-29 03:58:57 +04:00
clonedStore.Stats = s.Stats.clone()
2013-10-27 22:47:00 +04:00
clonedStore.CurrentVersion = s.CurrentVersion
2013-09-29 03:58:57 +04:00
s.worldLock.Unlock()
return clonedStore
2013-09-14 01:10:40 +04:00
}
2014-02-04 05:19:33 +04:00
// Recovery recovers the store system from a static state
// It needs to recover the parent field of the nodes.
2013-09-14 01:10:40 +04:00
// It needs to delete the expired nodes since the saved time and also
2014-02-04 05:19:33 +04:00
// needs to create monitoring go routines.
2013-10-14 21:12:30 +04:00
func (s *store) Recovery(state []byte) error {
2013-09-29 03:58:57 +04:00
s.worldLock.Lock()
defer s.worldLock.Unlock()
2013-09-29 03:26:19 +04:00
err := json.Unmarshal(state, s)
2013-09-14 01:10:40 +04:00
if err != nil {
2013-09-16 06:28:42 +04:00
return err
2013-09-14 01:10:40 +04:00
}
2013-11-05 09:51:14 +04:00
s.ttlKeyHeap = newTtlKeyHeap()
2013-10-14 21:12:30 +04:00
s.Root.recoverAndclean()
2013-09-16 06:28:42 +04:00
return nil
2013-09-14 01:10:40 +04:00
}
2013-09-29 03:26:19 +04:00
2013-10-14 21:12:30 +04:00
func (s *store) JsonStats() []byte {
2013-09-29 03:26:19 +04:00
s.Stats.Watchers = uint64(s.WatcherHub.count)
return s.Stats.toJson()
}