summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/MediaWiki/Deferred/TransactionalCallableUpdate.php
blob: d98d6983048da1d5f2b6eef4ea9ebd454ab542d0 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?php

namespace SMW\MediaWiki\Deferred;

use Closure;
use SMW\MediaWiki\Database;

/**
 * Extends DeferredCallableUpdate to allow handling of transaction related tasks
 * or isolations to ensure an undisturbed update process before and after
 * MediaWiki::preOutputCommit.
 *
 * @license GNU GPL v2+
 * @since 3.0
 *
 * @author mwjames
 */
class TransactionalCallableUpdate extends CallableUpdate {

	/**
	 * @var Database|null
	 */
	private $connection;

	/**
	 * @var boolean
	 */
	private $onTransactionIdle = false;

	/**
	 * @var int|null
	 */
	private $transactionTicket = null;

	/**
	 * @var array
	 */
	private $preCommitableCallbacks = [];

	/**
	 * @var array
	 */
	private $postCommitableCallbacks = [];

	/**
	 * @var boolean
	 */
	private $autoCommit = false;

	/**
	 * @since 3.0
	 *
	 * @param callable $callback|null
	 * @param Database|null $connection
	 */
	public function __construct( callable $callback = null, Database $connection ) {
		parent::__construct( $callback );
		$this->connection = $connection;
		$this->connection->onTransactionResolution( [ $this, 'cancelOnRollback' ], __METHOD__ );
	}

	/**
	 * @note MW 1.29+ showed transaction collisions (Exception thrown with
	 * an uncommitted database transaction), use 'onTransactionIdle' to isolate
	 * the update execution.
	 *
	 * @since 2.5
	 */
	public function waitOnTransactionIdle() {
		$this->onTransactionIdle = !$this->isCommandLineMode;
	}

	/**
	 * @since 3.0
	 */
	public function runAsAutoCommit() {
		$this->autoCommit = true;
	}

 	/**
	 * It tries to fetch a transactionTicket to assert whether transaction writes
	 * are active or not and if available will process Database::commitAndWaitForReplication
	 * during DeferredCallableUpdate::doUpdate to safely post commits to the
	 * master.
	 *
	 * @note If the commandLine is active then continue an update without a ticket
	 * to avoid any update lag or possible transaction lock.
	 *
	 * @since 3.0
	 */
	public function commitWithTransactionTicket() {
		if ( $this->isCommandLineMode === false && $this->isDeferrableUpdate === true ) {
			$this->transactionTicket = $this->connection->getEmptyTransactionTicket( $this->getOrigin() );
		}
	}

	/**
	 * Attaches a callback pre-execution of the source callback and is scheduled
	 * to be executed before the source callback.
	 *
	 * @since 3.0
	 *
	 * @param string $fname
	 * @param Closure $callback
	 */
	public function addPreCommitableCallback( $fname, callable $callback ) {
		if ( is_callable( $callback ) ) {
			$this->preCommitableCallbacks[$fname] = $callback;
		}
	}

	/**
	 * Attaches a callback post execution of the source callback and is scheduled
	 * to be executed after the source callback.
	 *
	 * @since 3.0
	 *
	 * @param string $fname
	 * @param Closure $callback
	 */
	public function addPostCommitableCallback( $fname, callable $callback ) {
		if ( is_callable( $callback ) ) {
			$this->postCommitableCallbacks[$fname] = $callback;
		}
	}

	/**
	 * @see DeferrableUpdate::doUpdate
	 *
	 * @since 3.0
	 */
	public function doUpdate() {

		if ( $this->onTransactionIdle ) {
			return $this->runOnTransactionIdle();
		}

		$this->runPreCommitCallbacks();

		$e = null;
		$autoTrx = null;

		if ( $this->autoCommit ) {
			$this->logger->info( [ 'DeferrableUpdate', 'Transactional, as auto commit', 'Update' ] );
			$autoTrx = $this->connection->getFlag( DBO_TRX );
			$this->connection->clearFlag( DBO_TRX );
		}

		try {
			parent::doUpdate();
		} catch ( \Exception $e ) {
		}

		if ( $this->autoCommit && $autoTrx ) {
			$this->connection->setFlag( DBO_TRX );
		}

		if ( $e ) {
			throw $e;
		}

		$this->runPostCommitCallbacks();

		if ( $this->transactionTicket !== null ) {
			$this->connection->commitAndWaitForReplication( $this->getOrigin(), $this->transactionTicket );
		}
	}

	/**
	 * @since 3.0
	 */
	public function cancelOnRollback( $trigger ) {
		if ( $trigger === Database::TRIGGER_ROLLBACK ) {
			$this->callback = [ $this, 'emptyCancelCallback' ];
		}
	}

	protected function addUpdate( $update ) {

		if ( $this->onTransactionIdle ) {
			$this->logger->info(
				[
					'DeferrableUpdate',
					'Transactional',
					'Added: {origin} (onTransactionIdle)'
				],
				[
					'method' => __METHOD__,
					'role' => 'developer',
					'origin' => $this->getOrigin()
				]
			);

			return $this->connection->onTransactionIdle( function() use( $update ) {
				$update->onTransactionIdle = false;
				parent::addUpdate( $update );
			} );
		}

		parent::addUpdate( $update );
	}

	protected function getLoggableContext() {
		return parent::getLoggableContext() + [
			'transactionTicket' => $this->transactionTicket
		];
	}

	private function runOnTransactionIdle() {
		$this->connection->onTransactionIdle( function() {
			$this->logger->info(
				[
					'DeferrableUpdate',
					'Transactional',
					'Update: {origin} (onTransactionIdle)'
				],
				[
					'method' => __METHOD__,
					'role' => 'developer',
					'origin' => $this->getOrigin()
				]
			);
			$this->onTransactionIdle = false;
			$this->doUpdate();
		} );
	}

	private function runPreCommitCallbacks() {
		foreach ( $this->preCommitableCallbacks as $fname => $preCallback ) {
			$this->logger->info(
				[
					'DeferrableUpdate',
					'Transactional',
					'Update: {origin} (pre-commitable callback: {fname})'
				],
				[
					'method' => __METHOD__,
					'role' => 'developer',
					'origin' => $this->getOrigin(),
					'fname' => $fname
				]
			);

			call_user_func( $preCallback, $this->transactionTicket );
		}
	}

	private function runPostCommitCallbacks() {
		foreach ( $this->postCommitableCallbacks as $fname => $postCallback ) {
			$this->logger->info(
				[
					'DeferrableUpdate',
					'Transactional',
					'Update: {origin} (post-commitable callback: {fname})'
				],
				[
					'method' => __METHOD__,
					'role' => 'developer',
					'origin' => $this->getOrigin(),
					'fname' => $fname
				]
			);

			call_user_func( $postCallback, $this->transactionTicket );
		}
	}

	protected function emptyCancelCallback() {
		$this->logger->info(
			[ 'DeferrableUpdate', 'cancelOnRollback' ],
			[ 'role' => 'developer', 'method' => __METHOD__ ]
		);
	}

}