File: heartbeats.h

package info (click to toggle)
megacmd 2.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 32,592 kB
  • sloc: cpp: 326,437; ansic: 34,524; python: 4,630; java: 3,965; sh: 2,869; objc: 2,459; makefile: 197; xml: 113
file content (128 lines) | stat: -rw-r--r-- 3,553 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
 * @file heartbeats.h
 * @brief Classes for heartbeating Backup configuration and status
 *
 * (c) 2020 by Mega Limited, Auckland, New Zealand
 *
 * This file is part of the MEGA SDK - Client Access Engine.
 *
 * Applications using the MEGA API must present a valid application key
 * and comply with the the rules set forth in the Terms of Service.
 *
 * The MEGA SDK is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @copyright Simplified (2-clause) BSD License.
 *
 * You should have received a copy of the license along with this
 * program.
 */

#pragma once

#include "types.h"
#include <memory>
#include <functional>
#include "mega/command.h"

namespace mega
{

#ifdef ENABLE_SYNC
struct UnifiedSync;
struct Syncs;

/**
 * @brief The HeartBeatBackupInfo class
 * This class holds the information that will be heartbeated
 */

class HeartBeatBackupInfo
{
    bool mModified = false;

public:
    HeartBeatBackupInfo();
    HeartBeatBackupInfo(HeartBeatBackupInfo&&) = delete;
    HeartBeatBackupInfo& operator=(HeartBeatBackupInfo&&) = delete;
    virtual ~HeartBeatBackupInfo() = default;

    virtual m_time_t lastAction() const;

    virtual handle lastItemUpdated() const;

    virtual m_time_t lastBeat() const;
    virtual void setLastBeat(const m_time_t &lastBeat);
    virtual void setLastAction(const m_time_t &lastAction);
    virtual void setLastSyncedItem(const handle &lastItemUpdated);

    std::atomic<bool> mSending{false};

protected:
    handle mLastItemUpdated = UNDEF; // handle of node most recently updated

    m_time_t mLastAction = -1;   //timestamps of the last action
    m_time_t mLastBeat = -1;     //timestamps of the last beat

    void updateLastActionTime();

    friend class BackupMonitor;
};

class HeartBeatSyncInfo : public HeartBeatBackupInfo
{
public:
    void updateSPHBStatus(UnifiedSync& us);

    using SPHBStatus = CommandBackupPutHeartBeat::SPHBStatus;
    SPHBStatus sphbStatus() { return mSPHBStatus; }

    SyncTransferCounts mSnapshotTransferCounts;
    SyncTransferCounts mResolvedTransferCounts;
private:
    SPHBStatus mSPHBStatus = CommandBackupPutHeartBeat::STATE_NOT_INITIALIZED;
};

class BackupInfoSync : public CommandBackupPut::BackupInfo
{
public:

    BackupInfoSync(const SyncConfig& config, const string& device, handle drive, CommandBackupPut::SPState calculatedState);
    BackupInfoSync(const UnifiedSync& us, bool pauseDown, bool pauseUp);

    static BackupType getSyncType(const SyncConfig& config);
    static CommandBackupPut::SPState getSyncState (const UnifiedSync &, bool pauseDown, bool pauseUp);
    static CommandBackupPut::SPState getSyncState(SyncError error, SyncRunState, bool pauseDown, bool pauseUp);
    static CommandBackupPut::SPState getSyncState(const SyncConfig& config, bool pauseDown, bool pauseUp);
    static handle getDriveId(const UnifiedSync&);

    bool operator==(const BackupInfoSync& o) const;
    bool operator!=(const BackupInfoSync& o) const;

private:
    static CommandBackupPut::SPState calculatePauseActiveState(bool pauseDown, bool pauseUp);
};


class BackupMonitor
{
public:
    explicit BackupMonitor(Syncs&);

    void beat(); // produce heartbeats!

    void updateOrRegisterSync(UnifiedSync&);

private:
    static constexpr int MAX_HEARBEAT_SECS_DELAY = 60*30; // max time to wait before a heartbeat for unchanged backup

    Syncs& syncs;

    void beatBackupInfo(UnifiedSync& us);
};

#endif

}