Completed
Push — add/sync-action ( 798b6b...3d72dc )
by
unknown
19:10 queued 09:41
created

Jetpack_Sync_Queue::delete_checkout_id()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * A buffer of items from the queue that can be checked out
5
 */
6
class Jetpack_Sync_Queue_Buffer {
7
	public $id;
8
	public $items_with_ids;
9
10
	public function __construct( $items_with_ids ) {
11
		$this->id             = uniqid();
12
		$this->items_with_ids = $items_with_ids;
13
	}
14
15
	public function get_items() {
16
		return Jetpack_Sync_Utils::get_item_values( $this->items_with_ids );
17
	}
18
19
	public function get_item_ids() {
20
		return Jetpack_Sync_Utils::get_item_ids( $this->items_with_ids );
21
	}
22
}
23
24
/**
25
 * A persistent queue that can be flushed in increments of N items,
26
 * and which blocks reads until checked-out buffers are checked in or
27
 * closed. This uses raw SQL for two reasons: speed, and not triggering
28
 * tons of added_option callbacks.
29
 */
30
class Jetpack_Sync_Queue {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
31
	public $id;
32
	private $checkout_size;
33
	private $row_iterator;
34
35
	function __construct( $id, $checkout_size = 10 ) {
36
		$this->id            = str_replace( '-', '_', $id ); // necessary to ensure we don't have ID collisions in the SQL
37
		$this->checkout_size = $checkout_size;
38
		$this->memory_limit  = 5000000; // 5MB
0 ignored issues
show
Bug introduced by
The property memory_limit does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
39
		$this->row_iterator  = 0;
40
	}
41
42
	function add( $item ) {
43
		global $wpdb;
44
		$added = false;
45
		// this basically tries to add the option until enough time has elapsed that
46
		// it has a unique (microtime-based) option key
47
		while ( ! $added ) {
48
			$rows_added = $wpdb->query( $wpdb->prepare(
49
				"INSERT INTO $wpdb->options (option_name, option_value,autoload) VALUES (%s, %s,%s)",
50
				$this->get_next_data_row_option_name(),
51
				serialize( $item ),
52
				'no'
53
			) );
54
			$added      = ( $rows_added !== 0 );
55
		}
56
	}
57
58
	// Attempts to insert all the items in a single SQL query. May be subject to query size limits!
59
	function add_all( $items ) {
60
		global $wpdb;
61
		$base_option_name = $this->get_next_data_row_option_name();
62
63
		$query = "INSERT INTO $wpdb->options (option_name, option_value,autoload) VALUES ";
64
65
		$rows = array();
66
67
		for ( $i = 0; $i < count( $items ); $i += 1 ) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
68
			$option_name  = esc_sql( $base_option_name . '-' . $i );
69
			$option_value = esc_sql( serialize( $items[ $i ] ) );
70
			$rows[]       = "('$option_name', '$option_value', 'no')";
71
		}
72
73
		$rows_added = $wpdb->query( $query . join( ',', $rows ) );
74
75
		if ( $rows_added !== count( $items ) ) {
76
			return new WP_Error( 'row_count_mismatch', "The number of rows inserted didn't match the size of the input array" );
77
		}
78
	}
79
80
	// Peek at the front-most item on the queue without checking it out
81
	function peek( $count = 1 ) {
82
		$items = $this->fetch_items( $count );
83
		if ( $items ) {
84
			return Jetpack_Sync_Utils::get_item_values( $items );
85
		}
86
87
		return array();
88
	}
89
90
	// lag is the difference in time between the age of the oldest item and the current time
91
	function lag() {
92
		global $wpdb;
93
94
		$last_item_name = $wpdb->get_var( $wpdb->prepare( 
95
			"SELECT option_name FROM $wpdb->options WHERE option_name LIKE %s ORDER BY option_name ASC LIMIT 1",
96
			"jpsq_{$this->id}-%"
97
		) );
98
99
		if ( ! $last_item_name ) {
100
			return null;
101
		}
102
103
		// break apart the item name to get the timestamp
104
		$matches = null;
105
		if ( preg_match( '/^jpsq_'.$this->id.'-(\d+\.\d+)-/', $last_item_name, $matches ) ) {
106
			return microtime(true) - floatval($matches[1]);	
107
		} else {
108
			return null;
109
		}		
110
	}
111
112
	function reset() {
113
		global $wpdb;
114
		$this->delete_checkout_id();
115
		$wpdb->query( $wpdb->prepare(
116
			"DELETE FROM $wpdb->options WHERE option_name LIKE %s", "jpsq_{$this->id}-%"
117
		) );
118
	}
