Skip to content

Conversation

ShahabT
Copy link
Contributor

@ShahabT ShahabT commented Oct 19, 2025

What changed?

Describe what has changed in this PR.

Why?

Tell your future self why have you made these changes.

How did you test it?

  • built
  • run locally and tested manually
  • covered by existing tests
  • added new unit test(s)
  • added new functional test(s)

Potential risks

Any change is risky. Identify all risks you are aware of. If none, remove this section.

@ShahabT ShahabT requested review from a team as code owners October 19, 2025 02:28
ctx, cancel := context.WithCancel(ctx)
key := uuid.New()
// Listening to registry changes in case the cluster becomes active while the poll is waiting
c.namespaceRegistry.RegisterStateChangeCallback(key, func(ns *namespace.Namespace, deletedFromDb bool) {
Copy link
Member

Choose a reason for hiding this comment

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

instead of doing it here, I think we should register the namespace state change listener at task queue level, when it sees the state change, it would unload the task queue and reload it.
this is hot path that the overhead here could be too heavy.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hmm... yeah if we normally expect polls on the passive side what you're suggesting makes more sense. I was not sure if polls on passive cluster is typical.

Copy link
Contributor

Choose a reason for hiding this comment

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

We should not register/unregister callbacks here, but we shouldn't do it at task queue level either, we we should register a single callback at the engine level:

  1. There's no reason to unload/reload at the task queue partition level, nothing below the partition cares about namespace state.
  2. The namespace registry calls the callback initially for every loaded namespace when registered, so that's a lot of unnecessary work.
  3. We don't need a new level of cancellation, we already keep a map of cancelfuncs at the engine level, we just need to register/unregister them in another map by namespace. See https://github.com/temporalio/temporal/blob/main/service/matching/matching_engine.go#L2570-L2574 and related.

Copy link
Contributor

Choose a reason for hiding this comment

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

Correction: two more things do look at active namespace state:

  1. AddTask skips sync match if passive
  2. taskValidator skips first validation if active
    I don't think there's any reason to unload/reload because of those two.

Copy link
Contributor

@dnr dnr Oct 22, 2025

Choose a reason for hiding this comment

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

On second thought, it might simplify a few other things if we did reload the partition on ns state change (metrics, query-only state in new matcher). So I'm leaning towards reloading the partition. But I still think the listener itself should be at the engine level and it can just iterate over loaded partitions and unload the ones that belong to ns that changed (which will automatically interrupt polls, of course). Or keep a map to be more efficient.

ctx, cancel := context.WithCancel(ctx)
key := uuid.New()
// Listening to registry changes in case the cluster becomes active while the poll is waiting
c.namespaceRegistry.RegisterStateChangeCallback(key, func(ns *namespace.Namespace, deletedFromDb bool) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We should not register/unregister callbacks here, but we shouldn't do it at task queue level either, we we should register a single callback at the engine level:

  1. There's no reason to unload/reload at the task queue partition level, nothing below the partition cares about namespace state.
  2. The namespace registry calls the callback initially for every loaded namespace when registered, so that's a lot of unnecessary work.
  3. We don't need a new level of cancellation, we already keep a map of cancelfuncs at the engine level, we just need to register/unregister them in another map by namespace. See https://github.com/temporalio/temporal/blob/main/service/matching/matching_engine.go#L2570-L2574 and related.

Comment on lines +372 to +373
return t, err
} // else the cluster has become active so continue regular poll path
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not necessarily true, the caller could have canceled.

@ShahabT
Copy link
Contributor Author

ShahabT commented Oct 22, 2025

Reloading approach makes the most sense to me too. Closing this PR. Moody or me will send another PR.

@ShahabT ShahabT closed this Oct 22, 2025
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