aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/IO/WriteBufferFromFileDescriptorDiscardOnFailure.cpp
blob: 69be24f0fae84e062a6af2ff699b47687f44e98c (plain) (blame)
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
#include <IO/WriteBufferFromFileDescriptorDiscardOnFailure.h>

namespace ProfileEvents
{
    extern const Event CannotWriteToWriteBufferDiscard;
}

namespace DB
{

void WriteBufferFromFileDescriptorDiscardOnFailure::nextImpl()
{
    size_t bytes_written = 0;
    while (bytes_written != offset())
    {
        ssize_t res = ::write(fd, working_buffer.begin() + bytes_written, offset() - bytes_written);

        if ((-1 == res || 0 == res) && errno != EINTR)
        {
            /// Never send this profile event to trace log because it may cause another
            /// write into the same fd and likely will trigger the same error
            /// and will lead to infinite recursion.
            ProfileEvents::incrementNoTrace(ProfileEvents::CannotWriteToWriteBufferDiscard);
            break;  /// Discard
        }

        if (res > 0)
            bytes_written += res;
    }
}

}