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

enhance background job management of region cache #1171

Merged
merged 10 commits into from
Feb 26, 2024

Conversation

zyguan
Copy link
Contributor

@zyguan zyguan commented Feb 22, 2024

There are many background cron jobs in region cache. This PR tries to reduce boilerplate code (like the following) by bgRunner.

ticker := time.NewTicker(interval)
r.wg.Add(1)
go func() {
	defer func() {
		r.wg.Done()
		ticker.Stop()
	}()
	for {
		select {
		case <-r.ctx.Done():
			return
		case <-ticker.C:
			// ...
		}
	}
}()

ref: #1104

Signed-off-by: zyguan <[email protected]>
@cfzjywxk
Copy link
Contributor

@crazycs520 PTAL

internal/locate/region_cache.go Show resolved Hide resolved
internal/locate/region_cache.go Show resolved Hide resolved
internal/locate/region_cache.go Show resolved Hide resolved
Copy link
Contributor

@cfzjywxk cfzjywxk left a comment

Choose a reason for hiding this comment

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

LGTM

if r.closed() {
return
}
ticker := time.NewTicker(interval)
Copy link
Contributor

Choose a reason for hiding this comment

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

How about moving this line after line 575? since the ticker is stop in another goroutine.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My point is: if the interval <= 0, it panic when calling schedule, which is able to be recovered by the caller; however if we move it into the goroutine, panic cannot be caught by users.

Copy link
Contributor

Choose a reason for hiding this comment

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

If so, we should check interval <= 0 here, instead of causing panic and recover to handle it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

IMO, panic on interval <= 0 is reasonable, but the panic should be recoverable. I will update it as you said anyway.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should try to check in advance to avoid panic, Instead of letting it panic, after all, this is a system software, users will panic when they see panic logs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've changed it.
BTW, panic let you kown you've passed a wrong arg, however, keeping silent sometimes hide potential risks.

if r.closed() {
return
}
ticker := time.NewTicker(interval)
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto.

Signed-off-by: zyguan <[email protected]>
@cfzjywxk cfzjywxk merged commit e72c4cd into tikv:master Feb 26, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants