summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Mulrow <jack.mulrow@mongodb.com>2018-08-09 12:10:27 -0400
committerJack Mulrow <jack.mulrow@mongodb.com>2018-08-10 09:26:42 -0400
commit60a9c20ec95c4cceda9413e18334c55bb85fe66e (patch)
tree0417693b78a01d7b9eb4a07b08539ee5530c8088
parent6f9bb8538a9d081d56437c032353f7fdb7e4e562 (diff)
downloadmongo-60a9c20ec95c4cceda9413e18334c55bb85fe66e.tar.gz
SERVER-36554 Make computeAtClusterTimeForOneShard return latest clusterTime if there is no cached last committed opTime
-rw-r--r--src/mongo/s/commands/cluster_commands_helpers.cpp11
-rw-r--r--src/mongo/s/commands/cluster_commands_helpers.h3
-rw-r--r--src/mongo/s/commands/compute_at_cluster_time_test.cpp24
3 files changed, 36 insertions, 2 deletions
diff --git a/src/mongo/s/commands/cluster_commands_helpers.cpp b/src/mongo/s/commands/cluster_commands_helpers.cpp
index dedd07a3acc..6535ff03d4d 100644
--- a/src/mongo/s/commands/cluster_commands_helpers.cpp
+++ b/src/mongo/s/commands/cluster_commands_helpers.cpp
@@ -616,7 +616,16 @@ boost::optional<LogicalTime> computeAtClusterTimeForOneShard(OperationContext* o
auto shardRegistry = Grid::get(opCtx)->shardRegistry();
invariant(shardRegistry);
- return shardRegistry->getShardNoReload(shardId)->getLastCommittedOpTime();
+
+ auto shard = shardRegistry->getShardNoReload(shardId);
+ uassert(ErrorCodes::ShardNotFound, str::stream() << "Could not find shard " << shardId, shard);
+
+ // Return the cached last committed opTime for the shard if there is one, otherwise return the
+ // lastest cluster time from the logical clock.
+ auto lastCommittedOpTime = shard->getLastCommittedOpTime();
+ return lastCommittedOpTime != LogicalTime::kUninitialized
+ ? lastCommittedOpTime
+ : LogicalClock::get(opCtx)->getClusterTime();
}
namespace {
diff --git a/src/mongo/s/commands/cluster_commands_helpers.h b/src/mongo/s/commands/cluster_commands_helpers.h
index bc848435357..6c5e89af0f0 100644
--- a/src/mongo/s/commands/cluster_commands_helpers.h
+++ b/src/mongo/s/commands/cluster_commands_helpers.h
@@ -215,7 +215,8 @@ std::set<ShardId> getTargetedShardsForQuery(OperationContext* opCtx,
const BSONObj& collation);
/**
- * Returns the latest known lastCommittedOpTime for the targeted shard.
+ * Returns the latest known lastCommittedOpTime for the targeted shard, or the latest in-memory
+ * cluster time if there is none.
*
* A null logical time is returned if the readConcern on the OperationContext is not snapshot.
*/
diff --git a/src/mongo/s/commands/compute_at_cluster_time_test.cpp b/src/mongo/s/commands/compute_at_cluster_time_test.cpp
index 4d34d390326..8dfccf5754a 100644
--- a/src/mongo/s/commands/compute_at_cluster_time_test.cpp
+++ b/src/mongo/s/commands/compute_at_cluster_time_test.cpp
@@ -123,6 +123,30 @@ TEST_F(AtClusterTimeTest, ComputeInvalidInvalid) {
ASSERT_EQ(*maxTime, kInMemoryLogicalTime);
}
+TEST_F(AtClusterTimeTest, ComputeForOneShard) {
+ auto shardOne = shardRegistry()->getShardNoReload(shardOneId);
+
+ LogicalTime timeOne(Timestamp(10, 2));
+ shardOne->updateLastCommittedOpTime(timeOne);
+ ASSERT_EQ(timeOne, shardOne->getLastCommittedOpTime());
+
+ auto atClusterTime = computeAtClusterTimeForOneShard(operationContext(), shardOneId);
+ ASSERT_EQ(*atClusterTime, timeOne);
+}
+
+TEST_F(AtClusterTimeTest, ComputeForOneShardNoCachedOpTime) {
+ auto shardOne = shardRegistry()->getShardNoReload(shardOneId);
+ ASSERT_EQ(LogicalTime(), shardOne->getLastCommittedOpTime());
+
+ auto atClusterTime = computeAtClusterTimeForOneShard(operationContext(), shardOneId);
+ ASSERT_EQ(*atClusterTime, kInMemoryLogicalTime);
+}
+
+TEST_F(AtClusterTimeTest, ComputeForOneShardNoShard) {
+ ASSERT_THROWS_CODE(computeAtClusterTimeForOneShard(operationContext(), ShardId("fakeShard")),
+ AssertionException,
+ ErrorCodes::ShardNotFound);
+}
class AtClusterTimeTargetingTest : public CatalogCacheTestFixture {
protected: