SessionConsistentConnectionManager.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. *
  18. * @file
  19. * @ingroup Database
  20. */
  21. namespace Wikimedia\Rdbms;
  22. /**
  23. * Database connection manager.
  24. *
  25. * This manages access to master and replica databases. It also manages state that indicates whether
  26. * the replica databases are possibly outdated after a write operation, and thus the master database
  27. * should be used for subsequent read operations.
  28. *
  29. * @note: Services that access overlapping sets of database tables, or interact with logically
  30. * related sets of data in the database, should share a SessionConsistentConnectionManager.
  31. * Services accessing unrelated sets of information may prefer to not share a
  32. * SessionConsistentConnectionManager, so they can still perform read operations against replica
  33. * databases after a (unrelated, per the assumption) write operation to the master database.
  34. * Generally, sharing a SessionConsistentConnectionManager improves consistency (by avoiding race
  35. * conditions due to replication lag), but can reduce performance (by directing more read
  36. * operations to the master database server).
  37. *
  38. * @since 1.29
  39. *
  40. * @author Daniel Kinzler
  41. * @author Addshore
  42. */
  43. class SessionConsistentConnectionManager extends ConnectionManager {
  44. /**
  45. * @var bool
  46. */
  47. private $forceWriteConnection = false;
  48. /**
  49. * Forces all future calls to getReadConnection() to return a write connection.
  50. * Use this before performing read operations that are critical for a future update.
  51. *
  52. * @since 1.29
  53. */
  54. public function prepareForUpdates() {
  55. $this->forceWriteConnection = true;
  56. }
  57. /**
  58. * @since 1.29
  59. *
  60. * @param string[]|null $groups
  61. *
  62. * @return IDatabase
  63. */
  64. public function getReadConnection( array $groups = null ) {
  65. if ( $this->forceWriteConnection ) {
  66. return parent::getWriteConnection();
  67. }
  68. return parent::getReadConnection( $groups );
  69. }
  70. /**
  71. * @since 1.29
  72. *
  73. * @return IDatabase
  74. */
  75. public function getWriteConnection() {
  76. $this->prepareForUpdates();
  77. return parent::getWriteConnection();
  78. }
  79. /**
  80. * @since 1.29
  81. *
  82. * @param string[]|null $groups
  83. *
  84. * @return DBConnRef
  85. */
  86. public function getReadConnectionRef( array $groups = null ) {
  87. if ( $this->forceWriteConnection ) {
  88. return parent::getWriteConnectionRef();
  89. }
  90. return parent::getReadConnectionRef( $groups );
  91. }
  92. /**
  93. * @since 1.29
  94. *
  95. * @return DBConnRef
  96. */
  97. public function getWriteConnectionRef() {
  98. $this->prepareForUpdates();
  99. return parent::getWriteConnectionRef();
  100. }
  101. }