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
GetSysCurrentRawTime(uint64_t & curTime)50 int TimeHelper::GetSysCurrentRawTime(uint64_t &curTime)
51 {
52 int errCode = OS::GetCurrentSysTimeInMicrosecond(curTime);
53 if (errCode != 0) {
54 return errCode;
55 }
56 curTime *= TO_100_NS;
57 return E_OK;
58 }
59
TimeHelper()60 TimeHelper::TimeHelper()
61 : storage_(nullptr),
62 metadata_(nullptr)
63 {
64 }
65
~TimeHelper()66 TimeHelper::~TimeHelper()
67 {
68 metadata_ = nullptr;
69 storage_ = nullptr;
70 }
71
Initialize(const ISyncInterface * inStorage,const std::shared_ptr<Metadata> & inMetadata)72 int TimeHelper::Initialize(const ISyncInterface *inStorage, const std::shared_ptr<Metadata> &inMetadata)
73 {
74 if ((inStorage == nullptr) || (inMetadata == nullptr)) {
75 return -E_INVALID_ARGS;
76 }
77 metadata_ = inMetadata;
78 storage_ = inStorage;
79 Timestamp currentSysTime = GetSysCurrentTime();
80 TimeOffset localTimeOffset = GetLocalTimeOffset();
81 Timestamp maxItemTime = GetMaxDataItemTime();
82 if (currentSysTime > MAX_VALID_TIME || maxItemTime > MAX_VALID_TIME) {
83 return -E_INVALID_TIME;
84 }
85 Timestamp virtualSysTime = static_cast<Timestamp>(currentSysTime + localTimeOffset);
86 if (virtualSysTime <= maxItemTime || virtualSysTime > BUFFER_VALID_TIME) {
87 localTimeOffset = static_cast<TimeOffset>(maxItemTime - currentSysTime + MS_TO_100_NS); // 1ms
88 int errCode = SaveLocalTimeOffset(localTimeOffset);
89 if (errCode != E_OK) {
90 LOGE("[TimeHelper] save local time offset failed,err=%d", errCode);
91 return errCode;
92 }
93 }
94 lastMonotonicTime_ = GetMonotonicTime();
95 metadata_->SetLastLocalTime(currentSysTime + static_cast<Timestamp>(localTimeOffset));
96 return E_OK;
97 }
98
GetTime()99 Timestamp TimeHelper::GetTime()
100 {
101 Timestamp currentSysTime = GetSysCurrentTime();
102 TimeOffset localTimeOffset = GetLocalTimeOffset();
103 Timestamp currentLocalTime = currentSysTime + localTimeOffset;
104 Timestamp lastLocalTime = metadata_->GetLastLocalTime();
105 Timestamp currentMonotonicTime = GetMonotonicTime();
106 Timestamp deltaTime = 1UL;
107 if (currentMonotonicTime != INVALID_TIMESTAMP && lastMonotonicTime_ != INVALID_TIMESTAMP) {
108 deltaTime = currentMonotonicTime - lastMonotonicTime_;
109 }
110 lastMonotonicTime_ = currentMonotonicTime;
111 if (currentLocalTime <= lastLocalTime || currentLocalTime > BUFFER_VALID_TIME) {
112 lastLocalTime += deltaTime;
113 currentLocalTime = lastLocalTime;
114 metadata_->SetLastLocalTime(lastLocalTime);
115 } else {
116 metadata_->SetLastLocalTime(currentLocalTime);
117 }
118 return currentLocalTime;
119 }
120
GetMaxDataItemTime()121 Timestamp TimeHelper::GetMaxDataItemTime()
122 {
123 Timestamp timestamp = 0;
124 storage_->GetMaxTimestamp(timestamp);
125 return timestamp;
126 }
127
GetLocalTimeOffset() const128 TimeOffset TimeHelper::GetLocalTimeOffset() const
129 {
130 return metadata_->GetLocalTimeOffset();
131 }
132
SaveLocalTimeOffset(TimeOffset offset)133 int TimeHelper::SaveLocalTimeOffset(TimeOffset offset)
134 {
135 return metadata_->SaveLocalTimeOffset(offset);
136 }
137
SetSendConfig(const std::string & dstTarget,bool nonBlock,uint32_t timeout,SendConfig & sendConf)138 void TimeHelper::SetSendConfig(const std::string &dstTarget, bool nonBlock, uint32_t timeout, SendConfig &sendConf)
139 {
140 SetSendConfigParam(storage_->GetDbProperties(), dstTarget, false, SEND_TIME_OUT, sendConf);
141 }
142
GetMonotonicTime()143 Timestamp TimeHelper::GetMonotonicTime()
144 {
145 Timestamp time = INVALID_TIMESTAMP;
146 int errCode = OS::GetMonotonicRelativeTimeInMicrosecond(time);
147 if (errCode != E_OK) {
148 LOGE("GetMonotonicTime ERR! errCode = %d", errCode);
149 }
150 return time;
151 }
152 } // namespace DistributedDB
153