diff options
author | Randolph Tan <randolph@10gen.com> | 2017-05-03 13:20:52 -0400 |
---|---|---|
committer | Randolph Tan <randolph@10gen.com> | 2017-05-04 16:29:38 -0400 |
commit | 2b3d18c8e9871f7cbb89650133ee694bc28636c2 (patch) | |
tree | 0bfed2d46be908e569e724b8cddfbc5f2e1a1b98 /src/mongo/db/logical_clock.cpp | |
parent | 67b8dfc2beb283b08f2549df4d99730325ee5de2 (diff) | |
download | mongo-2b3d18c8e9871f7cbb89650133ee694bc28636c2.tar.gz |
Revert "Revert "SERVER-28562 Move LogicalTime HMAC computation outside collection lock""
This reverts commit b7c013aa097b2d999ad3f942cdfce130558ef40f.
Diffstat (limited to 'src/mongo/db/logical_clock.cpp')
-rw-r--r-- | src/mongo/db/logical_clock.cpp | 98 |
1 files changed, 14 insertions, 84 deletions
diff --git a/src/mongo/db/logical_clock.cpp b/src/mongo/db/logical_clock.cpp index 6a4df1e6c60..c40324f7120 100644 --- a/src/mongo/db/logical_clock.cpp +++ b/src/mongo/db/logical_clock.cpp @@ -86,107 +86,35 @@ void LogicalClock::set(ServiceContext* service, std::unique_ptr<LogicalClock> cl LogicalClock::LogicalClock(ServiceContext* service) : _service(service) {} -SignedLogicalTime LogicalClock::getClusterTime() { +LogicalTime LogicalClock::getClusterTime() { stdx::lock_guard<stdx::mutex> lock(_mutex); return _clusterTime; } -void LogicalClock::setTimeProofService(std::unique_ptr<TimeProofService> tps) { +Status LogicalClock::advanceClusterTime(const LogicalTime newTime) { stdx::lock_guard<stdx::mutex> lock(_mutex); - _timeProofService = std::move(tps); - - // Ensure a clock with a time proof service cannot have a cluster time without a proof to - // simplify reasoning about signed logical times. - if (!_clusterTime.getProof()) { - _clusterTime = _makeSignedLogicalTime_inlock(_clusterTime.getTime()); - } -} - -bool LogicalClock::canVerifyAndSign() { - stdx::lock_guard<stdx::mutex> lock(_mutex); - return !!_timeProofService; -} - -SignedLogicalTime LogicalClock::_makeSignedLogicalTime_inlock(LogicalTime logicalTime) { - if (_timeProofService) { - // TODO: SERVER-28436 Implement KeysCollectionManager - // Replace dummy keyId with real id from key manager. - return SignedLogicalTime( - logicalTime, _timeProofService->getProof(logicalTime, _tempKey), 0); - } - return SignedLogicalTime(logicalTime, 0); -} - -Status LogicalClock::advanceClusterTime(const SignedLogicalTime& newTime) { - stdx::lock_guard<stdx::mutex> lock(_mutex); - if (!_timeProofService) { - return Status(ErrorCodes::CannotVerifyAndSignLogicalTime, - "Cannot accept logicalTime: " + newTime.getTime().toString() + - ". May not be a part of a sharded cluster"); - } - - const auto& newLogicalTime = newTime.getTime(); - - // No need to check proof if no time was given. - if (newLogicalTime == LogicalTime::kUninitialized) { - return Status::OK(); - } - - const auto newProof = newTime.getProof(); - // Logical time is only sent if a server's clock can verify and sign logical times, so any - // received logical times should have proofs. - invariant(newProof); - - auto res = _timeProofService->checkProof(newLogicalTime, newProof.get(), _tempKey); - if (res != Status::OK()) { - return res; - } // The rate limiter check cannot be moved into _advanceClusterTime_inlock to avoid code // repetition because it shouldn't be called on direct oplog operations. - auto rateLimitStatus = _passesRateLimiter_inlock(newTime.getTime()); + auto rateLimitStatus = _passesRateLimiter_inlock(newTime); if (!rateLimitStatus.isOK()) { return rateLimitStatus; } - return _advanceClusterTime_inlock(std::move(newTime)); -} - -Status LogicalClock::_advanceClusterTime_inlock(SignedLogicalTime newTime) { - if (newTime.getTime() > _clusterTime.getTime()) { + if (newTime > _clusterTime) { _clusterTime = newTime; } return Status::OK(); } -Status LogicalClock::advanceClusterTimeFromTrustedSource(SignedLogicalTime newTime) { - stdx::lock_guard<stdx::mutex> lock(_mutex); - // The rate limiter check cannot be moved into _advanceClusterTime_inlock to avoid code - // repetition because it shouldn't be called on direct oplog operations. - auto rateLimitStatus = _passesRateLimiter_inlock(newTime.getTime()); - if (!rateLimitStatus.isOK()) { - return rateLimitStatus; - } - - return _advanceClusterTime_inlock(std::move(newTime)); -} - -Status LogicalClock::signAndAdvanceClusterTime(LogicalTime newTime) { - stdx::lock_guard<stdx::mutex> lock(_mutex); - auto newSignedTime = _makeSignedLogicalTime_inlock(newTime); - - return _advanceClusterTime_inlock(std::move(newSignedTime)); -} - LogicalTime LogicalClock::reserveTicks(uint64_t nTicks) { invariant(nTicks > 0 && nTicks < (1U << 31)); stdx::lock_guard<stdx::mutex> lock(_mutex); - LogicalTime clusterTime = _clusterTime.getTime(); - LogicalTime nextClusterTime; + LogicalTime clusterTime = _clusterTime; const unsigned wallClockSecs = durationCount<Seconds>(_service->getFastClockSource()->now().toDurationSinceEpoch()); @@ -212,23 +140,25 @@ LogicalTime LogicalClock::reserveTicks(uint64_t nTicks) { // Save the next cluster time. clusterTime.addTicks(1); - nextClusterTime = clusterTime; + _clusterTime = clusterTime; // Add the rest of the requested ticks if needed. if (nTicks > 1) { - clusterTime.addTicks(nTicks - 1); + _clusterTime.addTicks(nTicks - 1); } - _clusterTime = _makeSignedLogicalTime_inlock(clusterTime); - return nextClusterTime; + return clusterTime; } -void LogicalClock::initClusterTimeFromTrustedSource(LogicalTime newTime) { +void LogicalClock::setClusterTimeFromTrustedSource(LogicalTime newTime) { stdx::lock_guard<stdx::mutex> lock(_mutex); - invariant(_clusterTime.getTime() == LogicalTime::kUninitialized); + // Rate limit checks are skipped here so a server with no activity for longer than // maxAcceptableLogicalClockDrift seconds can still have its cluster time initialized. - _clusterTime = _makeSignedLogicalTime_inlock(newTime); + + if (newTime > _clusterTime) { + _clusterTime = newTime; + } } Status LogicalClock::_passesRateLimiter_inlock(LogicalTime newTime) { |