119
120
	function size() {
121
		global $wpdb;
122
123
		return $wpdb->get_var( $wpdb->prepare(
124
			"SELECT count(*) FROM $wpdb->options WHERE option_name LIKE %s", "jpsq_{$this->id}-%"
125
		) );
126
	}
127
128
	// we use this peculiar implementation because it's much faster than count(*)
129
	function has_any_items() {
130
		global $wpdb;
131
		$value = $wpdb->get_var( $wpdb->prepare(
132
			"SELECT exists( SELECT option_name FROM $wpdb->options WHERE option_name LIKE %s )", "jpsq_{$this->id}-%"
133
		) );
134
135
		return ( $value === "1" );
136
	}
137
138
	function checkout() {
139
		if ( $this->get_checkout_id() ) {
140
			return new WP_Error( 'unclosed_buffer', 'There is an unclosed buffer' );
141
		}
142
143
		$limit = $this->checkout_size;
144
145
		$before_usage = memory_get_usage();
146
		$items = $this->fetch_items( $limit );
147
		$after_usage = memory_get_usage();
148
		$current_count = count( $items );
149
		if ( $current_count === 0 ) {
150
			return false;
151
		}
152
153
		while( ( $after_usage - $before_usage < $this->memory_limit && $limit == $current_count  ) ) {
154
			$limit = $current_count;
155
			$items = array_merge( $items, $this->fetch_items( $this->checkout_size, $limit ) );
156
			$after_usage = memory_get_usage();
157
			$current_count = count( $items );
158
		}
159
160
		$buffer = new Jetpack_Sync_Queue_Buffer( array_slice( $items, 0, $current_count) );
161
162
		$result = $this->set_checkout_id( $buffer->id );
163
164
		if ( ! $result || is_wp_error( $result ) ) {
165
			error_log( "Badness setting checkout ID (this should not happen)" );
166
			return $result;
167
		}
168
169
		return $buffer;
170
	}
171
	
172
	function checkin( $buffer ) {
173
		$is_valid = $this->validate_checkout( $buffer );
174
175
		if ( is_wp_error( $is_valid ) ) {
176
			error_log("Invalid checkin: ".$is_valid->get_error_message());
177
			return $is_valid;
178
		}
179
180
		$this->delete_checkout_id();
181
182
		return true;
183
	}
184
185
	function close( $buffer ) {
186
		$is_valid = $this->validate_checkout( $buffer );
187
188
		if ( is_wp_error( $is_valid ) ) {
189
			error_log("Invalid close: ".$is_valid->get_error_message());
190
			return $is_valid;
191
		}
192
193
		$this->delete_checkout_id();
194
195
		global $wpdb;
196
197
		// all this fanciness is basically so we can prepare a statement with an IN(id1, id2, id3) clause
198
		$ids_to_remove = $buffer->get_item_ids();
199
		if ( count( $ids_to_remove ) > 0 ) {
200
			$sql   = "DELETE FROM $wpdb->options WHERE option_name IN (" . implode( ', ', array_fill( 0, count( $ids_to_remove ), '%s' ) ) . ')';
201
			$query = call_user_func_array( array( $wpdb, 'prepare' ), array_merge( array( $sql ), $ids_to_remove ) );
202
			$wpdb->query( $query );
203
		}
204
205
		return true;
206
	}
207
208
	function flush_all() {
209
		$items = Jetpack_Sync_Utils::get_item_values( $this->fetch_items() );
210
		$this->reset();
211
212
		return $items;
213
	}
214
215
	function get_all() {
216
		return $this->fetch_items();
217
	}
218
219
	function set_checkout_size( $new_size ) {
220
		$this->checkout_size = $new_size;
221
	}
222
223
	function set_memory_limit( $new_memory_limit ) {
224
		$this->memory_limit = $new_memory_limit;
225
	}
226
227
	// use with caution, this could allow multiple processes to delete
228
	// and send from the queue at the same time
229
	function force_checkin() {
230
		$this->delete_checkout_id();
231
	}
232
233
	// used to lock checkouts from the queue.
234
	// tries to wait up to $timeout seconds for the queue to be empty
