Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed error detection check when sending data via socket #404

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions clickhouse/base/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,11 +457,12 @@ size_t SocketOutput::DoWrite(const void* data, size_t len) {
static const int flags = 0;
#endif

if (::send(s_, (const char*)data, (int)len, flags) != (int)len) {
const ssize_t ret = ::send(s_, (const char*)data, (int)len, flags);
Copy link
Collaborator

@Enmk Enmk Dec 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So is it possible that less data would be sent than requested? If yes, what will happen?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've had this happen (it happens very rarely, so I can't give an example of the sequence of actions that would lead to it). I don't know the exact reasons, since this is not typical for blocking sockets. Perhaps this happens when the server is slow in reading data from the socket compared to writing to the data on the client. I also found an increase in the frequency of such situations when using the 'strace' utility (but perhaps this was due to something else).
If, after the size has returned smaller, resend what was not sent, then all the data will be written to the table.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, but it seems like resent is not implemented

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is implemented in this place:

void WireFormat::WriteAll(OutputStream& output, const void* buf, size_t len) {
    const size_t original_len = len;
    const uint8_t* p = static_cast<const uint8_t*>(buf);

    size_t written_previously = 1; // 1 to execute loop at least once
    while (len > 0 && written_previously) {
        written_previously = output.Write(p, len);

        p += written_previously;
        len -= written_previously;
    }

    if (len) {
        throw ProtocolError("Failed to write " + std::to_string(original_len)
                + " bytes, only written " + std::to_string(original_len - len));
    }
}

As I understand it, all data is sent through this call.

if (ret < 0) {
throw std::system_error(getSocketErrorCode(), getErrorCategory(), "fail to send " + std::to_string(len) + " bytes of data");
}

return len;
return (size_t)ret;
}


Expand Down
Loading