Skip to content

Commit

Permalink
Merge pull request ClickHouse#63401 from ClickHouse/remove-copy-when-…
Browse files Browse the repository at this point in the history
…writing-to-cache

Remove copy from writing to fs cache
  • Loading branch information
alexey-milovidov authored May 7, 2024
2 parents cef4f74 + a517865 commit a20c626
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Disks/IO/CachedOnDiskWriteBufferFromFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ FileSegmentRangeWriter::FileSegmentRangeWriter(
{
}

bool FileSegmentRangeWriter::write(const char * data, size_t size, size_t offset, FileSegmentKind segment_kind)
bool FileSegmentRangeWriter::write(char * data, size_t size, size_t offset, FileSegmentKind segment_kind)
{
if (finalized)
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/Disks/IO/CachedOnDiskWriteBufferFromFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class FileSegmentRangeWriter
* Write a range of file segments. Allocate file segment of `max_file_segment_size` and write to
* it until it is full and then allocate next file segment.
*/
bool write(const char * data, size_t size, size_t offset, FileSegmentKind segment_kind);
bool write(char * data, size_t size, size_t offset, FileSegmentKind segment_kind);

void finalize();

Expand Down
14 changes: 9 additions & 5 deletions src/Interpreters/Cache/FileSegment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ void FileSegment::setRemoteFileReader(RemoteFileReaderPtr remote_file_reader_)
remote_file_reader = remote_file_reader_;
}

void FileSegment::write(const char * from, size_t size, size_t offset)
void FileSegment::write(char * from, size_t size, size_t offset)
{
ProfileEventTimeIncrement<Microseconds> watch(ProfileEvents::FileSegmentWriteMicroseconds);

Expand Down Expand Up @@ -389,16 +389,20 @@ void FileSegment::write(const char * from, size_t size, size_t offset)

try
{
if (!cache_writer)
cache_writer = std::make_unique<WriteBufferFromFile>(file_segment_path);

#ifdef ABORT_ON_LOGICAL_ERROR
/// This mutex is only needed to have a valid assertion in assertCacheCorrectness(),
/// which is only executed in debug/sanitizer builds (under ABORT_ON_LOGICAL_ERROR).
std::lock_guard lock(write_mutex);
#endif

cache_writer->write(from, size);
if (!cache_writer)
cache_writer = std::make_unique<WriteBufferFromFile>(file_segment_path, /* buf_size */0);

/// Size is equal to offset as offset for write buffer points to data end.
cache_writer->set(from, size, /* offset */size);
/// Reset the buffer when finished.
SCOPE_EXIT({ cache_writer->set(nullptr, 0); });
/// Flush the buffer.
cache_writer->next();

downloaded_size += size;
Expand Down
2 changes: 1 addition & 1 deletion src/Interpreters/Cache/FileSegment.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ friend class FileCache; /// Because of reserved_size in tryReserve().
bool reserve(size_t size_to_reserve, size_t lock_wait_timeout_milliseconds, FileCacheReserveStat * reserve_stat = nullptr);

/// Write data into reserved space.
void write(const char * from, size_t size, size_t offset);
void write(char * from, size_t size, size_t offset);

// Invariant: if state() != DOWNLOADING and remote file reader is present, the reader's
// available() == 0, and getFileOffsetOfBufferEnd() == our getCurrentWriteOffset().
Expand Down

0 comments on commit a20c626

Please sign in to comment.