235
	function lock( $timeout = 30 ) {
236
		$tries = 0;
237
238
		while( $this->has_any_items() && $tries < $timeout ) {
239
			sleep(1);			
240
			$tries += 1;
241
		}
242
243
		if ( $tries === 30 ) {
244
			return new WP_Error( 'lock_timeout', 'Timeout waiting for sync queue to empty' );
245
		}
246
247
		if ( $this->get_checkout_id() ) {
248
			return new WP_Error( 'unclosed_buffer', 'There is an unclosed buffer' );
249
		}
250
251
		// hopefully this means we can acquire a checkout?
252
		$result = $this->set_checkout_id( 'lock' );
253
254
		if ( ! $result || is_wp_error( $result ) ) {
255
			error_log( "badness setting checkout ID (this should not happen)" );
256
			return $result;
257
		}
258
259
		return true;
260
	}
261
262
	function unlock() {
263
		$this->delete_checkout_id();
264
	}
265
266
	private function get_checkout_id() {
267
		return get_transient( $this->get_checkout_transient_name() );
268
	}
269
270
	private function set_checkout_id( $checkout_id ) {
271
		return set_transient( $this->get_checkout_transient_name(), $checkout_id, 5*60 ); // 5 minute timeout
272
	}
273
274
	private function delete_checkout_id() {
275
		delete_transient( $this->get_checkout_transient_name() );
276
	}
277
278
	private function get_checkout_transient_name() {
279
		return "jpsq_{$this->id}_checkout";
280
	}
281
282
	private function get_next_data_row_option_name() {
283
		// this option is specifically chosen to, as much as possible, preserve time order
284
		// and minimise the possibility of collisions between multiple processes working 
285
		// at the same time
286
		// TODO: confirm we only need to support PHP 5.05+ (otherwise we'll need to emulate microtime as float, and avoid PHP_INT_MAX)
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
287
		// @see: http://php.net/manual/en/function.microtime.php
288
		$timestamp = sprintf( '%.6f', microtime( true ) );
289
290
		// row iterator is used to avoid collisions where we're writing data waaay fast in a single process
291
		if ( $this->row_iterator === PHP_INT_MAX ) {
292
			$this->row_iterator = 0;
293
		} else {
294
			$this->row_iterator += 1;
295
		}
296
297
		return 'jpsq_' . $this->id . '-' . $timestamp . '-' . getmypid() . '-' . $this->row_iterator;
298
	}
299
300
	private function fetch_items( $limit = null, $offset = null ) {
301
		global $wpdb;
302
303
		if ( $limit ) {
304
			if ( $offset ) {
305
				$query_sql = $wpdb->prepare( "SELECT option_name AS id, option_value AS value FROM $wpdb->options WHERE option_name LIKE %s ORDER BY option_name ASC LIMIT %d, %d", "jpsq_{$this->id}-%", $offset, $limit );
306
			} else {
307
				$query_sql = $wpdb->prepare( "SELECT option_name AS id, option_value AS value FROM $wpdb->options WHERE option_name LIKE %s ORDER BY option_name ASC LIMIT %d", "jpsq_{$this->id}-%", $limit );
308
			}
309
		} else {
310
			$query_sql = $wpdb->prepare( "SELECT option_name AS id, option_value AS value FROM $wpdb->options WHERE option_name LIKE %s ORDER BY option_name ASC", "jpsq_{$this->id}-%" );
311
		}
312
313
		$items = $wpdb->get_results( $query_sql );
314
		foreach ( $items as $item ) {
315
			$item->value = maybe_unserialize( $item->value );
316
		}
317
318
		return $items;
319
	}
320
321
	private function validate_checkout( $buffer ) {
322
		if ( ! $buffer instanceof Jetpack_Sync_Queue_Buffer ) {
323
			return new WP_Error( 'not_a_buffer', 'You must checkin an instance of Jetpack_Sync_Queue_Buffer' );
324
		}
325
326
		$checkout_id = $this->get_checkout_id();
327
328
		if ( ! $checkout_id ) {
329
			return new WP_Error( 'buffer_not_checked_out', 'There are no checked out buffers' );
330
		}
331
332
		if ( $checkout_id != $buffer->id ) {
333
			return new WP_Error( 'buffer_mismatch', 'The buffer you checked in was not checked out' );
334
		}
335
336
		return true;
337
	}
338
}
339
340
class Jetpack_Sync_Utils {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
341
342
	static function get_item_values( $items ) {
343
		return array_map( array( __CLASS__, 'get_item_value' ), $items );
344
	}
345
346
	static function get_item_ids( $items ) {
347
		return array_map( array( __CLASS__, 'get_item_id' ), $items );
348
	}
349
350
	static private function get_item_value( $item ) {
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
351
		return $item->value;
352
	}
353
354
	static private function get_item_id( $item ) {
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
355
		return $item->id;
356
	}
357
}
358