|
@@ -3,26 +3,35 @@ package main
|
|
|
import (
|
|
import (
|
|
|
"path"
|
|
"path"
|
|
|
"encoding/json"
|
|
"encoding/json"
|
|
|
|
|
+ "time"
|
|
|
|
|
+ "fmt"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
// CONSTANTS
|
|
// CONSTANTS
|
|
|
const (
|
|
const (
|
|
|
ERROR = -1 + iota
|
|
ERROR = -1 + iota
|
|
|
SET
|
|
SET
|
|
|
- GET
|
|
|
|
|
DELETE
|
|
DELETE
|
|
|
|
|
+ GET
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
type Store struct {
|
|
type Store struct {
|
|
|
- Nodes map[string]string `json:"nodes"`
|
|
|
|
|
|
|
+ Nodes map[string]Node `json:"nodes"`
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+type Node struct {
|
|
|
|
|
+ Value string
|
|
|
|
|
+ ExpireTime time.Time
|
|
|
|
|
+ update chan time.Time
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
type Response struct {
|
|
type Response struct {
|
|
|
- Action int `json:action`
|
|
|
|
|
- Key string `json:key`
|
|
|
|
|
- OldValue string `json:oldValue`
|
|
|
|
|
- NewValue string `json:newValue`
|
|
|
|
|
- Exist bool `json:exist`
|
|
|
|
|
|
|
+ Action int `json:"action"`
|
|
|
|
|
+ Key string `json:"key"`
|
|
|
|
|
+ OldValue string `json:"oldValue"`
|
|
|
|
|
+ NewValue string `json:"newValue"`
|
|
|
|
|
+ Exist bool `json:"exist"`
|
|
|
|
|
+ Expiration time.Time `json:"expiration"`
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@@ -36,25 +45,75 @@ func init() {
|
|
|
// make a new stroe
|
|
// make a new stroe
|
|
|
func createStore() *Store{
|
|
func createStore() *Store{
|
|
|
s := new(Store)
|
|
s := new(Store)
|
|
|
- s.Nodes = make(map[string]string)
|
|
|
|
|
|
|
+ s.Nodes = make(map[string]Node)
|
|
|
return s
|
|
return s
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// set the key to value, return the old value if the key exists
|
|
// set the key to value, return the old value if the key exists
|
|
|
-func (s *Store) Set(key string, value string) Response {
|
|
|
|
|
|
|
+func (s *Store) Set(key string, value string, expireTime time.Time) Response {
|
|
|
|
|
+
|
|
|
key = path.Clean(key)
|
|
key = path.Clean(key)
|
|
|
|
|
|
|
|
- oldValue, ok := s.Nodes[key]
|
|
|
|
|
|
|
+ var expire bool = false
|
|
|
|
|
+
|
|
|
|
|
+ expire = !expireTime.Equal(time.Unix(0,0))
|
|
|
|
|
+
|
|
|
|
|
+ // when the slow follower receive the set command
|
|
|
|
|
+ // the key may be expired, we need also to delete
|
|
|
|
|
+ // the previous value of key
|
|
|
|
|
+ if expire && expireTime.Sub(time.Now()) < 0 {
|
|
|
|
|
+ return s.Delete(key)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ node, ok := s.Nodes[key]
|
|
|
|
|
|
|
|
if ok {
|
|
if ok {
|
|
|
- s.Nodes[key] = value
|
|
|
|
|
- w.notify(SET, key, oldValue, value, true)
|
|
|
|
|
- return Response{SET, key, oldValue, value, true}
|
|
|
|
|
|
|
+ update := make(chan time.Time)
|
|
|
|
|
+ s.Nodes[key] = Node{value, expireTime, update}
|
|
|
|
|
+ w.notify(SET, key, node.Value, value, true)
|
|
|
|
|
+
|
|
|
|
|
+ // node is not permanent before
|
|
|
|
|
+ if !node.ExpireTime.Equal(time.Unix(0,0)) {
|
|
|
|
|
+ node.update <- expireTime
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // if current node is not permanent
|
|
|
|
|
+ if expire {
|
|
|
|
|
+ go s.expire(key, update, expireTime)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return Response{SET, key, node.Value, value, true, time.Unix(0, 0)}
|
|
|
|
|
|
|
|
} else {
|
|
} else {
|
|
|
- s.Nodes[key] = value
|
|
|
|
|
|
|
+ update := make(chan time.Time)
|
|
|
|
|
+ s.Nodes[key] = Node{value, expireTime, update}
|
|
|
w.notify(SET, key, "", value, false)
|
|
w.notify(SET, key, "", value, false)
|
|
|
- return Response{SET, key, "", value, false}
|
|
|
|
|
|
|
+ if expire {
|
|
|
|
|
+ go s.expire(key, update, expireTime)
|
|
|
|
|
+ }
|
|
|
|
|
+ return Response{SET, key, "", value, false, time.Unix(0, 0)}
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// delete the key when it expires
|
|
|
|
|
+func (s *Store) expire(key string, update chan time.Time, expireTime time.Time) {
|
|
|
|
|
+ duration := expireTime.Sub(time.Now())
|
|
|
|
|
+
|
|
|
|
|
+ for {
|
|
|
|
|
+ select {
|
|
|
|
|
+ // timeout delte key
|
|
|
|
|
+ case <-time.After(duration):
|
|
|
|
|
+ fmt.Println("expired at ", time.Now())
|
|
|
|
|
+ s.Delete(key)
|
|
|
|
|
+ return
|
|
|
|
|
+ case updateTime := <-update:
|
|
|
|
|
+ //update duration
|
|
|
|
|
+ if updateTime.Equal(time.Unix(0,0)) {
|
|
|
|
|
+ fmt.Println("node became stable")
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+ duration = updateTime.Sub(time.Now())
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -62,12 +121,12 @@ func (s *Store) Set(key string, value string) Response {
|
|
|
func (s *Store) Get(key string) Response {
|
|
func (s *Store) Get(key string) Response {
|
|
|
key = path.Clean(key)
|
|
key = path.Clean(key)
|
|
|
|
|
|
|
|
- value, ok := s.Nodes[key]
|
|
|
|
|
|
|
+ node, ok := s.Nodes[key]
|
|
|
|
|
|
|
|
if ok {
|
|
if ok {
|
|
|
- return Response{GET, key, value, value, true}
|
|
|
|
|
|
|
+ return Response{GET, key, node.Value, node.Value, true, node.ExpireTime}
|
|
|
} else {
|
|
} else {
|
|
|
- return Response{GET, key, "", value, false}
|
|
|
|
|
|
|
+ return Response{GET, key, "", "", false, time.Unix(0, 0)}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -75,16 +134,16 @@ func (s *Store) Get(key string) Response {
|
|
|
func (s *Store) Delete(key string) Response {
|
|
func (s *Store) Delete(key string) Response {
|
|
|
key = path.Clean(key)
|
|
key = path.Clean(key)
|
|
|
|
|
|
|
|
- oldValue, ok := s.Nodes[key]
|
|
|
|
|
|
|
+ node, ok := s.Nodes[key]
|
|
|
|
|
|
|
|
if ok {
|
|
if ok {
|
|
|
delete(s.Nodes, key)
|
|
delete(s.Nodes, key)
|
|
|
|
|
|
|
|
- w.notify(DELETE, key, oldValue, "", true)
|
|
|
|
|
|
|
+ w.notify(DELETE, key, node.Value, "", true)
|
|
|
|
|
|
|
|
- return Response{DELETE, key, oldValue, "", true}
|
|
|
|
|
|
|
+ return Response{DELETE, key, node.Value, "", true, node.ExpireTime}
|
|
|
} else {
|
|
} else {
|
|
|
- return Response{DELETE, key, "", "", false}
|
|
|
|
|
|
|
+ return Response{DELETE, key, "", "", false, time.Unix(0, 0)}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|