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 do Lab #16

Open
wants to merge 3 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
24 changes: 21 additions & 3 deletions common/consistenthash/consistenthash.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"hash/crc32"
"sort"
"sync"
"math"
)

var ErrNodeNotFound = errors.New("node not found")
Expand All @@ -21,8 +22,26 @@ func (r *Ring) search(key string) int {
/////////////////////////
// YOUR CODE GOES HERE //
/////////////////////////

return 0
//log.Printf("key: %v , hashed form: %v", key, hashId(key))
hashFormKey:=float64(hashId(key))
Copy link
Owner

Choose a reason for hiding this comment

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

NIT: Essa conversão para float é realmente necessária?

//log.Printf("hashFormKey: %v", hashFormKey)
min:=math.MaxFloat64
index:=-1
for i, node := range r.Nodes {
//log.Printf("node %v: Id = %v hashId = %v", i, node.Id, node.HashId)
nodeHash:=float64(node.HashId)
if nodeHash>hashFormKey && min>nodeHash-hashFormKey{
min=nodeHash-hashFormKey
index=i
}
}
//log.Printf("answer: %v", index)
if index==-1{
return 0
}else{
return index
}
//return 0
}

// NewRing will create a new Ring object and return a pointer to it.
Expand Down Expand Up @@ -79,7 +98,6 @@ func (r *Ring) Get(id string) string {
if i >= r.Nodes.Len() {
i = 0
}

return r.Nodes[i].Id
}

Expand Down
12 changes: 7 additions & 5 deletions dynamo/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ import (
type Cache struct {
data map[string]string
sync.Mutex
timestamp map[string]int64
}

// Create a new cache object and return a pointer to it.
func NewCache() *Cache {
var s Cache

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

s.timestamp=make(map[string]int64)
return &s
}

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.timestamp[key]
cache.Unlock()

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

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

return
}

// Retrieve all information from the server. This shouldn't be used in any way
// except for testing purposes.
func (cache *Cache) getAll() (data map[string]string, timestamps map[string]int64) {
func (cache *Cache) getAll() (data map[string]string, timestamp map[string]int64) {
data = cache.data
timestamps = make(map[string]int64)
return data, timestamps
timestamp = cache.timestamp
return data, timestamp
}
16 changes: 10 additions & 6 deletions dynamo/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,16 @@ 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 {
log.Printf("[COORDINATOR] Vote: %v\n", vote.value)
log.Printf("[COORDINATOR] Vote: %v %v\n", vote.value, vote.timestamp)
}

/////////////////////////
// YOUR CODE GOES HERE //
/////////////////////////
result = votes[0].value
max:=votes[0].timestamp
index:=0
for i,vote := range votes {
if vote.timestamp>max{
max=vote.timestamp
index=i
}
}
result = votes[index].value
return
}
5 changes: 1 addition & 4 deletions dynamo/ring.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ type Ring struct {
// NewRing creates a new Ring object and return a pointer to it.
func NewRing(server *Server) *Ring {
var newRing *Ring

newRing = new(Ring)
newRing.hashring = consistenthash.NewRing()
newRing.idToHostname = make(map[string]string)
Expand Down Expand Up @@ -52,19 +51,17 @@ func (ring *Ring) AddNode(id, hostname string) {
// coordinator is the first node that has a hash higher than the hash of the key.
func (ring *Ring) GetCoordinator(key string) (id, hostname string) {
id = ring.hashring.Get(key)
hostname = ring.idToHostname[id]
hostname = ring.idToHostname[id]
return
}

// GetNextCoordinator return the next node in the ring starting from the node
// represented by id.
func (ring *Ring) GetNextCoordinator(id string) (nextId, hostname string, err error) {
nextId, err = ring.hashring.GetNext(id)

if err != nil {
return
}

hostname = ring.idToHostname[nextId]
return
}
Expand Down