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

clean up tree.go and watcher.go #5

Merged
merged 1 commit into from
Jul 9, 2013
Merged
Show file tree
Hide file tree
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
16 changes: 8 additions & 8 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (c *SetCommand) CommandName() string {

// Set the value of key to value
func (c *SetCommand) Apply(server *raft.Server) (interface{}, error) {
return store.Set(c.Key, c.Value, c.ExpireTime, server.CommitIndex())
return etcdStore.Set(c.Key, c.Value, c.ExpireTime, server.CommitIndex())
}

// TestAndSet command
Expand All @@ -51,7 +51,7 @@ func (c *TestAndSetCommand) CommandName() string {

// Set the value of key to value
func (c *TestAndSetCommand) Apply(server *raft.Server) (interface{}, error) {
return store.TestAndSet(c.Key, c.PrevValue, c.Value, c.ExpireTime, server.CommitIndex())
return etcdStore.TestAndSet(c.Key, c.PrevValue, c.Value, c.ExpireTime, server.CommitIndex())
}

// Get command
Expand All @@ -66,7 +66,7 @@ func (c *GetCommand) CommandName() string {

// Set the value of key to value
func (c *GetCommand) Apply(server *raft.Server) (interface{}, error) {
res := store.Get(c.Key)
res := etcdStore.Get(c.Key)
return json.Marshal(res)
}

Expand All @@ -82,7 +82,7 @@ func (c *ListCommand) CommandName() string {

// Set the value of key to value
func (c *ListCommand) Apply(server *raft.Server) (interface{}, error) {
return store.List(c.Prefix)
return etcdStore.List(c.Prefix)
}

// Delete command
Expand All @@ -97,7 +97,7 @@ func (c *DeleteCommand) CommandName() string {

// Delete the key
func (c *DeleteCommand) Apply(server *raft.Server) (interface{}, error) {
return store.Delete(c.Key, server.CommitIndex())
return etcdStore.Delete(c.Key, server.CommitIndex())
}

// Watch command
Expand All @@ -112,13 +112,13 @@ func (c *WatchCommand) CommandName() string {
}

func (c *WatchCommand) Apply(server *raft.Server) (interface{}, error) {
ch := make(chan store.Response, 1)
watcher := store.CreateWatcher()

// add to the watchers list
store.AddWatcher(c.Key, ch, c.SinceIndex)
etcdStore.AddWatcher(c.Key, watcher, c.SinceIndex)

// wait for the notification for any changing
res := <-ch
res := <-watcher.C

return json.Marshal(res)
}
Expand Down
11 changes: 4 additions & 7 deletions etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,7 @@ type Info struct {

var server *raft.Server
var serverTransHandler transHandler
var logger *log.Logger

var storeMsg chan string
var etcdStore *store.Store

//------------------------------------------------------------------------------
//
Expand All @@ -129,7 +127,6 @@ var storeMsg chan string

func main() {
var err error
logger = log.New(os.Stdout, "", log.LstdFlags)
flag.Parse()

// Setup commands.
Expand Down Expand Up @@ -162,10 +159,10 @@ func main() {
serverTransHandler = createTranHandler(st)

// Setup new raft server.
s := store.CreateStore(maxSize)
etcdStore = store.CreateStore(maxSize)

// create raft server
server, err = raft.NewServer(name, dirPath, serverTransHandler, s, nil)
server, err = raft.NewServer(name, dirPath, serverTransHandler, etcdStore, nil)

if err != nil {
fatal("%v", err)
Expand Down Expand Up @@ -226,7 +223,7 @@ func main() {

if webPort != -1 {
// start web
s.SetMessager(&storeMsg)
etcdStore.SetMessager(&storeMsg)
go webHelper()
go web.Start(server, webPort)
}
Expand Down
9 changes: 5 additions & 4 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func VoteHttpHandler(w http.ResponseWriter, req *http.Request) {
func AppendEntriesHttpHandler(w http.ResponseWriter, req *http.Request) {
aereq := &raft.AppendEntriesRequest{}
err := decodeJsonRequest(req, aereq)

if err == nil {
debug("[recv] POST http://%s/log/append [%d]", server.Name(), len(aereq.Entries))
if resp := server.AppendEntries(aereq); resp != nil {
Expand Down Expand Up @@ -121,7 +122,7 @@ func SetHttpHandler(w *http.ResponseWriter, req *http.Request) {
duration, err := strconv.Atoi(strDuration)

if err != nil {
warn("raftd: Bad duration: %v", err)
warn("Bad duration: %v", err)
(*w).WriteHeader(http.StatusInternalServerError)
return
}
Expand Down Expand Up @@ -150,7 +151,7 @@ func TestAndSetHttpHandler(w http.ResponseWriter, req *http.Request) {
duration, err := strconv.Atoi(strDuration)

if err != nil {
warn("raftd: Bad duration: %v", err)
warn("Bad duration: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
Expand Down Expand Up @@ -266,7 +267,7 @@ func ListHttpHandler(w http.ResponseWriter, req *http.Request) {
command.Prefix = prefix

if body, err := command.Apply(server); err != nil {
warn("raftd: Unable to write file: %v", err)
warn("Unable to write file: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
} else {
Expand Down Expand Up @@ -309,7 +310,7 @@ func WatchHttpHandler(w http.ResponseWriter, req *http.Request) {
}

if body, err := command.Apply(server); err != nil {
warn("raftd: Unable to write file: %v", err)
warn("Unable to write file: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
} else {
Expand Down
Loading