blob: 7c4b2ad280843028bcd8337823e9bc0b40fed2f7 [file] [log] [blame]
Rolf Badorekef2bf512019-08-20 11:17:15 +03001#include <ostream>
2#include <cstdlib>
3#include <fstream>
4#include <string>
5#include <iostream>
6#include <boost/property_tree/ptree.hpp>
7#include <boost/property_tree/json_parser.hpp>
8#include <chrono>
9#include <arpa/inet.h>
10#include <sdl/asyncstorage.hpp>
11#include <boost/asio.hpp>
12#include <thread>
13#include "private/cli/commandmap.hpp"
14#include "private/configurationpaths.hpp"
15#include "private/createlogger.hpp"
16#include "private/engineimpl.hpp"
17#include "private/databaseconfigurationimpl.hpp"
18#include "private/configurationreader.hpp"
19#include "private/redis/databaseinfo.hpp"
20#include "private/asyncstorageimpl.hpp"
21#include "private/redis/asyncredisstorage.hpp"
22
23using namespace shareddatalayer;
24using namespace shareddatalayer::cli;
25using namespace shareddatalayer::redis;
26
27namespace
28{
29 void handler(std::shared_ptr<shareddatalayer::AsyncStorage> sdl, boost::asio::posix::stream_descriptor& sd)
30 {
31 sdl->handleEvents();
32 sd.async_read_some(boost::asio::null_buffers(), std::bind(handler, sdl, std::ref(sd)));
33 }
34
35 std::shared_ptr<AsyncStorage> createStorage(const std::string& nsStr, std::ostream& out)
36 {
37 try
38 {
39 std::shared_ptr<AsyncStorage> sdl(AsyncStorage::create());
40 boost::asio::io_service ios;
41 boost::asio::posix::stream_descriptor sd(ios);
42 sd.assign(sdl->fd());
43 sd.async_read_some(boost::asio::null_buffers(), std::bind(handler, sdl, std::ref(sd)));
44 sdl->waitReadyAsync(nsStr, [&ios](const std::error_code& error)
45 {
46 if (error)
47 std::cerr << "SDL waitReadyAsync failed. Error:\n" << error.message() << std::endl;
48 ios.stop();
49 });
50 ios.run();
51 sd.release();
52 out << "Storage to namespace " << nsStr << " created." << std::endl;
53 return sdl;
54 }
55 catch (const shareddatalayer::Exception& error)
56 {
57 out << "Storage create failed: " << error.what() << std::endl;
58 }
59 return nullptr;
60 }
61
62 std::string getHosts(const DatabaseConfiguration::Addresses& databaseAddresses)
63 {
64 std::string hosts("");
65 for (auto i(databaseAddresses.begin()); i != databaseAddresses.end(); ++i)
66 hosts = hosts + i->getHost() + " ";
67 return hosts;
68 }
69
70 std::string getPorts(const DatabaseConfiguration::Addresses& databaseAddresses)
71 {
72 std::string ports("");
73 for (auto i(databaseAddresses.begin()); i != databaseAddresses.end(); ++i)
74 ports = ports + std::to_string(ntohs(i->getPort())) + " ";
75 return ports;
76 }
77
78 void PrintStaticConfiguration(std::ostream& out)
79 {
80 auto engine(std::make_shared<EngineImpl>());
81 DatabaseConfigurationImpl databaseConfigurationImpl;
82 ConfigurationReader configurationReader(createLogger(SDL_LOG_PREFIX));
83 configurationReader.readDatabaseConfiguration(databaseConfigurationImpl);
84 auto staticAddresses(databaseConfigurationImpl.getServerAddresses());
85 auto defaultAddresses(databaseConfigurationImpl.getDefaultServerAddresses());
86 auto staticDbType(databaseConfigurationImpl.getDbType());
87 if (!staticAddresses.empty())
88 {
89 out << "\nStatic Server Addresses:" << std::endl;
90 out << "Static Host: " << getHosts(staticAddresses) << std::endl;
91 out << "Static Port: " << getPorts(staticAddresses) << std::endl;
92 if (staticDbType == DatabaseConfiguration::DbType::REDIS_CLUSTER)
93 out << "Static DB type: redis-cluster" << std::endl;
94 else if (staticDbType == DatabaseConfiguration::DbType::REDIS_STANDALONE)
95 out << "Static DB type: redis-standalone" << std::endl;
96 else
97 out << "Static DB type not defined" << std::endl;
98 }
99 if (!defaultAddresses.empty() && staticAddresses.empty())
100 {
101 out << "\nDefault Server Addresses:" << std::endl;
102 out << "Default Host: " << getHosts(defaultAddresses) << std::endl;
103 out << "Default Port: " << getPorts(defaultAddresses) << std::endl;
104 }
105 const auto var(DB_HOST_ENV_VAR_NAME);
106 const auto conf(getenv(var));
107 if (conf != nullptr)
108 out << var << ": " << conf << std::endl;
109 }
110
111 void PrintDatabaseInfo(const DatabaseInfo& databaseInfo, std::ostream& out)
112 {
113 out << "Used database configuration (databaseInfo):" << std::endl;
114 out << "Host: " << getHosts(databaseInfo.hosts) << std::endl;
115 out << "Port: " << getPorts(databaseInfo.hosts) << std::endl;
116 switch (databaseInfo.type)
117 {
118 case DatabaseInfo::Type::SINGLE:
119 out << "Database type: SINGLE" << std::endl;
120 break;
121 case DatabaseInfo::Type::REDUNDANT:
122 out << "Database type: REDUNDANT" << std::endl;
123 break;
124 case DatabaseInfo::Type::CLUSTER:
125 out << "Database type: CLUSTER" << std::endl;
126 break;
127 }
128 switch (databaseInfo.discovery)
129 {
130 case DatabaseInfo::Discovery::HIREDIS:
131 out << "Discovery type:: HIREDIS" << std::endl;
132 PrintStaticConfiguration(out);
133 break;
134 }
135 }
136
137 [[noreturn]] void timeoutThread(const int& timeout)
138 {
139 std::this_thread::sleep_for(std::chrono::seconds(timeout));
140 std::cerr << "Storage create timeout, aborting after " << timeout << " seconds"<< std::endl;
141 PrintStaticConfiguration(std::cerr);
142 std::exit(EXIT_FAILURE);
143 }
144
145 void setTimeout(const int& timeout)
146 {
147 if (timeout)
148 {
149 std::thread t(timeoutThread, timeout);
150 t.detach();
151 }
152 }
153
154 int TestConnectivityCommand(std::ostream& out,
155 const boost::program_options::variables_map& map)
156 {
157 const auto ns(map["ns"].as<std::string>());
158 const auto timeout(map["timeout"].as<int>());
159 setTimeout(timeout);
160 auto sdl(createStorage(ns, out));
161 if (sdl != nullptr)
162 {
163 auto asyncStorageImpl(std::dynamic_pointer_cast<AsyncStorageImpl>(sdl));
164 if (asyncStorageImpl != nullptr)
165 {
166 AsyncStorage& operationalHandler(asyncStorageImpl->getOperationHandler(ns));
167 AsyncRedisStorage* redisStorage = dynamic_cast<AsyncRedisStorage*>(&operationalHandler);
168 if (redisStorage != nullptr)
169 {
170 auto databaseinfo (redisStorage->getDatabaseInfo());
171 PrintDatabaseInfo(databaseinfo, out);
172 }
173 else
174 {
175 // @TODO Improve output for the case if dummy backend is used.
176 out << "Cannot get AsyncRedisStorage." << std::endl;
177 return EXIT_FAILURE;
178 }
179 }
180 else
181 {
182 out << "Cannot get AsyncStorageImpl." << std::endl;
183 return EXIT_FAILURE;
184 }
185 }
186 return EXIT_SUCCESS;
187 }
188}
189
190AUTO_REGISTER_COMMAND(std::bind(TestConnectivityCommand, std::placeholders::_1, std::placeholders::_3),
191 "test-connectivity",
192 "Test SDL backend connectivity",
193 "Check that SDL database backend is available and show discovered redis host address and port",
194 CommandMap::Category::UTIL, 30020,
195 ("ns", boost::program_options::value<std::string>()->default_value("sdltoolns"), "Used namespace")
196 ("timeout", boost::program_options::value<int>()->default_value(0), "Timeout (in seconds), Default is no timeout"));