Skip to content
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Changelog
## unreleased
### Features
### Fixes
- [167](https://github.com/tobiasschuerg/InfluxDB-Client-for-Arduino/pull/167) - Added `InfluxDBClient::writeRecord(const char *record)`.
- [167](https://github.com/tobiasschuerg/InfluxDB-Client-for-Arduino/pull/167) - Added possibility to disable retrying by setting `maxRetryAttempts` to zero: `client.setWriteOptions(WriteOptions().maxRetryAttempts(0));`

## 3.9.0 [2021-09-17]
### Features
Expand Down
8 changes: 6 additions & 2 deletions src/InfluxDbClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ bool InfluxDBClient::writePoint(Point & point) {
return false;
}

bool InfluxDBClient::Batch::append(String &line) {
bool InfluxDBClient::Batch::append(const char *line) {
if(pointer == _size) {
//overwriting, clean buffer
for(int i=0;i< _size; i++) {
Expand Down Expand Up @@ -330,6 +330,10 @@ char * InfluxDBClient::Batch::createData() {
}

bool InfluxDBClient::writeRecord(String &record) {
return writeRecord(record.c_str());
}

bool InfluxDBClient::writeRecord(const char *record) {
if(!_writeBuffer[_bufferPointer]) {
_writeBuffer[_bufferPointer] = new Batch(_writeOptions._batchSize);
}
Expand Down Expand Up @@ -411,7 +415,7 @@ bool InfluxDBClient::flushBufferInternal(bool flashOnlyFull) {
int statusCode = postData(data);
delete [] data;
// retry on unsuccessfull connection or retryable status codes
bool retry = statusCode < 0 || statusCode >= 429;
bool retry = (statusCode < 0 || statusCode >= 429) && _writeOptions._maxRetryAttempts > 0;
success = statusCode >= 200 && statusCode < 300;
// advance even on message failure x e <300;429)
if(success || !retry) {
Expand Down
3 changes: 2 additions & 1 deletion src/InfluxDbClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ class InfluxDBClient {
// Writes record in InfluxDB line protocol format to write buffer.
// Returns true if successful, false in case of any error
bool writeRecord(String &record);
bool writeRecord(const char *record);
// Writes record represented by Point to buffer
// Returns true if successful, false in case of any error
bool writePoint(Point& point);
Expand Down Expand Up @@ -172,7 +173,7 @@ class InfluxDBClient {
uint8_t retryCount = 0;
Batch(int size):_size(size) { buffer = new String[size]; }
~Batch() { delete [] buffer; }
bool append(String &line);
bool append(const char *line);
char *createData();
bool isFull() const {
return pointer == _size;
Expand Down
57 changes: 56 additions & 1 deletion test/Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ void Test::run() {
testTimestamp();
testRetryOnFailedConnection();
testRetryOnFailedConnectionWithFlush();
testNonRetry();
testBufferOverwriteBatchsize1();
testBufferOverwriteBatchsize5();
testServerTempDownBatchsize5();
Expand Down Expand Up @@ -566,7 +567,7 @@ void Test::testRepeatedInit() {
} while(0);
uint32_t endRAM = ESP.getFreeHeap();
long diff = endRAM-startRAM;
TEST_ASSERTM(diff>-100,String(diff));
TEST_ASSERTM(diff>-300,String(diff));
TEST_END();
}

Expand Down Expand Up @@ -2121,6 +2122,60 @@ void Test::testFlushing() {
deleteAll(Test::apiUrl);
}

#define WS_DEBUG_RAM(text) { Serial.printf_P(PSTR(text ": free_heap %d, max_alloc_heap %d, heap_fragmentation %d\n"), ESP.getFreeHeap(), ESP.getMaxFreeBlockSize(), ESP.getHeapFragmentation()); }


void Test::testNonRetry() {
TEST_INIT("testNonRetry");
const char *lines[] = {
"device_status,clientId=WS-E09806011111,Device=WS-ESP8266,Version=0.58-rc3,Location=Prague\\,CZ,WiFi=Bonitoo-ng free_heap=16568i,max_alloc_heap=11336i,heap_fragmentation=29i,uptime=28821.23,wifi_disconnects=0i",
"service_status,clientId=WS-E09806011111,Device=WS-ESP8266,Version=0.58-rc3,Location=Prague\\,CZ,WiFi=Bonitoo-ng,service=location state=3i,before_mem_free=36232i,before_mem_max_free_block=17544i,before_mem_framentation=49i,after_mem_free=35792i,after_mem_max_free_block=17544i,after_mem_framentation=48i",
"service_status,clientId=WS-E09806011111,Device=WS-ESP8266,Version=0.58-rc3,Location=Prague\\,CZ,WiFi=Bonitoo-ng,service=clock state=2i,before_mem_free=16704i,before_mem_max_free_block=11336i,before_mem_framentation=30i,after_mem_free=16704i,after_mem_max_free_block=11336i,after_mem_framentation=30i",
"service_status,clientId=WS-E09806011111,Device=WS-ESP8266,Version=0.58-rc3,Location=Prague\\,CZ,WiFi=Bonitoo-ng,service=update state=0i",
"service_status,clientId=WS-E09806011111,Device=WS-ESP8266,Version=0.58-rc3,Location=Prague\\,CZ,WiFi=Bonitoo-ng,service=astronomy state=2i,before_mem_free=16376i,before_mem_max_free_block=11336i,before_mem_framentation=28i,after_mem_free=16376i,after_mem_max_free_block=11336i,after_mem_framentation=28i",
"service_status,clientId=WS-E09806011111,Device=WS-ESP8266,Version=0.58-rc3,Location=Prague\\,CZ,WiFi=Bonitoo-ng,service=current_weather state=2i,before_mem_free=16728i,before_mem_max_free_block=11336i,before_mem_framentation=30i,after_mem_free=16376i,after_mem_max_free_block=11336i,after_mem_framentation=28i",
"service_status,clientId=WS-E09806011111,Device=WS-ESP8266,Version=0.58-rc3,Location=Prague\\,CZ,WiFi=Bonitoo-ng,service=forecast state=2i,before_mem_free=16704i,before_mem_max_free_block=11336i,before_mem_framentation=30i,after_mem_free=16376i,after_mem_max_free_block=11336i,after_mem_framentation=28i",
"service_status,clientId=WS-E09806011111,Device=WS-ESP8266,Version=0.58-rc3,Location=Prague\\,CZ,WiFi=Bonitoo-ng,service=iot_center state=0i",
};
WriteOptions wo;
WS_DEBUG_RAM("Before inst");
InfluxDBClient *client = new InfluxDBClient(Test::apiUrl, Test::orgName, Test::bucketName, Test::token);
WS_DEBUG_RAM("after inst");

//TEST not keeping batch for retry
Serial.println("Stop server");
TEST_ASSERT(waitServer(Test::managementUrl, false));
client->setHTTPOptions(HTTPOptions().httpReadTimeout(500));
TEST_ASSERT(!client->validateConnection());
// Disable retry
wo.maxRetryAttempts(0);
client->setWriteOptions(wo);
client->setHTTPOptions(HTTPOptions().connectionReuse(true));
TEST_ASSERT(!client->writeRecord(lines[0]));
TEST_ASSERT(!client->_writeBuffer[0]);

TEST_ASSERT(waitServer(Test::managementUrl, true));
TEST_ASSERT(client->validateConnection());

uint8_t size = sizeof(lines)/sizeof(lines[0]);
uint16_t batchSize = size +1;
wo.batchSize(batchSize).bufferSize(batchSize);
client->setWriteOptions(wo);

WS_DEBUG_RAM("Before");
for(int i=0;i<size;i++) {
TEST_ASSERTM(client->writeRecord(lines[i]), client->getLastErrorMessage());
WS_DEBUG_RAM("After write Line");
}
TEST_ASSERTM(client->flushBuffer(), client->getLastErrorMessage());
WS_DEBUG_RAM("After flush");
delete client;
WS_DEBUG_RAM("After delete");
TEST_END();
deleteAll(Test::apiUrl);
}


void Test::setServerUrl(InfluxDBClient &client, String serverUrl) {
client._connInfo.serverUrl = serverUrl;
client._service->_apiURL = serverUrl + "/api/v2/";
Expand Down
1 change: 1 addition & 0 deletions test/Test.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class Test : public TestBase {
static void testIsValidID();
static void testBuckets();
static void testFlushing();
static void testNonRetry();
};

#endif //_TEST_H_