1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
#include <unistd.h>
#include "threadname.hh"
#include "remote_logger.hh"
#include <sys/uio.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef PDNS_CONFIG_ARGS
#include "logger.hh"
#define WE_ARE_RECURSOR
#else
#include "dolog.hh"
#endif
bool CircularWriteBuffer::hasRoomFor(const std::string& str) const
{
if (d_buffer.size() + 2 + str.size() > d_buffer.capacity()) {
return false;
}
return true;
}
bool CircularWriteBuffer::write(const std::string& str)
{
if (!hasRoomFor(str)) {
return false;
}
uint16_t len = htons(str.size());
const char* ptr = reinterpret_cast<const char*>(&len);
d_buffer.insert(d_buffer.end(), ptr, ptr + 2);
d_buffer.insert(d_buffer.end(), str.begin(), str.end());
return true;
}
bool CircularWriteBuffer::flush(int fd)
{
if (d_buffer.empty()) {
// not optional, we report EOF otherwise
return false;
}
auto arr1 = d_buffer.array_one();
auto arr2 = d_buffer.array_two();
struct iovec iov[2];
int pos = 0;
size_t total = 0;
for(const auto& arr : {arr1, arr2}) {
if(arr.second) {
iov[pos].iov_base = arr.first;
iov[pos].iov_len = arr.second;
total += arr.second;
++pos;
}
}
ssize_t res = 0;
do {
res = writev(fd, iov, pos);
if (res < 0) {
if (errno == EINTR) {
continue;
}
if (errno == EAGAIN || errno == EWOULDBLOCK) {
return false;
}
/* we can't be sure we haven't sent a partial message,
and we don't want to send the remaining part after reconnecting */
d_buffer.clear();
throw std::runtime_error("Couldn't flush a thing: " + stringerror());
}
else if (!res) {
/* we can't be sure we haven't sent a partial message,
and we don't want to send the remaining part after reconnecting */
d_buffer.clear();
throw std::runtime_error("EOF");
}
}
while (res < 0);
// cout<<"Flushed "<<res<<" bytes out of " << total <<endl;
if (static_cast<size_t>(res) == d_buffer.size()) {
d_buffer.clear();
}
else {
while (res--) {
d_buffer.pop_front();
}
}
return true;
}
RemoteLogger::RemoteLogger(const ComboAddress& remote, uint16_t timeout, uint64_t maxQueuedBytes, uint8_t reconnectWaitTime, bool asyncConnect): d_writer(maxQueuedBytes), d_remote(remote), d_timeout(timeout), d_reconnectWaitTime(reconnectWaitTime), d_asyncConnect(asyncConnect)
{
if (!d_asyncConnect) {
reconnect();
}
d_thread = std::thread(&RemoteLogger::maintenanceThread, this);
}
bool RemoteLogger::reconnect()
{
try {
auto newSock = make_unique<Socket>(d_remote.sin4.sin_family, SOCK_STREAM, 0);
newSock->setNonBlocking();
newSock->connect(d_remote, d_timeout);
{
/* we are now successfully connected, time to take the lock and update the
socket */
std::unique_lock<std::mutex> lock(d_mutex);
d_socket = std::move(newSock);
}
}
catch (const std::exception& e) {
#ifdef WE_ARE_RECURSOR
g_log<<Logger::Warning<<"Error connecting to remote logger "<<d_remote.toStringWithPort()<<": "<<e.what()<<std::endl;
#else
warnlog("Error connecting to remote logger %s: %s", d_remote.toStringWithPort(), e.what());
#endif
return false;
}
return true;
}
void RemoteLogger::queueData(const std::string& data)
{
std::unique_lock<std::mutex> lock(d_mutex);
if (!d_writer.hasRoomFor(data)) {
/* not connected, queue is full, just drop */
if (!d_socket) {
++d_drops;
return;
}
try {
/* we try to flush some data */
if (!d_writer.flush(d_socket->getHandle())) {
/* but failed, let's just drop */
++d_drops;
return;
}
/* see if we freed enough data */
if (!d_writer.hasRoomFor(data)) {
/* we didn't */
++d_drops;
return;
}
}
catch(const std::exception& e) {
// cout << "Got exception writing: "<<e.what()<<endl;
++d_drops;
d_socket.reset();
return;
}
}
d_writer.write(data);
++d_queued;
}
void RemoteLogger::maintenanceThread()
try
{
#ifdef WE_ARE_RECURSOR
string threadName = "pdns-r/remLog";
#else
string threadName = "dnsdist/remLog";
#endif
setThreadName(threadName);
for (;;) {
if (d_exiting) {
break;
}
bool connected = true;
if (d_socket == nullptr) {
// if it was unset, it will remain so, we are the only ones setting it!
connected = reconnect();
}
/* we will just go to sleep if the reconnection just failed */
if (connected) {
try {
/* we don't want to take the lock while trying to reconnect */
std::unique_lock<std::mutex> lock(d_mutex);
if (d_socket) { // check if it is set
/* if flush() returns false, it means that we couldn't flush anything yet
either because there is nothing to flush, or because the outgoing TCP
buffer is full. That's fine by us */
d_writer.flush(d_socket->getHandle());
}
else {
connected = false;
}
}
catch(const std::exception& e) {
d_socket.reset();
connected = false;
}
if (!connected) {
/* let's try to reconnect right away, we are about to sleep anyway */
reconnect();
}
}
sleep(d_reconnectWaitTime);
}
}
catch(const std::exception& e)
{
cerr << "Remote Logger's maintenance thead died on: " << e.what() << endl;
}
catch(...) {
cerr << "Remote Logger's maintenance thead died on unknown exception" << endl;
}
RemoteLogger::~RemoteLogger()
{
d_exiting = true;
d_thread.join();
}
|