[SERVER-27745] Implement LogicalClock Created: 18/Jan/17  Updated: 05/Apr/17  Resolved: 03/Feb/17

Status: Closed
Project: Core Server
Component/s: Sharding
Affects Version/s: None
Fix Version/s: 3.5.3

Type: Task Priority: Major - P3
Reporter: Misha Tyulenev Assignee: Misha Tyulenev
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible
Sprint: Sharding 2017-02-13
Participants:

 Description   

LogicalClock class provides API to change the LogicalTime. Currently its done via global functions in global_timestamp where the equivalent of LogicalTime class is AtomicUInt64.

The suggested LogicalClock API:

 
class LogicalClock:
public:
      void setClusterTime(const LogicalTime& newTime);
      const LogicalTime& getClusterTime() const;
     
     // increments clusterTime and return the result. this method should sync with the wall clock 
     LogicalTime addTicks(unsigned ticks);
 
    // increments clusterTime but returns the time before the increment therefor "reserving" ticks.
    // Needed for the batch writes oplog functionality. this method should sync with the wall clock  
    LogicalTime reserveTicks(unsigned ticks);
  
  private:
      mutable stdx::mutex _mutex; // unless there is a measurable performance degradation 
                                                     // serialize on mutex
      LogicalTime _clusterTime;
 };
 
LogicalTime LogicalClock::reserveTicks(unsigned ticks) {
  
      const unsigned wallClockSecs = durationCount<Seconds>(
          getGlobalServiceContext()->getFastClockSource()->now().toDurationSinceEpoch());
    
      stdx::lock_guard<stdx::mutex> lock(_mutex);
      // Optimistic assumption that seconds did not change
      LogicalTime currentTime = _clusterTime;
     _clusterTime.addTicks(ticks);
  
      unsigned currentSecs = _clusterTime.asTimestamp().getSecs();
      // If the seconds need to be updated, try to do it. This can happen at most once per second.
      if (MONGO_unlikely(currentSecs < wallClockSecs)) {
          // First fix the seconds portion. 
         _clusterTime = LogicalTime(Timestamp(wallClockSecs, 1));
          currentTime = _clusterTime;
          _clusterTime.addTicks(ticks);
      }
  
      return currentTime;
  }
 

The Implementation should decouple RateCheck into a separate class and depend on it.
The task should include unit tests that will need mock for the wall clocks that will verify API



 Comments   
Comment by Githook User [ 03/Feb/17 ]

Author:

{u'username': u'mikety', u'name': u'Misha Tyulenev', u'email': u'misha@mongodb.com'}

Message: SERVER-27745: Implement LogicalClock
Branch: master
https://github.com/mongodb/mongo/commit/91bb1bca8e8b4f66a610409ec0ebe10245a02e5a

Generated at Thu Feb 08 04:16:04 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.