greenthreads.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # greenthreads.py -- Utility module for querying an ObjectStore with gevent
  2. # Copyright (C) 2013 eNovance SAS <licensing@enovance.com>
  3. #
  4. # Author: Fabien Boucher <fabien.boucher@enovance.com>
  5. #
  6. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  7. # General Public License as public by the Free Software Foundation; version 2.0
  8. # or (at your option) any later version. You can redistribute it and/or
  9. # modify it under the terms of either of these two licenses.
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. # You should have received a copy of the licenses; if not, see
  18. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  19. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  20. # License, Version 2.0.
  21. #
  22. """Utility module for querying an ObjectStore with gevent."""
  23. from typing import FrozenSet, Optional, Set, Tuple
  24. import gevent
  25. from gevent import pool
  26. from .object_store import (
  27. MissingObjectFinder,
  28. _collect_ancestors,
  29. _collect_filetree_revs,
  30. )
  31. from .objects import Commit, ObjectID, Tag
  32. def _split_commits_and_tags(obj_store, lst, *, ignore_unknown=False, pool=None):
  33. """Split object id list into two list with commit SHA1s and tag SHA1s.
  34. Same implementation as object_store._split_commits_and_tags
  35. except we use gevent to parallelize object retrieval.
  36. """
  37. commits = set()
  38. tags = set()
  39. def find_commit_type(sha):
  40. try:
  41. o = obj_store[sha]
  42. except KeyError:
  43. if not ignore_unknown:
  44. raise
  45. else:
  46. if isinstance(o, Commit):
  47. commits.add(sha)
  48. elif isinstance(o, Tag):
  49. tags.add(sha)
  50. commits.add(o.object[1])
  51. else:
  52. raise KeyError(f"Not a commit or a tag: {sha}")
  53. jobs = [pool.spawn(find_commit_type, s) for s in lst]
  54. gevent.joinall(jobs)
  55. return (commits, tags)
  56. class GreenThreadsMissingObjectFinder(MissingObjectFinder):
  57. """Find the objects missing from another object store.
  58. Same implementation as object_store.MissingObjectFinder
  59. except we use gevent to parallelize object retrieval.
  60. """
  61. def __init__(
  62. self,
  63. object_store,
  64. haves,
  65. wants,
  66. progress=None,
  67. get_tagged=None,
  68. concurrency=1,
  69. get_parents=None,
  70. ) -> None:
  71. def collect_tree_sha(sha):
  72. self.sha_done.add(sha)
  73. cmt = object_store[sha]
  74. _collect_filetree_revs(object_store, cmt.tree, self.sha_done)
  75. self.object_store = object_store
  76. p = pool.Pool(size=concurrency)
  77. have_commits, have_tags = _split_commits_and_tags(
  78. object_store, haves, ignore_unknown=True, pool=p
  79. )
  80. want_commits, want_tags = _split_commits_and_tags(
  81. object_store, wants, ignore_unknown=False, pool=p
  82. )
  83. all_ancestors: FrozenSet[ObjectID] = frozenset(
  84. _collect_ancestors(object_store, have_commits)[0]
  85. )
  86. missing_commits, common_commits = _collect_ancestors(
  87. object_store, want_commits, all_ancestors
  88. )
  89. self.sha_done = set()
  90. jobs = [p.spawn(collect_tree_sha, c) for c in common_commits]
  91. gevent.joinall(jobs)
  92. for t in have_tags:
  93. self.sha_done.add(t)
  94. missing_tags = want_tags.difference(have_tags)
  95. wants = missing_commits.union(missing_tags)
  96. self.objects_to_send: Set[
  97. Tuple[ObjectID, Optional[bytes], Optional[int], bool]
  98. ] = {(w, None, 0, False) for w in wants}
  99. if progress is None:
  100. self.progress = lambda x: None
  101. else:
  102. self.progress = progress
  103. self._tagged = get_tagged and get_tagged() or {}