-
Notifications
You must be signed in to change notification settings - Fork 9
/
pit-cs-tree.go
427 lines (372 loc) · 12.7 KB
/
pit-cs-tree.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package table
import (
"bytes"
"math/rand"
"time"
"github.com/cespare/xxhash"
"github.com/named-data/YaNFD/core"
"github.com/named-data/YaNFD/ndn"
"github.com/named-data/YaNFD/utils/priority_queue"
)
const expiredPitTickerInterval = 100 * time.Millisecond
// PitCsTree represents a PIT-CS implementation that uses a name tree
type PitCsTree struct {
basePitCsTable
root *pitCsTreeNode
nPitEntries int
pitTokenMap map[uint32]*nameTreePitEntry
nCsEntries int
csReplacement CsReplacementPolicy
csMap map[uint64]*nameTreeCsEntry
pitExpiryQueue priority_queue.Queue[*nameTreePitEntry, int64]
}
type nameTreePitEntry struct {
basePitEntry // compose with BasePitEntry
pitCsTable *PitCsTree // pointer to tree
node *pitCsTreeNode // the tree node associated with this entry
queueIndex int // index of entry in the expiring queue
}
type nameTreeCsEntry struct {
baseCsEntry // compose with BasePitEntry
node *pitCsTreeNode // the tree node associated with this entry
}
// pitCsTreeNode represents an entry in a PIT-CS tree.
type pitCsTreeNode struct {
component ndn.NameComponent
depth int
parent *pitCsTreeNode
children []*pitCsTreeNode
pitEntries []*nameTreePitEntry
csEntry *nameTreeCsEntry
}
// NewPitCS creates a new combined PIT-CS for a forwarding thread.
func NewPitCS() *PitCsTree {
pitCs := new(PitCsTree)
pitCs.root = new(pitCsTreeNode)
pitCs.root.component = nil // Root component will be nil since it represents zero components
pitCs.root.pitEntries = make([]*nameTreePitEntry, 0)
pitCs.expiringPitEntries = make(chan PitEntry, tableQueueSize)
pitCs.pitTokenMap = make(map[uint32]*nameTreePitEntry)
pitCs.pitExpiryQueue = priority_queue.New[*nameTreePitEntry, int64]()
// This value has already been validated from loading the configuration, so we know it will be one of the following (or else fatal)
switch csReplacementPolicy {
case "lru":
pitCs.csReplacement = NewCsLRU(pitCs)
default:
core.LogFatal(pitCs, "Unknown CS replacement policy ", csReplacementPolicy)
}
pitCs.csMap = make(map[uint64]*nameTreeCsEntry)
// Set up the expired PIT entries goroutine
go pitCs.expirationPitLoop()
return pitCs
}
func (p *PitCsTree) expirationPitLoop() {
for !core.ShouldQuit {
if p.pitExpiryQueue.Len() > 0 {
sleepTime := time.Duration(p.pitExpiryQueue.PeekPriority()-time.Now().UnixNano()) * time.Nanosecond
if sleepTime > 0 {
if sleepTime > expiredPitTickerInterval {
sleepTime = expiredPitTickerInterval
}
time.Sleep(sleepTime)
}
} else {
time.Sleep(expiredPitTickerInterval)
}
for p.pitExpiryQueue.Len() > 0 && p.pitExpiryQueue.PeekPriority() <= time.Now().UnixNano() {
entry := p.pitExpiryQueue.Pop()
entry.queueIndex = -1
p.expiringPitEntries <- entry
p.RemoveInterest(entry)
}
}
}
func (p *PitCsTree) updatePitExpiry(pitEntry PitEntry) {
e := pitEntry.(*nameTreePitEntry)
if e.queueIndex < 0 {
e.queueIndex = p.pitExpiryQueue.Push(e, e.expirationTime.UnixNano())
} else {
p.pitExpiryQueue.Update(e.queueIndex, e, e.expirationTime.UnixNano())
}
}
func (e *nameTreePitEntry) PitCs() PitCsTable {
return e.pitCsTable
}
// InsertInterest inserts an entry in the PIT upon receipt of an Interest.
// Returns tuple of PIT entry and whether the Nonce is a duplicate.
func (p *PitCsTree) InsertInterest(interest *ndn.Interest, hint *ndn.Name, inFace uint64) (PitEntry, bool) {
node := p.root.fillTreeToPrefix(interest.Name())
var entry *nameTreePitEntry
for _, curEntry := range node.pitEntries {
if curEntry.CanBePrefix() == interest.CanBePrefix() && curEntry.MustBeFresh() == interest.MustBeFresh() && ((hint == nil && curEntry.ForwardingHint() == nil) || hint.Equals(curEntry.ForwardingHint())) {
entry = curEntry
break
}
}
if entry == nil {
p.nPitEntries++
entry = new(nameTreePitEntry)
entry.node = node
entry.pitCsTable = p
entry.name = interest.Name()
entry.canBePrefix = interest.CanBePrefix()
entry.mustBeFresh = interest.MustBeFresh()
entry.forwardingHint = hint
entry.inRecords = make(map[uint64]*PitInRecord)
entry.outRecords = make(map[uint64]*PitOutRecord)
entry.satisfied = false
node.pitEntries = append(node.pitEntries, entry)
entry.token = p.generateNewPitToken()
entry.queueIndex = -1
p.pitTokenMap[entry.token] = entry
}
for face, inRecord := range entry.inRecords {
// Only considered a duplicate (loop) if from different face since is just retransmission and not loop if same face
if face != inFace && bytes.Equal(inRecord.LatestNonce, interest.Nonce()) {
return entry, true
}
}
// Cancel expiration time
entry.expirationTime = time.Unix(0, 0)
return entry, false
}
// RemoveInterest removes the specified PIT entry, returning true if the entry
// was removed and false if was not (because it does not exist).
func (p *PitCsTree) RemoveInterest(pitEntry PitEntry) bool {
e := pitEntry.(*nameTreePitEntry) // No error check needed because PitCsTree always uses nameTreePitEntry
for i, entry := range e.node.pitEntries {
if entry == pitEntry {
if len(e.node.pitEntries) > 1 {
e.node.pitEntries[i] = e.node.pitEntries[len(e.node.pitEntries)-1]
}
e.node.pitEntries = e.node.pitEntries[:len(e.node.pitEntries)-1]
if len(e.node.pitEntries) == 0 {
entry.node.pruneIfEmpty()
}
p.nPitEntries--
delete(p.pitTokenMap, e.token)
return true
}
}
return false
}
// FindInterestExactMatch returns the PIT entry for an exact match of the
// given interest.
func (p *PitCsTree) FindInterestExactMatch(interest *ndn.Interest) PitEntry {
node := p.root.findExactMatchEntry(interest.Name())
if node != nil {
for _, curEntry := range node.pitEntries {
if curEntry.CanBePrefix() == interest.CanBePrefix() && curEntry.MustBeFresh() == interest.MustBeFresh() {
return curEntry
}
}
}
return nil
}
// FindInterestPrefixMatchByData returns all interests that could be satisfied
// by the given data.
// Example: If we have interests /a and /a/b, a prefix search for data with name /a/b
// will return PitEntries for both /a and /a/b
func (p *PitCsTree) FindInterestPrefixMatchByData(data *ndn.Data, token *uint32) []PitEntry {
if token != nil {
if entry, ok := p.pitTokenMap[*token]; ok && entry.Token() == *token {
return []PitEntry{entry}
}
return nil
}
return p.findInterestPrefixMatchByName(data.Name())
}
func (p *PitCsTree) findInterestPrefixMatchByName(name *ndn.Name) []PitEntry {
matching := make([]PitEntry, 0)
dataNameLen := name.Size()
for curNode := p.root.findLongestPrefixEntry(name); curNode != nil; curNode = curNode.parent {
for _, entry := range curNode.pitEntries {
if entry.canBePrefix || curNode.depth == dataNameLen {
matching = append(matching, entry)
}
}
}
return matching
}
// PitSize returns the number of entries in the PIT.
func (p *PitCsTree) PitSize() int {
return p.nPitEntries
}
// CsSize returns the number of entries in the CS.
func (p *PitCsTree) CsSize() int {
return p.nCsEntries
}
// IsCsAdmitting returns whether the CS is admitting content.
func (p *PitCsTree) IsCsAdmitting() bool {
return csAdmit
}
// IsCsServing returns whether the CS is serving content.
func (p *PitCsTree) IsCsServing() bool {
return csServe
}
// InsertOutRecord inserts an outrecord for the given interest, updating the
// preexisting one if it already occcurs.
func (e *nameTreePitEntry) InsertOutRecord(interest *ndn.Interest, face uint64) *PitOutRecord {
var record *PitOutRecord
var ok bool
if record, ok = e.outRecords[face]; !ok {
record := new(PitOutRecord)
record.Face = face
record.LatestNonce = interest.Nonce()
record.LatestTimestamp = time.Now()
record.LatestInterest = interest
record.ExpirationTime = time.Now().Add(interest.Lifetime())
e.outRecords[face] = record
return record
}
// Existing record
record.LatestNonce = interest.Nonce()
record.LatestTimestamp = time.Now()
record.LatestInterest = interest
record.ExpirationTime = time.Now().Add(interest.Lifetime())
return record
}
// GetOutRecords returns all outrecords for the given PIT entry.
func (e *nameTreePitEntry) GetOutRecords() []*PitOutRecord {
records := make([]*PitOutRecord, 0)
for _, value := range e.outRecords {
records = append(records, value)
}
return records
}
func (p *pitCsTreeNode) findExactMatchEntry(name *ndn.Name) *pitCsTreeNode {
if name.Size() > p.depth {
for _, child := range p.children {
if name.At(child.depth - 1).Equals(child.component) {
return child.findExactMatchEntry(name)
}
}
} else if name.Size() == p.depth {
return p
}
return nil
}
func (p *pitCsTreeNode) findLongestPrefixEntry(name *ndn.Name) *pitCsTreeNode {
if name.Size() > p.depth {
for _, child := range p.children {
if name.At(child.depth - 1).Equals(child.component) {
return child.findLongestPrefixEntry(name)
}
}
}
return p
}
func (p *pitCsTreeNode) fillTreeToPrefix(name *ndn.Name) *pitCsTreeNode {
curNode := p.findLongestPrefixEntry(name)
for depth := curNode.depth + 1; depth <= name.Size(); depth++ {
newNode := new(pitCsTreeNode)
newNode.component = name.At(depth - 1).DeepCopy()
newNode.depth = depth
newNode.parent = curNode
curNode.children = append(curNode.children, newNode)
curNode = newNode
}
return curNode
}
func (p *pitCsTreeNode) pruneIfEmpty() {
for curNode := p; curNode.parent != nil && len(curNode.children) == 0 && len(curNode.pitEntries) == 0 && curNode.csEntry == nil; curNode = curNode.parent {
// Remove from parent's children
for i, child := range curNode.parent.children {
if child == p {
if len(curNode.parent.children) > 1 {
curNode.parent.children[i] = curNode.parent.children[len(curNode.parent.children)-1]
}
curNode.parent.children = curNode.parent.children[:len(curNode.parent.children)-1]
break
}
}
}
}
func (p *PitCsTree) generateNewPitToken() uint32 {
for {
token := rand.Uint32()
if _, ok := p.pitTokenMap[token]; !ok {
return token
}
}
}
func (p *PitCsTree) hashCsName(name *ndn.Name) uint64 {
return xxhash.Sum64String(name.String())
}
// FindMatchingDataFromCS finds the best matching entry in the CS (if any).
// If MustBeFresh is set to true in the Interest, only non-stale CS entries
// will be returned.
func (p *PitCsTree) FindMatchingDataFromCS(interest *ndn.Interest) CsEntry {
node := p.root.findExactMatchEntry(interest.Name())
if node != nil {
if !interest.CanBePrefix() {
if node.csEntry != nil {
p.csReplacement.BeforeUse(node.csEntry.index, node.csEntry.data)
return node.csEntry
}
// Return nil instead of node.csEntry so that
// the return type is nil rather than CSEntry{nil}
return nil
}
return node.findMatchingDataCSPrefix(interest)
}
return nil
}
// InsertData inserts a Data packet into the Content Store.
func (p *PitCsTree) InsertData(data *ndn.Data) {
index := p.hashCsName(data.Name())
if entry, ok := p.csMap[index]; ok {
// Replace existing entry
entry.data = data
if data.MetaInfo() == nil || data.MetaInfo().FinalBlockID() == nil {
entry.staleTime = time.Now()
} else {
entry.staleTime = time.Now().Add(*data.MetaInfo().FreshnessPeriod())
}
p.csReplacement.AfterRefresh(index, data)
} else {
// New entry
p.nCsEntries++
node := p.root.fillTreeToPrefix(data.Name())
node.csEntry = new(nameTreeCsEntry)
node.csEntry.node = node
node.csEntry.index = index
node.csEntry.data = data
p.csMap[index] = node.csEntry
p.csReplacement.AfterInsert(index, data)
// Tell replacement strategy to evict entries if needed
p.csReplacement.EvictEntries()
}
}
// eraseCsDataFromReplacementStrategy allows the replacement strategy to
// erase the data with the specified name from the Content Store.
func (p *PitCsTree) eraseCsDataFromReplacementStrategy(index uint64) {
if entry, ok := p.csMap[index]; ok {
entry.node.csEntry = nil
delete(p.csMap, index)
p.nCsEntries--
}
}
// Given a pitCsTreeNode that is the longest prefix match of an interest, look for any
// CS data rechable from this pitCsTreeNode. This function must be called only after
// the interest as far as possible with the nodes components in the PitCSTree.
// For example, if we have data for /a/b/v=10 and the interest is /a/b,
// p should be the `b` node, not the root node.
func (p *pitCsTreeNode) findMatchingDataCSPrefix(interest *ndn.Interest) CsEntry {
if p.csEntry != nil && (!interest.MustBeFresh() || time.Now().Before(p.csEntry.staleTime)) {
// A csEntry exists at this node and is acceptable to satisfy the interest
return p.csEntry
}
// No csEntry at current node, look farther down the tree
// We must have already matched the entire interest name
if p.depth >= interest.Name().Size() {
for _, child := range p.children {
potentialMatch := child.findMatchingDataCSPrefix(interest)
if potentialMatch != nil {
return potentialMatch
}
}
}
// If found none, then return
return nil
}