/Users/deen/code/yugabyte-db/src/yb/server/logical_clock.h
Line | Count | Source |
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 | | #ifndef YB_SERVER_LOGICAL_CLOCK_H_ |
34 | | #define YB_SERVER_LOGICAL_CLOCK_H_ |
35 | | |
36 | | #include <string> |
37 | | |
38 | | #include "yb/server/clock.h" |
39 | | #include "yb/util/status_fwd.h" |
40 | | |
41 | | namespace yb { |
42 | | class MonoDelta; |
43 | | class MonoTime; |
44 | | namespace server { |
45 | | |
46 | | // An implementation of Clock that behaves as a plain Lamport Clock. In a single node, single |
47 | | // tablet, setting this generates exactly the same Timestamp sequence as the original MvccManager |
48 | | // did, but it can be updated to make sure replicas generate new hybrid_times on becoming leader. |
49 | | // This can be used as a deterministic hybrid_time generator that has the same consistency |
50 | | // properties as a HybridTime clock. |
51 | | // |
52 | | // The Wait* methods are unavailable in this implementation and will return |
53 | | // Status::ServiceUnavailable(). |
54 | | // |
55 | | // NOTE: this class is thread safe. |
56 | | class LogicalClock : public Clock { |
57 | | public: |
58 | | CHECKED_STATUS Init() override; |
59 | | |
60 | | // Returns the current value of the clock without incrementing it. |
61 | | HybridTime Peek(); |
62 | | |
63 | | virtual void Update(const HybridTime& to_update) override; |
64 | | |
65 | | virtual void RegisterMetrics(const scoped_refptr<MetricEntity>& metric_entity) override; |
66 | | |
67 | | // Creates a logical clock whose first output value on a Now() call is 'hybrid_time'. |
68 | | static LogicalClock* CreateStartingAt(const HybridTime& hybrid_time); |
69 | | |
70 | | private: |
71 | | // Should use LogicalClock::CreatingStartingAt() |
72 | | explicit LogicalClock(HybridTime::val_type initial_time) |
73 | 581 | : now_(initial_time) {} |
74 | | |
75 | | // Used to get the hybrid_time for metrics. |
76 | | uint64_t NowForMetrics(); |
77 | | |
78 | | HybridTimeRange NowRange() override; |
79 | | |
80 | | std::atomic<uint64_t> now_; |
81 | | |
82 | | std::shared_ptr<void> metric_detacher_; |
83 | | }; |
84 | | |
85 | | } // namespace server |
86 | | } // namespace yb |
87 | | |
88 | | #endif /* YB_SERVER_LOGICAL_CLOCK_H_ */ |