http://www.mediawiki.org/wiki/Special:Code/MediaWiki/66355
Revision: 66355 Author: maxsem Date: 2010-05-13 15:37:22 +0000 (Thu, 13 May 2010) Log Message: ----------- Cassie: renamed $wgThriftPort --> $wgCassandraPort, error handling, comments Modified Paths: -------------- trunk/extensions/Cassandra/Cassandra.php trunk/extensions/Cassandra/Cassandra_body.php Modified: trunk/extensions/Cassandra/Cassandra.php =================================================================== --- trunk/extensions/Cassandra/Cassandra.php 2010-05-13 15:26:34 UTC (rev 66354) +++ trunk/extensions/Cassandra/Cassandra.php 2010-05-13 15:37:22 UTC (rev 66355) @@ -10,11 +10,12 @@ 'version' => 0.1, 'author' => 'Max Semenik', 'url' => 'http://www.mediawiki.org/wiki/Extension:Cassandra', - 'description' => 'Allows to store revision text in [http://incubator.apache.org/cassandra/ Apache Cassandra] database.', + 'description' => 'Allows to store revision text in [http://cassandra.apache.org/ Apache Cassandra] database.', //'descriptionmsg' => 'cassandra-desc', ); -$wgAutoloadClasses['ExternalStoreCassandra'] = dirname( __FILE__ ) . '/Cassandra_body.php'; +$wgAutoloadClasses['ExternalStoreCassandra'] = $wgAutoloadClasses['MWCassandraException'] + = dirname( __FILE__ ) . '/Cassandra_body.php'; if ( is_array( $wgExternalStores ) ) { $wgExternalStores[] = 'cassandra'; @@ -26,19 +27,26 @@ * Extension settings */ -// Directory where Thrift for PHP resides. +// Directory where Thrift bindings for PHP reside $wgThriftRoot = '/usr/share/php/Thrift'; -$wgThriftPort = 9160; -$wgCassandraKeyPrefix = ''; +// Port used for communicating with Cassandra. Must match <ThriftPort> +// in Cassandra's storage-conf.xml +$wgCassandraPort = 9160; + +// String prepended to saved key names, can be used to distinct between +// different wikis, etc. Does not affect the already saved revisions. +$wgCassandraKeyPrefix = $wgDBname; + /** * Read and write consistencies, see http://wiki.apache.org/cassandra/API#ConsistencyLevel * for details. * Avoid using cassandra_ConsistencyLevel here to prevent large parts * of Cassandra and Thrift from being loaded on every request. Shouldn't - * matter for real-world setups with byte code cache though. + * matter much for real-world setups with byte code cache though. */ $wgCassandraReadConsistency = 1; // cassandra_ConsistencyLevel::ONE $wgCassandraWriteConsistency = 1; // cassandra_ConsistencyLevel::ONE +// Column family to be used for storing data $wgCassandraColumnFamily = 'Standard1'; \ No newline at end of file Modified: trunk/extensions/Cassandra/Cassandra_body.php =================================================================== --- trunk/extensions/Cassandra/Cassandra_body.php 2010-05-13 15:26:34 UTC (rev 66354) +++ trunk/extensions/Cassandra/Cassandra_body.php 2010-05-13 15:37:22 UTC (rev 66355) @@ -31,22 +31,27 @@ public function store( $cluster, $data ) { global $wgCassandraKeyPrefix, $wgCassandraWriteConsistency, $wgCassandraColumnFamily; - $this->connect( $cluster ); - $key = 'ES:' . $wgCassandraKeyPrefix . sha1( $data ); - $columnPath = new cassandra_ColumnPath(); - $columnPath->column = 'data'; - $columnPath->super_column = null; - $columnPath->column_family = $wgCassandraColumnFamily; + try { + $this->connect( $cluster ); + $key = $wgCassandraKeyPrefix . sha1( $data ); - $this->client->insert( $this->keyspace, $key, $columnPath, $data, time(), $wgCassandraWriteConsistency ); - return "cassandra://$cluster/$key"; + $columnPath = new cassandra_ColumnPath(); + $columnPath->column = 'data'; + $columnPath->super_column = null; + $columnPath->column_family = $wgCassandraColumnFamily; + + $this->client->insert( $this->keyspace, $key, $columnPath, $data, time(), $wgCassandraWriteConsistency ); + return "cassandra://$cluster/$key"; + } catch ( TException $e ) { + throw new MWCassandraException( $e ); + } } function fetchFromURL( $url ) { global $wgCassandraReadConsistency, $wgCassandraColumnFamily; - try{ + try { $this->connect( $url ); $splitted = explode( '/', $url ); $key = end( $splitted ); @@ -61,7 +66,7 @@ $predicate = new cassandra_SlicePredicate(); $predicate->slice_range = $sliceRange; - $result = $this->client->get_slice($this->keyspace, $key, $columnParent, $predicate, $wgCassandraReadConsistency); + $result = $this->client->get_slice( $this->keyspace, $key, $columnParent, $predicate, $wgCassandraReadConsistency ); return $result[0]->column->value; } catch ( TException $e ) { @@ -72,25 +77,32 @@ } private function connect( $cluster ) { - global $wgThriftPort; + global $wgCassandraPort; $cluster = str_replace( 'cassandra://', '', $cluster ); list( $host, $this->keyspace ) = explode( '/', $cluster ); - $this->socket = new TSocket( $host, $wgThriftPort); - $this->transport = new TBufferedTransport( $this->socket, 1024, 1024 ); - $this->protocol = new TBinaryProtocolAccelerated( $this->transport ); - $this->client = new CassandraClient( $this->protocol ); - $this->transport->open(); + try { + $this->socket = new TSocket( $host, $wgCassandraPort ); + $this->transport = new TBufferedTransport( $this->socket, 1024, 1024 ); + $this->protocol = new TBinaryProtocolAccelerated( $this->transport ); + $this->client = new CassandraClient( $this->protocol ); + $this->transport->open(); + } catch ( TException $e ) { + throw new MWCassandraException( $e ); + } } } +/** + * Wrapper exception for better handling in MW + */ class MWCassandraException extends MWException { public $innerException; public function __construct( TException $e ) { $this->innerException = $e; - parent::__construct( 'Cassandra error ' . get_class( $e ) . ': ' . $e->__toString() + parent::__construct( 'Cassandra error ' . get_class( $e ) . ': ' . $e->why . "\n\nStack trace: " . $e->getTraceAsString() ); } _______________________________________________ MediaWiki-CVS mailing list MediaWiki-CVS@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs