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

Final solution ces27-lab2 #28

Open
wants to merge 1 commit 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
21 changes: 20 additions & 1 deletion common/consistenthash/consistenthash.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion 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
time int64
sync.Mutex
}

Expand All @@ -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)
Expand All @@ -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
Expand Down
11 changes: 10 additions & 1 deletion dynamo/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}