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 #include "time_helper.h"
17
18 #include "db_errno.h"
19 #include "log_print.h"
20 #include "platform_specific.h"
21
22 namespace DistributedDB {
23 std::mutex TimeHelper::systemTimeLock_;
24 Timestamp TimeHelper::lastSystemTimeUs_ = 0;
25 Timestamp TimeHelper::currentIncCount_ = 0;
26 std::atomic<Timestamp> TimeHelper::lastMonotonicTime_ = 0;
27
GetSysCurrentTime()28 Timestamp TimeHelper::GetSysCurrentTime()
29 {
30 uint64_t curTime = 0;
31 std::lock_guard<std::mutex> lock(systemTimeLock_);
32 int errCode = OS::GetCurrentSysTimeInMicrosecond(curTime);
33 if (errCode != E_OK) {
34 return INVALID_TIMESTAMP;
35 }
36
37 // If GetSysCurrentTime in 1us, we need increase the currentIncCount_
38 if (curTime == lastSystemTimeUs_) {
39 // if the currentIncCount_ has been increased MAX_INC_COUNT, keep the currentIncCount_
40 if (currentIncCount_ < MAX_INC_COUNT) {
41 currentIncCount_++;
42 }
43 } else {
44 lastSystemTimeUs_ = curTime;
45 currentIncCount_ = 0;
46 }
47 return (curTime * TO_100_NS) + currentIncCount_; // Currently Timestamp is uint64_t
48 }
49
TimeHelper()50 TimeHelper::TimeHelper()
51 : storage_(nullptr),
52 metadata_(nullptr)
53 {
54 }
55
~TimeHelper()56 TimeHelper::~TimeHelper()
57 {
58 metadata_ = nullptr;
59 storage_ = nullptr;
60 }
61
Initialize(const ISyncInterface * inStorage,std::shared_ptr<Metadata> & inMetadata)62 int TimeHelper::Initialize(const ISyncInterface *inStorage, std::shared_ptr<Metadata> &inMetadata)
63 {
64 if ((inStorage == nullptr) || (inMetadata == nullptr)) {
65 return -E_INVALID_ARGS;
66 }
67 metadata_ = inMetadata;
68 storage_ = inStorage;
69 Timestamp currentSysTime = GetSysCurrentTime();
70 TimeOffset localTimeOffset = GetLocalTimeOffset();
71 Timestamp maxItemTime = GetMaxDataItemTime();
72 if (currentSysTime > MAX_VALID_TIME || maxItemTime > MAX_VALID_TIME) {
73 return -E_INVALID_TIME;
74 }
75 Timestamp virtualSysTime = static_cast<Timestamp>(currentSysTime + localTimeOffset);
76 if (virtualSysTime <= maxItemTime || virtualSysTime > BUFFER_VALID_TIME) {
77 localTimeOffset = static_cast<TimeOffset>(maxItemTime - currentSysTime + MS_TO_100_NS); // 1ms
78 int errCode = SaveLocalTimeOffset(localTimeOffset);
79 if (errCode != E_OK) {
80 LOGE("[TimeHelper] save local time offset failed,err=%d", errCode);
81 return errCode;
82 }
83 }
84 lastMonotonicTime_ = GetMonotonicTime();
85 metadata_->SetLastLocalTime(currentSysTime + static_cast<Timestamp>(localTimeOffset));
86 return E_OK;
87 }
88
GetTime()89 Timestamp TimeHelper::GetTime()
90 {
91 Timestamp currentSysTime = GetSysCurrentTime();
92 TimeOffset localTimeOffset = GetLocalTimeOffset();
93 Timestamp currentLocalTime = currentSysTime + localTimeOffset;
94 Timestamp lastLocalTime = metadata_->GetLastLocalTime();
95 Timestamp currentMonotonicTime = GetMonotonicTime();
96 Timestamp deltaTime = 1UL;
97 if (currentMonotonicTime != INVALID_TIMESTAMP && lastMonotonicTime_ != INVALID_TIMESTAMP) {
98 deltaTime = currentMonotonicTime - lastMonotonicTime_;
99 }
100 lastMonotonicTime_ = currentMonotonicTime;
101 if (currentLocalTime <= lastLocalTime || currentLocalTime > BUFFER_VALID_TIME) {
102 lastLocalTime += deltaTime;
103 currentLocalTime = lastLocalTime;
104 metadata_->SetLastLocalTime(lastLocalTime);
105 } else {
106 metadata_->SetLastLocalTime(currentLocalTime);
107 }
108 return currentLocalTime;
109 }
110
GetMaxDataItemTime()111 Timestamp TimeHelper::GetMaxDataItemTime()
112 {
113 Timestamp timestamp = 0;
114 storage_->GetMaxTimestamp(timestamp);
115 return timestamp;
116 }
117
GetLocalTimeOffset() const118 TimeOffset TimeHelper::GetLocalTimeOffset() const
119 {
120 return metadata_->GetLocalTimeOffset();
121 }
122
SaveLocalTimeOffset(TimeOffset offset)123 int TimeHelper::SaveLocalTimeOffset(TimeOffset offset)
124 {
125 return metadata_->SaveLocalTimeOffset(offset);
126 }
127
SetSendConfig(const std::string & dstTarget,bool nonBlock,uint32_t timeout,SendConfig & sendConf)128 void TimeHelper::SetSendConfig(const std::string &dstTarget, bool nonBlock, uint32_t timeout, SendConfig &sendConf)
129 {
130 SetSendConfigParam(storage_->GetDbProperties(), dstTarget, false, SEND_TIME_OUT, sendConf);
131 }
132
GetMonotonicTime()133 Timestamp TimeHelper::GetMonotonicTime()
134 {
135 Timestamp time = INVALID_TIMESTAMP;
136 int errCode = OS::GetMonotonicRelativeTimeInMicrosecond(time);
137 if (errCode != E_OK) {
138 LOGE("GetMonotonicTime ERR! errCode = %d", errCode);
139 }
140 return time;
141 }
142 } // namespace DistributedDB
143