YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/integration-tests/clock_synchronization-itest.cc
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) YugaByte, Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
4
// in compliance with the License.  You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software distributed under the License
9
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10
// or implied.  See the License for the specific language governing permissions and limitations
11
// under the License.
12
//
13
14
#include "yb/client/client.h"
15
#include "yb/client/schema.h"
16
#include "yb/client/session.h"
17
#include "yb/client/table.h"
18
#include "yb/client/table_creator.h"
19
#include "yb/client/yb_op.h"
20
21
#include "yb/common/column_id.h"
22
#include "yb/common/ql_protocol.pb.h"
23
24
#include "yb/integration-tests/mini_cluster.h"
25
#include "yb/integration-tests/yb_mini_cluster_test_base.h"
26
27
// RepeatedPtrField would NOT call dtor if class is not defined, so need this include below.
28
#include "yb/master/master_client.pb.h" // for TabletLocationsPB
29
30
#include "yb/rpc/messenger.h"
31
32
#include "yb/server/hybrid_clock.h"
33
34
#include "yb/util/random.h"
35
#include "yb/util/result.h"
36
#include "yb/util/status_log.h"
37
38
DECLARE_uint64(max_clock_sync_error_usec);
39
DECLARE_bool(disable_clock_sync_error);
40
DECLARE_int32(ht_lease_duration_ms);
41
DECLARE_string(time_source);
42
43
namespace yb {
44
45
const std::string kMock = "mock";
46
47
class ClockSynchronizationTest : public YBMiniClusterTestBase<MiniCluster> {
48
 public:
49
1
  ClockSynchronizationTest() : random_(0) {
50
1
    server::HybridClock::RegisterProvider(kMock, mock_clock_.AsProvider());
51
1
  }
52
53
1
  void SetUp() override {
54
1
    FLAGS_ht_lease_duration_ms = 0;
55
1
    SetupFlags();
56
57
1
    YBMiniClusterTestBase::SetUp();
58
1
    MiniClusterOptions opts;
59
60
1
    opts.num_tablet_servers = 3;
61
1
    cluster_.reset(new MiniCluster(opts));
62
1
    ASSERT_OK(cluster_->Start());
63
64
0
    client::YBSchemaBuilder b;
65
0
    b.AddColumn("key")->Type(INT64)->NotNull()->HashPrimaryKey();
66
0
    b.AddColumn("value")->Type(INT64)->NotNull();
67
0
    CHECK_OK(b.Build(&schema_));
68
69
0
    client_ = ASSERT_RESULT(cluster_->CreateClient());
70
0
  }
71
72
0
  void DoTearDown() override {
73
0
    client_.reset();
74
0
    cluster_->Shutdown();
75
0
  }
76
77
0
  void CreateTable() {
78
79
0
    ASSERT_OK(client_->CreateNamespace(kNamespace));
80
81
    // Create the table.
82
0
    table_name_.reset(new client::YBTableName(YQL_DATABASE_CQL, kNamespace, kTableName));
83
0
    std::unique_ptr<client::YBTableCreator> table_creator(client_->NewTableCreator());
84
0
    ASSERT_OK(table_creator->table_name(*table_name_.get())
85
0
                  .table_type(client::YBTableType::YQL_TABLE_TYPE)
86
0
                  .schema(&schema_)
87
0
                  .num_tablets(10)
88
0
                  .wait(true)
89
0
                  .Create());
90
0
    ASSERT_OK(client_->OpenTable(*table_name_, &table_));
91
0
  }
92
93
0
  void PerformOps(int num_writes_per_tserver) {
94
0
    google::protobuf::RepeatedPtrField<master::TabletLocationsPB> tablets;
95
0
    ASSERT_OK(
96
0
        client_->GetTablets(*table_name_, 0, &tablets, /* partition_list_version =*/ nullptr));
97
0
    std::shared_ptr<client::YBSession> session =  client_->NewSession();
98
0
    for (int i = 0; i < num_writes_per_tserver; i++) {
99
0
      auto ql_write = std::make_shared<client::YBqlWriteOp>(table_);
100
0
      auto *const req = ql_write->mutable_request();
101
0
      req->set_client(QLClient::YQL_CLIENT_CQL);
102
0
      req->set_type(QLWriteRequestPB_QLStmtType_QL_STMT_INSERT);
103
0
      QLExpressionPB *hash_column = req->add_hashed_column_values();
104
0
      int64_t val = random_.Next64();
105
0
      hash_column->mutable_value()->set_int64_value(val);
106
107
0
      QLColumnValuePB *column = req->add_column_values();
108
0
      column->set_column_id(kFirstColumnId + 1);
109
0
      column->mutable_expr()->mutable_value()->set_int64_value(val);
110
0
      EXPECT_OK(session->ApplyAndFlush(ql_write));
111
0
      EXPECT_EQ(QLResponsePB::YQL_STATUS_OK, ql_write->response().status());
112
0
    }
113
0
  }
114
115
1
  virtual void SetupFlags() {
116
1
    FLAGS_time_source = "mock";
117
1
  }
118
119
  std::unique_ptr<client::YBClient> client_;
120
  client::YBSchema schema_;
121
  std::unique_ptr<client::YBTableName> table_name_;
122
  std::shared_ptr<client::YBTable> table_;
123
  std::unique_ptr<rpc::Messenger> client_messenger_;
124
  Random random_;
125
  MockClock mock_clock_;
126
  constexpr static const char* const kNamespace = "my_namespace";
127
  constexpr static const char* const kTableName = "my_table";
128
};
129
130
0
TEST_F(ClockSynchronizationTest, TestClockSkewError) {
131
0
  mock_clock_.Set({0, FLAGS_max_clock_sync_error_usec + 1});
132
133
0
  CreateTable();
134
0
  PerformOps(100);
135
0
}
136
137
} // namespace yb