summaryrefslogtreecommitdiff
blob: 608f1b0d7ddaa6a9547d9f4de1d911b7cf778bab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php

use MediaWiki\MediaWikiServices;
use Wikimedia\Rdbms\LoadBalancer;

/**
 * Database factory class, this will determine whether to use the main database
 * or an external database defined in configuration file
 */
class MWEchoDbFactory {

	/**
	 * The cluster for the database
	 * @var string|false
	 */
	private $cluster;

	private $shared;

	private $sharedCluster;

	/**
	 * @param string|false $cluster
	 * @param string|false $shared
	 * @param string|false $sharedCluster
	 */
	public function __construct( $cluster = false, $shared = false, $sharedCluster = false ) {
		$this->cluster = $cluster;
		$this->shared = $shared;
		$this->sharedCluster = $sharedCluster;
	}

	/**
	 * Create a db factory instance from default Echo configuration
	 * A singleton is not necessary because it's actually handled
	 * inside core database object
	 *
	 * @return MWEchoDbFactory
	 */
	public static function newFromDefault() {
		global $wgEchoCluster, $wgEchoSharedTrackingDB, $wgEchoSharedTrackingCluster;

		return new self( $wgEchoCluster, $wgEchoSharedTrackingDB, $wgEchoSharedTrackingCluster );
	}

	/**
	 * @return bool
	 */
	public function isReadOnly() {
		return ( $this->getLB()->getReadOnlyReason() !== false );
	}

	/**
	 * Get the database load balancer
	 * @return LoadBalancer
	 */
	protected function getLB() {
		// Use the external db defined for Echo
		if ( $this->cluster ) {
			$lb = MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->getExternalLB( $this->cluster );
		} else {
			$lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
		}

		return $lb;
	}

	/**
	 * @return LoadBalancer
	 */
	protected function getSharedLB() {
		if ( $this->sharedCluster ) {
			$lb = MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->getExternalLB( $this->sharedCluster );
		} else {
			$lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
		}

		return $lb;
	}

	/**
	 * Get the database connection for Echo
	 * @param int $db Index of the connection to get
	 * @param mixed $groups Query groups.
	 * @return \Wikimedia\Rdbms\IDatabase
	 */
	public function getEchoDb( $db, $groups = [] ) {
		return $this->getLB()->getConnection( $db, $groups );
	}

	/**
	 * @param int $db Index of the connection to get
	 * @param array $groups Query groups
	 * @return bool|\Wikimedia\Rdbms\IDatabase false if no shared db is configured
	 */
	public function getSharedDb( $db, $groups = [] ) {
		if ( !$this->shared ) {
			return false;
		}

		return $this->getSharedLB()->getConnection( $db, $groups, $this->shared );
	}

	/**
	 * Wrapper function for wfGetDB, some extensions like MobileFrontend is
	 * using this to issue sql queries against Echo database directly.  This
	 * is totally not accepted and should be updated to use Echo database access
	 * objects
	 *
	 * @deprecated Use newFromDefault() instead to create a db factory
	 * @param int $db Index of the connection to get
	 * @param mixed $groups Query groups.
	 * @param string|bool $wiki The wiki ID, or false for the current wiki
	 * @return \Wikimedia\Rdbms\IDatabase
	 */
	public static function getDB( $db, $groups = [], $wiki = false ) {
		global $wgEchoCluster;

		$services = MediaWikiServices::getInstance();

		// Use the external db defined for Echo
		if ( $wgEchoCluster ) {
			$lb = $services->getDBLoadBalancerFactory()->getExternalLB( $wgEchoCluster );
		} else {
			if ( $wiki === false ) {
				$lb = $services->getDBLoadBalancer();
			} else {
				$lb = $services->getDBLoadBalancerFactory()->getMainLB( $wiki );
			}
		}

		return $lb->getConnection( $db, $groups, $wiki );
	}

	/**
	 * Wait for the slaves of the database
	 */
	public function waitForSlaves() {
		$this->waitFor( $this->getMasterPosition() );
	}

	/**
	 * Get the current master position for the wiki and echo
	 * db when they have at least one slave in their cluster.
	 *
	 * @return array
	 */
	public function getMasterPosition() {
		$position = [
			'wikiDb' => false,
			'echoDb' => false,
		];
		$lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
		if ( $lb->getServerCount() > 1 ) {
			$position['wikiDb'] = $lb->getMasterPos();
		};

		if ( $this->cluster ) {
			$lb = $this->getLB();
			if ( $lb->getServerCount() > 1 ) {
				$position['echoDb'] = $lb->getMasterPos();
			}
		}

		return $position;
	}

	/**
	 * Receives the output of self::getMasterPosition. Waits
	 * for slaves to catch up to the master position at that
	 * point.
	 *
	 * @param array $position
	 */
	public function waitFor( array $position ) {
		if ( $position['wikiDb'] ) {
			MediaWikiServices::getInstance()->getDBLoadBalancer()->waitFor( $position['wikiDb'] );
		}
		if ( $position['echoDb'] ) {
			$this->getLB()->waitFor( $position['echoDb'] );
		}
	}

	/**
	 * Check whether it makes sense to retry a failed lookup on the master.
	 * @return bool True if there are multiple servers and changes were made in this request; false otherwise
	 */
	public function canRetryMaster() {
		return $this->getLB()->getServerCount() > 1 && $this->getLB()->hasOrMadeRecentMasterChanges();
	}
}