Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
session-expiry-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 <boost/scoped_ptr.hpp>
16 #include <string>
17 #include <gtest/gtest.h>
18 
19 #include "rpc/thrift-client.h"
20 #include "service/impala-server.h"
22 #include "common/init.h"
23 #include "service/fe-support.h"
24 #include "util/impalad-metrics.h"
25 #include "util/time.h"
26 
27 #include "common/names.h"
28 
29 using namespace apache::hive::service::cli::thrift;
30 using namespace apache::thrift;
31 using namespace impala;
32 
33 DECLARE_int32(idle_session_timeout);
34 DECLARE_int32(be_port);
35 DECLARE_int32(beeswax_port);
36 
37 // TODO: When sleep(..) queries can be cancelled, write a test that confirms long-running
38 // queries are cancelled during session expiry.
39 // TODO: Come up with a short-running test that confirms a session will keep itself alive
40 // that doesn't depend upon being rescheduled in a timely fashion.
41 
42 TEST(SessionTest, TestExpiry) {
43  FLAGS_idle_session_timeout = 1;
44  InProcessImpalaServer* impala =
45  new InProcessImpalaServer("localhost", FLAGS_be_port, 0, 0, "", 0);
47  impala->StartWithClientServers(FLAGS_beeswax_port, FLAGS_beeswax_port + 1, false));
48  IntCounter* expired_metric =
50  ImpaladMetricKeys::NUM_SESSIONS_EXPIRED);
51  DCHECK(expired_metric != NULL);
52  IntGauge* beeswax_session_metric =
54  ImpaladMetricKeys::IMPALA_SERVER_NUM_OPEN_BEESWAX_SESSIONS);
55  IntGauge* hs2_session_metric =
57  ImpaladMetricKeys::IMPALA_SERVER_NUM_OPEN_HS2_SESSIONS);
58  EXPECT_EQ(expired_metric->value(), 0L);
59  EXPECT_EQ(beeswax_session_metric->value(), 0L);
60 
61  scoped_ptr<ThriftClient<ImpalaServiceClient> > beeswax_clients[5];
62  scoped_ptr<ThriftClient<ImpalaHiveServer2ServiceClient> > hs2_clients[5];
63 
64  // Create five Beeswax clients and five HS2 clients (each HS2 gets one session each)
65  for (int i = 0; i < 5; ++i) {
66  beeswax_clients[i].reset(new ThriftClient<ImpalaServiceClient>(
67  "localhost", FLAGS_beeswax_port));
68  EXPECT_TRUE(beeswax_clients[i]->Open().ok());
69 
70  hs2_clients[i].reset(new ThriftClient<ImpalaHiveServer2ServiceClient>(
71  "localhost", FLAGS_beeswax_port + 1));
72  EXPECT_TRUE(hs2_clients[i]->Open().ok());
73  TOpenSessionResp response;
74  TOpenSessionReq request;
75  hs2_clients[i]->iface()->OpenSession(response, request);
76  }
77 
78  int64_t start = UnixMillis();
79  while (expired_metric->value() != 10 && UnixMillis() - start < 5000) {
80  SleepForMs(100);
81  }
82 
83  ASSERT_EQ(expired_metric->value(), 10L) << "Sessions did not expire within 5s";
84  ASSERT_EQ(beeswax_session_metric->value(), 5L)
85  << "Beeswax sessions unexpectedly closed after expiration";
86  ASSERT_EQ(hs2_session_metric->value(), 5L)
87  << "HiveServer2 sessions unexpectedly closed after expiration";
88 
89  TPingImpalaServiceResp resp;
90  ASSERT_THROW({beeswax_clients[0]->iface()->PingImpalaService(resp);}, TException)
91  << "Ping succeeded even after session expired";
92 }
93 
94 int main(int argc, char** argv) {
95  InitCommonRuntime(argc, argv, true);
96  InitFeSupport();
97  ::testing::InitGoogleTest(&argc, argv);
98  return RUN_ALL_TESTS();
99 }
void InitFeSupport()
Definition: fe-support.cc:346
TEST(SessionTest, TestExpiry)
class SimpleMetric< int64_t, TMetricKind::COUNTER > IntCounter
Definition: metrics.h:320
void InitCommonRuntime(int argc, char **argv, bool init_jvm, TestInfo::Mode m=TestInfo::NON_TEST)
Definition: init.cc:122
DECLARE_int32(idle_session_timeout)
void SleepForMs(const int64_t duration_ms)
Sleeps the current thread for at least duration_ms milliseconds.
Definition: time.cc:21
int64_t UnixMillis()
Definition: time.h:51
M * FindMetricForTesting(const std::string &key)
Used for testing only.
Definition: metrics.h:253
#define EXIT_IF_ERROR(stmt)
Definition: status.h:248
Status StartWithClientServers(int beeswax_port, int hs2_port, bool use_statestore)
int main(int argc, char **argv)