Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added exponentialBase to WriteApi #35

Merged
merged 3 commits into from
Aug 13, 2020
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Features
1. [#32](https://github.com/influxdata/influxdb-client-php/pull/32): Added retryInterval, maxRetries and maxRetryDelay to WriteOptions in WriteApi
1. [#36](https://github.com/influxdata/influxdb-client-php/pull/35): Added exponentialBase to WriteApi

### Bug Fixes
1. [#33](https://github.com/influxdata/influxdb-client-php/pull/33): Removed unused flushInterval from WriteApi
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,10 @@ The writes are processed in batches which are configurable by `WriteOptions`:
| --- | --- | --- |
| **writeType** | type of write SYNCHRONOUS / BATCHING / | SYNCHRONOUS |
| **batchSize** | the number of data point to collect in batch | 10 |
| **retryInterval** | the number of milliseconds to retry unsuccessful write. The retry interval is "exponentially" used when the InfluxDB server does not specify "Retry-After" header. | 1000 |
| **maxRetries** | the number of max retries when write fails | 3 |
| **maxRetryDelay** | maximum delay when retrying write in milliseconds | 15000 |

| **retryInterval** | the number of milliseconds to retry unsuccessful write. The retry interval is "exponentially" used when the InfluxDB server does not specify "Retry-After" header. | 5000 |
| **maxRetries** | the number of max retries when write fails | 5 |
| **maxRetryDelay** | maximum delay when retrying write in milliseconds | 180000 |
| **exponentialBase** | the base for the exponential retry delay, the next delay is computed as `retryInterval * exponentialBase^(attempts-1)` | 5 |
```php
use InfluxDB2\Client;
use InfluxDB2\WriteType as WriteType;
Expand Down
5 changes: 3 additions & 2 deletions src/InfluxDB2/WriteApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private function writeRawInternal(string $data, array $queryParams, int $attempt
} catch (ApiException $e) {
$code = $e->getCode();

if ($code == null || !($code == 429 || $code == 503) || $attempts > $this->writeOptions->maxRetries) {
if ($code == null || ($code < 429) || $attempts > $this->writeOptions->maxRetries) {
throw $e;
}

Expand All @@ -127,7 +127,8 @@ private function writeRawInternal(string $data, array $queryParams, int $attempt

usleep($timeout);

$this->writeRawInternal($data, $queryParams, $attempts + 1, $retryInterval * 2);
$this->writeRawInternal($data, $queryParams, $attempts + 1,
$retryInterval * $this->writeOptions->exponentialBase);
}
}

Expand Down
11 changes: 8 additions & 3 deletions src/InfluxDB2/WriteOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
class WriteOptions
{
const DEFAULT_BATCH_SIZE = 10;
const DEFAULT_RETRY_INTERVAL = 1000;
const DEFAULT_MAX_RETRIES = 3;
const DEFAULT_MAX_RETRY_DELAY = 15000;
const DEFAULT_RETRY_INTERVAL = 5000;
const DEFAULT_MAX_RETRIES = 5;
const DEFAULT_MAX_RETRY_DELAY = 180000;
const DEFAULT_EXPONENTIAL_BASE = 5;

public $writeType;
public $batchSize;
public $retryInterval;
public $maxRetries;
public $maxRetryDelay;
public $exponentialBase;

/**
* WriteOptions constructor.
Expand All @@ -24,6 +26,8 @@ class WriteOptions
* 'maxRetries' => max number of retries when write fails
* The retry interval is used when the InfluxDB server does not specify "Retry-After" header.
* 'maxRetryDelay' => maximum delay when retrying write
* 'exponentialBase' => the base for the exponential retry delay, the next delay is computed as
* `retry_interval * exponentialBase^(attempts - 1)`
* ]
* @param array $writeOptions Array containing the write parameters (See above)
*/
Expand All @@ -35,6 +39,7 @@ public function __construct(array $writeOptions = null)
$this->retryInterval = $writeOptions["retryInterval"] ?? self::DEFAULT_RETRY_INTERVAL;
$this->maxRetries = $writeOptions["maxRetries"] ?? self::DEFAULT_MAX_RETRIES;
$this->maxRetryDelay = $writeOptions["maxRetryDelay"] ?? self::DEFAULT_MAX_RETRY_DELAY;
$this->exponentialBase = $writeOptions["exponentialBase"] ?? self::DEFAULT_EXPONENTIAL_BASE;
}
}

4 changes: 4 additions & 0 deletions tests/WriteApiBatchingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ public function testRetryCount()
new Response(429));

$this->writeApi->writeOptions->batchSize = 1;
$this->writeApi->writeOptions->retryInterval = 1000;
$this->writeApi->writeOptions->maxRetries = 3;
$this->writeApi->writeOptions->maxRetryDelay = 15000;
$this->writeApi->writeOptions->exponentialBase = 2;

$point = Point::measurement('h2o')
->addTag('location', 'europe')
Expand Down
5 changes: 5 additions & 0 deletions tests/WriteApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ public function testRetryCount()
// not called
new Response(429));

$this->writeApi->writeOptions->retryInterval = 1000;
$this->writeApi->writeOptions->maxRetries = 3;
$this->writeApi->writeOptions->maxRetryDelay = 15000;
$this->writeApi->writeOptions->exponentialBase = 2;

$point = Point::measurement('h2o')
->addTag('location', 'europe')
->addField('level', 2);
Expand Down