-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathevict.go
More file actions
183 lines (160 loc) · 5.93 KB
/
Copy pathevict.go
File metadata and controls
183 lines (160 loc) · 5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package cleanup
import (
"context"
"fmt"
"log"
"time"
"github.com/interuss/dss/pkg/logging"
dssmodels "github.com/interuss/dss/pkg/models"
ridmodels "github.com/interuss/dss/pkg/rid/models"
ridrepos "github.com/interuss/dss/pkg/rid/repos"
rids "github.com/interuss/dss/pkg/rid/store"
scdmodels "github.com/interuss/dss/pkg/scd/models"
scdrepos "github.com/interuss/dss/pkg/scd/repos"
scds "github.com/interuss/dss/pkg/scd/store"
"github.com/interuss/stacktrace"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
var (
EvictCmd = &cobra.Command{
Use: "evict",
Short: "List and evict expired entities",
RunE: evict,
}
flags = pflag.NewFlagSet("evict", pflag.ExitOnError)
checkScdOirs = flags.Bool("scd_oir", true, "set this flag to true to check for expired SCD operational intents")
checkScdSubs = flags.Bool("scd_sub", true, "set this flag to true to check for expired SCD subscriptions")
checkRidISAs = flags.Bool("rid_isa", true, "set this flag to true to check for expired RID ISAs")
checkRidSubs = flags.Bool("rid_sub", true, "set this flag to true to check for expired RID subscriptions")
scdTtl = flags.Duration("scd_ttl", time.Hour*24*112, "time-to-live duration used for determining SCD entries expiration, defaults to 2*56 days")
ridTtl = flags.Duration("rid_ttl", time.Minute*30, "time-to-live duration used for determining RID entries expiration, defaults to 30 minutes")
deleteExpired = flags.Bool("delete", false, "set this flag to true to delete the expired entities")
locality = flags.String("locality", "", "self-identification string of this DSS instance")
timeout = flags.Duration("timeout", 5*time.Minute, "Timeout for the command")
)
func init() {
EvictCmd.Flags().AddFlagSet(flags)
}
func evict(cmd *cobra.Command, _ []string) error {
var (
ctx = cmd.Context()
scdThreshold = time.Now().Add(-*scdTtl)
ridThreshold = time.Now().Add(-*ridTtl)
)
log.Printf("WARNING: The usage of this tool may have an impact on performance when deleting entities. Read more in the README.")
ctx, cancel := context.WithTimeout(ctx, *timeout)
defer cancel()
logger := logging.WithValuesFromContext(ctx, logging.Logger)
scdStore, err := scds.Init(ctx, logger, false, false)
if err != nil {
return err
}
ridStore, err := rids.Init(ctx, logger, false)
if err != nil {
return err
}
var (
expiredOpIntents []*scdmodels.OperationalIntent
scdExpiredSub []*scdmodels.Subscription
expiredISAs []*ridmodels.IdentificationServiceArea
ridExpiredSub []*ridmodels.Subscription
)
scdAction := func(ctx context.Context, r scdrepos.Repository) (err error) {
if *checkScdOirs {
expiredOpIntents, err = r.ListExpiredOperationalIntents(ctx, scdThreshold)
if err != nil {
return fmt.Errorf("listing expired operational intents: %w", err)
}
if *deleteExpired {
for _, opIntent := range expiredOpIntents {
if err = r.DeleteOperationalIntent(ctx, opIntent.ID); err != nil {
return fmt.Errorf("deleting expired operational intents: %w", err)
}
}
}
}
if *checkScdSubs {
scdExpiredSub, err = r.ListExpiredSubscriptions(ctx, scdThreshold)
if err != nil {
return fmt.Errorf("SCD listing expired subscriptions: %w", err)
}
if *deleteExpired {
for _, sub := range scdExpiredSub {
if err = r.DeleteSubscription(ctx, sub.ID); err != nil {
return fmt.Errorf("SCD deleting expired subscriptions: %w", err)
}
}
}
}
return nil
}
if _, err = scdStore.Transact(ctx, "", nil, scdAction); err != nil {
return fmt.Errorf("failed to execute SCD transaction: %w", err)
}
ridAction := func(ctx context.Context, r ridrepos.Repository) (err error) {
if *checkRidISAs {
expiredISAs, err = r.ListExpiredISAs(ctx, *locality, ridThreshold)
if err != nil {
return stacktrace.Propagate(err, "Failed to list expired ISAs")
}
if *deleteExpired {
for _, isa := range expiredISAs {
_, err := r.DeleteISA(ctx, isa)
if err != nil {
return stacktrace.Propagate(err, "Failed to delete ISAs")
}
}
}
}
if *checkRidSubs {
ridExpiredSub, err = r.ListExpiredSubscriptions(ctx, *locality, ridThreshold)
if err != nil {
return stacktrace.Propagate(err,
"Failed to list RID expired Subscriptions")
}
if *deleteExpired {
for _, sub := range ridExpiredSub {
_, err := r.DeleteSubscription(ctx, sub)
if err != nil {
return stacktrace.Propagate(err,
"Failed to delete RID Subscription")
}
}
}
}
return nil
}
if _, err = ridStore.Transact(ctx, "", nil, ridAction); err != nil {
return fmt.Errorf("failed to execute RID transaction: %w", err)
}
for _, opIntent := range expiredOpIntents {
logExpiredEntity("operational intent", opIntent.ID, scdThreshold, *deleteExpired, opIntent.EndTime != nil)
}
for _, sub := range scdExpiredSub {
logExpiredEntity("SCD subscription", sub.ID, scdThreshold, *deleteExpired, sub.EndTime != nil)
}
for _, isa := range expiredISAs {
logExpiredEntity("ISA", isa.ID, ridThreshold, *deleteExpired, isa.EndTime != nil)
}
for _, sub := range ridExpiredSub {
logExpiredEntity("RID subscription", sub.ID, ridThreshold, *deleteExpired, sub.EndTime != nil)
}
if len(expiredOpIntents) == 0 && len(scdExpiredSub) == 0 && len(expiredISAs) == 0 && len(ridExpiredSub) == 0 {
log.Printf("no SCD entity older than %s and no RID entity older than %s found", scdThreshold.String(), ridThreshold.String())
} else if !*deleteExpired {
log.Printf("no entity was deleted, run the command again with the `--delete` flag to do so")
}
return nil
}
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()))
}