Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deadlock on pod scan status updates #1554

Merged
merged 1 commit into from
Sep 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 19 additions & 23 deletions deepfence_worker/ingesters/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ingesters

import (
"encoding/json"
"fmt"
"time"

"github.com/deepfence/ThreatMapper/deepfence_utils/directory"
Expand Down Expand Up @@ -70,52 +69,49 @@ func CommitFuncStatus[Status any](ts utils.Neo4jScanType) func(ns string, data [
return err
}

if ts != utils.NEO4J_CLOUD_COMPLIANCE_SCAN && ts != utils.NEO4J_COMPLIANCE_SCAN {
err = updatePodScanStatus(ts, recordMap, session)
if err != nil {
return err
}
}

err = tx.Commit()
if err != nil {
return err
}

if ts != utils.NEO4J_CLOUD_COMPLIANCE_SCAN && ts != utils.NEO4J_COMPLIANCE_SCAN {
updatePodScanStatus(ts, recordMap, session)
}

return nil
}
}

func updatePodScanStatus(ts utils.Neo4jScanType,
recordMap []map[string]interface{}, session neo4j.Session) error {

query := `UNWIND $batch as row
MATCH (n:Pod)
CALL {
WITH row
MATCH (p)-[r:SCANNED]->(c:Container)
WHERE p.node_id = row.scan_id AND c.pod_name IS NOT NULL
RETURN c.pod_name AS pod_name
}
WITH n, row
WHERE n.pod_name = pod_name
SET n.%s = row.scan_status`
query := `
UNWIND $batch as row
MATCH (s:` + string(ts) + `{node_id: row.scan_id})-[:SCANNED]->(c:Container)
WHERE c.pod_id IS NOT NULL
MATCH (n:Pod{node_id: c.pod_id})
SET n.` + ingestersUtil.ScanStatusField[ts] + `=row.scan_status`

tx, err := session.BeginTransaction(neo4j.WithTxTimeout(30 * time.Second))
if err != nil {
return err
}

_, err = tx.Run(fmt.Sprintf(query, ingestersUtil.ScanStatusField[ts]),
map[string]interface{}{"batch": recordMap})
log.Debug().Msgf("query: %v", query)
_, err = tx.Run(query,
map[string]interface{}{
"batch": recordMap,
},
)

if err != nil {
log.Error().Msgf("Error in pod status update query: %+v", err)
return err
}

err = tx.Commit()
if err != nil {
log.Info().Msgf("Error in commit for pod status update query: %v", err)
return err
}
return nil
}

Expand Down