UsageStatistics_StorageWriter.php
9.06 KB
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<?php
namespace W3TC;
/**
* Manages data statistics.
* Metrics:
*
*/
class UsageStatistics_StorageWriter {
public static $slot_interval_secs = 60;
private $keep_history_interval_secs = 600;
private $cache_storage;
/**
* Cached values, just keep state between calls
*/
private $hotspot_endtime;
private $new_hotspot_endtime = 0;
private $now;
/**
* begin_ sets the state what should it perform later
* finish reacts to that state and finishes flushing
* values:
* not_needed - no flushinig required now
* require_db - database access has to be available to decide
* flushing_began_by_cache - that process has been selected to
* flush hotspot data based on cache data state, still that has to be
* verified in database
*/
private $flush_state;
public function __construct() {
$this->cache_storage = Dispatcher::get_usage_statistics_cache();
}
public function counter_add( $metric, $value ) {
if ( !is_null( $this->cache_storage ) )
$this->cache_storage->counter_add( $metric, $value );
}
public function get_hotspot_end() {
$option_storage = $this->get_option_storage();
return $option_storage->get_hotspot_end();
}
private function get_option_storage() {
if ( is_multisite() )
return new _OptionStorageWpmu();
else
return new _OptionStorageSingleSite();
}
public function maybe_flush_hotspot_data() {
$result = $this->begin_flush_hotspot_data();
if ( $result == 'not_needed' )
return;
$this->finish_flush_hotspot_data();
}
/**
* Returns if finish_* should be called.
* It tries to pass as litte processes as possible to
* flushing_begain if multiple processes come here
* at the same time when hotspot time ended.
*/
public function begin_flush_hotspot_data() {
if ( is_null( $this->hotspot_endtime ) ) {
// if cache not recognized - means nothing is cached at all
// so stats not collected
if ( is_null( $this->cache_storage ) )
return 'not_needed';
$v = $this->cache_storage->get( 'hotspot_endtime' );
$this->hotspot_endtime = ( isset( $v['content'] ) ? $v['content'] : 0 );
}
$hotspot_endtime_int = (int)$this->hotspot_endtime;
$this->now = time();
if ( $hotspot_endtime_int <= 0 ) {
$this->flush_state = 'require_db';
} else if ( $this->now < $hotspot_endtime_int ) {
$this->flush_state = 'not_needed';
} else {
// rand value makes value unique for each process,
// so as a result next replace works as a lock
// passing only single process further
$this->new_hotspot_endtime = $this->now + self::$slot_interval_secs +
( rand( 1, 9999 ) / 10000.0 );
$succeeded = $this->cache_storage->set_if_maybe_equals( 'hotspot_endtime',
array( 'content' => $this->hotspot_endtime ),
array( 'content' => $this->new_hotspot_endtime ) );
$this->flush_state =
( $succeeded ? 'flushing_began_by_cache' : 'not_needed' );
}
return $this->flush_state;
}
public function finish_flush_hotspot_data() {
$option_storage = $this->get_option_storage();
if ( $this->flush_state == 'not_needed' )
return;
if ( $this->flush_state != 'require_db' &&
$this->flush_state != 'flushing_began_by_cache' )
throw new Exception( 'unknown usage stats state ' . $this->flush_state );
// check whats there in db
$this->hotspot_endtime = $option_storage->get_hotspot_end();
$hotspot_endtime_int = (int)$this->hotspot_endtime;
if ( $this->now < $hotspot_endtime_int ) {
// update cache, since there is something old/missing in cache
$this->cache_storage->set( 'hotspot_endtime',
array( 'content' => $this->hotspot_endtime ) );
return; // not neeeded really, db state after
}
if ( $this->new_hotspot_endtime <= 0 )
$this->new_hotspot_endtime = $this->now +
self::$slot_interval_secs +
( rand( 1, 9999 ) / 10000.0 );
if ( $hotspot_endtime_int <= 0 ) {
// no data in options, initialization
$this->cache_storage->set( 'hotspot_endtime',
array( 'content' => $this->new_hotspot_endtime ) );
update_site_option( 'w3tc_stats_hotspot_start', time() );
$option_storage->set_hotspot_end( $this->new_hotspot_endtime );
return;
}
// try to become the process who makes flushing by
// performing atomic database update
// rand value makes value unique for each process,
// so as a result next replace works as a lock
// passing only single process further
$succeeded = $option_storage->prolong_hotspot_end(
$this->hotspot_endtime, $this->new_hotspot_endtime );
if ( !$succeeded )
return;
$this->cache_storage->set( 'hotspot_endtime',
array( 'content' => $this->new_hotspot_endtime ) );
// flush data
$metrics = array();
$metrics = apply_filters( 'w3tc_usage_statistics_metrics', $metrics );
$metric_values = array();
$metric_values['timestamp_start'] = get_site_option( 'w3tc_stats_hotspot_start' );
$metric_values['timestamp_end'] = $hotspot_endtime_int;
// try to limit time between get and reset of counter value
// to loose as small as posssible
foreach ( $metrics as $metric ) {
$metric_values[$metric] = $this->cache_storage->counter_get( $metric );
$this->cache_storage->counter_set( $metric, 0 );
}
$history_encoded = get_site_option( 'w3tc_stats_history' );
$history = null;
if ( !empty( $history_encoded ) )
$history = json_decode( $history_encoded, true );
if ( !is_array( $history ) )
$history = array();
$time_keep_border = time() - $this->keep_history_interval_secs;
if ( $hotspot_endtime_int < $time_keep_border )
$history = array(
array(
'timestamp_start' => $time_keep_border,
'timestamp_end' => (int)$this->new_hotspot_endtime -
self::$slot_interval_secs - 1
)
); // this was started too much time from now
else {
// add collected
$history[] = $metric_values;
// if we empty place later - fill it
for ( ;; ) {
$metric_values = array(
'timestamp_start' => $metric_values['timestamp_end']
);
$metric_values['timestamp_end'] =
$metric_values['timestamp_start'] + self::$slot_interval_secs;
if ( $metric_values['timestamp_end'] < $this->now )
$history[] = $metric_values;
else
break;
}
// make sure we have at least one value in history
for ( ;count( $history ) > 1; ) {
if ( !isset( $history[0]['timestamp_end'] ) ||
$history[0]['timestamp_end'] < $time_keep_border )
array_shift( $history );
else
break;
}
}
update_site_option( 'w3tc_stats_hotspot_start', $this->now );
update_site_option( 'w3tc_stats_history', json_encode( $history ) );
}
}
/**
* Can update option by directly incrementing current value,
* not via get+set operation
*/
class _OptionStorageSingleSite {
private $option_hotspot_end = 'w3tc_stats_hotspot_end';
public function get_hotspot_end() {
global $wpdb;
$row = $wpdb->get_row( $wpdb->prepare(
'SELECT option_value ' .
'FROM ' . $wpdb->options . ' ' .
'WHERE option_name = %s LIMIT 1',
$this->option_hotspot_end ) );
if ( !is_object( $row ) )
return false;
$v = $row->option_value;
return $v;
}
public function set_hotspot_end( $new_value ) {
update_site_option( $this->option_hotspot_end, $new_value );
}
/**
* Performs atomic update of option value
* from old to new value. Makes sure that only single process updates it.
* Only single process gets true return value when multiple tries to do that.
*/
public function prolong_hotspot_end( $old_value, $new_value ) {
global $wpdb;
$q = $wpdb->prepare(
'UPDATE ' . $wpdb->options . ' ' .
'SET option_value = %s ' .
'WHERE option_name = %s AND option_value = %s', $new_value,
$this->option_hotspot_end, $old_value );
$result = $wpdb->query( $q );
$succeeded = ( $result > 0 );
return $succeeded;
}
}
/**
* Can update option by directly incrementing current value,
* not via get+set operation
*/
class _OptionStorageWpmu {
private $option_hotspot_end = 'w3tc_stats_hotspot_end';
public function get_hotspot_end() {
global $wpdb;
$row = $wpdb->get_row( $wpdb->prepare(
'SELECT meta_value ' .
'FROM ' . $wpdb->sitemeta . ' ' .
'WHERE site_id = %d AND meta_key = %s',
$wpdb->siteid, $this->option_hotspot_end ) );
if ( !is_object( $row ) )
return false;
$v = $row->meta_value;
return $v;
}
/**
* Performs atomic update of option value
* from old to new value. Makes sure that only single process updates it.
* Only single process gets true return value when multiple tries to do that.
*/
public function set_hotspot_end( $new_value ) {
update_site_option( $this->option_hotspot_end, $new_value );
}
/**
* Performs atomic update of option value
* from old to new value. Makes sure that only single process updates it.
* Only single process gets true return value when multiple tries to do that.
*/
public function prolong_hotspot_end( $old_value, $new_value ) {
global $wpdb;
$result = $wpdb->query( $wpdb->prepare(
'UPDATE ' . $wpdb->sitemeta . ' ' .
'SET meta_value = %s ' .
'WHERE site_id = %d AND meta_key = %s AND meta_value = %s',
$new_value, $wpdb->siteid, $this->option_hotspot_end, $old_value ) );
$succeeded = ( $result > 0 );
return $succeeded;
}
}