Update 更新文档
This commit is contained in:
125
cleaned_source_code/Sqbase/qfaulttolerance.h
Normal file
125
cleaned_source_code/Sqbase/qfaulttolerance.h
Normal file
@@ -0,0 +1,125 @@
|
||||
#ifndef QFAULTTOLERANCE_H
|
||||
#define QFAULTTOLERANCE_H
|
||||
#include <QObject>
|
||||
#include <QTimer>
|
||||
#include <QElapsedTimer>
|
||||
#include <QDateTime>
|
||||
#include <QMutex>
|
||||
#include <QMap>
|
||||
#include <QList>
|
||||
#include "BZStruct.h"
|
||||
enum class ConnectionState {
|
||||
Disconnected,
|
||||
Connecting,
|
||||
Connected,
|
||||
Degraded,
|
||||
Failed
|
||||
};
|
||||
struct HeartbeatConfig {
|
||||
int intervalMs = 30000;
|
||||
int timeoutMs = 60000;
|
||||
int maxMissedHeartbeats = 3;
|
||||
bool enabled = true;
|
||||
};
|
||||
struct ReconnectConfig {
|
||||
int initialDelayMs = 1000;
|
||||
int maxDelayMs = 60000;
|
||||
int maxAttempts = 10;
|
||||
bool exponentialBackoff = true;
|
||||
bool enabled = true;
|
||||
};
|
||||
struct FailoverConfig {
|
||||
QList<QString> backupServers;
|
||||
int switchTimeoutMs = 5000;
|
||||
bool autoSwitch = true;
|
||||
bool enableLoadBalancing = false;
|
||||
};
|
||||
struct ConnectionStats {
|
||||
ConnectionState state = ConnectionState::Disconnected;
|
||||
int totalConnections = 0;
|
||||
int successfulConnections = 0;
|
||||
int failedConnections = 0;
|
||||
int totalReconnects = 0;
|
||||
qint64 totalUptimeMs = 0;
|
||||
qint64 currentUptimeMs = 0;
|
||||
QDateTime lastConnectionTime;
|
||||
QDateTime lastDisconnectionTime;
|
||||
void updateState(ConnectionState newState) {
|
||||
state = newState;
|
||||
if (newState == ConnectionState::Connected) {
|
||||
lastConnectionTime = QDateTime::currentDateTime();
|
||||
totalConnections++;
|
||||
successfulConnections++;
|
||||
} else if (newState == ConnectionState::Failed) {
|
||||
lastDisconnectionTime = QDateTime::currentDateTime();
|
||||
failedConnections++;
|
||||
}
|
||||
}
|
||||
};
|
||||
class QFaultTolerance : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
static QFaultTolerance* instance();
|
||||
void setHeartbeatConfig(const HeartbeatConfig& config);
|
||||
void setReconnectConfig(const ReconnectConfig& config);
|
||||
void setFailoverConfig(const FailoverConfig& config);
|
||||
HeartbeatConfig getHeartbeatConfig() const;
|
||||
ReconnectConfig getReconnectConfig() const;
|
||||
FailoverConfig getFailoverConfig() const;
|
||||
void registerConnection(const QString& connectionId, const QString& serverAddress);
|
||||
void unregisterConnection(const QString& connectionId);
|
||||
void updateConnectionState(const QString& connectionId, ConnectionState state);
|
||||
ConnectionState getConnectionState(const QString& connectionId) const;
|
||||
void startHeartbeat(const QString& connectionId);
|
||||
void stopHeartbeat(const QString& connectionId);
|
||||
void heartbeatReceived(const QString& connectionId);
|
||||
void initiateReconnect(const QString& connectionId);
|
||||
void cancelReconnect(const QString& connectionId);
|
||||
void initiateFailover(const QString& failedConnectionId);
|
||||
QString getNextServer(const QString& connectionId) const;
|
||||
ConnectionStats getConnectionStats(const QString& connectionId) const;
|
||||
QMap<QString, ConnectionStats> getAllConnectionStats() const;
|
||||
bool isSystemHealthy() const;
|
||||
void enableFaultTolerance(bool enabled);
|
||||
void resetStatistics();
|
||||
signals:
|
||||
void connectionStateChanged(const QString& connectionId, ConnectionState state);
|
||||
void heartbeatTimeout(const QString& connectionId);
|
||||
void reconnectAttempt(const QString& connectionId, int attempt, int delayMs);
|
||||
void reconnectSuccess(const QString& connectionId);
|
||||
void reconnectFailed(const QString& connectionId);
|
||||
void failoverInitiated(const QString& fromConnectionId, const QString& toServer);
|
||||
void systemHealthChanged(bool healthy);
|
||||
private:
|
||||
explicit QFaultTolerance(QObject *parent = nullptr);
|
||||
~QFaultTolerance();
|
||||
void checkHeartbeats();
|
||||
void attemptReconnect(const QString& connectionId);
|
||||
void updateSystemHealth();
|
||||
struct ConnectionInfo {
|
||||
QString serverAddress;
|
||||
ConnectionState state = ConnectionState::Disconnected;
|
||||
ConnectionStats stats;
|
||||
HeartbeatConfig heartbeatConfig;
|
||||
ReconnectConfig reconnectConfig;
|
||||
QElapsedTimer lastHeartbeatTimer;
|
||||
int missedHeartbeats = 0;
|
||||
bool heartbeatEnabled = false;
|
||||
int reconnectAttempts = 0;
|
||||
int currentReconnectDelay = 0;
|
||||
QTimer* reconnectTimer = nullptr;
|
||||
ConnectionInfo() = default;
|
||||
ConnectionInfo(const QString& server) : serverAddress(server) {}
|
||||
};
|
||||
mutable QMutex m_mutex;
|
||||
QMap<QString, ConnectionInfo> m_connections;
|
||||
HeartbeatConfig m_heartbeatConfig;
|
||||
ReconnectConfig m_reconnectConfig;
|
||||
FailoverConfig m_failoverConfig;
|
||||
bool m_faultToleranceEnabled = true;
|
||||
QTimer* m_heartbeatCheckTimer;
|
||||
QTimer* m_healthCheckTimer;
|
||||
bool m_systemHealthy = true;
|
||||
};
|
||||
#endif
|
||||
Reference in New Issue
Block a user