Skip to content

Commit de63ded

Browse files
authored
Merge pull request #266 from turbot/v0.9.x
Merge branch 'v0.9.x' into develop
2 parents 8c145b6 + ef6ad8f commit de63ded

File tree

8 files changed

+86
-18
lines changed

8 files changed

+86
-18
lines changed

collection_state/artifact_collection_state.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,9 @@ func (s *ArtifactCollectionState) MigrateFromLegacyState(bytes []byte) error {
263263
func (s *ArtifactCollectionState) Validate() error {
264264
var errorList []error
265265
for _, trunkState := range s.TrunkStates {
266+
if trunkState == nil {
267+
continue // skip nil trunk states
268+
}
266269
if trunkErr := trunkState.Validate(); trunkErr != nil {
267270
errorList = append(errorList, trunkErr)
268271
}
@@ -320,3 +323,12 @@ func (s *ArtifactCollectionState) String() any {
320323
}
321324
return stringBuilder.String()
322325
}
326+
327+
// TrimNilTrunkStates removes all entries from TrunkStates where the value is nil.
328+
func (s *ArtifactCollectionState) TrimNilTrunkStates() {
329+
for k, v := range s.TrunkStates {
330+
if v == nil {
331+
delete(s.TrunkStates, k)
332+
}
333+
}
334+
}

collection_state/artifact_collection_state_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,58 @@ func TestArtifactCollectionState_MigrateFromLegacyState(t *testing.T) {
8080
}
8181
}
8282

83+
func TestArtifactCollectionState_Validate_NilTrunks(t *testing.T) {
84+
t.Run("all nil trunks", func(t *testing.T) {
85+
state := &ArtifactCollectionState{
86+
TrunkStates: map[string]*TimeRangeCollectionState{
87+
"/trunk1": nil,
88+
"/trunk2": nil,
89+
},
90+
}
91+
err := state.Validate()
92+
if err != nil {
93+
t.Errorf("expected Validate to return nil for all nil trunks, got: %v", err)
94+
}
95+
})
96+
97+
t.Run("mixed nil and valid trunks", func(t *testing.T) {
98+
valid := &TimeRangeCollectionState{
99+
TimeRanges: []*TimeRangeObjectState{},
100+
}
101+
state := &ArtifactCollectionState{
102+
TrunkStates: map[string]*TimeRangeCollectionState{
103+
"/trunk1": nil,
104+
"/trunk2": valid,
105+
},
106+
}
107+
err := state.Validate()
108+
if err != nil {
109+
t.Errorf("expected Validate to return nil for valid trunks, got: %v", err)
110+
}
111+
})
112+
113+
t.Run("mixed nil and invalid trunks", func(t *testing.T) {
114+
invalid := &TimeRangeCollectionState{
115+
TimeRanges: []*TimeRangeObjectState{
116+
{
117+
Granularity: 1,
118+
TimeRange: DirectionalTimeRange{}, // invalid
119+
},
120+
},
121+
}
122+
state := &ArtifactCollectionState{
123+
TrunkStates: map[string]*TimeRangeCollectionState{
124+
"/trunk1": nil,
125+
"/trunk2": invalid,
126+
},
127+
}
128+
err := state.Validate()
129+
if err == nil {
130+
t.Errorf("expected Validate to return error for invalid trunks, got nil")
131+
}
132+
})
133+
}
134+
83135
// buildArtifactCollectionStateLegacy constructs a legacy artifact collection state for tests
84136
func buildArtifactCollectionStateLegacy(trunks map[string]*TimeRangeCollectionStateLegacy, lastModifiedTime time.Time) *ArtifactCollectionStateLegacy {
85137
return &ArtifactCollectionStateLegacy{

collection_state/saveable_collection_state.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,6 @@ func (s *SaveableCollectionState) Init(collectionTimeRange DirectionalTimeRange,
4747
// save the granularity
4848
s.granularity = granularity
4949

50-
// NOTE: if granularity is zero, we DO NOT support collecting for a time range so clear the time range
51-
if granularity == 0 {
52-
slog.Info("Granularity is zero - clearing collection time range")
53-
collectionTimeRange = DirectionalTimeRange{}
54-
}
5550
// if we are recollecting, clear BEFORE call to Init, as Init will set the active range
5651
// which we must not do until we have cleared the state
5752
if recollect {

collection_state/time_range_collection_state.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,6 @@ func (t *TimeRangeCollectionState) OnCollectionComplete() error {
150150
if t.currentCollectionTimeRange == nil {
151151
return fmt.Errorf("cannot complete collection - no current collection set, Init must be called first")
152152
}
153-
// if we have no granularity we have nothing to do
154-
if t.Granularity == 0 {
155-
slog.Info("Granularity is zero - no collection complete action required")
156-
return nil
157-
}
158153

159154
// set the upper boundary time of the active range to the upper boundary time of the collection time range
160155
t.activeRange.setUpperBoundaryTime(t.currentCollectionTimeRange.EndTime())

collection_state/time_range_object_state.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@ func (s *TimeRangeObjectState) IsEmpty() bool {
5252

5353
// so we have no end objects
5454

55-
// if we have no time information and no end objects then we are empty
56-
if s.Granularity == 0 {
57-
return true
58-
}
59-
6055
// if the start time equals the end time (the initial state) then we are empty
6156
return s.TimeRange.UpperBoundary.Equal(s.TimeRange.LowerBoundary)
6257
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ require (
182182
golang.org/x/crypto v0.36.0 // indirect
183183
golang.org/x/mod v0.22.0 // indirect
184184
golang.org/x/net v0.38.0 // indirect
185-
golang.org/x/oauth2 v0.23.0 // indirect
185+
golang.org/x/oauth2 v0.27.0 // indirect
186186
golang.org/x/sys v0.31.0 // indirect
187187
golang.org/x/term v0.30.0 // indirect
188188
golang.org/x/text v0.23.0 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -881,8 +881,8 @@ golang.org/x/oauth2 v0.0.0-20220822191816-0ebed06d0094/go.mod h1:h4gKUeWbJ4rQPri
881881
golang.org/x/oauth2 v0.0.0-20220909003341-f21342109be1/go.mod h1:h4gKUeWbJ4rQPri7E0u6Gs4e9Ri2zaLxzw5DI5XGrYg=
882882
golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783/go.mod h1:h4gKUeWbJ4rQPri7E0u6Gs4e9Ri2zaLxzw5DI5XGrYg=
883883
golang.org/x/oauth2 v0.1.0/go.mod h1:G9FE4dLTsbXUu90h/Pf85g4w1D+SSAgR+q46nJZ8M4A=
884-
golang.org/x/oauth2 v0.23.0 h1:PbgcYx2W7i4LvjJWEbf0ngHV6qJYr86PkAV3bXdLEbs=
885-
golang.org/x/oauth2 v0.23.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
884+
golang.org/x/oauth2 v0.27.0 h1:da9Vo7/tDv5RH/7nZDz1eMGS/q1Vv1N/7FCrBhI9I3M=
885+
golang.org/x/oauth2 v0.27.0/go.mod h1:onh5ek6nERTohokkhCD/y2cV4Do3fxFHFuAejCkRWT8=
886886
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
887887
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
888888
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=

row_source/row_source_impl.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,14 @@ func (r *RowSourceImpl[S, T]) Init(_ context.Context, params *RowSourceParams, o
113113
// it will also adjust the from and to time if the collection order is reverse
114114
r.setCollectionTimeRange(params, granularity)
115115

116+
// After setting the collection time range, trim nil trunk states
117+
// NOTE: this is done here so that the collection state is clean before we call Init on it.
118+
// This is important as we may have nil trunk states in the collection state file if the previous
119+
// collection was done using older version of the code
120+
if artifactState, ok := r.CollectionState.State.(*collection_state.ArtifactCollectionState); ok {
121+
artifactState.TrimNilTrunkStates()
122+
}
123+
116124
// init the collection state with the time range and granularity
117125
// NOTE: the collection state will set it;s collection order based on the time range collection order
118126
// (which we set from our CollectionOrder field)
@@ -220,6 +228,17 @@ func (r *RowSourceImpl[S, T]) OnCollectionComplete() error {
220228
slog.Info("OnCollectionComplete: Collection state is nil - not setting end time")
221229
return nil
222230
}
231+
232+
// Before saving, trim nil trunk states
233+
// Having a null trunk state in the collection state file is valid. There might be some locations in the
234+
// bucket which have no files in them, which would result in null trunk states, as there is no way to know
235+
// this in advance we add the null trunk states to the collection state.
236+
// However, we don't want to save these null trunk states to the collection state file as they do not
237+
// make sense. So we trim them before saving.
238+
if artifactState, ok := r.CollectionState.State.(*collection_state.ArtifactCollectionState); ok {
239+
artifactState.TrimNilTrunkStates()
240+
}
241+
223242
// so the source collection was successful, set the end time of the collection state to the collection `to`
224243
// this ensures that when we run the next collection, we will start from the end time of the previous collection
225244
if err := r.CollectionState.OnCollectionComplete(); err != nil {

0 commit comments

Comments
 (0)