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 500 // 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  private:
38  void updateStatistics(uint32_t increment);
39  bool createStats(const std::string &key, unsigned int value);
40  unsigned long send(ReadingSet *readings);
41  void blockPause();
42  void releasePause();
43  private:
44  NorthPlugin *m_plugin;
45  DataLoad *m_loader;
46  NorthService *m_service;
47  volatile bool m_shutdown;
48  std::thread *m_thread;
49  Logger *m_logger;
50  bool m_paused;
51  bool m_sending;
52  std::mutex m_pauseMutex;
53  std::condition_variable m_pauseCV;
54  PerformanceMonitor *m_perfMonitor;
55  // Statistics send via thread
56  std::thread *m_statsThread;
57  std::mutex m_flushStatsMtx;
58  // Statistics save map
59  std::condition_variable m_statsCv;
60  std::mutex m_statsMtx;
61  std::map<std::string, unsigned int>
62  m_statsPendingEntries;
63  int m_statsUpdateFails;
64  // confirmed stats table entries
65  std::unordered_set<std::string>
66  m_statsDbEntriesCache;
67  unsigned int m_repeatedFailure;
68  unsigned int m_sendBackoffTime;
69 };
70 #endif
Fledge Logger class used to log to syslog.
Definition: logger.h:26
The NorthService class.
Definition: north_service.h:34
bool isDryRun()
Check status of dryrun flag.
Definition: data_send.cpp:514
A class used in the North service to load data from the buffer.
Definition: data_load.h:24
Definition: data_sender.h:25
Class to handle the performance monitors.
Definition: perfmonitors.h:35
Reading set class.
Definition: reading_set.h:26
void pause()
Cause the data sender process to pause sending data until a corresponding release call is made...
Definition: data_send.cpp:279
DataSender(NorthPlugin *plugin, DataLoad *loader, NorthService *north)
Constructor for the data sending class.
Definition: data_send.cpp:42
void release()
Release the paused data sender thread.
Definition: data_send.cpp:293
~DataSender()
Destructor for the data sender class.
Definition: data_send.cpp:72
Class that represents a north plugin.
Definition: north_plugin.h:33
void sendThread()
The sending thread entry point.
Definition: data_send.cpp:91
void flushStatistics()
Flush statistics to storage service.
Definition: data_send.cpp:349