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

[Asset Inventory] Run fetchers periodically #2902

Merged
merged 13 commits into from
Jan 8, 2025

Conversation

kubasobon
Copy link
Member

Summary of your changes

Fixes an issue where fetchers ran only once.

Related Issues

See #2887

Checklist

  • I have added tests that prove my fix is effective or that my feature works
  • I have added the necessary README/documentation (if appropriate)

@kubasobon kubasobon self-assigned this Jan 7, 2025
Copy link

mergify bot commented Jan 7, 2025

This pull request does not have a backport label. Could you fix it @kubasobon? 🙏
To fixup this pull request, you need to add the backport labels for the needed
branches, such as:

  • backport-v./d./d./d is the label to automatically backport to the 8./d branch. /d is the digit
    NOTE: backport-v8.x has been added to help with the transition to the new branch 8.x.

@mergify mergify bot added the backport-v8.x label Jan 7, 2025
@kubasobon kubasobon marked this pull request as ready for review January 7, 2025 13:52
@kubasobon kubasobon requested a review from a team as a code owner January 7, 2025 13:52
Copy link
Member

@romulets romulets left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, has it never worked? 😬

go func(fetcher AssetFetcher) {
fetcher.Fetch(ctx, a.assetCh)
}(fetcher)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we remove the the fetcher loop on top of this function?

Copy link
Member Author

@kubasobon kubasobon Jan 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, no. I added this so all fetchers run every fetcherPeriod tick. But I think canceled context takes precedence. If it's canceled for whatever reason, let's not run any more fetchers.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And the extra one on top is so that it starts immediately, on second 0. Otherwise it would take until the first fetcherPeriod tick to run first fetchers.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm true, just checked the Ticker and we can't make it run straight away.

Nitpick: Maybe move this code to a function? Just so we have a nice wrapped function, it prevents if in the future we need to fix something and we do in one place only

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A note: in manager (used in fetching of cspm mostly), we use a timer, first with a 0 value and then reset with the period value, to do the trick of "trigger now and then per period".

timer := time.NewTimer(0)
defer timer.Stop()
for {
select {
case <-ctx.Done():
m.log.Info("Fetchers manager canceled")
return
case <-timer.C:
// update the interval
timer.Reset(m.interval)
// this is blocking so the stop will not be called until all the fetchers are finished
// in case there is a blocking fetcher it will halt (til the m.timeout)
go m.fetchIteration(ctx)
}
}

I'm not saying we should do the same with asset inventory since I think the ticker is better for what we want.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Yeah, I know the trick, but I always find calling timer.Reset() every time more messy. Nothing against the implementation, just personal taste I guess.

@kubasobon
Copy link
Member Author

kubasobon commented Jan 7, 2025

Yes, unfortunately it seems like it always ran just once and that's it. See #2887

@kubasobon kubasobon requested a review from romulets January 7, 2025 14:14
Copy link
Member

@romulets romulets left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left a suggestion, but looks good otherwise

go func(fetcher AssetFetcher) {
fetcher.Fetch(ctx, a.assetCh)
}(fetcher)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm true, just checked the Ticker and we can't make it run straight away.

Nitpick: Maybe move this code to a function? Just so we have a nice wrapped function, it prevents if in the future we need to fix something and we do in one place only

@kubasobon
Copy link
Member Author

I've extracted duplicate code to runAllFetchersOnce(). I hope the comment and function name make it even clearer that it's not a looping situation.

const indexTemplate = "logs-cloud_asset_inventory.asset_inventory-%s_%s_%s_%s-default"
const (
indexTemplate = "logs-cloud_asset_inventory.asset_inventory-%s_%s_%s_%s-default"
minimalPeriod = 30 * time.Second
Copy link
Collaborator

@orouz orouz Jan 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather keep it. It's not a default value, but a guardian value. If the customer sets value too low, this will start to spawn many goroutines that hit API. I can lower it if you think it makes sense, but we definitely need to make sure there is a reasonable minimum threshold. Obviously the worst-case scenario is period: 0s, where we get an infinite loop of new goroutines, but even 5s or 10s is too small IMHO.

Copy link
Member Author

@kubasobon kubasobon Jan 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

np with keeping it imo. but this value isn't configurable by the users, it's hardcoded in the integration package.

@kubasobon kubasobon merged commit bb7a3e9 into elastic:main Jan 8, 2025
8 checks passed
@kubasobon kubasobon deleted the asset-inventory-period branch January 8, 2025 10:33
mergify bot pushed a commit that referenced this pull request Jan 8, 2025
mergify bot pushed a commit that referenced this pull request Jan 8, 2025
mergify bot pushed a commit that referenced this pull request Jan 8, 2025
kubasobon added a commit that referenced this pull request Jan 8, 2025
)

[Asset Inventory] Run fetchers periodically (#2902)

(cherry picked from commit bb7a3e9)

Co-authored-by: Kuba Soboń <[email protected]>
kubasobon added a commit that referenced this pull request Jan 8, 2025
…2907)

[Asset Inventory] Run fetchers periodically (#2902)

(cherry picked from commit bb7a3e9)

Co-authored-by: Kuba Soboń <[email protected]>
kubasobon added a commit that referenced this pull request Jan 8, 2025
…2906)

[Asset Inventory] Run fetchers periodically (#2902)

(cherry picked from commit bb7a3e9)

Co-authored-by: Kuba Soboń <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants