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

Solução final #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 1 addition & 5 deletions common/consistenthash/consistenthash.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 r.Nodes.LowerBound(hashId(key))
}

// NewRing will create a new Ring object and return a pointer to it.
Expand Down
11 changes: 11 additions & 0 deletions common/consistenthash/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,14 @@ type Nodes []*Node
func (n Nodes) Len() int { return len(n) }
func (n Nodes) Swap(i, j int) { n[i], n[j] = n[j], n[i] }
func (n Nodes) Less(i, j int) bool { return n[i].HashId < n[j].HashId }

// LowerBound assumes array is sorted
func (n Nodes) LowerBound(val uint32) int {
Copy link
Owner

@PauloAguiar PauloAguiar Oct 19, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpicking: o nome ficou meio confuso. No começo eu achei que dado um valor, você ia retornar o elemento do anel que é o lower bound (o que estaria incorreto), mas na verdade você retorna o elemento para o qual val é lower bound.

for index, elem := range n {
if elem.HashId >= val {
return index
}
}

return 0
}
7 changes: 5 additions & 2 deletions dynamo/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
timestamps map[string]int64
sync.Mutex
}

Expand All @@ -20,6 +21,7 @@ func NewCache() *Cache {
var s Cache

s.data = make(map[string]string)
s.timestamps = make(map[string]int64)

return &s
}
Expand All @@ -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.timestamps[key]
cache.Unlock()

log.Printf("[CACHE] Getting Key '%v' with Value '%v' @ timestamp '%v'\n", key, value, timestamp)
Expand All @@ -43,6 +45,7 @@ func (cache *Cache) Put(key string, value string, timestamp int64) {

cache.Lock()
cache.data[key] = value
cache.timestamps[key] = timestamp
cache.Unlock()

return
Expand All @@ -52,6 +55,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
}
12 changes: 7 additions & 5 deletions dynamo/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,15 @@ 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) {
for _, vote := range votes {
index := 0
for i, vote := range votes {
log.Printf("[COORDINATOR] Vote: %v\n", vote.value)

if votes[i].timestamp > votes[index].timestamp {
index = i
}
}

/////////////////////////
// YOUR CODE GOES HERE //
/////////////////////////
result = votes[0].value
result = votes[index].value
return
}