Skip to content

Conversation

ShaneHarvey
Copy link
Member

@ShaneHarvey ShaneHarvey commented May 24, 2021

Maintain individual generations for each serviceId. Note that new tests for this change (to exercise the LB serviceId code path) depend on connection pinning and have been added in #630

@ShaneHarvey ShaneHarvey changed the base branch from PYTHON-2676 to master May 28, 2021 21:33
Maintain individual generations for each serviceId.
@ShaneHarvey ShaneHarvey requested a review from prashantmital June 5, 2021 00:18
@ShaneHarvey ShaneHarvey marked this pull request as ready for review June 5, 2021 00:18
@ShaneHarvey ShaneHarvey removed the request for review from prashantmital June 5, 2021 00:19
@ShaneHarvey
Copy link
Member Author

I just had an idea which simplify the _PoolGeneration class a good amount. Please hold off reviewing this until I update.

@ShaneHarvey ShaneHarvey requested a review from prashantmital June 8, 2021 20:43
pymongo/pool.py Outdated

def get_overall(self):
"""Get the Pool's overall generation."""
return self._generations[None]
Copy link
Member Author

@ShaneHarvey ShaneHarvey Jun 8, 2021

Choose a reason for hiding this comment

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

The simplification I was going to make was to combine the get and get_overall methods since get(None) is equivalent to get_overall() but I decided that actually makes for more cryptic code. Instead I think we should keep them separate. get_overall() is called to get the "overall pool generation" before a connection is created. get(service_id) is called after a service_id is known (eg in SocketInfo._ismaster and Pool.stale_generation).

Copy link
Contributor

Choose a reason for hiding this comment

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

When we are in LB mode what is the significance of the overall generation ID? Aren't generation numbers only relevant on a per-service ID basis in that case?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's used in 2 places when in LB mode:

  • First, when an error occurs before the handshake completes, the service_id will be None. According to the spec, this means that we have to clear all the connections in the pool.
  • Second, the "overall" generation is used to determine if the pool has been cleared in the "remove_stale_sockets" background job.

The second one is when it's relevant to keep track of the total number of times the pool has been cleared (which is essentially what self._generations[None] tracks).

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks.

pymongo/pool.py Outdated

def get_overall(self):
"""Get the Pool's overall generation."""
return self._generations[None]
Copy link
Contributor

Choose a reason for hiding this comment

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

When we are in LB mode what is the significance of the overall generation ID? Aren't generation numbers only relevant on a per-service ID basis in that case?

pymongo/pool.py Outdated
# Maps service_id to generation.
self._generations = collections.defaultdict(int)
# Overall pool generation.
self._generations[None] = 0
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we use a better sentinel than None for tracking overall generation ID? Does this even need to be in the _generations map? I think we can just track it as an integer class attribute.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done, what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

LGTM!

@ShaneHarvey ShaneHarvey merged commit 112ee69 into mongodb:master Jun 15, 2021
ShaneHarvey added a commit that referenced this pull request Jun 15, 2021
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.

2 participants