From ef03e13a8730839189937f5222cbee1a8cf569cc Mon Sep 17 00:00:00 2001 From: Branden J Brown Date: Thu, 13 Mar 2025 20:54:31 -0400 Subject: [PATCH] don't handle keys that aren't owned --- chord/topology.go | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/chord/topology.go b/chord/topology.go index 68b56a5..4f90ca3 100644 --- a/chord/topology.go +++ b/chord/topology.go @@ -47,9 +47,15 @@ func (n *Node) Neighbors(s []Peer) (Peer, []Peer) { return n.pred, append(s, n.succ...) } +func (n *Node) localLocked(id ID) bool { + return contains(n.self.id, n.succ[0].id, id) +} + // IsLocal reports whether this node owns the given key. func (n *Node) IsLocal(id ID) bool { - return contains(n.self.id, n.Successor().id, id) + n.mu.Lock() + defer n.mu.Unlock() + return n.localLocked(id) } // Closest finds the locally known peer which is the closest predecessor of key. @@ -85,19 +91,26 @@ func (n *Node) Closest(id ID) Peer { return n.self } -// Get obtains the value for a key owned by the node. -func (n *Node) Get(k ID) (v string, found bool) { +// GetLocal obtains the value for a key if it is local to and owned by the node. +func (n *Node) GetLocal(k ID) (v string, found bool) { n.mu.Lock() defer n.mu.Unlock() - v, found = n.data[k] + if n.localLocked(k) { + v, found = n.data[k] + } return v, found } -// Set sets the value for a key. -func (n *Node) Set(k ID, v string) { +// SetLocal sets the value for a key. +// Returns false if the key is not owned by the node. +func (n *Node) SetLocal(k ID, v string) bool { n.mu.Lock() defer n.mu.Unlock() - n.data[k] = v + if n.localLocked(k) { + n.data[k] = v + return true + } + return false } // Peer is the ID and address of a node.