• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 DeviceSyncTarget &target, 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(uint32_t timeout) = 0;
70 
71     virtual int GetNextTarget(uint32_t timeout) = 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 std::string GetTargetUserId() const = 0;
80 
81     virtual void SetTargetUserId(const std::string &userId) = 0;
82 
83     virtual void SetTaskExecStatus(int status) = 0;
84 
85     virtual int GetTaskExecStatus() const = 0;
86 
87     virtual bool IsAutoSync() const = 0;
88 
89     virtual bool IsSyncTaskNeedRetry() const = 0;
90 
91     virtual void SetSyncRetry(bool isRetry) = 0;
92 
93     virtual int GetSyncRetryTimes() const = 0;
94 
95     virtual int GetSyncRetryTimeout(int retryTime) const = 0;
96 
97     // Set a Timer used for timeout
98     virtual int StartTimer() = 0;
99 
100     // delete timer
101     virtual void StopTimer() = 0;
102 
103     // modify timer
104     virtual int ModifyTimer(int milliSeconds) = 0;
105 
106     // Set a RetryTime for the sync task
107     virtual void SetRetryTime(int retryTime) = 0;
108 
109     // Get a RetryTime for the sync task
110     virtual int GetRetryTime() const = 0;
111 
112     // Set Retry status for the sync task
113     virtual void SetRetryStatus(int isNeedRetry) = 0;
114 
115     // Get Retry status for the sync task
116     virtual int GetRetryStatus() const = 0;
117 
118     virtual TimerId GetTimerId() const = 0;
119 
120     virtual void IncSequenceId() = 0;
121 
122     virtual uint32_t GetSequenceId() const = 0;
123 
124     virtual void ReSetSequenceId() = 0;
125 
126     virtual uint32_t GetRequestSessionId() const = 0;
127 
128     virtual int GetTimeoutTime() const = 0;
129 
130     virtual void SetTimeOffset(TimeOffset offset) = 0;
131 
132     virtual TimeOffset GetTimeOffset() const = 0;
133 
134     virtual void SetTimeoutCallback(const TimerAction &timeOutCallback) = 0;
135 
136     virtual int StartStateMachine() = 0;
137 
138     virtual int ReceiveMessageCallback(Message *inMsg) = 0;
139 
140     virtual void RegOnSyncTask(const std::function<int(void)> &callback) = 0;
141 
142     virtual int IncUsedCount() = 0;
143 
144     virtual void SafeExit() = 0;
145 
146     // Get current localtime from TimeHelper
147     virtual Timestamp GetCurrentLocalTime() const = 0;
148 
149     // Set the remount software version num
150     virtual void SetRemoteSoftwareVersion(uint32_t version) = 0;
151 
152     // Get the remount software version num
153     virtual uint32_t GetRemoteSoftwareVersion() const = 0;
154 
155     // Get the remount software version id, when called GetRemoteSoftwareVersion this id will be increase.
156     // Used to check if the version num is overdue
157     virtual uint64_t GetRemoteSoftwareVersionId() const = 0;
158 
159     // Judge if the communicator is normal
160     virtual bool IsCommNormal() const = 0;
161 
162     virtual void ClearSyncOperation() = 0;
163 
164     // Judge if the sec option check is err
165     virtual int GetTaskErrCode() const = 0;
166 
167     virtual void SetTaskErrCode(int errCode)  = 0;
168 
169     virtual void ClearAllSyncTask() = 0;
170 
171     virtual bool IsAutoLiftWaterMark() const = 0;
172 
173     virtual void IncNegotiationCount() = 0;
174 
175     virtual bool IsNeedTriggerQueryAutoSync(Message *inMsg, QuerySyncObject &query) = 0;
176 
177     virtual bool IsAutoSubscribe() const = 0;
178 
179     // some sync task can be skipped if there is no change in data base since last sync
180     virtual bool IsCurrentSyncTaskCanBeSkipped() const = 0;
181 
182     virtual void SchemaChange() = 0;
183 
184     virtual void Dump(int fd) = 0;
185 
186     virtual void AbortMachineIfNeed(uint32_t syncId) = 0;
187 
188     virtual bool IsSchemaCompatible() const = 0;
189 
190     virtual void SetDbAbility(DbAbility &remoteDbAbility) = 0;
191 
192     virtual void TimeChange() = 0;
193 
194     virtual int32_t GetResponseTaskCount() = 0;
195 
196     virtual bool IsRetryTask() const = 0;
197 
198     virtual bool IsSavingTask(uint32_t timeout) const = 0;
199 protected:
~ISyncTaskContext()200     virtual ~ISyncTaskContext() {};
201 };
202 } // namespace DistributedDB
203 
204 #endif // I_SYNC_TASK_CONTEXT_H
205