Exemple #1
0
void QuotaForInterval::check(
    size_t max_amount, size_t used_amount,
    const String & quota_name, const String & user_name, const char * resource_name)
{
    if (max_amount && used_amount > max_amount)
    {
        std::stringstream message;
        message << "Quota for user '" << user_name << "' for ";

        if (duration == 3600)
            message << "1 hour";
        else if (duration == 60)
            message << "1 minute";
        else if (duration % 3600 == 0)
            message << (duration / 3600) << " hours";
        else if (duration % 60 == 0)
            message << (duration / 60) << " minutes";
        else
            message << duration << " seconds";

        message << " has been exceeded. "
            << resource_name << ": " << used_amount << ", max: " << max_amount << ". "
            << "Interval will end at " << LocalDateTime(rounded_time.load(std::memory_order_relaxed) + duration) << ". "
            << "Name of quota template: '" << quota_name << "'.";

        throw Exception(message.str(), ErrorCodes::QUOTA_EXPIRED);
    }
}
Exemple #2
0
LocalDateTime LocalDateTime::operator - (const Timespan& span) const
{
	// First calculate the adjusted UTC time, then calculate the
	// locally adjusted time by constructing a new LocalDateTime.
	DateTime tmp(utcTime(), -span.totalMicroseconds());
	return LocalDateTime(tmp);
}
Exemple #3
0
StatusFile::StatusFile(const std::string & path_)
	: path(path_)
{
	/// Если файл уже существует. NOTE Незначительный race condition.
	if (Poco::File(path).exists())
	{
		std::string contents;
		{
			ReadBufferFromFile in(path, 1024);
			LimitReadBuffer limit_in(in, 1024);
			WriteBufferFromString out(contents);
			copyData(limit_in, out);
		}

		if (!contents.empty())
			LOG_INFO(&Logger::get("StatusFile"), "Status file " << path << " already exists - unclean restart. Contents:\n" << contents);
		else
			LOG_INFO(&Logger::get("StatusFile"), "Status file " << path << " already exists and is empty - probably unclean hardware restart.");
	}

	fd = open(path.c_str(), O_WRONLY | O_CREAT, 0666);

	if (-1 == fd)
		throwFromErrno("Cannot open file " + path);

	try
	{
		int flock_ret = flock(fd, LOCK_EX | LOCK_NB);
		if (-1 == flock_ret)
		{
			if (errno == EWOULDBLOCK)
				throw Exception("Cannot lock file " + path + ". Another server instance in same directory is already running.");
			else
				throwFromErrno("Cannot lock file " + path);
		}

		if (0 != ftruncate(fd, 0))
			throwFromErrno("Cannot ftruncate " + path);

		if (0 != lseek(fd, 0, SEEK_SET))
			throwFromErrno("Cannot lseek " + path);

		/// Записываем в файл информацию о текущем экземпляре сервера.
		{
			WriteBufferFromFileDescriptor out(fd, 1024);
			out
				<< "PID: " << getpid() << "\n"
				<< "Started at: " << LocalDateTime(time(0)) << "\n"
				<< "Revision: " << ClickHouseRevision::get() << "\n";
		}
	}
	catch (...)
	{
		close(fd);
		throw;
	}
}
Exemple #4
0
String QuotaForInterval::toString() const
{
    std::stringstream res;

    auto loaded_rounded_time = rounded_time.load(std::memory_order_relaxed);

    res << std::fixed << std::setprecision(3)
        << "Interval:       " << LocalDateTime(loaded_rounded_time) << " - " << LocalDateTime(loaded_rounded_time + duration) << ".\n"
        << "Queries:        " << used.queries         << ".\n"
        << "Errors:         " << used.errors         << ".\n"
        << "Result rows:    " << used.result_rows     << ".\n"
        << "Result bytes:   " << used.result_bytes     << ".\n"
        << "Read rows:      " << used.read_rows     << ".\n"
        << "Read bytes:     " << used.read_bytes     << ".\n"
        << "Execution time: " << used.execution_time_usec / 1000000.0 << " sec.\n";

    return res.str();
}
Exemple #5
0
void SMTPChannel::log(const Message& msg)
{
	try
	{
		MailMessage message;
		message.setSender(_sender);
		message.addRecipient(MailRecipient(MailRecipient::PRIMARY_RECIPIENT, _recipient));
		message.setSubject("Log Message from " + _sender);
		std::stringstream content;
		content << "Log Message\r\n"
			<< "===========\r\n\r\n"
			<< "Host: " << Environment::nodeName() << "\r\n"
			<< "Logger: " << msg.getSource() << "\r\n";

		if (_local)
		{
			DateTime dt(msg.getTime());
			content	<< "Timestamp: " << DateTimeFormatter::format(LocalDateTime(dt), DateTimeFormat::RFC822_FORMAT) << "\r\n";
		}
		else
			content	<< "Timestamp: " << DateTimeFormatter::format(msg.getTime(), DateTimeFormat::RFC822_FORMAT) << "\r\n";

		content	<< "Priority: " << NumberFormatter::format(msg.getPriority()) << "\r\n"
			<< "Process ID: " << NumberFormatter::format(msg.getPid()) << "\r\n"
			<< "Thread: " << msg.getThread() << " (ID: " << msg.getTid() << ")\r\n"
			<< "Message text: " << msg.getText() << "\r\n\r\n";

		message.addContent(new StringPartSource(content.str()));
	
		if (!_attachment.empty())
		{
			{
				Poco::FileInputStream fis(_attachment, std::ios::in | std::ios::binary | std::ios::ate);
				if (fis.good())
				{
					int size = fis.tellg();
					char* pMem = new char [size];
					fis.seekg(std::ios::beg);
					fis.read(pMem, size);
					message.addAttachment(_attachment, new StringPartSource(std::string(pMem, size), _type, _attachment));
					delete [] pMem;
				}
			}
			if (_delete) File(_attachment).remove();
		}

		SMTPClientSession session(_mailHost);
		session.login();
		session.sendMessage(message);
		session.close();
	} 
	catch (Exception&) 
	{ 
		if (_throw) throw; 
	}
}
Exemple #6
0
int main(int argc, char ** argv)
{
	DayNum_t today = DateLUT::instance().toDayNum(time(0));

	for (DayNum_t date = today; DayNum_t(date + 10) > today; --date)
	{
		std::string name = DB::ActiveDataPartSet::getPartName(date, date, 0, 0, 0);
		std::cerr << name << '\n';

		time_t time = DateLUT::instance().YYYYMMDDToDate(DB::parse<UInt32>(name));
		std::cerr << LocalDateTime(time) << '\n';
	}

	return 0;
}
Exemple #7
0
int main(int, char **)
{
    DayNum_t today = DateLUT::instance().toDayNum(time(nullptr));

    for (DayNum_t date = today; DayNum_t(date + 10) > today; --date)
    {
        DB::MergeTreePartInfo part_info("partition", 0, 0, 0);
        std::string name = part_info.getPartNameV0(date, date);
        std::cerr << name << '\n';

        time_t time = DateLUT::instance().YYYYMMDDToDate(DB::parse<UInt32>(name));
        std::cerr << LocalDateTime(time) << '\n';
    }

    return 0;
}
Exemple #8
0
void RTMFPServer::handleShellCommand(RTMFPReceiving * received) {
	if (!received) return;
	std::string tmp;
	std::string resp = "Cumulus server, build time: " __DATE__ " " __TIME__ ", start time: ";
	resp += _startDatetimeStr;
	resp += ", cur time: " + DateTimeFormatter::format(LocalDateTime(),"%b %d %Y %H:%M:%S");
	resp += "\n";

	//std::string req(received->bufdata());
	if (std::strcmp(received->bufdata(), "status") == 0) {
#if 0
		resp += "sessions_n: " + Poco::NumberFormatter::format(_sessions.count()) 
			+ " sessions_n_peak: " + Poco::NumberFormatter::format(_sessions.peakCount) 
			+ " task_handle_peak_qsize: " + Poco::NumberFormatter::format(peak_qsize()) + "\n"
			+ "rcvp: " + Poco::NumberFormatter::format(rcvpCnt > 0 ? (rcvpTm / rcvpCnt) : 0)  
			+ " peak_rcvp: " + Poco::NumberFormatter::format(peakRcvp)
			+ " psnd: " + Poco::NumberFormatter::format(psndCnt > 0 ? (psndTm / psndCnt) : 0)
			+ " peak_psnd: " + Poco::NumberFormatter::format(peakPsnd)
			+ "\n";
#endif
		poolThreads.status_string(tmp);
		resp += tmp; 
		sockets.status_string(tmp);
		resp += tmp;
		status_string(tmp);
		resp += tmp;
	}
	else if (std::strcmp(received->bufdata(), "help") == 0) {
		resp += "Commands: help status quit\n";
	}
	else if (std::strcmp(received->bufdata(), "quit") == 0) {
		//resp += "Good bye\n";	
		resp = "";	
	}
	else {
		resp += "Please type in `help` for details.\n";
	}

	received->socket.sendTo(resp.c_str(), resp.length(), received->address, 0); 
}
Exemple #9
0
void RTMFPServer::run() {

	try {
		_startDatetimeStr = DateTimeFormatter::format(LocalDateTime(), "%b %d %Y %H:%M:%S");
		_pSocket->bind(SocketAddress("0.0.0.0",_port));
		NOTE("RTMFP server sendbufsize %d recvbufsize %d recvtmo %d sendtmo %d", 
				_pSocket->getSendBufferSize(),
				_pSocket->getReceiveBufferSize(),
				_pSocket->getReceiveTimeout().milliseconds(),
				_pSocket->getSendTimeout().milliseconds()
				);

		sockets.add(*_pSocket,*this);  //_mainSockets
		NOTE("RTMFP server starts on %u port",_port);

		if(_shellPort > 0) { 
			_shellSocket.bind(SocketAddress("0.0.0.0", _shellPort));
			sockets.add(_shellSocket, *this);
			NOTE("RTMFP server shell command portal on %u prot", _shellPort);
		}

		onStart();

		RTMFPManager manager(*this);
		bool terminate=false;
		while(!terminate)
			handle(terminate);

	} catch(Exception& ex) {
		FATAL("RTMFPServer, %s",ex.displayText().c_str());
	} catch (exception& ex) {
		FATAL("RTMFPServer, %s",ex.what());
	} catch (...) {
		FATAL("RTMFPServer, unknown error");
	}

	sockets.remove(*_pSocket); // _mainSockets

	// terminate handle
	terminate();
	
	// clean sessions, and send died message if need
	_handshake.clear();
	_sessions.clear();

	// stop receiving and sending engine (it waits the end of sending last session messages)
	poolThreads.clear();

	// close UDP socket
	_pSocket->close();

	// close shell command port 
	if(_shellPort > 0) { 
		_shellSocket.close();
	}

	sockets.clear();
	//_mainSockets.clear();
	_port=0;
	onStop();

	if(_pCirrus) {
		delete _pCirrus;
		_pCirrus = NULL;
	}

	if(_pSocket) {
		delete _pSocket;
		_pSocket = NULL;
	}
	
	NOTE("RTMFP server stops");
}