Skip to content

Commit

Permalink
svs: refactor hash
Browse files Browse the repository at this point in the history
  • Loading branch information
pulsejet committed Jan 14, 2025
1 parent 5079fca commit f658d02
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 50 deletions.
55 changes: 8 additions & 47 deletions std/sync/svs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"math"
rand "math/rand/v2"
"sort"
"sync"
"sync/atomic"
"time"
Expand All @@ -28,8 +27,7 @@ type SvSync struct {

mutex sync.Mutex
state svMap
names map[uint64]enc.Name
mtime map[uint64]time.Time
mtime map[string]time.Time

suppress bool
merge svMap
Expand Down Expand Up @@ -90,8 +88,7 @@ func NewSvSync(opts SvSyncOpts) *SvSync {

mutex: sync.Mutex{},
state: make(svMap),
names: make(map[uint64]enc.Name),
mtime: make(map[uint64]time.Time),
mtime: make(map[string]time.Time),

suppress: false,
merge: make(svMap),
Expand Down Expand Up @@ -152,7 +149,7 @@ func (s *SvSync) SetSeqNo(name enc.Name, seqNo uint64) error {
s.mutex.Lock()
defer s.mutex.Unlock()

hash := s.hashName(name)
hash := name.String()

entry := s.state.get(hash, s.o.BootTime)
if seqNo <= entry.SeqNo {
Expand All @@ -173,7 +170,7 @@ func (s *SvSync) IncrSeqNo(name enc.Name) uint64 {
s.mutex.Lock()
defer s.mutex.Unlock()

hash := s.hashName(name)
hash := name.String()

entry := s.state.get(hash, s.o.BootTime)
s.state.set(hash, s.o.BootTime, entry.SeqNo+1)
Expand All @@ -189,14 +186,6 @@ func (s *SvSync) GetBootTime() uint64 {
return s.o.BootTime
}

func (s *SvSync) hashName(name enc.Name) uint64 {
hash := name.Hash()
if _, ok := s.names[hash]; !ok {
s.names[hash] = name.Clone()
}
return hash
}

func (s *SvSync) onReceiveStateVector(sv *spec_svs.StateVector) {
s.mutex.Lock()
defer s.mutex.Unlock()
Expand All @@ -206,7 +195,7 @@ func (s *SvSync) onReceiveStateVector(sv *spec_svs.StateVector) {
recvSv := make(svMap, len(sv.Entries))

for _, node := range sv.Entries {
hash := s.hashName(node.Name)
hash := node.Name.String()

for _, entry := range node.SeqNoEntries {
recvSv.set(hash, entry.BootstrapTime, entry.SeqNo)
Expand Down Expand Up @@ -312,15 +301,16 @@ func (s *SvSync) sendSyncInterest() {
}

// Critical section
svWire := func() enc.Wire {
sv := func() *spec_svs.StateVector {
s.mutex.Lock()
defer s.mutex.Unlock()

// [Spec*] Sending always triggers Steady State
s.enterSteadyState()

return s.encodeSv()
return s.state.tlv()
}()
svWire := (&spec_svs.SvsData{StateVector: sv}).Encode()

// SVS v3 Sync Data
syncName := s.o.GroupPrefix.Append(enc.NewVersionComponent(3))
Expand Down Expand Up @@ -390,35 +380,6 @@ func (s *SvSync) onSyncInterest(interest ndn.Interest) {
s.recvSv <- params.StateVector
}

// Call with mutex locked
func (s *SvSync) encodeSv() enc.Wire {
entries := make([]*spec_svs.StateVectorEntry, 0, len(s.state))
for nameHash, seqEntrs := range s.state {
seqEntrPtrs := make([]*spec_svs.SeqNoEntry, 0, len(seqEntrs))
for _, e := range seqEntrs {
if e.SeqNo > 0 {
seqEntrPtrs = append(seqEntrPtrs, &e)
}
}

entries = append(entries, &spec_svs.StateVectorEntry{
Name: s.names[nameHash],
SeqNoEntries: seqEntrPtrs,
})
}

// Sort entries by in the NDN canonical order
sort.Slice(entries, func(i, j int) bool {
return entries[i].Name.Compare(entries[j].Name) < 0
})

params := spec_svs.SvsData{
StateVector: &spec_svs.StateVector{Entries: entries},
}

return params.Encode()
}

// Call with mutex locked
func (s *SvSync) enterSteadyState() {
s.suppress = false
Expand Down
38 changes: 35 additions & 3 deletions std/sync/sv_map.go → std/sync/svs_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ package sync
import (
"sort"

enc "github.com/named-data/ndnd/std/encoding"
"github.com/named-data/ndnd/std/log"
spec_svs "github.com/named-data/ndnd/std/ndn/svs/v3"
)

// Map representation of the state vector.
type svMap map[uint64][]spec_svs.SeqNoEntry
type svMap map[string][]spec_svs.SeqNoEntry

// Get seq entry for a bootstrap time.
func (m svMap) get(hash uint64, btime uint64) spec_svs.SeqNoEntry {
func (m svMap) get(hash string, btime uint64) spec_svs.SeqNoEntry {
for _, entry := range m[hash] {
if entry.BootstrapTime == btime {
return entry
Expand All @@ -23,7 +25,7 @@ func (m svMap) get(hash uint64, btime uint64) spec_svs.SeqNoEntry {
}

// Set seq entry for a bootstrap time.
func (m svMap) set(hash uint64, btime uint64, seq uint64) {
func (m svMap) set(hash string, btime uint64, seq uint64) {
for i, entry := range m[hash] {
if entry.BootstrapTime == btime {
m[hash][i].SeqNo = seq
Expand Down Expand Up @@ -60,3 +62,33 @@ func (m svMap) isNewerThan(other svMap, existOnly bool) bool {
}
return false
}

func (m svMap) tlv() *spec_svs.StateVector {
entries := make([]*spec_svs.StateVectorEntry, 0, len(m))
for hash, seqEntrs := range m {
seqEntrPtrs := make([]*spec_svs.SeqNoEntry, 0, len(seqEntrs))
for _, e := range seqEntrs {
if e.SeqNo > 0 {
seqEntrPtrs = append(seqEntrPtrs, &e)
}
}

name, err := enc.NameFromStr(hash)
if err != nil {
log.Error(nil, "Invalid name in SV map", "hash", hash)
continue
}

entries = append(entries, &spec_svs.StateVectorEntry{
Name: name,
SeqNoEntries: seqEntrPtrs,
})
}

// Sort entries by in the NDN canonical order
sort.Slice(entries, func(i, j int) bool {
return entries[i].Name.Compare(entries[j].Name) < 0
})

return &spec_svs.StateVector{Entries: entries}
}

0 comments on commit f658d02

Please sign in to comment.