Skip to content

Commit 4eec96f

Browse files
committed
Merge pull request #4 from Automattic/cleanup-queries
Cleanup queries
2 parents 3881f56 + 84740a2 commit 4eec96f

File tree

6 files changed

+74
-26
lines changed

6 files changed

+74
-26
lines changed

ThriftSQL.phar

1.13 KB
Binary file not shown.

src/ThriftSQL/Hive.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,22 @@ public function connect() {
8080

8181
public function queryAndFetchAll( $queryStr ) {
8282
try {
83+
$sleeper = new \ThriftSQL\Utils\Sleeper();
84+
$queryCleaner = new \ThriftSQL\Utils\QueryCleaner();
85+
8386
$TExecuteStatementResp = $this->_client->ExecuteStatement( new \ThriftSQL\TExecuteStatementReq( array(
8487
'sessionHandle' => $this->_sessionHandle,
85-
'statement' => $queryStr,
88+
'statement' => $queryCleaner->clean( $queryStr ),
8689
'runAsync' => true,
8790
) ) );
8891

8992
// Wait for results
90-
$iteration = 0;
93+
$sleeper->reset();
9194
do {
9295

93-
usleep( $this->_getSleepUsec( $iteration ) );
96+
$slept = $sleeper->sleep()->getSleptSecs();
9497

95-
if ( $iteration > 250 ) {
98+
if ( $slept > 18000 ) { // 5 Hours
9699
// TODO: Actually kill the query then throw exception.
97100
throw new \ThriftSQL\Exception( 'Hive Query Killed!' );
98101
}
@@ -202,15 +205,6 @@ public function disconnect() {
202205

203206
}
204207

205-
private function _getSleepUsec( $iteration ) {
206-
// Max out at 30 second sleep per check
207-
if ( 14 < $iteration ) {
208-
return 30000000;
209-
}
210-
211-
return pow( 2, $iteration ) * 1000;
212-
}
213-
214208
private function _isOperationFinished( $state ) {
215209
return ( \ThriftSQL\TOperationState::FINISHED_STATE == $state );
216210
}

src/ThriftSQL/Impala.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,20 @@ public function connect() {
5252

5353
public function queryAndFetchAll( $queryStr ) {
5454
try {
55+
$sleeper = new \ThriftSQL\Utils\Sleeper();
56+
$queryCleaner = new \ThriftSQL\Utils\QueryCleaner();
57+
5558
$QueryHandle = $this->_client->query( new \ThriftSQL\Query( array(
56-
'query' => $queryStr,
59+
'query' => $queryCleaner->clean( $queryStr ),
5760
) ) );
5861

5962
// Wait for results
60-
$iteration = 0;
63+
$sleeper->reset();
6164
do {
6265

63-
usleep( $this->_getSleepUsec( $iteration ) );
66+
$slept = $sleeper->sleep()->getSleptSecs();
6467

65-
if ( $iteration > 250 ) {
68+
if ( $slept > 18000 ) { // 5 Hours
6669
// TODO: Actually kill the query then throw exception.
6770
throw new \ThriftSQL\Exception( 'Impala Query Killed!' );
6871
}
@@ -157,15 +160,6 @@ public function disconnect() {
157160

158161
}
159162

160-
private function _getSleepUsec( $iteration ) {
161-
// Max out at 30 second sleep per check
162-
if ( 14 < $iteration ) {
163-
return 30000000;
164-
}
165-
166-
return pow( 2, $iteration ) * 1000;
167-
}
168-
169163
private function _isOperationFinished( $state ) {
170164
return ( \ThriftSQL\QueryState::FINISHED == $state );
171165
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
namespace ThriftSQL\Utils;
3+
4+
/**
5+
* Util to clean up queries; e.g. remove trailing `;`.
6+
*/
7+
8+
class QueryCleaner {
9+
10+
public function clean( $queryStr ) {
11+
// Very simplistic
12+
return trim( $queryStr, "; \t\n\r\0\x0B" );
13+
}
14+
15+
}

src/ThriftSQL/Utils/Sleeper.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
namespace ThriftSQL\Utils;
3+
4+
/**
5+
* Util to do exponential sleeping.
6+
*/
7+
8+
class Sleeper {
9+
10+
private $_iterations = 0;
11+
private $_slept = 0;
12+
13+
public function reset() {
14+
$this->_iterations = 0;
15+
$this->_slept = 0;
16+
17+
return $this;
18+
}
19+
20+
public function sleep() {
21+
$mSecs = $this->_getSleepMS();
22+
23+
usleep( $mSecs * 1000 );
24+
25+
$this->_iterations++;
26+
$this->_slept += $mSecs;
27+
28+
return $this;
29+
}
30+
31+
public function getSleptSecs() {
32+
return $this->_slept / 1000;
33+
}
34+
35+
private function _getSleepMS() {
36+
if ( 14 < $this->_iterations ) {
37+
return 30000; // Max out at 30 second sleep per check
38+
}
39+
40+
return pow( 2, $this->_iterations );
41+
}
42+
43+
}

src/autoload_classmap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@
192192
'ThriftSQL\TTypeQualifiers' => __DIR__ . '/Packages/TCLIService/Types.php',
193193
'ThriftSQL\TUnionTypeEntry' => __DIR__ . '/Packages/TCLIService/Types.php',
194194
'ThriftSQL\TUserDefinedTypeEntry' => __DIR__ . '/Packages/TCLIService/Types.php',
195+
'ThriftSQL\Utils\QueryCleaner' => __DIR__ . '/ThriftSQL/Utils/QueryCleaner.php',
196+
'ThriftSQL\Utils\Sleeper' => __DIR__ . '/ThriftSQL/Utils/Sleeper.php',
195197
'Thrift\Base\TBase' => __DIR__ . '/Thrift/Base/TBase.php',
196198
'Thrift\ClassLoader\ThriftClassLoader' => __DIR__ . '/Thrift/ClassLoader/ThriftClassLoader.php',
197199
'Thrift\Exception\TApplicationException' => __DIR__ . '/Thrift/Exception/TApplicationException.php',

0 commit comments

Comments
 (0)