/** * Copyright (C) 2017 MongoDB, Inc. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License, version 3, * as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . * * As a special exception, the copyright holders give permission to link the * code of portions of this program with the OpenSSL library under certain * conditions as described in each individual source file and distribute * linked combinations including the program with the OpenSSL library. You * must comply with the GNU Affero General Public License in all respects for * all of the code used other than as permitted herein. If you modify file(s) * with this exception, you may extend this exception to your version of the * file(s), but you are not obligated to do so. If you do not wish to do so, * delete this exception statement from your version. If you delete this * exception statement from all source files in the program, then also delete * it in the license file. */ #include "mongo/db/logical_clock.h" #include "mongo/bson/timestamp.h" #include "mongo/db/logical_time.h" #include "mongo/db/service_context_noop.h" #include "mongo/db/signed_logical_time.h" #include "mongo/db/time_proof_service.h" #include "mongo/platform/basic.h" #include "mongo/stdx/memory.h" #include "mongo/unittest/unittest.h" namespace mongo { namespace { /** * Setup LogicalClock with invalid initial time. */ class LogicalClockTestBase : public unittest::Test { protected: void setUp() { _serviceContext = stdx::make_unique(); std::array tempKey = {}; TimeProofService::Key key(std::move(tempKey)); auto pTps = stdx::make_unique(std::move(key)); _timeProofService = pTps.get(); _clock = stdx::make_unique(_serviceContext.get(), std::move(pTps)); } void tearDown() { _clock.reset(); _serviceContext.reset(); } LogicalClock* getClock() { return _clock.get(); } SignedLogicalTime makeSignedLogicalTime(LogicalTime logicalTime) { return SignedLogicalTime(logicalTime, _timeProofService->getProof(logicalTime)); } const unsigned currentWallClockSecs() { return durationCount( _serviceContext->getFastClockSource()->now().toDurationSinceEpoch()); } private: TimeProofService* _timeProofService; std::unique_ptr _serviceContext; std::unique_ptr _clock; }; // Check that the initial time does not change during logicalClock creation. TEST_F(LogicalClockTestBase, roundtrip) { // Create different logicalClock instance to validate that the initial time is preserved. ServiceContextNoop serviceContext; Timestamp tX(1); std::array tempKey = {}; TimeProofService::Key key(std::move(tempKey)); auto pTps = stdx::make_unique(std::move(key)); auto time = LogicalTime(tX); LogicalClock logicalClock(&serviceContext, std::move(pTps)); logicalClock.initClusterTimeFromTrustedSource(time); auto storedTime(logicalClock.getClusterTime()); ASSERT_TRUE(storedTime.getTime() == time); } // Verify the reserve ticks functionality. TEST_F(LogicalClockTestBase, reserveTicks) { auto t1 = getClock()->reserveTicks(1); auto t2(getClock()->getClusterTime()); ASSERT_TRUE(t1 == t2.getTime()); auto t3 = getClock()->reserveTicks(1); t1.addTicks(1); ASSERT_TRUE(t3 == t1); t3 = getClock()->reserveTicks(100); t1.addTicks(1); ASSERT_TRUE(t3 == t1); t3 = getClock()->reserveTicks(1); t1.addTicks(100); ASSERT_TRUE(t3 == t1); } // Verify the advanceClusterTime functionality. TEST_F(LogicalClockTestBase, advanceClusterTime) { auto t1 = getClock()->reserveTicks(1); t1.addTicks(100); SignedLogicalTime l1 = makeSignedLogicalTime(t1); ASSERT_OK(getClock()->advanceClusterTimeFromTrustedSource(l1)); auto l2(getClock()->getClusterTime()); ASSERT_TRUE(l1.getTime() == l2.getTime()); } // Verify rate limiter rejects logical times whose seconds values are too far ahead. TEST_F(LogicalClockTestBase, RateLimiterRejectsLogicalTimesTooFarAhead) { Timestamp tooFarAheadTimestamp( currentWallClockSecs() + durationCount(LogicalClock::kMaxAcceptableLogicalClockDrift) + 10, // Add 10 seconds to ensure limit is exceeded. 1); SignedLogicalTime l1 = makeSignedLogicalTime(LogicalTime(tooFarAheadTimestamp)); ASSERT_EQ(ErrorCodes::ClusterTimeFailsRateLimiter, getClock()->advanceClusterTime(l1)); ASSERT_EQ(ErrorCodes::ClusterTimeFailsRateLimiter, getClock()->advanceClusterTimeFromTrustedSource(l1)); } // Verify cluster time can be initialized to a very old time. TEST_F(LogicalClockTestBase, InitFromTrustedSourceCanAcceptVeryOldLogicalTime) { Timestamp veryOldTimestamp( currentWallClockSecs() - (durationCount(LogicalClock::kMaxAcceptableLogicalClockDrift) * 5)); auto veryOldTime = LogicalTime(veryOldTimestamp); getClock()->initClusterTimeFromTrustedSource(veryOldTime); ASSERT_TRUE(getClock()->getClusterTime().getTime() == veryOldTime); } } // unnamed namespace } // namespace mongo