Skip to content

Commit

Permalink
Pre-allocate expiredConns slice
Browse files Browse the repository at this point in the history
Signed-off-by: heanlan <[email protected]>
  • Loading branch information
heanlan committed Sep 13, 2021
1 parent 3872f71 commit ebd339a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
5 changes: 3 additions & 2 deletions pkg/agent/flowexporter/connections/conntrack_connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,11 @@ func (cs *ConntrackConnectionStore) checkConnActive(conn *flowexporter.Connectio
return false
}

func (cs *ConntrackConnectionStore) GetExpiredConns(expiredConns []flowexporter.Connection, currTime time.Time) ([]flowexporter.Connection, time.Duration) {
func (cs *ConntrackConnectionStore) GetExpiredConns(expiredConns []flowexporter.Connection, currTime time.Time, maxSize int) ([]flowexporter.Connection, time.Duration) {
cs.AcquireConnStoreLock()
defer cs.ReleaseConnStoreLock()
for {
i := 0
for i < maxSize {
pqItem := cs.connectionStore.expirePriorityQueue.GetTopExpiredItem(currTime)
if pqItem == nil {
break
Expand Down
5 changes: 3 additions & 2 deletions pkg/agent/flowexporter/connections/deny_connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,11 @@ func (ds *DenyConnectionStore) AddOrUpdateConn(conn *flowexporter.Connection, ti
}
}

func (ds *DenyConnectionStore) GetExpiredConns(expiredConns []flowexporter.Connection, currTime time.Time) ([]flowexporter.Connection, time.Duration) {
func (ds *DenyConnectionStore) GetExpiredConns(expiredConns []flowexporter.Connection, currTime time.Time, maxSize int) ([]flowexporter.Connection, time.Duration) {
ds.AcquireConnStoreLock()
defer ds.ReleaseConnStoreLock()
for {
i := 0
for i < maxSize {
pqItem := ds.connectionStore.expirePriorityQueue.GetTopExpiredItem(currTime)
if pqItem == nil {
break
Expand Down
18 changes: 15 additions & 3 deletions pkg/agent/flowexporter/exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ import (
"antrea.io/antrea/pkg/util/env"
)

// We pre-allocate the slice to store expired connections with a fixed size. The
// advantage is every time we export, the connection store lock will only be held
// by a bounded time. The disadvantages are: 1. the constant is irrespective of
// the number of export-needed connections 2. when the number of expired connections
// goes over the constant, the export can not be finished in a single round. It
// could be delayed by conntrack connections polling routine, which also acquires
// the connection store lock. The possible solutions are: 1) take a fraction of the
// size of connection store to approximate the number of expired connections, while
// having a min and a max to handle edge cases, e.g. min(50 + 0.1 * connectionStore.size(), 200)
// 2) do some experiments to find out the optimized constant which have the best performance.
const maxConnsToExport = 64

var (
IANAInfoElementsCommon = []string{
"flowStartSeconds",
Expand Down Expand Up @@ -209,10 +221,10 @@ func (exp *flowExporter) Run(stopCh <-chan struct{}) {

func (exp *flowExporter) sendFlowRecords() (time.Duration, error) {
currTime := time.Now()
expiredConns := make([]flowexporter.Connection, 0)
expiredConns := make([]flowexporter.Connection, 0, maxConnsToExport*2)
var expireTime1, expireTime2 time.Duration
expiredConns, expireTime1 = exp.conntrackConnStore.GetExpiredConns(expiredConns, currTime)
expiredConns, expireTime2 = exp.denyConnStore.GetExpiredConns(expiredConns, currTime)
expiredConns, expireTime1 = exp.conntrackConnStore.GetExpiredConns(expiredConns, currTime, maxConnsToExport)
expiredConns, expireTime2 = exp.denyConnStore.GetExpiredConns(expiredConns, currTime, maxConnsToExport)
// Select the shorter time out among two connection stores to do the next round of export.
nextExpireTime := getMinTime(expireTime1, expireTime2)
for i := range expiredConns {
Expand Down

0 comments on commit ebd339a

Please sign in to comment.