diff --git a/common/consistenthash/consistenthash.go b/common/consistenthash/consistenthash.go index dde99a9..32d47f2 100644 --- a/common/consistenthash/consistenthash.go +++ b/common/consistenthash/consistenthash.go @@ -21,8 +21,27 @@ func (r *Ring) search(key string) int { ///////////////////////// // YOUR CODE GOES HERE // ///////////////////////// + var hashNumber = hashId(key) + var norm = 360 + var value = 0 + var ret = 0 + r.Lock() + defer r.Unlock() + + var i = 0 + for i < r.Nodes.Len() && r.Nodes[i].Id != key { + value = int(r.Nodes[i].HashId - hashNumber) + if value < 0 { + value = -value + } + if value < norm { + norm = value + ret = i + } + i++ + } - return 0 + return ret } // 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..21f403a 100644 --- a/dynamo/cache.go +++ b/dynamo/cache.go @@ -12,6 +12,7 @@ import ( // Cache is the struct that handle all the data storage for the dynamo server. type Cache struct { data map[string]string + time int64 sync.Mutex } @@ -29,7 +30,7 @@ func NewCache() *Cache { func (cache *Cache) Get(key string) (value string, timestamp int64) { cache.Lock() value = cache.data[key] - timestamp = 0 + timestamp = cache.time cache.Unlock() log.Printf("[CACHE] Getting Key '%v' with Value '%v' @ timestamp '%v'\n", key, value, timestamp) @@ -43,6 +44,7 @@ func (cache *Cache) Put(key string, value string, timestamp int64) { cache.Lock() cache.data[key] = value + cache.time = timestamp cache.Unlock() return diff --git a/dynamo/coordinator.go b/dynamo/coordinator.go index 408cc9e..8f332ab 100644 --- a/dynamo/coordinator.go +++ b/dynamo/coordinator.go @@ -190,9 +190,18 @@ func aggregateVotes(votes []*vote) (result string) { log.Printf("[COORDINATOR] Vote: %v\n", vote.value) } + var rightVote = 0 + var i = 0 + for _, vote := range votes { + if(vote.timestamp > votes[rightVote].timestamp){ + rightVote = i + } + i++ + } + ///////////////////////// // YOUR CODE GOES HERE // ///////////////////////// - result = votes[0].value + result = votes[rightVote].value return }