diff --git a/CHANGELOG.md b/CHANGELOG.md index ed77883c..8d63e61e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 74d79f0a..a0ecec33 100644 --- a/README.md +++ b/README.md @@ -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; diff --git a/src/InfluxDB2/WriteApi.php b/src/InfluxDB2/WriteApi.php index cad54668..9730f9a3 100644 --- a/src/InfluxDB2/WriteApi.php +++ b/src/InfluxDB2/WriteApi.php @@ -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; } @@ -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); } } diff --git a/src/InfluxDB2/WriteOptions.php b/src/InfluxDB2/WriteOptions.php index bad24ffc..b0b9681c 100644 --- a/src/InfluxDB2/WriteOptions.php +++ b/src/InfluxDB2/WriteOptions.php @@ -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. @@ -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) */ @@ -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; } } diff --git a/tests/WriteApiBatchingTest.php b/tests/WriteApiBatchingTest.php index 8a4b649b..4da96148 100644 --- a/tests/WriteApiBatchingTest.php +++ b/tests/WriteApiBatchingTest.php @@ -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') diff --git a/tests/WriteApiTest.php b/tests/WriteApiTest.php index cf1b320c..093acf8d 100644 --- a/tests/WriteApiTest.php +++ b/tests/WriteApiTest.php @@ -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);