SQLAzureShardManager.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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\SQLAzure;
  20. use Doctrine\DBAL\Sharding\ShardManager;
  21. use Doctrine\DBAL\Sharding\ShardingException;
  22. use Doctrine\DBAL\Connection;
  23. use Doctrine\DBAL\Types\Type;
  24. /**
  25. * Sharding using the SQL Azure Federations support.
  26. *
  27. * @author Benjamin Eberlei <kontakt@beberlei.de>
  28. */
  29. class SQLAzureShardManager implements ShardManager
  30. {
  31. /**
  32. * @var string
  33. */
  34. private $federationName;
  35. /**
  36. * @var bool
  37. */
  38. private $filteringEnabled;
  39. /**
  40. * @var string
  41. */
  42. private $distributionKey;
  43. /**
  44. * @var string
  45. */
  46. private $distributionType;
  47. /**
  48. * @var Connection
  49. */
  50. private $conn;
  51. /**
  52. * @var string
  53. */
  54. private $currentDistributionValue;
  55. /**
  56. * @param Connection $conn
  57. */
  58. public function __construct(Connection $conn)
  59. {
  60. $this->conn = $conn;
  61. $params = $conn->getParams();
  62. if ( ! isset($params['sharding']['federationName'])) {
  63. throw ShardingException::missingDefaultFederationName();
  64. }
  65. if ( ! isset($params['sharding']['distributionKey'])) {
  66. throw ShardingException::missingDefaultDistributionKey();
  67. }
  68. if ( ! isset($params['sharding']['distributionType'])) {
  69. throw ShardingException::missingDistributionType();
  70. }
  71. $this->federationName = $params['sharding']['federationName'];
  72. $this->distributionKey = $params['sharding']['distributionKey'];
  73. $this->distributionType = $params['sharding']['distributionType'];
  74. $this->filteringEnabled = (isset($params['sharding']['filteringEnabled'])) ? (bool)$params['sharding']['filteringEnabled'] : false;
  75. }
  76. /**
  77. * Get name of the federation
  78. *
  79. * @return string
  80. */
  81. public function getFederationName()
  82. {
  83. return $this->federationName;
  84. }
  85. /**
  86. * Get the distribution key
  87. *
  88. * @return string
  89. */
  90. public function getDistributionKey()
  91. {
  92. return $this->distributionKey;
  93. }
  94. /**
  95. * Get the Doctrine Type name used for the distribution
  96. *
  97. * @return string
  98. */
  99. public function getDistributionType()
  100. {
  101. return $this->distributionType;
  102. }
  103. /**
  104. * Enabled/Disable filtering on the fly.
  105. *
  106. * @param bool $flag
  107. * @return void
  108. */
  109. public function setFilteringEnabled($flag)
  110. {
  111. $this->filteringEnabled = (bool)$flag;
  112. }
  113. /**
  114. * {@inheritDoc}
  115. */
  116. public function selectGlobal()
  117. {
  118. if ($this->conn->isTransactionActive()) {
  119. throw ShardingException::activeTransaction();
  120. }
  121. $sql = "USE FEDERATION ROOT WITH RESET";
  122. $this->conn->exec($sql);
  123. $this->currentDistributionValue = null;
  124. }
  125. /**
  126. * {@inheritDoc}
  127. */
  128. public function selectShard($distributionValue)
  129. {
  130. if ($this->conn->isTransactionActive()) {
  131. throw ShardingException::activeTransaction();
  132. }
  133. if ($distributionValue === null || is_bool($distributionValue) || !is_scalar($distributionValue)) {
  134. throw ShardingException::noShardDistributionValue();
  135. }
  136. $platform = $this->conn->getDatabasePlatform();
  137. $sql = sprintf(
  138. "USE FEDERATION %s (%s = %s) WITH RESET, FILTERING = %s;",
  139. $platform->quoteIdentifier($this->federationName),
  140. $platform->quoteIdentifier($this->distributionKey),
  141. $this->conn->quote($distributionValue),
  142. ($this->filteringEnabled ? 'ON' : 'OFF')
  143. );
  144. $this->conn->exec($sql);
  145. $this->currentDistributionValue = $distributionValue;
  146. }
  147. /**
  148. * {@inheritDoc}
  149. */
  150. public function getCurrentDistributionValue()
  151. {
  152. return $this->currentDistributionValue;
  153. }
  154. /**
  155. * {@inheritDoc}
  156. */
  157. public function getShards()
  158. {
  159. $sql = "SELECT member_id as id,
  160. distribution_name as distribution_key,
  161. CAST(range_low AS CHAR) AS rangeLow,
  162. CAST(range_high AS CHAR) AS rangeHigh
  163. FROM sys.federation_member_distributions d
  164. INNER JOIN sys.federations f ON f.federation_id = d.federation_id
  165. WHERE f.name = " . $this->conn->quote($this->federationName);
  166. return $this->conn->fetchAll($sql);
  167. }
  168. /**
  169. * {@inheritDoc}
  170. */
  171. public function queryAll($sql, array $params = array(), array $types = array())
  172. {
  173. $shards = $this->getShards();
  174. if (!$shards) {
  175. throw new \RuntimeException("No shards found for " . $this->federationName);
  176. }
  177. $result = array();
  178. $oldDistribution = $this->getCurrentDistributionValue();
  179. foreach ($shards as $shard) {
  180. $this->selectShard($shard['rangeLow']);
  181. foreach ($this->conn->fetchAll($sql, $params, $types) as $row) {
  182. $result[] = $row;
  183. }
  184. }
  185. if ($oldDistribution === null) {
  186. $this->selectGlobal();
  187. } else {
  188. $this->selectShard($oldDistribution);
  189. }
  190. return $result;
  191. }
  192. /**
  193. * Split Federation at a given distribution value.
  194. *
  195. * @param mixed $splitDistributionValue
  196. */
  197. public function splitFederation($splitDistributionValue)
  198. {
  199. $type = Type::getType($this->distributionType);
  200. $sql = "ALTER FEDERATION " . $this->getFederationName() . " " .
  201. "SPLIT AT (" . $this->getDistributionKey() . " = " .
  202. $this->conn->quote($splitDistributionValue, $type->getBindingType()) . ")";
  203. $this->conn->exec($sql);
  204. }
  205. }