Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
simple-scheduler-test.cc
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <gtest/gtest.h>
16 
17 #include <boost/scoped_ptr.hpp>
18 
19 #include "common/logging.h"
20 #include "simple-scheduler.h"
21 
22 #include "common/names.h"
23 
24 using namespace impala;
25 
26 DECLARE_string(pool_conf_file);
27 
28 namespace impala {
29 
31  protected:
33  // The simple scheduler tries to resolve all backend hostnames to
34  // IP addresses to compute locality. For the purposes of this test
35  // we need a set of resolvable hostnames, so here we use IP
36  // addresses which are always resolvable (to themselves!).
37 
38  // Setup hostname_scheduler_
39  num_backends_ = 2;
40  base_port_ = 1000;
41  vector<TNetworkAddress> backends;
42  backends.resize(num_backends_);
43  backends.at(0).hostname = "127.0.0.0";
44  backends.at(0).port = base_port_;
45  backends.at(1).hostname = "localhost";
46  backends.at(1).port = base_port_;
47 
48  hostname_scheduler_.reset(new SimpleScheduler(backends, NULL, NULL, NULL, NULL));
49 
50  // Setup local_remote_scheduler_
51  backends.resize(4);
52  int k = 0;
53  for (int i = 0; i < 2; ++i) {
54  for (int j = 0; j < 2; ++j) {
55  stringstream ss;
56  ss << "127.0.0." << i;
57  backends.at(k).hostname = ss.str();
58  backends.at(k).port = base_port_ + j;
59  ++k;
60  }
61  }
62  local_remote_scheduler_.reset(new SimpleScheduler(backends, NULL, NULL, NULL, NULL));
63  }
64 
67 
68  // This 2 backends: localhost and 127.0.0.0
69  boost::scoped_ptr<SimpleScheduler> hostname_scheduler_;
70 
71  // This scheduler has 4 backends; 2 on each ipaddresses and has 4 different ports.
72  boost::scoped_ptr<SimpleScheduler> local_remote_scheduler_;
73 };
74 
75 
76 TEST_F(SimpleSchedulerTest, LocalMatches) {
77  // If data location matches some back end, use the matched backends in round robin.
78  vector<TNetworkAddress> data_locations;
79  data_locations.resize(5);
80  for (int i = 0; i < 5; ++i) {
81  data_locations.at(i).hostname = "127.0.0.1";
82  data_locations.at(i).port = 0;
83  }
85 
86  local_remote_scheduler_->GetBackends(data_locations, &backends);
87 
88  // Expect 5 round robin backends
89  EXPECT_EQ(5, backends.size());
90  for (int i = 0; i < 5; ++i) {
91  EXPECT_EQ(backends.at(i).address.hostname, "127.0.0.1");
92  EXPECT_EQ(backends.at(i).address.port, base_port_ + i % 2);
93  }
94 }
95 
96 TEST_F(SimpleSchedulerTest, HostnameTest) {
97  // This test tests hostname resolution.
98  // Both localhost and 127.0.0.1 should match backend 127.0.0.1 only.
99  vector<TNetworkAddress> data_locations;
100  data_locations.resize(2);
101  data_locations.at(0).hostname = "localhost";
102  data_locations.at(0).port = 0;
103  data_locations.at(1).hostname = "127.0.0.1";
104  data_locations.at(1).port = 0;
105 
107 
108  hostname_scheduler_->GetBackends(data_locations, &backends);
109 
110  // Expect 2 round robin backends
111  EXPECT_EQ(2, backends.size());
112  for (int i = 0; i < 2; ++i) {
113  EXPECT_EQ(backends.at(i).address.hostname, "127.0.0.1");
114  EXPECT_EQ(backends.at(i).address.port, base_port_);
115  }
116 }
117 
118 TEST_F(SimpleSchedulerTest, NonLocalHost) {
119  // If data location doesn't match any of the ipaddress,
120  // expect round robin ipaddress/port list
121  vector<TNetworkAddress> data_locations;
122  data_locations.resize(5);
123  for (int i = 0; i < 5; ++i) {
124  data_locations.at(i).hostname = "non exists ipaddress";
125  data_locations.at(i).port = 0;
126  }
128 
129  local_remote_scheduler_->GetBackends(data_locations, &backends);
130 
131  // Expect 5 round robin on ipaddress, then on port
132  // 1. 127.0.0.1:1000
133  // 2. 127.0.0.0:1000
134  // 3. 127.0.0.1:1001
135  // 4. 127.0.0.0:1001
136  // 5. 127.0.0.1:1000
137  EXPECT_EQ(5, backends.size());
138  EXPECT_EQ(backends.at(0).address.hostname, "127.0.0.1");
139  EXPECT_EQ(backends.at(0).address.port, 1000);
140  EXPECT_EQ(backends.at(1).address.hostname, "127.0.0.0");
141  EXPECT_EQ(backends.at(1).address.port, 1000);
142  EXPECT_EQ(backends.at(2).address.hostname, "127.0.0.1");
143  EXPECT_EQ(backends.at(2).address.port, 1001);
144  EXPECT_EQ(backends.at(3).address.hostname, "127.0.0.0");
145  EXPECT_EQ(backends.at(3).address.port, 1001);
146  EXPECT_EQ(backends.at(4).address.hostname, "127.0.0.1");
147  EXPECT_EQ(backends.at(4).address.port, 1000);
148 }
149 
150 }
151 
152 int main(int argc, char **argv) {
153  google::InitGoogleLogging(argv[0]);
155  ::testing::InitGoogleTest(&argc, argv);
156  return RUN_ALL_TESTS();
157 }
int main(int argc, char **argv)
TEST_F(InstructionCounterTest, Count)
static list< string > backends
boost::scoped_ptr< SimpleScheduler > local_remote_scheduler_
void InitThreading()
Initialises the threading subsystem. Must be called before a Thread is created.
Definition: thread.cc:261
boost::scoped_ptr< SimpleScheduler > hostname_scheduler_
uint64_t Test(T *ht, const ProbeTuple *input, uint64_t num_tuples)
DECLARE_string(pool_conf_file)
std::vector< TBackendDescriptor > BackendList
List of server descriptors.
Definition: scheduler.h:45