-
Notifications
You must be signed in to change notification settings - Fork 12
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
Add partition delete logic in sync
#243
Changes from all commits
5f6f82e
887119a
af756ab
b94fb60
7f884ac
6a2ec48
0d02068
c9f32eb
fe66066
50057e1
6aa1e25
181b855
6100059
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,13 @@ package repository | |
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/apache/yunikorn-core/pkg/webservice/dao" | ||
"github.com/jackc/pgx/v5" | ||
"github.com/oklog/ulid/v2" | ||
|
||
"github.com/G-Research/yunikorn-history-server/internal/model" | ||
) | ||
|
||
func (s *PostgresRepository) UpsertPartitions(ctx context.Context, partitions []*dao.PartitionInfo) error { | ||
|
@@ -54,19 +57,53 @@ func (s *PostgresRepository) UpsertPartitions(ctx context.Context, partitions [] | |
return nil | ||
} | ||
|
||
func (s *PostgresRepository) GetAllPartitions(ctx context.Context) ([]*dao.PartitionInfo, error) { | ||
rows, err := s.dbpool.Query(ctx, "SELECT * FROM partitions") | ||
func (s *PostgresRepository) GetAllPartitions(ctx context.Context) ([]*model.PartitionInfo, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here, you should probably get only active partitions. We don't need to get deleted partitions to operate on them. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, Thanks for the awesome reviews 🚀 |
||
rows, err := s.dbpool.Query(ctx, `SELECT * FROM partitions`) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not get partitions from DB: %v", err) | ||
} | ||
defer rows.Close() | ||
|
||
var partitions []*model.PartitionInfo | ||
for rows.Next() { | ||
var p model.PartitionInfo | ||
if err := rows.Scan( | ||
&p.Id, | ||
&p.ClusterID, | ||
&p.Name, | ||
&p.Capacity.Capacity, | ||
&p.Capacity.UsedCapacity, | ||
&p.Capacity.Utilization, | ||
&p.TotalNodes, | ||
&p.Applications, | ||
&p.TotalContainers, | ||
&p.State, | ||
&p.LastStateTransitionTime, | ||
&p.DeletedAt, | ||
); err != nil { | ||
return nil, fmt.Errorf("could not scan partition from DB: %v", err) | ||
} | ||
partitions = append(partitions, &p) | ||
} | ||
|
||
if err := rows.Err(); err != nil { | ||
return nil, fmt.Errorf("failed to read rows: %v", err) | ||
} | ||
return partitions, nil | ||
} | ||
|
||
func (s *PostgresRepository) GetActivePartitions(ctx context.Context) ([]*model.PartitionInfo, error) { | ||
rows, err := s.dbpool.Query(ctx, `SELECT * FROM partitions WHERE deleted_at IS NULL`) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not get partitions from DB: %v", err) | ||
} | ||
defer rows.Close() | ||
|
||
var partitions []*dao.PartitionInfo | ||
var partitions []*model.PartitionInfo | ||
for rows.Next() { | ||
var p dao.PartitionInfo | ||
var id string | ||
var p model.PartitionInfo | ||
if err := rows.Scan( | ||
&id, | ||
&p.Id, | ||
&p.ClusterID, | ||
&p.Name, | ||
&p.Capacity.Capacity, | ||
|
@@ -77,6 +114,7 @@ func (s *PostgresRepository) GetAllPartitions(ctx context.Context) ([]*dao.Parti | |
&p.TotalContainers, | ||
&p.State, | ||
&p.LastStateTransitionTime, | ||
&p.DeletedAt, | ||
); err != nil { | ||
return nil, fmt.Errorf("could not scan partition from DB: %v", err) | ||
} | ||
|
@@ -88,3 +126,26 @@ func (s *PostgresRepository) GetAllPartitions(ctx context.Context) ([]*dao.Parti | |
} | ||
return partitions, nil | ||
} | ||
|
||
// DeleteInactivePartitions deletes partitions that are not in the list of activePartitions. | ||
func (s *PostgresRepository) DeleteInactivePartitions(ctx context.Context, activePartitions []*dao.PartitionInfo) error { | ||
partitionNames := make([]string, len(activePartitions)) | ||
for i, p := range activePartitions { | ||
partitionNames[i] = p.Name | ||
} | ||
deletedAt := time.Now().Unix() | ||
query := ` | ||
UPDATE partitions | ||
SET deleted_at = $1 | ||
WHERE id IN ( | ||
SELECT id | ||
FROM partitions | ||
WHERE deleted_at IS NULL AND NOT(name = ANY($2)) | ||
) | ||
` | ||
_, err := s.dbpool.Exec(ctx, query, deletedAt, partitionNames) | ||
if err != nil { | ||
return fmt.Errorf("could not mark partitions as deleted in DB: %w", err) | ||
} | ||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we be preparing for pagination parameters here? It seems unlikely we'd ever want to return everything to the front end all at once...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, we also have several other APIs that need to be updated with pagination. This task is tracked in #245