• 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 #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::atomic<Timestamp> TimeHelper::lastMonotonicTime_ = 0;
24 
GetSysCurrentRawTime(uint64_t & curTime)25 int TimeHelper::GetSysCurrentRawTime(uint64_t &curTime)
26 {
27     int errCode = OS::GetCurrentSysTimeInMicrosecond(curTime);
28     if (errCode != 0) {
29         return errCode;
30     }
31     if (curTime > (UINT64_MAX / TO_100_NS)) {
32         LOGD("curTime is too large %" PRIu64, curTime);
33         return -E_OUT_OF_DATE;
34     }
35     curTime *= TO_100_NS;
36     return E_OK;
37 }
38 
TimeHelper()39 TimeHelper::TimeHelper()
40     : storage_(nullptr),
41       metadata_(nullptr)
42 {
43 }
44 
~TimeHelper()45 TimeHelper::~TimeHelper()
46 {
47     metadata_ = nullptr;
48     storage_ = nullptr;
49 }
50 
Initialize(const ISyncInterface * inStorage,const std::shared_ptr<Metadata> & inMetadata)51 int TimeHelper::Initialize(const ISyncInterface *inStorage, const std::shared_ptr<Metadata> &inMetadata)
52 {
53     if ((inStorage == nullptr) || (inMetadata == nullptr)) {
54         return -E_INVALID_ARGS;
55     }
56     metadata_ = inMetadata;
57     storage_ = inStorage;
58     Timestamp currentSysTime = GetSysCurrentTime();
59     TimeOffset localTimeOffset = GetLocalTimeOffset();
60     Timestamp maxItemTime = GetMaxDataItemTime();
61     if (currentSysTime > MAX_VALID_TIME || maxItemTime > MAX_VALID_TIME) {
62         return -E_INVALID_TIME;
63     }
64     Timestamp virtualSysTime = static_cast<Timestamp>(currentSysTime + localTimeOffset);
65     if (virtualSysTime <= maxItemTime || virtualSysTime > BUFFER_VALID_TIME) {
66         localTimeOffset = static_cast<TimeOffset>(maxItemTime - currentSysTime + MS_TO_100_NS); // 1ms
67         // cal timeOffset without time tick, should not be written into db
68         int errCode = metadata_->SaveLocalTimeOffset(localTimeOffset, false);
69         if (errCode != E_OK) {
70             LOGE("[TimeHelper] save local time offset failed,err=%d", errCode);
71             return errCode;
72         }
73     }
74     lastMonotonicTime_ = GetMonotonicTime();
75     metadata_->SetLastLocalTime(currentSysTime + static_cast<Timestamp>(localTimeOffset));
76     return E_OK;
77 }
78 
GetTime()79 Timestamp TimeHelper::GetTime()
80 {
81     Timestamp currentSysTime = GetSysCurrentTime();
82     TimeOffset localTimeOffset = GetLocalTimeOffset();
83     Timestamp currentLocalTime = currentSysTime + static_cast<Timestamp>(localTimeOffset);
84     Timestamp lastLocalTime = metadata_->GetLastLocalTime();
85     Timestamp currentMonotonicTime = GetMonotonicTime();
86     Timestamp deltaTime = 1UL;
87     if (currentMonotonicTime != INVALID_TIMESTAMP && lastMonotonicTime_ != INVALID_TIMESTAMP) {
88         deltaTime = currentMonotonicTime - lastMonotonicTime_;
89     }
90     lastMonotonicTime_ = currentMonotonicTime;
91     if (currentLocalTime <= lastLocalTime || currentLocalTime > BUFFER_VALID_TIME) {
92         lastLocalTime += deltaTime;
93         currentLocalTime = lastLocalTime;
94         metadata_->SetLastLocalTime(lastLocalTime);
95     } else {
96         metadata_->SetLastLocalTime(currentLocalTime);
97     }
98     return currentLocalTime;
99 }
100 
GetMaxDataItemTime()101 Timestamp TimeHelper::GetMaxDataItemTime()
102 {
103     Timestamp timestamp = 0;
104     storage_->GetMaxTimestamp(timestamp);
105     return timestamp;
106 }
107 
GetLocalTimeOffset() const108 TimeOffset TimeHelper::GetLocalTimeOffset() const
109 {
110     return metadata_->GetLocalTimeOffset();
111 }
112 
SetSendConfig(const std::string & dstTarget,bool nonBlock,uint32_t timeout,SendConfig & sendConf)113 void TimeHelper::SetSendConfig(const std::string &dstTarget, bool nonBlock, uint32_t timeout, SendConfig &sendConf)
114 {
115     SetSendConfigParam(storage_->GetDbProperties(), dstTarget, false, SEND_TIME_OUT, sendConf);
116 }
117 
GetMonotonicTime()118 Timestamp TimeHelper::GetMonotonicTime()
119 {
120     Timestamp time = INVALID_TIMESTAMP;
121     int errCode = OS::GetMonotonicRelativeTimeInMicrosecond(time);
122     if (errCode != E_OK) {
123         LOGE("GetMonotonicTime ERR! errCode = %d", errCode);
124     }
125     return time;
126 }
127 } // namespace DistributedDB
128