forked from mostlygeek/MongoSession
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DynamoSessionHandler.php
667 lines (547 loc) · 16.2 KB
/
DynamoSessionHandler.php
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
<?php
/**
* Uses DynamoDB as a session handler in PHP
*
* @author Nick Temple (commercemeister@gmail.com)
* @author Benson Wong (mostlygeek@gmail.com) (Original MongDB implementation)
* @license http://www.opensource.org/licenses/mit-license.html
*/
require_once('aws-sdk-for-php/sdk.class.php');
/*
The MIT License
Copyright (c) 2012 Nick Temple (commercemeister@gmail.com)
Copyright (c) 2010 Benson Wong (mostlygeek@gmail.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
class DynamoSessionHandler
{
/** @var DynamoSessionHandler */
protected static $_instance;
/** @var ddbIdTable */
protected $_table;
/**
* Default options for the connection
*
* @var array
*/
// protected $_defaults = array(
// 'servers' => array('localhost:27017'),
// 'options' => array(
// 'persist' => 'mongo-session'
// )
// );
/**
* Instantiate
*
* @param string $db
* @param string $collection
* @param array $config for the mongo connection
*/
protected function __construct(/*$db, $collection, Array $config*/)
{
$this->_table = new ddbIdTable('session');
// $this->_table = new ddbIdTable('session', 10, 10, true);
// $conf = (empty($config)) ? $this->_defaults : $config;
// $uri = 'mongodb://'.implode(',', $conf['servers']);
// $mongo = new Mongo($uri, $conf['options']);
// $this->_mongo = $mongo->selectCollection($db, $collection);
// $this->_mongo->ensureIndex(array('_id' => true, 'lock' => true));
// $this->_mongo->ensureIndex(array('expire' => true));
}
public function getTable() {
return $this->_table;
}
/**
* Gets the current instance
*
* @return DynamoSessionHandler null if register() has not been called yet
*/
public static function getInstance()
{
return self::$_instance;
}
/**
* Registers the handler into PHP
* @param string $db
* @param string $collection
* @param array $config
*/
public static function register($db, $collection, $config = array())
{
$m = new self(/*$db, $collection, $config*/);
self::$_instance = $m;
// boom.
session_set_save_handler(
array($m, 'noop'), // open
array($m, 'noop'), // close
array($m, 'read'),
array($m, 'write'),
array($m, 'destroy'),
array($m, 'gc')
);
}
/**
* Gets a global (across *all* machines) lock on the session
*
* @param string $id session id
*/
protected function _lock($id)
{
$remaining = 30000000; // 30 seconds timeout, 30Million microsecs
$timeout = 5000; // 5000 microseconds (5 ms)
$result = $this->_table->lock_lock($id);
if ($result) {
return true;
}
// Possible solution for locking a non-existant document
// Either we don't have a document, or we couldn't obtain the lock
$doc = $this->_table->get($id);
if (!isset($doc['id'])) {
$this->write($id, serialize(''));
}
// Now try to get the lock again.
do {
$result = $this->_table->lock_lock($id);
if ($result) {
return true;
}
usleep($timeout);
$remaining = $remaining - $timeout;
// wait a little longer next time, 1 sec max wait
$timeout = ($timeout < 1000000) ? $timeout * 2 : 1000000;
} while ($remaining > 0);
// aww shit.
// How can we handle this better?
$this->_table->lock_release($id);
throw new Exception('Could not get session lock');
}
/**
* A no-op function, somethings just aren't worth doing.
*/
public function noop() {}
/**
* Reads the session from DynamoDB
*
* @param string $id
* @return string
*/
public function read($id)
{
$this->_lock($id);
$doc = $this->_table->get($id, true); // Do a consistent read. Performance?
if (!isset($doc['d'])) {
return '';
} else {
return $doc['d'];
}
}
/**
* Writes the session data back to mongo
*
* @param string $id
* @param string $data
* @return bool
*/
public function write($id, $data)
{
$doc = array(
'id' => $id,
'lock' => 0,
'lock_ts' => time(),
'd' => $data,
'expire' => time() + intval(ini_get('session.gc_maxlifetime'))
);
$this->_table->save($doc);
return true; // @todo error checking
}
/**
* Destroy's the session
*
* @param string id
* @return bool
*/
public function destroy($id)
{
$this->_table->delete($id);
return true; // @todo error checking
}
/**
* Triggers the garbage collector, we do this with a mongo
* safe=false delete, as that will return immediately without
* blocking php.
*
* Maybe it'll delete stuff, maybe it won't. The next GC
* will get'em.... eventually :)
*
* @return bool
*/
public function gc($max)
{
// @todo - garbage collection
// $results = $this->_mongo->remove(
// array('expire' => array('$lt' => time()))
// );
}
}
/**
* Table class for DynamoDB with simplifying assumptions.
*/
class ddbIdTable {
var $TableName;
var $response;
var $schema;
/** @var AmazonDynamoDB */
var $dynamodb;
var $request;
/**
* Create a class to access a specific DynomDB table
*
* @param mixed $tableName
* @param mixed $ReadCapacityUnits
* @param mixed $WriteCapacityUnits
* @return ddbIdTable
*/
function __construct($tableName, $ReadCapacityUnits = 10, $WriteCapacityUnits = 5, $create = false) {
$this->dynamodb = new AmazonDynamoDB();
$this->TableName = $tableName;
$this->schema = array(
'TableName' => $tableName,
'KeySchema' => array( 'HashKeyElement' => array( 'AttributeName' => 'id', 'AttributeType' => AmazonDynamoDB::TYPE_STRING )),
'ProvisionedThroughput' => array( 'ReadCapacityUnits' => $ReadCapacityUnits, 'WriteCapacityUnits' => $WriteCapacityUnits )
);
if ($create) {
// Create the table if necessary
$status = $this->describeTable();
if ($status == 400) {
$this->createTable($this->schema);
}
}
}
/**
* Create a table
*
* @param mixed $schema
*/
function createTable($schema = null) {
if ($schema == null)
$schema = $this->schema;
$this->response = $this->dynamodb->create_table($schema);
// Sleep and poll until it's created
do {
sleep(1);
$this->describeTable();
}
while ((string) $this->response->body->Table->TableStatus !== 'ACTIVE');
return true;
}
function dropTable() {
$this->response = $dynamodb->delete_table(array( 'TableName' => $this->TableName ));
return $this->response->isOk();
}
/**
* describe a table, returning the status.
* Used to see if it exists.
*
*/
function describeTable() {
$this->response = $this->dynamodb->describe_table(array('TableName' => $this->TableName));
return $this->response->status;
}
/**
* Inserts or updates an item
*
* - if an item with the same id exists, it is replaced
* - if an item does not exist, it is added
*
* @param string $item
* @return CFResponse
*/
function save(&$item) {
$item = (array) $item;
if (! isset($item['id'])) {
$item['id'] = ddbUtil::uuid();
}
$r = new ddbRequest($this->TableName);
$r->setItem($item);
$params = $r->getParams();
$result = $this->dynamodb->put_item($params);
return $result;
}
/**
* Get an item by id
*
* @param mixed $id
* @param mixed $consistent
* @param mixed $attributes
* @return mixed
*/
function get($id, $consistent = false, $attributes = null) {
$r = new ddbRequest($this->TableName);
$r->setKey($id);
$r->setAttributesToGet($attributes);
$r->setConsistentRead($consistent);
$params = $r->getParams();
$this->response = $this->dynamodb->get_item($params);
if (! $this->response->isOk()) {
throw new Exception('Invlaid get operation.');
}
return ddbUtil::parse_item($this->response->body->Item);
}
/**
* Delete item by id
*
* @param mixed $id
* @return bool
*/
function delete($id) {
$r = new ddbRequest($this->TableName);
$r->setKey($id);
$params = $r->getParams();
$this->dynamodb->delete_item($params);
return $this->response->isOk();
}
function updateAttributes($item, $action = AmazonDynamoDB::ACTION_PUT) {
$r = new ddbRequest($this->TableName);
$r->setKey($item['id']);
unset($item['id']);
$r->setUpdates($item,$action);
$params = $r->getParams();
$this->response = $this->dynamodb->update_item($params);
}
function updateAttribute($id, $key, $value, $action = AmazonDynamoDB::ACTION_PUT) {
$item = array(
'id' => $id,
$key => $value
);
return $this->updateAttributes($item, $action);
}
/**
* Attempts to lock the specified field
* if it is a 0. Returns the the old value and previous
* timestamp on success, FALSE if the lock can't be obtained
*
* @param mixed $id
* @param mixed $field
* @param mixed $lock
* @param mixed $force
* @return mixed
*/
function lock_lock($id, $field = 'lock', $lock = true, $force=false) {
$r = new ddbRequest($this->TableName);
if ($lock) {
$expected_value = 0;
$new_value = 1;
} else {
$expected_value = 1;
$new_value = 0;
}
$r->setKey($id);
$r->setReturnValues(AmazonDynamoDB::RETURN_UPDATED_OLD);
if (!$force) {
$r->setExpected(array($field => $expected_value));
}
$r->setUpdates(array($field => $new_value, $field . '_ts' => time()), AmazonDynamoDB::ACTION_PUT);
$params = $r->getParams();
$this->response = $this->dynamodb->update_item($params);
$status = $this->response->status;
if ($status == '200') {
return ddbUtil::parse_item($this->response->body);
} else {
return false;
}
}
/**
* Resets the existing field to 0
*
* @param mixed $id
* @param mixed $field
*/
function lock_unlock($id, $field = 'lock') {
return $this->lock_lock($id, $field, false);
}
function lock_release($id, $field= 'lock') {
return $this->lock_lock($id, $field, false, false);
}
// Increment operation
function increment($id, $field, $value) {
$this->updateAttributes(array('id' => $id, $field => $value). AmazonDynamoDB::ACTION_ADD);
}
}
/**
* Build up a DynamoDB Request from component parts
*/
class ddbRequest {
var $TableName;
var $Key = null;
var $Item = null;
var $Expected = null;
var $ReturnValues = null;
var $ConsisteRead = false;
var $AttributesToGet = null;
var $AttributeUpdates = null;
function __construct($TableName) {
$this->TableName = $TableName;
}
function getParams() {
$params = array(
'TableName' => $this->TableName
);
if ($this->Key) $params['Key'] = $this->Key;
if ($this->Item) $params['Item'] = $this->Item;
if ($this->Expected) $params['Expected'] = $this->Expected;
if ($this->ReturnValues) $params['ReturnValues'] = $this->ReturnValues;
if ($this->ConsisteRead) $params['ConsistentRead'] = 'true';
if ($this->AttributesToGet) $params['AttributesToGet'] = $this->AttributesToGet;
if ($this->AttributeUpdates) $params['AttributeUpdates'] = $this->AttributeUpdates;
return $params;
}
function setKey($id, $range = null) {
if ($range) {
$this->Key = array('HashKeyElement' => ddbUtil::mapType($id), 'RangeKeyElement' => ddbUtil::mapType($range));
} else {
$this->Key = array('HashKeyElement' => ddbUtil::mapType($id));
}
}
function setItem($item) {
if (is_object($item)) $item = (array) $item;
$record = array();
foreach ($item as $k => $v) {
if (strlen("$k$v") > 63*1024) {
throw new Exception("Attribute too large"); //@todo store in S3, or break up among multiple attributes
}
$record[$k] = ddbUtil::mapType($v);
}
$this->Item = $record;
return $record;
}
function setUpdates($item, $action = AmazonDynamoDB::ACTION_PUT) {
if (!$this->AttributeUpdates) $this->AttributeUpdates = array();
foreach ($item as $k=>$v) {
$this->AttributeUpdates[$k] = array( 'Action' => $action, 'Value' => ddbUtil::mapType($v) );
}
}
function setExpected($item) {
if (!$this->Expected) $this->Expected = array();
unset($item['id']);
foreach ($item as $k => $v) {
$this->Expected[$k] = array('Value' => ddbUtil::mapType($v));
}
}
function setConsistentRead($value = true) {
$this->consisteRead = $value;
}
function setAttributesToGet($values = null) {
$this->AttributesToGet = $values;
}
/**
* Return Options
*
* AmazonDynamoDB::RETURN_ALL_OLD
* AmazonDynamoDB::RETURN_ALL_NEW
* AmazonDynamoDB::RETURN_NONE
* AmazonDynamoDB::RETURN_UPDATED_NEW
* AmazonDynamoDB::RETURN_UPDATED_OLD
* @param mixed $value
*/
function setReturnValues($value = AmazonDynamoDB::RETURN_ALL_OLD) {
$this->ReturnValues = $value;
}
}
/**
* Utilities for working with DynamoDB
*/
class ddbUtil {
/**
* Maps a PHP data type to those used in DynamoDB
* Returns either an array (to be used directly in the
* request) or the actual type.
*
* const TYPE_STRING = 'S';
* const TYPE_NUMBER = 'N';
* const TYPE_ARRAY_OF_STRINGS = 'SS';
* const TYPE_ARRAY_OF_NUMBERS = 'NS';
*
* @param mixed $var
* @param mixed $return_array
*/
static function mapType($var, $return_array = true) {
$vartype = gettype($var);
switch($vartype) {
case 'boolean':
$var = $var ? 0 : 1;
$type = AmazonDynamoDB::TYPE_NUMBER; break;
case 'integer':
case 'double':
case 'float':
$type = AmazonDynamoDB::TYPE_NUMBER; break;
case 'string':
$type = AmazonDynamoDB::TYPE_STRING; break;
case 'array':
// We only support arrays of strings right now
$type = AmazonDynamoDB::TYPE_ARRAY_OF_STRINGS;
$var2 = array();
// Force all data to string type
foreach ($var as $item) {
$var2[] = (string) $item;
}
$var = $var2;
break;
default:
throw new Exception('Unmappable type:' . $type);
}
if ($vartype != 'array') $var = (string) $var;
if ($return_array) {
return array($type => $var);
} else {
return $type;
}
}
/**
* Parse the response for data.
*
* This handles cases of strings and integes,
* not necessarily arrays of same.
*
* @param mixed $data
*/
static function parse_item($data) {
$item = array();
$data = (array)$data;
foreach ($data as $k => $v) {
$v = array_values((array) $v);
$item[$k] = array_pop($v);
}
return $item;
}
/**
* Create a unique ID
*/
static function uuid()
{
// The field names refer to RFC 4122 section 4.1.2
return sprintf('%04x%04x%04x%03x4%04x%04x%04x%04x',
mt_rand(0, 65535), mt_rand(0, 65535), // 32 bits for "time_low"
mt_rand(0, 65535), // 16 bits for "time_mid"
mt_rand(0, 4095), // 12 bits before the 0100 of (version) 4 for "time_hi_and_version"
bindec(substr_replace(sprintf('%016b', mt_rand(0, 65535)), '01', 6, 2)),
// 8 bits, the last two of which (positions 6 and 7) are 01, for "clk_seq_hi_res"
// (hence, the 2nd hex digit after the 3rd hyphen can only be 1, 5, 9 or d)
// 8 bits for "clk_seq_low"
mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535) // 48 bits for "node"
);
}
}