Skip to content

sdk/python: write in 4 MiB chunks to avoid the native 1 GiB buffer limit - #7863

Merged
jiefenghuang merged 5 commits into
juicedata:mainfrom
adenzhou1350:codex/fix-python-large-write
Nov 19, 2026
Merged

jiefenghuang merged 5 commits into
juicedata:mainfrom
adenzhou1350:codex/fix-python-large-write

Conversation

@adenzhou1350

@adenzhou1350 adenzhou1350 commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Problem

A Python SDK write() larger than 1 GiB aborts the process: libjfs.toBuf views the buffer as *[1<<30]byte, so jfs_pwrite panics with slice bounds out of range even though its count is int32. Writes over 2 GiB also overflow the c_int32 count.

Change

  • _File.write() passes the buffer to jfs_pwrite in 4 MiB chunks (same size as read()/readinto()), advancing the pointer without copying.
  • If any chunk fails, raise OSError (no short write); length covers the chunks already written while the position is unchanged, so retrying the same data is idempotent.
  • Add a regression test writing 1 GiB + 1 byte with and without buffering, verified by size and SHA256.

Validation

Linux, Python 3.12, real libjfs, SQLite meta + local file storage:

  • The new test panics on the base commit and passes with this change.
  • Full pysdk_test.py: 64 tests; only WarmupTests.test_basic_warmup fails, and it fails identically on the base commit (local cache environment).
  • Peak RSS writing 1 GiB + 1 byte (1 GiB is the payload itself):
1 GiB chunks 4 MiB chunks
unbuffered 2.13G 1.30G
buffered 3.21G 2.36G

Wall time also dropped (test: ~36s → ~23s). The remaining ~1G with buffering comes from the pre-existing memoryview.tobytes() copy in write(), to be handled separately.

Signed-off-by: Xucheng Zhou <aden1350@outlook.com>
- Split writes into 4 MiB native calls instead of 1 GiB to bound libjfs
  buffer memory (1 GiB + 1 byte test: peak RSS 3.2G -> 2.4G).
- If a later chunk fails, return the bytes already written instead of
  raising, matching short-write semantics.
- Build the test payload in memory instead of via a temp file.
- Drop the docs note and unrelated whitespace changes.
@zxh326 zxh326 changed the title sdk/python: split writes at the native buffer limit sdk/python: write in 4 MiB chunks to avoid the native 1 GiB buffer limit Nov 18, 2026

@zxh326 zxh326 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

/LGTM

cc @jiefenghuang

@zxh326
zxh326 removed the request for review from CaitinChen November 18, 2026 08:49
Comment thread sdk/python/juicefs/juicefs/juicefs.py Resolved
@codecov

codecov Bot commented Nov 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 21.67%. Comparing base (a9cd03b) to head (0949459).
⚠️ Report is 19 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff             @@
##             main    #7863       +/-   ##
===========================================
- Coverage   52.08%   21.67%   -30.42%     
===========================================
  Files          34       33        -1     
  Lines       27825    27136      -689     
===========================================
- Hits        14493     5881     -8612     
- Misses      11539    20656     +9117     
+ Partials     1793      599     -1194     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Raise EIO if jfs_pwrite makes no progress instead of looping forever,
advance the position by the bytes already written when a chunk fails,
and build the loop-invariant ctypes arguments once.
Replace the 1 GiB + 1 byte test with an 8 MiB + 1 byte write that spies on
jfs_pwrite and checks every native call stays within 4 MiB, cutting the
test from ~23s and >2 GiB RSS to about a second. Drop the toBuf comment.
@jiefenghuang
jiefenghuang merged commit a7c74f2 into juicedata:main Nov 19, 2026
29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants