Skip to content
This repository has been archived by the owner on Jun 4, 2019. It is now read-only.

Commit

Permalink
Merge pull request #13 from philips/cluster-flag
Browse files Browse the repository at this point in the history
etcdctl: add support for the -C flag
  • Loading branch information
philips committed Oct 10, 2013
2 parents eb45b0c + 0c37fcc commit 73f9c0f
Show file tree
Hide file tree
Showing 27 changed files with 410 additions and 127 deletions.
7 changes: 5 additions & 2 deletions etcdctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ import (
)

var (
cluster = flag.String("C", "0.0.0.0:4001", "a list of machine addresses in the cluster")
client = etcd.NewClient()
client *etcd.Client
)

func main() {
cluster := ClusterValue{"http://localhost:4001"}
flag.Var(&cluster, "C", "a comma seperated list of machine addresses in the cluster e.g. 127.0.0.1:4001,127.0.0.1:4002")
flag.Parse()

client = etcd.NewClient(cluster.GetMachines())

args := flag.Args()

if len(args) == 0 {
Expand Down
27 changes: 27 additions & 0 deletions flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"strings"
)

type ClusterValue struct {
machines string
}

func (c *ClusterValue) String() string {
return c.machines
}

func (c *ClusterValue) Set(value string) error {
if len(value) == 0 {
return nil
}

c.machines = value

return nil
}

func (c *ClusterValue) GetMachines() []string {
return strings.Split(c.machines, ",")
}
16 changes: 16 additions & 0 deletions third_party/github.com/coreos/etcd/error/error.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2013 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.
*/

package error

import (
Expand Down
16 changes: 16 additions & 0 deletions third_party/github.com/coreos/etcd/store/keyword_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2013 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.
*/

package store

import (
Expand Down
16 changes: 16 additions & 0 deletions third_party/github.com/coreos/etcd/store/keywords.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2013 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.
*/

package store

import (
Expand Down
16 changes: 16 additions & 0 deletions third_party/github.com/coreos/etcd/store/stats.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2013 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.
*/

package store

import (
Expand Down
187 changes: 106 additions & 81 deletions third_party/github.com/coreos/etcd/store/store.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
/*
Copyright 2013 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.
*/

package store

import (
"encoding/json"
"fmt"
etcdErr "github.com/coreos/etcd/error"
"path"
"strconv"
"sync"
"time"

etcdErr "github.com/coreos/etcd/error"
)

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -325,75 +342,81 @@ func (s *Store) Get(key string) ([]byte, error) {
return json.Marshal(resps)
}

func (s *Store) RawGet(key string) ([]*Response, error) {
// Update stats
s.BasicStats.Gets++
func (s *Store) rawGetNode(key string, node *Node) ([]*Response, error) {
resps := make([]*Response, 1)

key = path.Clean("/" + key)

nodes, keys, ok := s.Tree.list(key)
isExpire := !node.ExpireTime.Equal(PERMANENT)

if ok {
resps[0] = &Response{
Action: "GET",
Index: s.Index,
Key: key,
Value: node.Value,
}

node, ok := nodes.(*Node)
// Update ttl
if isExpire {
TTL := int64(node.ExpireTime.Sub(time.Now()) / time.Second)
resps[0].Expiration = &node.ExpireTime
resps[0].TTL = TTL
}

if ok {
resps := make([]*Response, 1)
return resps, nil
}

isExpire := !node.ExpireTime.Equal(PERMANENT)
func (s *Store) rawGetNodeList(key string, keys []string, nodes []*Node) ([]*Response, error) {
resps := make([]*Response, len(nodes))

resps[0] = &Response{
Action: "GET",
Index: s.Index,
Key: key,
Value: node.Value,
}
// TODO: check if nodes and keys are the same length
for i := 0; i < len(nodes); i++ {
var TTL int64
var isExpire bool = false

// Update ttl
if isExpire {
TTL := int64(node.ExpireTime.Sub(time.Now()) / time.Second)
resps[0].Expiration = &node.ExpireTime
resps[0].TTL = TTL
}
isExpire = !nodes[i].ExpireTime.Equal(PERMANENT)

return resps, nil
resps[i] = &Response{
Action: "GET",
Index: s.Index,
Key: path.Join(key, keys[i]),
}

nodes, _ := nodes.([]*Node)

resps := make([]*Response, len(nodes))
for i := 0; i < len(nodes); i++ {

var TTL int64
var isExpire bool = false
if len(nodes[i].Value) != 0 {
resps[i].Value = nodes[i].Value
} else {
resps[i].Dir = true
}

isExpire = !nodes[i].ExpireTime.Equal(PERMANENT)
// Update ttl
if isExpire {
TTL = int64(nodes[i].ExpireTime.Sub(time.Now()) / time.Second)
resps[i].Expiration = &nodes[i].ExpireTime
resps[i].TTL = TTL
}

resps[i] = &Response{
Action: "GET",
Index: s.Index,
Key: path.Join(key, keys[i]),
}
}

if len(nodes[i].Value) != 0 {
resps[i].Value = nodes[i].Value
} else {
resps[i].Dir = true
}
return resps, nil
}

// Update ttl
if isExpire {
TTL = int64(nodes[i].ExpireTime.Sub(time.Now()) / time.Second)
resps[i].Expiration = &nodes[i].ExpireTime
resps[i].TTL = TTL
}
func (s *Store) RawGet(key string) ([]*Response, error) {
// Update stats
s.BasicStats.Gets++

}
key = path.Clean("/" + key)

return resps, nil
nodes, keys, ok := s.Tree.list(key)
if !ok {
return nil, etcdErr.NewError(100, "get: "+key)
}

return nil, etcdErr.NewError(100, "get: "+key)
switch node := nodes.(type) {
case *Node:
return s.rawGetNode(key, node)
case []*Node:
return s.rawGetNodeList(key, keys, node)
default:
panic("invalid cast ")
}
}

func (s *Store) Delete(key string, index uint64) ([]byte, error) {
Expand All @@ -415,43 +438,41 @@ func (s *Store) internalDelete(key string, index uint64) ([]byte, error) {

node, ok := s.Tree.get(key)

if ok {

resp := Response{
Action: "DELETE",
Key: key,
PrevValue: node.Value,
Index: index,
}
if !ok {
return nil, etcdErr.NewError(100, "delete: "+key)
}

if node.ExpireTime.Equal(PERMANENT) {
resp := Response{
Action: "DELETE",
Key: key,
PrevValue: node.Value,
Index: index,
}

s.Tree.delete(key)
if node.ExpireTime.Equal(PERMANENT) {

} else {
resp.Expiration = &node.ExpireTime
// Kill the expire go routine
node.update <- PERMANENT
s.Tree.delete(key)
s.Tree.delete(key)

}
} else {
resp.Expiration = &node.ExpireTime
// Kill the expire go routine
node.update <- PERMANENT
s.Tree.delete(key)

msg, err := json.Marshal(resp)
}

s.watcher.notify(resp)
msg, err := json.Marshal(resp)

// notify the messager
if s.messager != nil && err == nil {
s.messager <- string(msg)
}
s.watcher.notify(resp)

s.addToResponseMap(index, &resp)
// notify the messager
if s.messager != nil && err == nil {
s.messager <- string(msg)
}

return msg, err
s.addToResponseMap(index, &resp)

} else {
return nil, etcdErr.NewError(100, "delete: "+key)
}
return msg, err
}

// Set the value of the key to the value if the given prevValue is equal to the value of the key
Expand All @@ -465,12 +486,16 @@ func (s *Store) TestAndSet(key string, prevValue string, value string, expireTim
resp := s.internalGet(key)

if resp == nil {
return nil, etcdErr.NewError(100, "testandset: "+key)
if prevValue != "" {
errmsg := fmt.Sprintf("TestAndSet: key not found and previousValue is not empty %s:%s ", key, prevValue)
return nil, etcdErr.NewError(100, errmsg)
}
return s.internalSet(key, value, expireTime, index)
}

if resp.Value == prevValue {

// If test success, do set
// If test succeed, do set
return s.internalSet(key, value, expireTime, index)
} else {

Expand Down
Loading

0 comments on commit 73f9c0f

Please sign in to comment.