diff --git a/common/consistenthash/consistenthash.go b/common/consistenthash/consistenthash.go index dde99a9..65a18e2 100644 --- a/common/consistenthash/consistenthash.go +++ b/common/consistenthash/consistenthash.go @@ -18,11 +18,7 @@ type Ring struct { // search will find the index of the node that is responsible for the range that // includes the hashed value of key. func (r *Ring) search(key string) int { - ///////////////////////// - // YOUR CODE GOES HERE // - ///////////////////////// - - return 0 + return int(hashId(key))% r.Nodes.Len(); } // NewRing will create a new Ring object and return a pointer to it. diff --git a/dynamo/cache.go b/dynamo/cache.go index 0483a39..66881c1 100644 --- a/dynamo/cache.go +++ b/dynamo/cache.go @@ -5,13 +5,10 @@ import ( "sync" ) -/////////////////////////////// -// THIS FILE IS MISSING CODE // -/////////////////////////////// - // Cache is the struct that handle all the data storage for the dynamo server. type Cache struct { data map[string]string + timestamps map[string]int64 sync.Mutex } @@ -20,6 +17,7 @@ func NewCache() *Cache { var s Cache s.data = make(map[string]string) + s.timestamps = make(map[string]int64) return &s } @@ -29,7 +27,7 @@ func NewCache() *Cache { func (cache *Cache) Get(key string) (value string, timestamp int64) { cache.Lock() value = cache.data[key] - timestamp = 0 + timestamp = cache.timestamps[key] cache.Unlock() log.Printf("[CACHE] Getting Key '%v' with Value '%v' @ timestamp '%v'\n", key, value, timestamp) @@ -43,6 +41,7 @@ func (cache *Cache) Put(key string, value string, timestamp int64) { cache.Lock() cache.data[key] = value + cache.timestamps[key] = timestamp cache.Unlock() return @@ -52,6 +51,6 @@ func (cache *Cache) Put(key string, value string, timestamp int64) { // except for testing purposes. func (cache *Cache) getAll() (data map[string]string, timestamps map[string]int64) { data = cache.data - timestamps = make(map[string]int64) + timestamps = cache.timestamps return data, timestamps } diff --git a/dynamo/coordinator.go b/dynamo/coordinator.go index 408cc9e..538e609 100644 --- a/dynamo/coordinator.go +++ b/dynamo/coordinator.go @@ -186,13 +186,14 @@ func (server *Server) Voting(key string, quorum int) (string, error) { // aggregateVotes will select the right value from the votes received. func aggregateVotes(votes []*vote) (result string) { + selectedVote := votes[0] for _, vote := range votes { - log.Printf("[COORDINATOR] Vote: %v\n", vote.value) + log.Printf("[COORDINATOR] Vote: %v @ timestamp: %v\n", vote.value, vote.timestamp) + if vote.timestamp > selectedVote.timestamp { + selectedVote = vote + } } - ///////////////////////// - // YOUR CODE GOES HERE // - ///////////////////////// - result = votes[0].value + result = selectedVote.value return }