Fledge
An open source edge computing platform for industrial users
data_sender.h
1 #ifndef _DATA_SENDER_H
2 #define _DATA_SENDER_H
3 
4 #include <north_plugin.h>
5 #include <reading_set.h>
6 #include <logger.h>
7 #include <thread>
8 #include <mutex>
9 #include <condition_variable>
10 #include <perfmonitors.h>
11 
12 // Send statistics to storage in seconds
13 #define FLUSH_STATS_INTERVAL 5
14 // Failure counter before re-recreating statics rows
15 #define STATS_UPDATE_FAIL_THRESHOLD 3
16 
17 // BAckoff sending when we see repeated failures
18 #define FAILURE_BACKOFF_THRESHOLD 10 // Number of consequetive failures to trigger backoff
19 #define MIN_SEND_BACKOFF 50 // Min backoff in milliseconds
20 #define MAX_SEND_BACKOFF 60000 // Max backoff in milliseconds
21 
22 class DataLoad;
23 class NorthService;
24 
25 class DataSender {
26  public:
27  DataSender(NorthPlugin *plugin, DataLoad *loader, NorthService *north);
28  ~DataSender();
29  void sendThread();
30  void updatePlugin(NorthPlugin *plugin) { m_plugin = plugin; };
31  void pause();
32  void release();
33  void setPerfMonitor(PerformanceMonitor *perfMonitor) { m_perfMonitor = perfMonitor; };
34  bool isRunning() { return !m_shutdown; };
35  void flushStatistics();
36  bool isDryRun();
37  void configChange();
38  private:
39  void updateStatistics(uint32_t increment);
40  bool createStats(const std::string &key, unsigned int value);
41  unsigned long send(ReadingSet *readings);
42  void blockPause();
43  void releasePause();
44  private:
45  NorthPlugin *m_plugin;
46  DataLoad *m_loader;
47  NorthService *m_service;
48  volatile bool m_shutdown;
49  std::thread *m_thread;
50  Logger *m_logger;
51  bool m_paused;
52  bool m_sending;
53  std::mutex m_pauseMutex;
54  std::condition_variable m_pauseCV;
55  PerformanceMonitor *m_perfMonitor;
56  // Statistics send via thread
57  std::thread *m_statsThread;
58  std::mutex m_flushStatsMtx;
59  // Statistics save map
60  std::condition_variable m_statsCv;
61  std::mutex m_statsMtx;
62  std::map<std::string, unsigned int>
63  m_statsPendingEntries;
64  int m_statsUpdateFails;
65  // confirmed stats table entries
66  std::unordered_set<std::string>
67  m_statsDbEntriesCache;
68  unsigned int m_repeatedFailure;
69  unsigned int m_sendBackoffTime;
70  std::mutex m_backoffMutex;
71  std::condition_variable m_backoffCV;
72 };
73 #endif
DataSender
Definition: data_sender.h:25
DataSender::pause
void pause()
Cause the data sender process to pause sending data until a corresponding release call is made.
Definition: data_send.cpp:286
DataSender::DataSender
DataSender(NorthPlugin *plugin, DataLoad *loader, NorthService *north)
Constructor for the data sending class.
Definition: data_send.cpp:42
DataSender::flushStatistics
void flushStatistics()
Flush statistics to storage service.
Definition: data_send.cpp:356
DataSender::release
void release()
Release the paused data sender thread.
Definition: data_send.cpp:300
NorthPlugin
Class that represents a north plugin.
Definition: north_plugin.h:33
DataSender::~DataSender
~DataSender()
Destructor for the data sender class.
Definition: data_send.cpp:72
ReadingSet
Reading set class.
Definition: reading_set.h:26
DataSender::configChange
void configChange()
Notify the data sender that there has been a configuration change.
Definition: data_send.cpp:532
DataLoad
A class used in the North service to load data from the buffer.
Definition: data_load.h:24
DataSender::isDryRun
bool isDryRun()
Check status of dryrun flag.
Definition: data_send.cpp:521
Logger
Fledge Logger class used to log to syslog.
Definition: logger.h:42
NorthService
The NorthService class.
Definition: north_service.h:42
PerformanceMonitor
Class to handle the performance monitors.
Definition: perfmonitors.h:35
DataSender::sendThread
void sendThread()
The sending thread entry point.
Definition: data_send.cpp:96