YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/integration-tests/external_mini_cluster-test.cc
Line
Count
Source (jump to first uncovered line)
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
//
18
// The following only applies to changes made to this file as part of YugaByte development.
19
//
20
// Portions Copyright (c) YugaByte, Inc.
21
//
22
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
23
// in compliance with the License.  You may obtain a copy of the License at
24
//
25
// http://www.apache.org/licenses/LICENSE-2.0
26
//
27
// Unless required by applicable law or agreed to in writing, software distributed under the License
28
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
29
// or implied.  See the License for the specific language governing permissions and limitations
30
// under the License.
31
//
32
33
#include "yb/gutil/strings/substitute.h"
34
#include "yb/gutil/strings/util.h"
35
36
#include "yb/integration-tests/external_mini_cluster.h"
37
38
#include "yb/util/metrics.h"
39
#include "yb/util/net/net_util.h"
40
#include "yb/util/result.h"
41
#include "yb/util/test_util.h"
42
43
METRIC_DECLARE_entity(server);
44
METRIC_DECLARE_gauge_uint64(threads_running);
45
46
namespace yb {
47
48
class EMCTest : public YBTest {
49
 public:
50
2
  EMCTest() {
51
2
    master_peer_ports_ = { 0, 0, 0 };
52
2
  }
53
54
 protected:
55
  std::vector<uint16_t> master_peer_ports_;
56
};
57
58
1
TEST_F(EMCTest, TestBasicOperation) {
59
1
  ExternalMiniClusterOptions opts;
60
1
  opts.num_masters = master_peer_ports_.size();
61
1
  opts.num_tablet_servers = 3;
62
1
  opts.master_rpc_ports = master_peer_ports_;
63
64
1
  ExternalMiniCluster cluster(opts);
65
1
  ASSERT_OK(cluster.Start());
66
67
  // Verify each of the masters.
68
4
  for (size_t i = 0; i < opts.num_masters; i++) {
69
3
    SCOPED_TRACE(i);
70
3
    ExternalMaster* master = CHECK_NOTNULL(cluster.master(i));
71
3
    HostPort master_rpc = master->bound_rpc_hostport();
72
6
    EXPECT_TRUE(HasPrefixString(master_rpc.ToString(), "127.0.0.1:")) << master_rpc.ToString();
73
74
3
    HostPort master_http = master->bound_http_hostport();
75
6
    EXPECT_TRUE(HasPrefixString(master_http.ToString(), "127.0.0.1:")) << master_http.ToString();
76
77
    // Retrieve a thread metric, which should always be present on any master.
78
3
    int64_t value = ASSERT_RESULT(master->GetInt64Metric(
79
3
        &METRIC_ENTITY_server,
80
3
        "yb.master",
81
3
        &METRIC_threads_running,
82
3
        "value"));
83
3
    LOG(INFO) << "Master " << i << ": " << METRIC_threads_running.name() << '=' << value;
84
3
    EXPECT_GT(value, 0);
85
3
  }
86
87
  // Verify each of the tablet servers.
88
4
  for (size_t i = 0; i < opts.num_tablet_servers; i++) {
89
3
    SCOPED_TRACE(i);
90
3
    const ExternalTabletServer* const ts = CHECK_NOTNULL(cluster.tablet_server(i));
91
3
    const HostPort ts_rpc = ts->bound_rpc_hostport();
92
3
    const HostPort ts_http = ts->bound_http_hostport();
93
3
    const string expected_prefix = strings::Substitute("$0:", cluster.GetBindIpForTabletServer(i));
94
95
    // Let TS 0 be on 127.0.0.1 address on MAC.
96
3
    if (opts.bind_to_unique_loopback_addresses && i > 0) {
97
0
      EXPECT_NE(expected_prefix, "127.0.0.1:") << "Should bind to unique per-server hosts";
98
0
    }
99
100
6
    EXPECT_TRUE(HasPrefixString(ts_rpc.ToString(), expected_prefix)) << ts_rpc.ToString();
101
6
    EXPECT_TRUE(HasPrefixString(ts_http.ToString(), expected_prefix)) << ts_http.ToString();
102
103
    // Retrieve a thread metric, which should always be present on any TS.
104
3
    int64_t value = ASSERT_RESULT(ts->GetInt64Metric(
105
3
        &METRIC_ENTITY_server, "yb.tabletserver", &METRIC_threads_running, "value"));
106
3
    LOG(INFO) << "TServer " << i << ": " << METRIC_threads_running.name() << '=' << value;
107
3
    EXPECT_GT(value, 0);
108
3
  }
109
110
  // Restart a master and a tablet server. Make sure they come back up with the same ports.
111
1
  ExternalMaster* master = cluster.master(0);
112
1
  HostPort master_rpc = master->bound_rpc_hostport();
113
1
  HostPort master_http = master->bound_http_hostport();
114
115
1
  master->Shutdown();
116
1
  ASSERT_OK(master->Restart());
117
118
1
  ASSERT_EQ(master_rpc.ToString(), master->bound_rpc_hostport().ToString());
119
1
  ASSERT_EQ(master_http.ToString(), master->bound_http_hostport().ToString());
120
121
1
  ExternalTabletServer* ts = cluster.tablet_server(0);
122
123
1
  HostPort ts_rpc = ts->bound_rpc_hostport();
124
1
  HostPort ts_http = ts->bound_http_hostport();
125
126
1
  ts->Shutdown();
127
1
  ASSERT_OK(ts->Restart());
128
129
1
  ASSERT_EQ(ts_rpc.ToString(), ts->bound_rpc_hostport().ToString());
130
1
  ASSERT_EQ(ts_http.ToString(), ts->bound_http_hostport().ToString());
131
132
1
  cluster.Shutdown();
133
1
}
134
135
1
TEST_F(EMCTest, TestUniquePorts) {
136
1
  ExternalMiniClusterOptions opts;
137
1
  ExternalMiniCluster cluster(opts);
138
139
1
  std::set<uint16_t> ports;
140
101
  for (int idx = 0; idx < 100; idx++) {
141
100
    uint16_t port = cluster.AllocateFreePort();
142
100
    if (ports.count(port) == 0) {
143
100
      LOG(INFO) << "allocated port: " << port;
144
100
      ports.insert(port);
145
0
    } else {
146
0
      FAIL() << "port: " << port << " already allocated.";
147
0
    }
148
100
  }
149
1
}
150
151
} // namespace yb