1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef I_SYNC_TASK_CONTEXT_H
17 #define I_SYNC_TASK_CONTEXT_H
18 
19 #include "db_ability.h"
20 #include "icommunicator.h"
21 #include "ikvdb_sync_interface.h"
22 #include "meta_data.h"
23 #include "query_sync_object.h"
24 #include "runtime_context.h"
25 #include "sync_operation.h"
26 #include "sync_target.h"
27 #include "time_helper.h"
28 namespace DistributedDB {
29 using CommErrHandler = std::function<void(int, bool)>;
30 
31 class ISyncTaskContext : public virtual RefObject {
32 public:
33     enum RETRY_STATUS { NO_NEED_RETRY, NEED_RETRY };
34 
35     enum TASK_EXEC_STATUS { INIT, RUNNING, FAILED, FINISHED };
36 
37     // Initialize the context
38     virtual int Initialize(const std::string &deviceId, ISyncInterface *syncInterface,
39         const std::shared_ptr<Metadata> &metadata, ICommunicator *communicator) = 0;
40 
41     // Add a sync task target with the operation to the queue
42     virtual int AddSyncOperation(SyncOperation *operation) = 0;
43 
44     // Add a sync task target to the queue
45     virtual int AddSyncTarget(ISyncTarget *target) = 0;
46 
47     // Set the status of this task cotext
48     virtual void SetOperationStatus(int status) = 0;
49 
50     // Clear the data of this context
51     virtual void Clear() = 0;
52 
53     // Remove a sync target by syncId
54     virtual int RemoveSyncOperation(int syncId) = 0;
55 
56     // If the targetQueue is empty
57     virtual bool IsTargetQueueEmpty() const = 0;
58 
59     // Get the status of this task
60     virtual int GetOperationStatus() const = 0;
61 
62     // Set the mode of this task
63     virtual void SetMode(int mode) = 0;
64 
65     // Get the mode of this task
66     virtual int GetMode() const = 0;
67 
68     // Move to next target to sync
69     virtual void MoveToNextTarget() = 0;
70 
71     virtual int GetNextTarget() = 0;
72 
73     // Get the current task syncId
74     virtual uint32_t GetSyncId() const = 0;
75 
76     // Get the current task deviceId.
77     virtual std::string GetDeviceId() const = 0;
78 
79     virtual void SetTaskExecStatus(int status) = 0;
80 
81     virtual int GetTaskExecStatus() const = 0;
82 
83     virtual bool IsAutoSync() const = 0;
84 
85     virtual bool IsSyncTaskNeedRetry() const = 0;
86 
87     virtual void SetSyncRetry(bool isRetry) = 0;
88 
89     virtual int GetSyncRetryTimes() const = 0;
90 
91     virtual int GetSyncRetryTimeout(int retryTime) const = 0;
92 
93     // Set a Timer used for timeout
94     virtual int StartTimer() = 0;
95 
96     // delete timer
97     virtual void StopTimer() = 0;
98 
99     // modify timer
100     virtual int ModifyTimer(int milliSeconds) = 0;
101 
102     // Set a RetryTime for the sync task
103     virtual void SetRetryTime(int retryTime) = 0;
104 
105     // Get a RetryTime for the sync task
106     virtual int GetRetryTime() const = 0;
107 
108     // Set Retry status for the sync task
109     virtual void SetRetryStatus(int isNeedRetry) = 0;
110 
111     // Get Retry status for the sync task
112     virtual int GetRetryStatus() const = 0;
113 
114     virtual TimerId GetTimerId() const = 0;
115 
116     virtual void IncSequenceId() = 0;
117 
118     virtual uint32_t GetSequenceId() const = 0;
119 
120     virtual void ReSetSequenceId() = 0;
121 
122     virtual uint32_t GetRequestSessionId() const = 0;
123 
124     virtual int GetTimeoutTime() const = 0;
125 
126     virtual void SetTimeOffset(TimeOffset offset) = 0;
127 
128     virtual TimeOffset GetTimeOffset() const = 0;
129 
130     virtual void SetTimeoutCallback(const TimerAction &timeOutCallback) = 0;
131 
132     virtual int StartStateMachine() = 0;
133 
134     virtual int ReceiveMessageCallback(Message *inMsg) = 0;
135 
136     virtual void RegOnSyncTask(const std::function<int(void)> &callback) = 0;
137 
138     virtual int IncUsedCount() = 0;
139 
140     virtual void SafeExit() = 0;
141 
142     // Get current localtime from TimeHelper
143     virtual Timestamp GetCurrentLocalTime() const = 0;
144 
145     // Set the remount software version num
146     virtual void SetRemoteSoftwareVersion(uint32_t version) = 0;
147 
148     // Get the remount software version num
149     virtual uint32_t GetRemoteSoftwareVersion() const = 0;
150 
151     // Get the remount software version id, when called GetRemoteSoftwareVersion this id will be increase.
152     // Used to check if the version num is overdue
153     virtual uint64_t GetRemoteSoftwareVersionId() const = 0;
154 
155     // Judge if the communicator is normal
156     virtual bool IsCommNormal() const = 0;
157 
158     virtual void ClearSyncOperation() = 0;
159 
160     // Judge if the sec option check is err
161     virtual int GetTaskErrCode() const = 0;
162 
163     virtual void SetTaskErrCode(int errCode)  = 0;
164 
165     virtual void ClearAllSyncTask() = 0;
166 
167     virtual bool IsAutoLiftWaterMark() const = 0;
168 
169     virtual void IncNegotiationCount() = 0;
170 
171     virtual bool IsNeedTriggerQueryAutoSync(Message *inMsg, QuerySyncObject &query) = 0;
172 
173     virtual bool IsAutoSubscribe() const = 0;
174 
175     // some sync task can be skipped if there is no change in data base since last sync
176     virtual bool IsCurrentSyncTaskCanBeSkipped() const = 0;
177 
178     virtual void SchemaChange() = 0;
179 
180     virtual void Dump(int fd) = 0;
181 
182     virtual void AbortMachineIfNeed(uint32_t syncId) = 0;
183 
184     virtual bool IsSchemaCompatible() const = 0;
185 
186     virtual void SetDbAbility(DbAbility &remoteDbAbility) = 0;
187 
188     virtual void TimeChange() = 0;
189 
190     virtual int32_t GetResponseTaskCount() = 0;
191 protected:
~ISyncTaskContext()192     virtual ~ISyncTaskContext() {};
193 };
194 } // namespace DistributedDB
195 
196 #endif // I_SYNC_TASK_CONTEXT_H
197