@@ -22,6 +22,7 @@ import (
2222 "strconv"
2323 "strings"
2424 "sync"
25+ "sync/atomic"
2526 "syscall"
2627
2728 "golang.org/x/time/rate"
@@ -79,7 +80,7 @@ type Aggregator struct {
7980 rateLimitMu sync.RWMutex
8081
8182 // Used to find the correct mutex for the pid, some pids can share the same mutex
82- muIndex int
83+ muIndex atomic. Uint64
8384 muArray []* sync.RWMutex
8485}
8586
@@ -185,7 +186,7 @@ func NewAggregator(parentCtx context.Context, k8sChan <-chan interface{},
185186 liveProcesses : make (map [uint32 ]struct {}),
186187 rateLimiters : make (map [uint32 ]* rate.Limiter ),
187188 pgStmts : make (map [string ]string ),
188- muIndex : 0 ,
189+ muIndex : atomic. Uint64 {} ,
189190 muArray : nil ,
190191 }
191192
@@ -227,11 +228,10 @@ func NewAggregator(parentCtx context.Context, k8sChan <-chan interface{},
227228 a .muArray = make ([]* sync.RWMutex , countMuArray )
228229
229230 // set distinct mutex for every live process
230- a .muIndex = 0
231231 for pid := range a .liveProcesses {
232- a .muArray [a .muIndex ] = & sync.RWMutex {}
233- sockMaps [pid ].mu = a .muArray [a .muIndex ]
234- a .muIndex ++
232+ a .muArray [a .muIndex . Load () ] = & sync.RWMutex {}
233+ sockMaps [pid ].mu = a .muArray [a .muIndex . Load () ]
234+ a .muIndex . Add ( 1 )
235235 a .getAlreadyExistingSockets (pid )
236236 }
237237
@@ -456,9 +456,16 @@ func (a *Aggregator) processExec(d *proc.ProcEvent) {
456456 a .liveProcesses [d .Pid ] = struct {}{}
457457
458458 // create lock on demand
459- a .muArray [a .muIndex % len (a .muArray )] = & sync.RWMutex {}
460- a .muIndex ++
461- a .clusterInfo .SocketMaps [d .Pid ].mu = a .muArray [a .muIndex % len (a .muArray )]
459+ a .muArray [(a .muIndex .Load ())% uint64 (len (a .muArray ))] = & sync.RWMutex {}
460+ a .muIndex .Add (1 )
461+
462+ // if duplicate exec event comes, underlying mutex will be changed
463+ // if first assigned mutex is locked and another exec event comes, mutex will be changed
464+ // and unlock of unlocked mutex now is a possibility
465+ // to avoid this case, if a socket map already has a mutex, don't change it
466+ if a .clusterInfo .SocketMaps [d .Pid ].mu == nil {
467+ a .clusterInfo .SocketMaps [d .Pid ].mu = a .muArray [(a .muIndex .Load ())% uint64 (len (a .muArray ))]
468+ }
462469}
463470
464471func (a * Aggregator ) processExit (pid uint32 ) {
@@ -1325,7 +1332,7 @@ func (a *Aggregator) parseSqlCommand(d *l7_req.L7Event) (string, error) {
13251332 a .pgStmtsMu .RLock ()
13261333 query , ok := a .pgStmts [a .getPgStmtKey (d .Pid , d .Fd , stmtName )]
13271334 a .pgStmtsMu .RUnlock ()
1328- if ! ok { // we don't have the query for the prepared statement
1335+ if ! ok || query == "" { // we don't have the query for the prepared statement
13291336 // Execute (name of prepared statement) [(parameter)]
13301337 return fmt .Sprintf ("EXECUTE %s *values*" , stmtName ), nil
13311338 }
0 commit comments