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

feat(graphdb): split deletion transactions. #303

Merged
merged 2 commits into from
Dec 9, 2024
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
111 changes: 84 additions & 27 deletions pkg/kubehound/storage/graphdb/janusgraph_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
const (
channelSizeBatchFactor = 4 // TODO maybe move that into a config file?
StorageProviderName = "janusgraph"
deleteBatchSize = 10000
)

var (
Expand Down Expand Up @@ -70,19 +71,47 @@ func (jgp *JanusGraphProvider) Prepare(ctx context.Context) error {
tx := g.Tx()
defer tx.Close()

gtx, err := tx.Begin()
if err != nil {
return err
}

err = <-gtx.V().Drop().Iterate()
if err != nil {
return err
}

err = tx.Commit()
if err != nil {
return err
for {
// Begin a new transaction.
gtx, err := tx.Begin()
if err != nil {
return err
}

// Retrieve the number of vertices in the graph.
page, err := gtx.V().Count().Next()
if err != nil {
return err
}

// Decode the number of vertices from the page.
count, err := page.GetInt()
if err != nil {
return err
}

// If there are no more vertices to delete, break the loop.
if count == 0 {
break
}

// Delete the vertices in the graph.
err = <-gtx.V().Limit(deleteBatchSize).Drop().Iterate()
if err != nil {
return err
}

// Commit the transaction.
if err := tx.Commit(); err != nil {
return err
}

// Check context for cancellation.
select {
case <-ctx.Done():
return ctx.Err()
default:
}
}

return nil
Expand Down Expand Up @@ -154,7 +183,7 @@ func (jgp *JanusGraphProvider) Close(ctx context.Context) error {
return nil
}

// Raw returns a handle to the underlying provider to allow implementation specific operations e.g graph queries.
// Clean removes all vertices in the graph for the given cluster.
func (jgp *JanusGraphProvider) Clean(ctx context.Context, cluster string) error {
var err error
span, ctx := span.SpanRunFromContext(ctx, span.IngestorClean)
Expand All @@ -165,19 +194,47 @@ func (jgp *JanusGraphProvider) Clean(ctx context.Context, cluster string) error
tx := g.Tx()
defer tx.Close()

gtx, err := tx.Begin()
if err != nil {
return err
}

err = <-gtx.V().Has("cluster", cluster).Drop().Iterate()
if err != nil {
return err
}

err = tx.Commit()
if err != nil {
return err
for {
// Begin a new transaction.
gtx, err := tx.Begin()
if err != nil {
return err
}

// Retrieve the number of vertices in the graph for the given cluster.
page, err := gtx.V().Has("cluster", cluster).Count().Next()
if err != nil {
return err
}

// Decode the number of vertices from the page.
count, err := page.GetInt()
if err != nil {
return err
}

// If there are no more vertices to delete, break the loop.
if count == 0 {
break
}

// Delete the vertices in the graph for the given cluster.
err = <-gtx.V().Has("cluster", cluster).Limit(deleteBatchSize).Drop().Iterate()
if err != nil {
return err
}

// Commit the transaction.
if err := tx.Commit(); err != nil {
return err
}

// Check context for cancellation.
select {
case <-ctx.Done():
return ctx.Err()
default:
}
}

return nil
Expand Down
Loading