PoolingShardManager.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\DBAL\Sharding;
  20. use Doctrine\DBAL\Sharding\ShardChoser\ShardChoser;
  21. /**
  22. * Shard Manager for the Connection Pooling Shard Strategy
  23. *
  24. * @author Benjamin Eberlei <kontakt@beberlei.de>
  25. */
  26. class PoolingShardManager implements ShardManager
  27. {
  28. /**
  29. * @var PoolingShardConnection
  30. */
  31. private $conn;
  32. /**
  33. * @var ShardChoser
  34. */
  35. private $choser;
  36. /**
  37. * @var string|null
  38. */
  39. private $currentDistributionValue;
  40. /**
  41. * @param PoolingShardConnection $conn
  42. */
  43. public function __construct(PoolingShardConnection $conn)
  44. {
  45. $params = $conn->getParams();
  46. $this->conn = $conn;
  47. $this->choser = $params['shardChoser'];
  48. }
  49. /**
  50. * @return void
  51. */
  52. public function selectGlobal()
  53. {
  54. $this->conn->connect(0);
  55. $this->currentDistributionValue = null;
  56. }
  57. /**
  58. * @param string $distributionValue
  59. *
  60. * @return void
  61. */
  62. public function selectShard($distributionValue)
  63. {
  64. $shardId = $this->choser->pickShard($distributionValue, $this->conn);
  65. $this->conn->connect($shardId);
  66. $this->currentDistributionValue = $distributionValue;
  67. }
  68. /**
  69. * @return string|null
  70. */
  71. public function getCurrentDistributionValue()
  72. {
  73. return $this->currentDistributionValue;
  74. }
  75. /**
  76. * @return array
  77. */
  78. public function getShards()
  79. {
  80. $params = $this->conn->getParams();
  81. $shards = array();
  82. foreach ($params['shards'] as $shard) {
  83. $shards[] = array('id' => $shard['id']);
  84. }
  85. return $shards;
  86. }
  87. /**
  88. * @param string $sql
  89. * @param array $params
  90. * @param array $types
  91. *
  92. * @return array
  93. *
  94. * @throws \RuntimeException
  95. */
  96. public function queryAll($sql, array $params, array $types)
  97. {
  98. $shards = $this->getShards();
  99. if (!$shards) {
  100. throw new \RuntimeException("No shards found.");
  101. }
  102. $result = array();
  103. $oldDistribution = $this->getCurrentDistributionValue();
  104. foreach ($shards as $shard) {
  105. $this->conn->connect($shard['id']);
  106. foreach ($this->conn->fetchAll($sql, $params, $types) as $row) {
  107. $result[] = $row;
  108. }
  109. }
  110. if ($oldDistribution === null) {
  111. $this->selectGlobal();
  112. } else {
  113. $this->selectShard($oldDistribution);
  114. }
  115. return $result;
  116. }
  117. }