horizontal_shard.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # ext/horizontal_shard.py
  2. # Copyright (C) 2005-2017 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: http://www.opensource.org/licenses/mit-license.php
  7. """Horizontal sharding support.
  8. Defines a rudimental 'horizontal sharding' system which allows a Session to
  9. distribute queries and persistence operations across multiple databases.
  10. For a usage example, see the :ref:`examples_sharding` example included in
  11. the source distribution.
  12. """
  13. from .. import util
  14. from ..orm.session import Session
  15. from ..orm.query import Query
  16. __all__ = ['ShardedSession', 'ShardedQuery']
  17. class ShardedQuery(Query):
  18. def __init__(self, *args, **kwargs):
  19. super(ShardedQuery, self).__init__(*args, **kwargs)
  20. self.id_chooser = self.session.id_chooser
  21. self.query_chooser = self.session.query_chooser
  22. self._shard_id = None
  23. def set_shard(self, shard_id):
  24. """return a new query, limited to a single shard ID.
  25. all subsequent operations with the returned query will
  26. be against the single shard regardless of other state.
  27. """
  28. q = self._clone()
  29. q._shard_id = shard_id
  30. return q
  31. def _execute_and_instances(self, context):
  32. def iter_for_shard(shard_id):
  33. context.attributes['shard_id'] = shard_id
  34. result = self._connection_from_session(
  35. mapper=self._mapper_zero(),
  36. shard_id=shard_id).execute(
  37. context.statement,
  38. self._params)
  39. return self.instances(result, context)
  40. if self._shard_id is not None:
  41. return iter_for_shard(self._shard_id)
  42. else:
  43. partial = []
  44. for shard_id in self.query_chooser(self):
  45. partial.extend(iter_for_shard(shard_id))
  46. # if some kind of in memory 'sorting'
  47. # were done, this is where it would happen
  48. return iter(partial)
  49. def get(self, ident, **kwargs):
  50. if self._shard_id is not None:
  51. return super(ShardedQuery, self).get(ident)
  52. else:
  53. ident = util.to_list(ident)
  54. for shard_id in self.id_chooser(self, ident):
  55. o = self.set_shard(shard_id).get(ident, **kwargs)
  56. if o is not None:
  57. return o
  58. else:
  59. return None
  60. class ShardedSession(Session):
  61. def __init__(self, shard_chooser, id_chooser, query_chooser, shards=None,
  62. query_cls=ShardedQuery, **kwargs):
  63. """Construct a ShardedSession.
  64. :param shard_chooser: A callable which, passed a Mapper, a mapped
  65. instance, and possibly a SQL clause, returns a shard ID. This id
  66. may be based off of the attributes present within the object, or on
  67. some round-robin scheme. If the scheme is based on a selection, it
  68. should set whatever state on the instance to mark it in the future as
  69. participating in that shard.
  70. :param id_chooser: A callable, passed a query and a tuple of identity
  71. values, which should return a list of shard ids where the ID might
  72. reside. The databases will be queried in the order of this listing.
  73. :param query_chooser: For a given Query, returns the list of shard_ids
  74. where the query should be issued. Results from all shards returned
  75. will be combined together into a single listing.
  76. :param shards: A dictionary of string shard names
  77. to :class:`~sqlalchemy.engine.Engine` objects.
  78. """
  79. super(ShardedSession, self).__init__(query_cls=query_cls, **kwargs)
  80. self.shard_chooser = shard_chooser
  81. self.id_chooser = id_chooser
  82. self.query_chooser = query_chooser
  83. self.__binds = {}
  84. self.connection_callable = self.connection
  85. if shards is not None:
  86. for k in shards:
  87. self.bind_shard(k, shards[k])
  88. def connection(self, mapper=None, instance=None, shard_id=None, **kwargs):
  89. if shard_id is None:
  90. shard_id = self.shard_chooser(mapper, instance)
  91. if self.transaction is not None:
  92. return self.transaction.connection(mapper, shard_id=shard_id)
  93. else:
  94. return self.get_bind(
  95. mapper,
  96. shard_id=shard_id,
  97. instance=instance
  98. ).contextual_connect(**kwargs)
  99. def get_bind(self, mapper, shard_id=None,
  100. instance=None, clause=None, **kw):
  101. if shard_id is None:
  102. shard_id = self.shard_chooser(mapper, instance, clause=clause)
  103. return self.__binds[shard_id]
  104. def bind_shard(self, shard_id, bind):
  105. self.__binds[shard_id] = bind