Skip to content

Commit df1d60a

Browse files
committed
Refactor exponential sleeping into seperate class
This is shared by Hive and Impala, likely we will need to do the same thing for other big SQL engines.
1 parent 3881f56 commit df1d60a

File tree

4 files changed

+54
-24
lines changed

4 files changed

+54
-24
lines changed

src/ThriftSQL/Hive.php

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

8181
public function queryAndFetchAll( $queryStr ) {
8282
try {
83+
$sleeper = new \ThriftSQL\Utils\Sleeper();
84+
8385
$TExecuteStatementResp = $this->_client->ExecuteStatement( new \ThriftSQL\TExecuteStatementReq( array(
8486
'sessionHandle' => $this->_sessionHandle,
8587
'statement' => $queryStr,
8688
'runAsync' => true,
8789
) ) );
8890

8991
// Wait for results
90-
$iteration = 0;
92+
$sleeper->reset();
9193
do {
9294

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

95-
if ( $iteration > 250 ) {
97+
if ( $slept > 18000 ) { // 5 Hours
9698
// TODO: Actually kill the query then throw exception.
9799
throw new \ThriftSQL\Exception( 'Hive Query Killed!' );
98100
}
@@ -202,15 +204,6 @@ public function disconnect() {
202204

203205
}
204206

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-
214207
private function _isOperationFinished( $state ) {
215208
return ( \ThriftSQL\TOperationState::FINISHED_STATE == $state );
216209
}

src/ThriftSQL/Impala.php

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

5353
public function queryAndFetchAll( $queryStr ) {
5454
try {
55+
$sleeper = new \ThriftSQL\Utils\Sleeper();
56+
5557
$QueryHandle = $this->_client->query( new \ThriftSQL\Query( array(
5658
'query' => $queryStr,
5759
) ) );
5860

5961
// Wait for results
60-
$iteration = 0;
62+
$sleeper->reset();
6163
do {
6264

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

65-
if ( $iteration > 250 ) {
67+
if ( $slept > 18000 ) { // 5 Hours
6668
// TODO: Actually kill the query then throw exception.
6769
throw new \ThriftSQL\Exception( 'Impala Query Killed!' );
6870
}
@@ -157,15 +159,6 @@ public function disconnect() {
157159

158160
}
159161

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-
169162
private function _isOperationFinished( $state ) {
170163
return ( \ThriftSQL\QueryState::FINISHED == $state );
171164
}

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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@
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\Sleeper' => __DIR__ . '/ThriftSQL/Utils/Sleeper.php',
195196
'Thrift\Base\TBase' => __DIR__ . '/Thrift/Base/TBase.php',
196197
'Thrift\ClassLoader\ThriftClassLoader' => __DIR__ . '/Thrift/ClassLoader/ThriftClassLoader.php',
197198
'Thrift\Exception\TApplicationException' => __DIR__ . '/Thrift/Exception/TApplicationException.php',

0 commit comments

Comments
 (0)