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

Instance management: Dispose of instance after 5s #765

Merged
merged 8 commits into from
Oct 18, 2023
Merged

Instance management: Dispose of instance after 5s #765

merged 8 commits into from
Oct 18, 2023

Conversation

marefr
Copy link
Member

@marefr marefr commented Oct 10, 2023

What this PR does / why we need it:
Alternative to #762 and #763. This currently doesn't work because haven't put any effort to resolve the tests, but mainly wanted to show the changes in instance_manager.go

Which issue(s) this PR fixes:

Fixes #753

Special notes for your reviewer:

Comment on lines +221 to +222
disposed atomic.Bool
disposedTimes atomic.Int64
Copy link
Member Author

Choose a reason for hiding this comment

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

So I had to change these since race detection complained about these due to concurrent reads/write in tests. In my head, given that we dispose within im.locker.Lock(cacheKey), there should only ever be one goroutine doing the creation of a new instance and calling Dispose hency why the instance itself shouldn't have to do any additional locking in their Dispose method.

@@ -17,6 +18,7 @@ var (
Name: "active_instances",
Help: "The number of active plugin instances",
})
disposeTTL = 5 * time.Second
Copy link
Member Author

Choose a reason for hiding this comment

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

Lets discuss what this should be

Copy link
Contributor

Choose a reason for hiding this comment

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

if this TTL is to avoid multiple instances calling Get at the same time I think 5s is a good value

Copy link
Member Author

Choose a reason for hiding this comment

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

Cool lets start with this and evaluate if it works or not

@marefr marefr marked this pull request as ready for review October 11, 2023 16:56
@marefr marefr requested a review from a team as a code owner October 11, 2023 16:56
@marefr marefr requested review from wbrowne, andresmgot and xnyo and removed request for a team October 11, 2023 16:56
Copy link
Contributor

@andresmgot andresmgot left a comment

Choose a reason for hiding this comment

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

what happens if in the time window in which an instance is "marked" to be deleted and it's actually deleted another one is created? I guess that's what we are fixing but I am not sure I am seeing the fix. I am thinking on:

  • An instance is marked to be deleted (in 5s)
  • A new instance is created (after 1s)
  • The instance gets deleted?

@marefr
Copy link
Member Author

marefr commented Oct 17, 2023

what happens if in the time window in which an instance is "marked" to be deleted and it's actually deleted another one is created? I guess that's what we are fixing but I am not sure I am seeing the fix. I am thinking on:

  • An instance is marked to be deleted (in 5s)
  • A new instance is created (after 1s)
  • The instance gets deleted?

@andresmgot not sure I understand. With your example it should be fine given that a new instance is created and new requests coming in will use that instance. Then instance is deleted after 5s, but shouldn't be any usage left of this one. Does this make sense?

Copy link
Member

@xnyo xnyo left a comment

Choose a reason for hiding this comment

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

Interesting work, the changes make sense 👍 . The tricky part is nailing down the TTL, if it's too low we will encounter the same issue again.

In my head solution should work and it's simple and straightforward. I was also thinking maybe we can keep a counter of how many times an instance is retrieved, and decrement it when it's being "released". So adding a Release method to the instance manager alongside Get. Then the caller is responsible for defer-calling Release. This would allow us to call Dispose when the counter reaches 0 (so nobody is using a given instance). However this complicates things, changes the interface and breaks plugins that use manual instance management (but we are discouraging that), so probably not worth it, but wanted to hear your thoughts on this as well

backend/instancemgmt/instance_manager.go Show resolved Hide resolved
@andresmgot
Copy link
Contributor

andresmgot commented Oct 18, 2023

@andresmgot not sure I understand. With your example it should be fine given that a new instance is created and new requests coming in will use that instance. Then instance is deleted after 5s, but shouldn't be any usage left of this one. Does this make sense?

Right, I thought that calling Dispose after the new instance got created caused the connection to be closed and not re-opened. I thought that was the original issue but I may be wrong. I have actually checked, reintroducing the bug that grafana/grafana#75249 fixed that this PR also fixes the issue so I think we are good to go.

Copy link
Contributor

@andresmgot andresmgot left a comment

Choose a reason for hiding this comment

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

LGTM!

@@ -17,6 +18,7 @@ var (
Name: "active_instances",
Help: "The number of active plugin instances",
})
disposeTTL = 5 * time.Second
Copy link
Contributor

Choose a reason for hiding this comment

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

if this TTL is to avoid multiple instances calling Get at the same time I think 5s is a good value

Copy link
Member

@wbrowne wbrowne left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@xnyo xnyo left a comment

Choose a reason for hiding this comment

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

LGTM! Awesome work!

@marefr marefr changed the title Instance management: Dispose of instance after X time Instance management: Dispose of instance after 5s Oct 18, 2023
@marefr marefr merged commit 9be45ea into main Oct 18, 2023
2 checks passed
@marefr marefr deleted the 753_fix branch October 18, 2023 14:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Instancemgmt: Instance possibly being disposed while still being in use
4 participants