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

util: define and implement core interfaces for async api #1591

Merged
merged 1 commit into from
Feb 27, 2025

Conversation

zyguan
Copy link
Contributor

@zyguan zyguan commented Feb 25, 2025

ref #1586

@ti-chi-bot ti-chi-bot bot added dco-signoff: yes Indicates the PR's author has signed the dco. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Feb 25, 2025
@zyguan
Copy link
Contributor Author

zyguan commented Feb 25, 2025

/retest

@zyguan
Copy link
Contributor Author

zyguan commented Feb 26, 2025

@cfzjywxk PTAL

once sync.Once
e Executor
f func(T, error)
gs []func(T, error) (T, error)
Copy link
Contributor

Choose a reason for hiding this comment

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

What does gs stand for?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

f and g are common function names (in mathematics), and gs stands for a list of g (a common naming convention in FP).

l.lock.Unlock()
return 0, errors.New("runloop: already executing")
}
// assert l.state == stateIdle
Copy link
Contributor

Choose a reason for hiding this comment

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

Could be removed.

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 leave this comment on purpose. It's not a runnable code in golang, just reminds us that the state should be idle in the following code.

l.lock.Unlock()
return count, ctx.Err()
default:
f()
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 passing the ctx to f() so it could be terminated? Timeout cancel may be needed.

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 prefer keeping it simple, if f here accepts a ctx as an arg, then Callback functions (cb.f, cb.gs and call related methods) should accept a ctx as well.

In fact, ctx is typically referenced in the cb.f. eg.

func sendAll(ctx, reqs) {
  completed := 0
  e := NewExecutor(...)
  for req in reqs {
    cb := NewCallback(e, func(resp, err) {
      handleResponse(ctx, resp, err)
      completed++
    }
    cli.asyncSend(ctx, req, cb)
  }
  for completed < len(reqs) {
    e.Exec()
  }
}

l.Go(func() { atomic.StoreUint32(&n, 1) })
require.Eventually(t, func() bool { return atomic.LoadUint32(&n) == 1 }, time.Second, time.Millisecond)

// use a customized pool
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// use a customized pool
// Use a customized pool

@ti-chi-bot ti-chi-bot bot added needs-1-more-lgtm Indicates a PR needs 1 more LGTM. approved labels Feb 27, 2025
Copy link
Contributor

@ekexium ekexium left a comment

Choose a reason for hiding this comment

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

Generally LGTM. Just a few questions

  1. What is the expected scope of a RunLoop? Is it one per TiDB instance?
  2. Do we need to consider the behavior when shutting down? Do we need to drop or finish the pending tasks?

@zyguan
Copy link
Contributor Author

zyguan commented Feb 27, 2025

  1. What is the expected scope of a RunLoop? Is it one per TiDB instance?

The scope shall be small, typically one per query/txn-cmd, here is an example.

  1. Do we need to consider the behavior when shutting down? Do we need to drop or finish the pending tasks?

Yes, but it depends on the caller, RunLoop itself cannot perceive the business logic. Eg. when we send multiple requests, we can either collect all errors, or just return the first error (drop the rest).

@ti-chi-bot ti-chi-bot bot added the lgtm label Feb 27, 2025
Copy link

ti-chi-bot bot commented Feb 27, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: cfzjywxk, ekexium

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ti-chi-bot ti-chi-bot bot removed the needs-1-more-lgtm Indicates a PR needs 1 more LGTM. label Feb 27, 2025
Copy link

ti-chi-bot bot commented Feb 27, 2025

[LGTM Timeline notifier]

Timeline:

  • 2025-02-27 01:32:18.208062333 +0000 UTC m=+492286.161220599: ☑️ agreed by cfzjywxk.
  • 2025-02-27 04:02:36.909574372 +0000 UTC m=+501304.862732638: ☑️ agreed by ekexium.

@ti-chi-bot ti-chi-bot bot merged commit 5ac118b into tikv:master Feb 27, 2025
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved dco-signoff: yes Indicates the PR's author has signed the dco. lgtm size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants