From b1839843478324a6837bd4b5464a28e0f93a564a Mon Sep 17 00:00:00 2001 From: rolincova Date: Wed, 12 Aug 2020 10:10:36 +0200 Subject: [PATCH 1/3] feat: Added exponentialBase to WriteApi --- CHANGELOG.md | 1 + README.md | 6 +++--- src/InfluxDB2/WriteApi.php | 5 +++-- src/InfluxDB2/WriteOptions.php | 9 +++++++-- tests/WriteApiBatchingTest.php | 3 +++ tests/WriteApiTest.php | 4 ++++ 6 files changed, 21 insertions(+), 7 deletions(-) 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..e12a7397 100644 --- a/README.md +++ b/README.md @@ -150,9 +150,9 @@ 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 | - +| **maxRetries** | the number of max retries when write fails | 5 | +| **maxRetryDelay** | maximum delay when retrying write in milliseconds | 180000 | +| **exponential_base** | the base for the exponential retry delay, the next delay is computed as `retryInterval * exponentialBase^(attempts-1) + random(jitterInterval)` | 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..0f0dd95e 100644 --- a/src/InfluxDB2/WriteOptions.php +++ b/src/InfluxDB2/WriteOptions.php @@ -6,14 +6,16 @@ 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_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 * exponential_base^(attempts - 1) + random(jitter_interval)` * ] * @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..12e8e2ae 100644 --- a/tests/WriteApiBatchingTest.php +++ b/tests/WriteApiBatchingTest.php @@ -201,6 +201,9 @@ public function testRetryCount() new Response(429)); $this->writeApi->writeOptions->batchSize = 1; + $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..b437506e 100644 --- a/tests/WriteApiTest.php +++ b/tests/WriteApiTest.php @@ -145,6 +145,10 @@ public function testRetryCount() // not called new Response(429)); + $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); From 80f6e1f9cc6f0ebd1ddcda5a218d61b31726554a Mon Sep 17 00:00:00 2001 From: rolincova Date: Wed, 12 Aug 2020 10:24:52 +0200 Subject: [PATCH 2/3] feat: Added exponentialBase to WriteApi --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e12a7397..f7e35431 100644 --- a/README.md +++ b/README.md @@ -152,7 +152,7 @@ The writes are processed in batches which are configurable by `WriteOptions`: | **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 | 5 | | **maxRetryDelay** | maximum delay when retrying write in milliseconds | 180000 | -| **exponential_base** | the base for the exponential retry delay, the next delay is computed as `retryInterval * exponentialBase^(attempts-1) + random(jitterInterval)` | 5 | +| **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; From ea32a59dc11dcb21a9d513ec940c40cddfa296a1 Mon Sep 17 00:00:00 2001 From: rolincova Date: Wed, 12 Aug 2020 10:33:40 +0200 Subject: [PATCH 3/3] feat: changed default retry interval to 5s --- README.md | 2 +- src/InfluxDB2/WriteOptions.php | 4 ++-- tests/WriteApiBatchingTest.php | 1 + tests/WriteApiTest.php | 1 + 4 files changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f7e35431..a0ecec33 100644 --- a/README.md +++ b/README.md @@ -149,7 +149,7 @@ 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 | +| **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 | diff --git a/src/InfluxDB2/WriteOptions.php b/src/InfluxDB2/WriteOptions.php index 0f0dd95e..b0b9681c 100644 --- a/src/InfluxDB2/WriteOptions.php +++ b/src/InfluxDB2/WriteOptions.php @@ -5,7 +5,7 @@ class WriteOptions { const DEFAULT_BATCH_SIZE = 10; - const DEFAULT_RETRY_INTERVAL = 1000; + const DEFAULT_RETRY_INTERVAL = 5000; const DEFAULT_MAX_RETRIES = 5; const DEFAULT_MAX_RETRY_DELAY = 180000; const DEFAULT_EXPONENTIAL_BASE = 5; @@ -27,7 +27,7 @@ class WriteOptions * 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 * exponential_base^(attempts - 1) + random(jitter_interval)` + * `retry_interval * exponentialBase^(attempts - 1)` * ] * @param array $writeOptions Array containing the write parameters (See above) */ diff --git a/tests/WriteApiBatchingTest.php b/tests/WriteApiBatchingTest.php index 12e8e2ae..4da96148 100644 --- a/tests/WriteApiBatchingTest.php +++ b/tests/WriteApiBatchingTest.php @@ -201,6 +201,7 @@ 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; diff --git a/tests/WriteApiTest.php b/tests/WriteApiTest.php index b437506e..093acf8d 100644 --- a/tests/WriteApiTest.php +++ b/tests/WriteApiTest.php @@ -145,6 +145,7 @@ 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;