Refine dependency descriptor based selection forwarder (#1808)
* Don't update dependency info if unordered packet received * Trace all active svc chains for downtrack * Try to keep lower decode target decodable * remove comments * Test case * clean code * solve comments
This commit is contained in:
@@ -33,18 +33,23 @@ func (s selectorDecision) String() string {
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
type SelectorDecisionCache struct {
|
||||
initialized bool
|
||||
base uint64
|
||||
last uint64
|
||||
masks []uint64
|
||||
numEntries uint64
|
||||
initialized bool
|
||||
base uint64
|
||||
last uint64
|
||||
masks []uint64
|
||||
numEntries uint64
|
||||
numNackEntries uint64
|
||||
|
||||
onExpectEntityChanged map[uint64][]func(entity uint64, decision selectorDecision)
|
||||
}
|
||||
|
||||
func NewSelectorDecisionCache(maxNumElements uint64) *SelectorDecisionCache {
|
||||
func NewSelectorDecisionCache(maxNumElements uint64, numNackEntries uint64) *SelectorDecisionCache {
|
||||
numElements := (maxNumElements*2 + 63) / 64
|
||||
return &SelectorDecisionCache{
|
||||
masks: make([]uint64, numElements),
|
||||
numEntries: numElements * 32, // 2 bits per entry
|
||||
masks: make([]uint64, numElements),
|
||||
numEntries: numElements * 32, // 2 bits per entry
|
||||
numNackEntries: numNackEntries,
|
||||
onExpectEntityChanged: make(map[uint64][]func(entity uint64, decision selectorDecision)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,19 +62,39 @@ func (s *SelectorDecisionCache) AddDropped(entity uint64) {
|
||||
}
|
||||
|
||||
func (s *SelectorDecisionCache) GetDecision(entity uint64) (selectorDecision, error) {
|
||||
if !s.initialized || entity > s.last || entity < s.base {
|
||||
if !s.initialized || entity < s.base {
|
||||
return selectorDecisionMissing, nil
|
||||
}
|
||||
|
||||
if entity > s.last {
|
||||
return selectorDecisionUnknown, nil
|
||||
}
|
||||
|
||||
offset := s.last - entity
|
||||
if offset >= s.numEntries {
|
||||
// asking for something too old
|
||||
return selectorDecisionUnknown, fmt.Errorf("too old, oldest: %d, asking: %d", s.last-s.numEntries+1, entity)
|
||||
return selectorDecisionMissing, fmt.Errorf("too old, oldest: %d, asking: %d", s.last-s.numEntries+1, entity)
|
||||
}
|
||||
|
||||
return s.getEntity(entity), nil
|
||||
}
|
||||
|
||||
func (s *SelectorDecisionCache) ExpectDecision(entity uint64, f func(entity uint64, decision selectorDecision)) bool {
|
||||
if !s.initialized || entity < s.base {
|
||||
return false
|
||||
}
|
||||
|
||||
if entity < s.last {
|
||||
offset := s.last - entity
|
||||
if offset >= s.numEntries {
|
||||
return false // too old
|
||||
}
|
||||
}
|
||||
|
||||
s.onExpectEntityChanged[entity] = append(s.onExpectEntityChanged[entity], f)
|
||||
return true
|
||||
}
|
||||
|
||||
func (s *SelectorDecisionCache) addEntity(entity uint64, sd selectorDecision) {
|
||||
if !s.initialized {
|
||||
s.initialized = true
|
||||
@@ -90,16 +115,60 @@ func (s *SelectorDecisionCache) addEntity(entity uint64, sd selectorDecision) {
|
||||
}
|
||||
|
||||
for e := s.last + 1; e != entity; e++ {
|
||||
s.setEntity(e, selectorDecisionMissing)
|
||||
s.setEntity(e, selectorDecisionUnknown)
|
||||
}
|
||||
|
||||
// update [last+1-nack, entity-nack) to missing
|
||||
missingStart := s.last
|
||||
if missingStart > s.numNackEntries+s.base {
|
||||
missingStart -= s.numNackEntries
|
||||
} else {
|
||||
missingStart = s.base
|
||||
}
|
||||
missingEnd := entity
|
||||
if missingEnd > s.numNackEntries+s.base {
|
||||
missingEnd -= s.numNackEntries
|
||||
} else {
|
||||
missingEnd = s.base
|
||||
}
|
||||
if missingEnd > missingStart {
|
||||
for e := missingStart; e != missingEnd; e++ {
|
||||
s.setEntityIfUnknown(e, selectorDecisionMissing)
|
||||
}
|
||||
}
|
||||
|
||||
s.setEntity(entity, sd)
|
||||
s.last = entity
|
||||
|
||||
for e, fns := range s.onExpectEntityChanged {
|
||||
if e+s.numEntries < s.last {
|
||||
delete(s.onExpectEntityChanged, e)
|
||||
for _, f := range fns {
|
||||
f(e, selectorDecisionMissing)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SelectorDecisionCache) setEntityIfUnknown(entity uint64, sd selectorDecision) {
|
||||
if s.getEntity(entity) == selectorDecisionUnknown {
|
||||
s.setEntity(entity, sd)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SelectorDecisionCache) setEntity(entity uint64, sd selectorDecision) {
|
||||
index, bitpos := s.getPos(entity)
|
||||
s.masks[index] &= ^(0x3 << bitpos) // clear before bitwise OR
|
||||
s.masks[index] |= (uint64(sd) & 0x3) << bitpos
|
||||
|
||||
if sd != selectorDecisionUnknown {
|
||||
if fns, ok := s.onExpectEntityChanged[entity]; ok {
|
||||
delete(s.onExpectEntityChanged, entity)
|
||||
for _, f := range fns {
|
||||
f(entity, sd)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SelectorDecisionCache) getEntity(entity uint64) selectorDecision {
|
||||
|
||||
Reference in New Issue
Block a user