r106705 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r106704‎ | r106705 | r106706 >
Date:21:58, 19 December 2011
Author:aaron
Status:deferred
Tags:
Comment:
Use base 36 for lock/session SHA-1 keys to save memory on lock DB/server
Modified paths:
  • /branches/FileBackend/phase3/includes/filerepo/backend/lockmanager/DBLockManager.php (modified) (history)
  • /branches/FileBackend/phase3/includes/filerepo/backend/lockmanager/LSLockManager.php (modified) (history)
  • /branches/FileBackend/phase3/includes/filerepo/backend/lockmanager/LockManager.php (modified) (history)
  • /branches/FileBackend/phase3/maintenance/locking/LockServerDaemon.php (modified) (history)
  • /branches/FileBackend/phase3/maintenance/locking/file_locks.sql (modified) (history)

Diff [purge]

Index: branches/FileBackend/phase3/maintenance/locking/file_locks.sql
@@ -1,11 +1,11 @@
22 -- Table to handle resource locking (EX) with row-level locking
33 CREATE TABLE /*_*/filelocks_exclusive (
4 - fle_key binary(40) NOT NULL PRIMARY KEY
 4+ fle_key binary(31) NOT NULL PRIMARY KEY
55 ) ENGINE=InnoDB, CHECKSUM=0;
66
77 -- Table to handle resource locking (SH) with row-level locking
88 CREATE TABLE /*_*/filelocks_shared (
9 - fls_key binary(40) NOT NULL,
 9+ fls_key binary(31) NOT NULL,
1010 fls_session integer unsigned NOT NULL,
1111 PRIMARY KEY (fls_key,fls_session)
1212 ) ENGINE=InnoDB, CHECKSUM=0;
Index: branches/FileBackend/phase3/maintenance/locking/LockServerDaemon.php
@@ -228,7 +228,7 @@
229229 list( $session, $key, $command, $type, $values ) = $m;
230230 if ( sha1( $session . $command . $type . $values . $this->authKey ) !== $key ) {
231231 return 'BAD_KEY';
232 - } elseif ( strlen( $session ) !== 40 ) {
 232+ } elseif ( strlen( $session ) !== 31 ) {
233233 return 'BAD_SESSION';
234234 }
235235 $values = explode( '|', $values );
@@ -248,7 +248,7 @@
249249 return 'BAD_TYPE';
250250 }
251251 foreach ( $values as $value ) {
252 - if ( strlen( $value ) !== 40 ) {
 252+ if ( strlen( $value ) !== 31 ) {
253253 return 'BAD_FORMAT';
254254 }
255255 }
Index: branches/FileBackend/phase3/includes/filerepo/backend/lockmanager/DBLockManager.php
@@ -358,7 +358,7 @@
359359 * Get the bucket for lock key.
360360 * This should avoid throwing any exceptions.
361361 *
362 - * @param $key string (40 char hex key)
 362+ * @param $key string (31 char hex key)
363363 * @return integer
364364 */
365365 protected function getBucketFromKey( $key ) {
Index: branches/FileBackend/phase3/includes/filerepo/backend/lockmanager/LSLockManager.php
@@ -63,7 +63,7 @@
6464 for ( $i = 0; $i < 5; $i++ ) {
6565 $this->session .= mt_rand( 0, 2147483647 );
6666 }
67 - $this->session = sha1( $this->session );
 67+ $this->session = wfBaseConvert( sha1( $this->session ), 16, 36, 31 );
6868 }
6969
7070 protected function doLock( array $keys, $type ) {
@@ -267,7 +267,7 @@
268268 /**
269269 * Get the bucket for lock key
270270 *
271 - * @param $key string (40 char hex key)
 271+ * @param $key string (31 char hex key)
272272 * @return integer
273273 */
274274 protected function getBucketFromKey( $key ) {
Index: branches/FileBackend/phase3/includes/filerepo/backend/lockmanager/LockManager.php
@@ -47,7 +47,7 @@
4848 * @return Status
4949 */
5050 final public function lock( array $paths, $type = self::LOCK_EX ) {
51 - $keys = array_unique( array_map( 'sha1', $paths ) );
 51+ $keys = array_unique( array_map( 'LockManager::sha1Base36', $paths ) );
5252 return $this->doLock( $keys, $this->lockTypeMap[$type] );
5353 }
5454
@@ -59,11 +59,21 @@
6060 * @return Status
6161 */
6262 final public function unlock( array $paths, $type = self::LOCK_EX ) {
63 - $keys = array_unique( array_map( 'sha1', $paths ) );
 63+ $keys = array_unique( array_map( 'LockManager::sha1Base36', $paths ) );
6464 return $this->doUnlock( $keys, $this->lockTypeMap[$type] );
6565 }
6666
6767 /**
 68+ * Get the base 36 SHA-1 of a string, padded to 31 digits
 69+ *
 70+ * @param $path string
 71+ * @return string
 72+ */
 73+ final protected static function sha1Base36( $path ) {
 74+ return wfBaseConvert( sha1( $path ), 16, 36, 31 );
 75+ }
 76+
 77+ /**
6878 * Lock resources with the given keys and lock type
6979 *
7080 * @param $key Array List of keys to lock (40 char hex hashes)

Status & tagging log