-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[cmds][scd/store] Add 'db-evictor' command enabling cleanup of expire…
…d op intents and subscriptions
- Loading branch information
Showing
5 changed files
with
172 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/interuss/dss/pkg/cockroach" | ||
"github.com/interuss/dss/pkg/cockroach/flags" | ||
dssmodels "github.com/interuss/dss/pkg/models" | ||
scdmodels "github.com/interuss/dss/pkg/scd/models" | ||
"github.com/interuss/dss/pkg/scd/repos" | ||
scdc "github.com/interuss/dss/pkg/scd/store/cockroach" | ||
) | ||
|
||
var ( | ||
listOpIntents = flag.Bool("op_intents", true, "set this flag to true to list expired operational intents") | ||
listScdSubs = flag.Bool("scd_subs", true, "set this flag to true to list expired SCD subscriptions") | ||
ttl = flag.Duration("ttl", time.Hour*24*112, "time-to-live duration used for determining expiration") | ||
deleteExpired = flag.Bool("delete", false, "set this flag to true to delete the expired entities") | ||
) | ||
|
||
// TODO: add documentation where relevant | ||
// TODO: add relevant wrappers for k8s usage | ||
|
||
func main() { | ||
flag.Parse() | ||
|
||
var ( | ||
ctx = context.Background() | ||
threshold = time.Now().Add(-*ttl) | ||
) | ||
|
||
connectParameters := flags.ConnectParameters() | ||
connectParameters.ApplicationName = "db-evictor" | ||
connectParameters.DBName = scdc.DatabaseName | ||
scdCrdb, err := cockroach.Dial(ctx, connectParameters) | ||
if err != nil { | ||
log.Panicf("Failed to connect to database with %+v: %v", connectParameters, err) | ||
} | ||
|
||
scdStore, err := scdc.NewStore(ctx, scdCrdb) | ||
if err != nil { | ||
log.Panicf("Failed to create strategic conflict detection store with %+v: %v", connectParameters, err) | ||
} | ||
|
||
var ( | ||
expiredOpIntents []*scdmodels.OperationalIntent | ||
expiredSubs []*scdmodels.Subscription | ||
) | ||
action := func(ctx context.Context, r repos.Repository) (err error) { | ||
if *listOpIntents { | ||
expiredOpIntents, err = r.ListExpiredOperationalIntents(ctx, threshold) | ||
if err != nil { | ||
return fmt.Errorf("listing expired operational intents: %w", err) | ||
} | ||
if *deleteExpired { | ||
for _, opIntent := range expiredOpIntents { | ||
err = r.DeleteOperationalIntent(ctx, opIntent.ID) | ||
} | ||
} | ||
} | ||
|
||
if *listScdSubs { | ||
expiredSubs, err = r.ListExpiredSubscriptions(ctx, threshold) | ||
if err != nil { | ||
return fmt.Errorf("listing expired operational intents: %w", err) | ||
} | ||
if *deleteExpired { | ||
for _, sub := range expiredSubs { | ||
err = r.DeleteSubscription(ctx, sub.ID) | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
if err = scdStore.Transact(ctx, action); err != nil { | ||
log.Panicf("Failed to execute CRDB transaction: %v", err) | ||
} | ||
|
||
for _, opIntent := range expiredOpIntents { | ||
logExpiredEntity("operational intent", opIntent.ID, threshold, *deleteExpired, opIntent.EndTime != nil) | ||
} | ||
for _, sub := range expiredSubs { | ||
logExpiredEntity("subscription", sub.ID, threshold, *deleteExpired, sub.EndTime != nil) | ||
} | ||
if len(expiredOpIntents) == 0 && len(expiredSubs) == 0 { | ||
log.Printf("no entity older than %s found", threshold.String()) | ||
} | ||
} | ||
|
||
func logExpiredEntity(entity string, entityID dssmodels.ID, threshold time.Time, deleted, hasEndTime bool) { | ||
logMsg := "found" | ||
if deleted { | ||
logMsg = "deleted" | ||
} | ||
|
||
expMsg := "last update before %s (missing end time)" | ||
if hasEndTime { | ||
expMsg = "end time before %s" | ||
} | ||
log.Printf("%s %s %s; expired due to %s", logMsg, entity, entityID.String(), fmt.Sprintf(expMsg, threshold.String())) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters