From c7042e45004b5830b595943bd142e16915b891cb Mon Sep 17 00:00:00 2001 From: brunocorrea23 Date: Wed, 19 Oct 2016 19:42:38 -0200 Subject: [PATCH] solucao lab2 --- common/consistenthash/consistenthash.go | 10 ++++++++++ dynamo/cache.go | 5 ++++- dynamo/coordinator.go | 22 +++++++++++++++++++++- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/common/consistenthash/consistenthash.go b/common/consistenthash/consistenthash.go index dde99a9..f66581c 100644 --- a/common/consistenthash/consistenthash.go +++ b/common/consistenthash/consistenthash.go @@ -21,6 +21,16 @@ func (r *Ring) search(key string) int { ///////////////////////// // YOUR CODE GOES HERE // ///////////////////////// + r.Lock() + defer r.Unlock() + + hash := hashId(key) + + for i, node := range r.Nodes { + if node.HashId > hash { + return i + } + } return 0 } diff --git a/dynamo/cache.go b/dynamo/cache.go index 0483a39..c0736e6 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 map[string]int64 sync.Mutex } @@ -20,6 +21,7 @@ func NewCache() *Cache { var s Cache s.data = make(map[string]string) + s.time = make(map[string]int64) return &s } @@ -29,7 +31,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[key] cache.Unlock() log.Printf("[CACHE] Getting Key '%v' with Value '%v' @ timestamp '%v'\n", key, value, timestamp) @@ -43,6 +45,7 @@ func (cache *Cache) Put(key string, value string, timestamp int64) { cache.Lock() cache.data[key] = value + cache.time[key] = timestamp cache.Unlock() return diff --git a/dynamo/coordinator.go b/dynamo/coordinator.go index 408cc9e..18f8289 100644 --- a/dynamo/coordinator.go +++ b/dynamo/coordinator.go @@ -186,13 +186,33 @@ 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) { + var ( + horario_max int64 + ) + + horario_max = 0 + for _, vote := range votes { log.Printf("[COORDINATOR] Vote: %v\n", vote.value) + + if vote.timestamp > horario_max { + result = vote.value + horario_max = vote.timestamp + } + } ///////////////////////// // YOUR CODE GOES HERE // ///////////////////////// - result = votes[0].value + //result = votes[0].value + return } + + +// type vote struct { +// value string +// timestamp int64 +// err error +// } \ No newline at end of file