2012-03-09 04:45:27 +00:00
|
|
|
|
#include <iomanip>
|
|
|
|
|
|
2012-03-09 03:06:09 +00:00
|
|
|
|
#include <Poco/URI.h>
|
2012-03-09 03:56:12 +00:00
|
|
|
|
#include <Poco/NumberParser.h>
|
2012-03-09 03:06:09 +00:00
|
|
|
|
|
2012-03-09 04:45:27 +00:00
|
|
|
|
#include <statdaemons/Stopwatch.h>
|
|
|
|
|
|
2012-03-09 03:06:09 +00:00
|
|
|
|
#include <DB/Core/ErrorCodes.h>
|
|
|
|
|
|
|
|
|
|
#include <DB/IO/ReadBufferFromIStream.h>
|
2012-03-09 06:36:29 +00:00
|
|
|
|
#include <DB/IO/ReadBufferFromString.h>
|
|
|
|
|
#include <DB/IO/ConcatReadBuffer.h>
|
2012-03-09 15:46:52 +00:00
|
|
|
|
#include <DB/IO/CompressedReadBuffer.h>
|
|
|
|
|
#include <DB/IO/CompressedWriteBuffer.h>
|
2012-03-09 03:06:09 +00:00
|
|
|
|
#include <DB/IO/WriteBufferFromOStream.h>
|
|
|
|
|
#include <DB/IO/WriteBufferFromString.h>
|
|
|
|
|
#include <DB/IO/WriteHelpers.h>
|
|
|
|
|
|
2012-03-09 04:45:27 +00:00
|
|
|
|
#include <DB/DataStreams/IProfilingBlockInputStream.h>
|
|
|
|
|
|
2012-03-09 03:06:09 +00:00
|
|
|
|
#include <DB/Interpreters/executeQuery.h>
|
|
|
|
|
|
2012-03-09 15:46:52 +00:00
|
|
|
|
#include "HTTPHandler.h"
|
|
|
|
|
|
2012-03-09 03:06:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
2012-03-09 06:36:29 +00:00
|
|
|
|
/// Позволяет получать параметры URL даже если запрос POST.
|
2012-03-09 03:06:09 +00:00
|
|
|
|
struct HTMLForm : public Poco::Net::HTMLForm
|
|
|
|
|
{
|
|
|
|
|
HTMLForm(Poco::Net::HTTPRequest & request)
|
|
|
|
|
{
|
|
|
|
|
Poco::URI uri(request.getURI());
|
|
|
|
|
std::istringstream istr(uri.getRawQuery());
|
|
|
|
|
readUrl(istr);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2012-03-09 15:46:52 +00:00
|
|
|
|
void HTTPHandler::processQuery(Poco::Net::NameValueCollection & params, std::ostream & ostr, std::istream & istr)
|
2012-03-09 03:06:09 +00:00
|
|
|
|
{
|
|
|
|
|
BlockInputStreamPtr query_plan;
|
2012-03-09 06:36:29 +00:00
|
|
|
|
|
|
|
|
|
/** Часть запроса может быть передана в параметре query, а часть - POST-ом.
|
|
|
|
|
* В таком случае, считается, что запрос - параметр query, затем перевод строки, а затем - данные POST-а.
|
|
|
|
|
*/
|
|
|
|
|
std::string query_param = params.get("query", "");
|
|
|
|
|
if (!query_param.empty())
|
|
|
|
|
query_param += '\n';
|
|
|
|
|
|
|
|
|
|
ReadBufferFromString in_param(query_param);
|
2012-03-09 15:46:52 +00:00
|
|
|
|
SharedPtr<ReadBuffer> in_post = new ReadBufferFromIStream(istr);
|
|
|
|
|
SharedPtr<ReadBuffer> in_post_maybe_compressed;
|
|
|
|
|
|
|
|
|
|
/// Если указано decompress, то будем разжимать то, что передано POST-ом.
|
|
|
|
|
if (0 != Poco::NumberParser::parseUnsigned(params.get("decompress", "0")))
|
|
|
|
|
in_post_maybe_compressed = new CompressedReadBuffer(*in_post);
|
|
|
|
|
else
|
|
|
|
|
in_post_maybe_compressed = in_post;
|
|
|
|
|
|
|
|
|
|
ConcatReadBuffer in(in_param, *in_post_maybe_compressed);
|
2012-03-09 06:36:29 +00:00
|
|
|
|
|
2012-03-09 15:46:52 +00:00
|
|
|
|
/// Если указано compress, то будем сжимать результат.
|
|
|
|
|
SharedPtr<WriteBuffer> out = new WriteBufferFromOStream(ostr);
|
|
|
|
|
SharedPtr<WriteBuffer> out_maybe_compressed;
|
|
|
|
|
|
|
|
|
|
if (0 != Poco::NumberParser::parseUnsigned(params.get("compress", "0")))
|
|
|
|
|
out_maybe_compressed = new CompressedWriteBuffer(*out);
|
|
|
|
|
else
|
|
|
|
|
out_maybe_compressed = out;
|
|
|
|
|
|
2012-03-09 03:06:09 +00:00
|
|
|
|
Context context = server.global_context;
|
|
|
|
|
|
2012-03-09 03:56:12 +00:00
|
|
|
|
/// Некоторые настройки могут быть переопределены в запросе.
|
|
|
|
|
if (params.has("asynchronous"))
|
|
|
|
|
context.settings.asynchronous = 0 != Poco::NumberParser::parseUnsigned(params.get("asynchronous"));
|
|
|
|
|
if (params.has("max_block_size"))
|
|
|
|
|
context.settings.max_block_size = Poco::NumberParser::parseUnsigned(params.get("max_block_size"));
|
|
|
|
|
if (params.has("max_query_size"))
|
|
|
|
|
context.settings.max_query_size = Poco::NumberParser::parseUnsigned(params.get("max_query_size"));
|
|
|
|
|
if (params.has("max_threads"))
|
|
|
|
|
context.settings.max_threads = Poco::NumberParser::parseUnsigned(params.get("max_threads"));
|
|
|
|
|
|
2012-03-09 04:45:27 +00:00
|
|
|
|
Stopwatch watch;
|
2012-03-09 15:46:52 +00:00
|
|
|
|
executeQuery(in, *out_maybe_compressed, context, query_plan);
|
2012-03-09 04:45:27 +00:00
|
|
|
|
watch.stop();
|
2012-03-09 03:06:09 +00:00
|
|
|
|
|
|
|
|
|
if (query_plan)
|
|
|
|
|
{
|
|
|
|
|
std::stringstream log_str;
|
|
|
|
|
log_str << "Query plan:\n";
|
|
|
|
|
query_plan->dumpTree(log_str);
|
|
|
|
|
LOG_DEBUG(log, log_str.str());
|
2012-03-09 04:45:27 +00:00
|
|
|
|
|
|
|
|
|
/// Выведем информацию о том, сколько считано строк и байт.
|
|
|
|
|
BlockInputStreams leaves = query_plan->getLeaves();
|
|
|
|
|
size_t rows = 0;
|
|
|
|
|
size_t bytes = 0;
|
|
|
|
|
|
|
|
|
|
for (BlockInputStreams::const_iterator it = leaves.begin(); it != leaves.end(); ++it)
|
|
|
|
|
{
|
|
|
|
|
if (const IProfilingBlockInputStream * profiling = dynamic_cast<const IProfilingBlockInputStream *>(&**it))
|
|
|
|
|
{
|
|
|
|
|
const BlockStreamProfileInfo & info = profiling->getInfo();
|
|
|
|
|
rows += info.rows;
|
|
|
|
|
bytes += info.bytes;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (rows != 0)
|
|
|
|
|
{
|
|
|
|
|
LOG_INFO(log, std::fixed << std::setprecision(3)
|
|
|
|
|
<< "Read " << rows << " rows, " << bytes / 1048576.0 << " MB in " << watch.elapsedSeconds() << " sec., "
|
|
|
|
|
<< static_cast<size_t>(rows / watch.elapsedSeconds()) << " rows/sec., " << bytes / 1048576.0 / watch.elapsedSeconds() << " MB/sec.");
|
|
|
|
|
}
|
2012-03-09 03:06:09 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-03-09 15:46:52 +00:00
|
|
|
|
void HTTPHandler::handleRequest(Poco::Net::HTTPServerRequest & request, Poco::Net::HTTPServerResponse & response)
|
2012-03-09 03:06:09 +00:00
|
|
|
|
{
|
|
|
|
|
std::ostream & ostr = response.send();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
LOG_TRACE(log, "Request URI: " << request.getURI());
|
|
|
|
|
|
|
|
|
|
HTMLForm params(request);
|
|
|
|
|
std::istream & istr = request.stream();
|
|
|
|
|
processQuery(params, ostr, istr);
|
|
|
|
|
|
|
|
|
|
LOG_INFO(log, "Done processing query");
|
|
|
|
|
}
|
|
|
|
|
catch (Poco::Exception & e)
|
|
|
|
|
{
|
|
|
|
|
std::stringstream s;
|
|
|
|
|
s << "Code: " << ErrorCodes::POCO_EXCEPTION << ", e.code() = " << e.code()
|
|
|
|
|
<< ", e.message() = " << e.message() << ", e.what() = " << e.what();
|
|
|
|
|
ostr << s.str() << std::endl;
|
|
|
|
|
LOG_ERROR(log, s.str());
|
|
|
|
|
}
|
|
|
|
|
catch (std::exception & e)
|
|
|
|
|
{
|
|
|
|
|
std::stringstream s;
|
|
|
|
|
s << "Code: " << ErrorCodes::STD_EXCEPTION << ". " << e.what();
|
|
|
|
|
ostr << s.str() << std::endl;
|
|
|
|
|
LOG_ERROR(log, s.str());
|
|
|
|
|
}
|
|
|
|
|
catch (...)
|
|
|
|
|
{
|
|
|
|
|
std::stringstream s;
|
|
|
|
|
s << "Code: " << ErrorCodes::UNKNOWN_EXCEPTION << ". Unknown exception.";
|
|
|
|
|
ostr << s.str() << std::endl;
|
|
|
|
|
LOG_ERROR(log, s.str());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|