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 Lab 2 #21

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
20 changes: 16 additions & 4 deletions common/consistenthash/consistenthash.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,23 @@ 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 //
/////////////////////////
var (
// key's hash
keyHash uint32 = hashId(key)
i int = 0
)

// verifies what node has his hash greater than the key's hash
for i < r.Nodes.Len() && keyHash > r.Nodes[i].HashId {
i++
}

return 0
// if no node has hash greater than the key's hash, return the node with minor hash
if i == r.Nodes.Len() && keyHash > r.Nodes[r.Nodes.Len() - 1].HashId {
return 0
} else {
return i
}
}

// NewRing will create a new Ring object and return a pointer to it.
Expand Down
18 changes: 11 additions & 7 deletions dynamo/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -21,15 +18,20 @@ func NewCache() *Cache {

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

// map used for timestamps storage associated
// by the key
s.timestamps = make(map[string]int64)

return &s
}

// Get the value of a key from the storage. This will handle concurrent get
// requests by locking the structure.
func (cache *Cache) Get(key string) (value string, timestamp int64) {
cache.Lock()
// return the value and timestamp associated to the key
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 @@ -42,7 +44,9 @@ func (cache *Cache) Put(key string, value string, timestamp int64) {
log.Printf("[CACHE] Putting Key '%v' with Value '%v' @ timestamp '%v'\n", key, value, timestamp)

cache.Lock()
cache.data[key] = value
// store the value and timestamp received for that key
cache.data[key] = value
cache.timestamps[key] = timestamp
cache.Unlock()

return
Expand All @@ -52,6 +56,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
}
19 changes: 14 additions & 5 deletions dynamo/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,22 @@ 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 {

var (
resultElement int = 0
)

// for each element that can be voted
for element, vote := range votes {
log.Printf("[COORDINATOR] Vote: %v\n", vote.value)

// verify what value has the major timestamp
if vote.timestamp > votes[resultElement].timestamp {
resultElement = element
}
}

/////////////////////////
// YOUR CODE GOES HERE //
/////////////////////////
result = votes[0].value
// return the value with major timestamp
result = votes[resultElement].value
return
}