Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Ql/Cql/CqlConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public function connect()
(int)$this->_config()->getItem('send_timeout', 1000)
);

$this->_transport = new TFramedTransport($this->_socket);
$this->_transport = new DalFramedTransport($this->_socket);
$this->_protocol = new TBinaryProtocolAccelerated($this->_transport);
$this->_client = new CassandraClient($this->_protocol);

Expand Down
78 changes: 78 additions & 0 deletions src/Ql/Cql/DalFramedTransport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
namespace Packaged\Dal\Ql\Cql;

use Thrift\Transport\TFramedTransport;
use Thrift\Transport\TTransport;

/**
* Framed transport that serves reads from the current frame by offset.
*
* The binary protocol reads a CQL result one 1-8 byte field at a time.
* TFramedTransport re-slices the remaining frame on every read, so a frame of
* n bytes costs O(n^2) copying; a few MB is enough to exhaust the request's
* execution time. Reading by offset keeps it linear.
*/
class DalFramedTransport extends TFramedTransport
{
/** @var TTransport */
private $_transport;
/** @var bool */
private $_readFramed;
/** @var string */
private $_buffer = '';
/** @var int */
private $_offset = 0;
/** @var int */
private $_length = 0;

public function __construct($transport = null, $read = true, $write = true)
{
parent::__construct($transport, $read, $write);
$this->_transport = $transport;
$this->_readFramed = $read;
}

public function read($len)
{
if(!$this->_readFramed)
{
return $this->_transport->read($len);
}

if($this->_offset >= $this->_length)
{
$this->_readFrame();
if($this->_length === 0)
{
return '';
}
}

$out = substr($this->_buffer, $this->_offset, $len);
$this->_offset += strlen($out);
if($this->_offset >= $this->_length)
{
$this->_setBuffer('');
}
return $out;
}

public function putBack($data)
{
$remaining = $this->_offset < $this->_length ? substr($this->_buffer, $this->_offset) : '';
$this->_setBuffer($data . $remaining);
}

private function _readFrame()
{
$size = unpack('N', $this->_transport->readAll(4))[1];
$this->_setBuffer($size > 0 ? $this->_transport->readAll($size) : '');
}

private function _setBuffer(string $buffer)
{
$this->_buffer = $buffer;
$this->_offset = 0;
$this->_length = strlen($buffer);
}
}
85 changes: 85 additions & 0 deletions tests/Ql/Cql/DalFramedTransportTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
namespace Packaged\Dal\Tests\Ql\Cql;

use Packaged\Dal\Ql\Cql\DalFramedTransport;
use PHPUnit\Framework\TestCase;
use Thrift\Transport\TMemoryBuffer;

class DalFramedTransportTest extends TestCase
{
private function _frame(string $payload): string
{
return pack('N', strlen($payload)) . $payload;
}

public function testReadsSmallFieldsAcrossFrames()
{
$transport = new DalFramedTransport(
new TMemoryBuffer($this->_frame('abcdef') . $this->_frame('ghij'))
);
$transport->open();

$this->assertSame('abcd', $transport->read(4));
$this->assertSame('ef', $transport->read(4));
$this->assertSame('gh', $transport->readAll(2));
$this->assertSame('ij', $transport->read(10));
}

public function testReadAllSpansFrames()
{
$transport = new DalFramedTransport(
new TMemoryBuffer($this->_frame('abc') . $this->_frame('def'))
);
$this->assertSame('abcdef', $transport->readAll(6));
}

public function testPutBackIsReadFirst()
{
$transport = new DalFramedTransport(new TMemoryBuffer($this->_frame('abcdef')));

$this->assertSame('ab', $transport->read(2));
$transport->putBack('XY');
$this->assertSame('XYcd', $transport->read(4));
$this->assertSame('ef', $transport->read(4));

$transport->putBack('Z');
$this->assertSame('Z', $transport->read(4));
}

public function testUnframedReadPassesThrough()
{
$transport = new DalFramedTransport(new TMemoryBuffer('raw-bytes'), false, false);
$this->assertSame('raw', $transport->read(3));
}

public function testWritesAreFramed()
{
$buffer = new TMemoryBuffer();
$transport = new DalFramedTransport($buffer);
$transport->write('hello');
$transport->flush();
$this->assertSame($this->_frame('hello'), $buffer->getBuffer());
}

/**
* The binary protocol reads one field at a time, so a large CQL result is
* consumed as millions of 1-8 byte reads from a single frame.
*/
public function testLargeFrameReadInSmallFieldsIsLinear()
{
$size = 2 * 1024 * 1024;
$payload = str_repeat('0123456789abcdef', $size / 16);
$transport = new DalFramedTransport(new TMemoryBuffer($this->_frame($payload)));

$start = microtime(true);
$out = '';
while(strlen($out) < $size)
{
$out .= $transport->read(4);
}
$elapsed = microtime(true) - $start;

$this->assertSame($payload, $out);
$this->assertLessThan(2.0, $elapsed);
}
}
Loading