YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/common/hybrid_time.cc
Line
Count
Source (jump to first uncovered line)
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
//
18
// The following only applies to changes made to this file as part of YugaByte development.
19
//
20
// Portions Copyright (c) YugaByte, Inc.
21
//
22
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
23
// in compliance with the License.  You may obtain a copy of the License at
24
//
25
// http://www.apache.org/licenses/LICENSE-2.0
26
//
27
// Unless required by applicable law or agreed to in writing, software distributed under the License
28
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
29
// or implied.  See the License for the specific language governing permissions and limitations
30
// under the License.
31
//
32
33
#include "yb/common/hybrid_time.h"
34
35
#include <atomic>
36
37
#include <boost/algorithm/string/trim.hpp>
38
#include <boost/date_time/c_local_time_adjustor.hpp>
39
#include <boost/date_time/posix_time/time_formatters.hpp>
40
41
#include "yb/util/date_time.h"
42
#include "yb/util/memcmpable_varint.h"
43
#include "yb/util/result.h"
44
45
using std::string;
46
47
namespace yb {
48
49
namespace {
50
51
std::atomic<bool> pretty_to_string_mode_{false};
52
53
}
54
55
const HybridTime HybridTime::kMin(kMinHybridTimeValue);
56
const HybridTime HybridTime::kMax(kMaxHybridTimeValue);
57
const HybridTime HybridTime::kInitial(kInitialHybridTimeValue);
58
const HybridTime HybridTime::kInvalid(kInvalidHybridTimeValue);
59
60
0
bool HybridTime::DecodeFrom(Slice *input) {
61
0
  return GetMemcmpableVarint64(input, &v).ok();
62
0
}
63
64
0
void HybridTime::AppendAsUint64To(faststring *dst) const {
65
0
  PutMemcmpableVarint64(dst, v);
66
0
}
67
68
0
void HybridTime::AppendAsUint64To(std::string* dst) const {
69
0
  PutMemcmpableVarint64(dst, v);
70
0
}
71
72
1.09M
string HybridTime::ToString() const {
73
1.09M
  switch (v) {
74
22.6k
    case kInvalidHybridTimeValue:
75
22.6k
      return "<invalid>";
76
10.3k
    case kMaxHybridTimeValue:
77
10.3k
      return "<max>";
78
49
    case kMinHybridTimeValue:
79
49
      return "<min>";
80
4.69k
    case kInitialHybridTimeValue:
81
4.69k
      return "<initial>";
82
1.06M
    default:
83
1.06M
      auto logical = GetLogicalValue();
84
1.06M
      if (!pretty_to_string_mode_.load(std::memory_order_acquire)) {
85
363k
        if (logical) {
86
195k
          return Format("{ physical: $0 logical: $1 }", GetPhysicalValueMicros(), logical);
87
167k
        } else {
88
167k
          return Format("{ physical: $0 }", GetPhysicalValueMicros());
89
167k
        }
90
698k
      }
91
      // When time is rendered with separate minutes and seconds it is easier to understand that
92
      // one value is 2 seconds later that another one.
93
      // With default rendering difference appears in the middle of 10+ digits number.
94
698k
      boost::posix_time::ptime start(boost::gregorian::date(1970, 1, 1));
95
698k
      auto usec = GetPhysicalValueMicros();
96
698k
      auto utc_time = start + boost::posix_time::microseconds(usec);
97
698k
      auto local_time =
98
698k
          boost::date_time::c_local_adjustor<boost::posix_time::ptime>::utc_to_local(utc_time);
99
698k
      auto date = local_time.date();
100
698k
      auto time_of_day = local_time.time_of_day();
101
698k
      auto days = (boost::posix_time::ptime(date) - start).hours() / 24;
102
698k
      auto time_of_day_str = boost::posix_time::to_simple_string(time_of_day);
103
698k
      if (logical) {
104
38.7k
        return Format("{ days: $0 time: $1 logical: $2 }", days, time_of_day_str, logical);
105
659k
      } else {
106
659k
        return Format("{ days: $0 time: $1 }", days, time_of_day_str);
107
659k
      }
108
1.09M
  }
109
1.09M
}
110
111
6.90k
void HybridTime::TEST_SetPrettyToString(bool flag) {
112
6.90k
  pretty_to_string_mode_ = flag;
113
6.90k
}
114
115
180k
string HybridTime::ToDebugString() const {
116
180k
  return kHybridTimeDebugStrPrefix + ToString();
117
180k
}
118
119
85.1M
uint64_t HybridTime::ToUint64() const {
120
85.1M
  return v;
121
85.1M
}
122
123
103k
Status HybridTime::FromUint64(uint64_t value) {
124
103k
  v = value;
125
103k
  return Status::OK();
126
103k
}
127
128
33.2M
MicrosTime HybridTime::CeilPhysicalValueMicros() const {
129
33.2M
  if (*this == kMin) {
130
32.6M
    return 0;
131
32.6M
  }
132
613k
  auto result = GetPhysicalValueMicros();
133
613k
  if (GetLogicalValue()) {
134
10.8k
    ++result;
135
10.8k
  }
136
613k
  return result;
137
613k
}
138
139
0
Result<HybridTime> HybridTime::ParseHybridTime(std::string input) {
140
0
  boost::trim(input);
141
142
0
  HybridTime ht;
143
  // The HybridTime is given in microseconds and will contain 16 chars.
144
0
  static const std::regex int_regex("[0-9]{16}");
145
0
  if (std::regex_match(input, int_regex)) {
146
0
    return HybridTime::FromMicros(std::stoul(input));
147
0
  }
148
0
  if (!input.empty() && input[0] == '-') {
149
0
    return HybridTime::FromMicros(
150
0
        VERIFY_RESULT(WallClock()->Now()).time_point -
151
0
        VERIFY_RESULT(DateTime::IntervalFromString(input.substr(1))).ToMicroseconds());
152
0
  }
153
0
  auto ts =
154
0
      VERIFY_RESULT(DateTime::TimestampFromString(input, DateTime::HumanReadableInputFormat));
155
0
  return HybridTime::FromMicros(ts.ToInt64());
156
0
}
157
158
const char* const HybridTime::kHybridTimeDebugStrPrefix = "HT";
159
160
}  // namespace yb