Skip to content

[Bug]: JSONLINE LOAD with PARALLEL 'true' never shards large files and the failed probe makes it slower than serial #31432

Open

Description

@iamlinjunhong

Environment

  • MatrixOne commit 26e3289dd6 (detached), built from this workspace, single-node ./mo-service -launch (CN+TN+Log in one process), default etc/launch-derived config with only port offsets changed.
  • macOS 15, arm64 (Apple Silicon, 10 cores), local files on the same SSD, mysql client 9.x with --default-character-set=utf8mb4.
  • Server default sql_mode includes STRICT_TRANS_TABLES; also reproduced with set sql_mode='' (see below).

Actual Behavior

For a single JSONLINE file large enough that the parallel shard calculator returns mcpu >= 2 (file >= 256 MiB, floor(size/128MiB) shards), LOAD DATA ... parallel 'true':

  1. Never shards the file. The compile-stage log shows one single range [offset:0 offset:-1] although mcpu is 2:
compileExternScanParallelReadWrite, len of cnList is 1, cn addr is 127.0.0.1:38400, mcpu is 2, filepath is .../jl_300m.jl, file size is 318064140
compileExternScanParallelReadWrite, [offset:0 offset:-1 ]
  1. The failed boundary probe still reads the whole tail. The probe opens the file at the would-be split offset guess and parses forward until EOF looking for a "legal" record; for JSONLINE it never finds one, so it reads _size/2 bytes for nothing before falling back to a single shard:
compileExternScan cost 1.456616167s   # 303MiB jsonline, probe tail ~150MB
compileExternScan cost 3.117702791s   # 324MiB single-column jsonline, probe tail ~162MB

For comparison the same compile stage for a 330MB CSV shard split costs 0.17ms.

  1. Net effect: parallel is slower than plain serial on the identical file, because the probe adds seconds and buys zero parallelism:
load (same 303MiB jl file, same table) wall time
serial (no PARALLEL) 8.1 / 8.4 / 11.2 s
parallel 'true' 10.1 / 10.8 / 12.2 / 12.4 s

And the identical-size CSV workload shards fine and benefits: CSV 330MB serial 2.4s -> parallel 'true' 4.9s total while producing two balanced ranges [offset:0 offset:173207345 offset:173207345 offset:-1] and importing 24M rows correctly.

Results were identical in a set sql_mode='' (non-strict) session: still [offset:0 offset:-1], so strict mode is not the only trigger; both the strict and non-strict probe paths fail for JSONLINE.

Imported data is correct - this is strictly a parallelism/performance defect (silent opt-out plus wasted tail reads), not corruption.

Expected Behavior

parallel 'true' on a >=256MiB JSONLINE file should split the file at line boundaries into floor(size/128MiB) ranges, exactly as it does for CSV on the same code path (ReadFileOffset). JSONLINE records are one JSON object per physical line (the parser is configured with FieldsTerminatedBy='\t' precisely so each line stays a single token; lines are delimited by \n and contain no quoting/newline ambiguity), so byte-offset splitting is unambiguous for this format. The officially maintained BVT already exercises load data infile {'format'='jsonline','jsondata'='object'} ... parallel 'true' (test/distributed/cases/load_data/issue_29768_jsonline_null.sql), so this option combination is supported and should not be silently downgraded. At minimum, the fallback must not pay for a full-tail probe scan that it then discards, and must not be silent.

Steps to Reproduce

#!/bin/bash
# MatrixOne JSONLINE parallel sharding degradation - minimal reproduction.
# Requires a running single-node mo-service (default ports here; adjust) and python3.
# NOTE: pass an explicit --default-character-set=utf8mb4 to the mysql client.
set -u
MO_PORT=6001   # adjust to your mo-service frontend port
MO=(mysql --connect-timeout=10 -h 127.0.0.1 -P "$MO_PORT" -uroot -p111 --default-character-set=utf8mb4 --protocol=tcp)

# 1) 303MiB JSONLINE file, 8M records, 3 columns (file must be >= 2*128MiB so the
#    shard calculator returns mcpu=2; smaller files never probe at all)
python3 - <<'EOF'
with open('/tmp/jl_300m.jl','w') as f:
    for i in range(8_000_000):
        f.write('{"id": %d, "v": "s%d", "pad": "%d"}\n' % (i, i%97, i%10))
# 318064140 bytes
EOF

"${MO[@]}" -e "drop database if exists jlrepro; create database jlrepro"
"${MO[@]}" -e "create table jlrepro.t (id bigint primary key, v varchar(16), pad int)"

# 2) serial baseline
"${MO[@]}" -e "load data infile {'filepath'='/tmp/jl_300m.jl','format'='jsonline','jsondata'='object'} into table jlrepro.t;"
"${MO[@]}" -e "select count(*), sum(id) from jlrepro.t"   # 8000000 / 31999996000000
"${MO[@]}" -e "truncate table jlrepro.t"

# 3) user asks for parallel
"${MO[@]}" -e "load data infile {'filepath'='/tmp/jl_300m.jl','format'='jsonline','jsondata'='object'} into table jlrepro.t parallel 'true';"
"${MO[@]}" -e "select count(*) from jlrepro.t"             # 8000000 (correct, just slow)

# 4) inspect mo-service console/log: expect two ranges, actually one single range
#    plus a multi-second compileExternScan:
#      ... mcpu is 2, filepath ... file size is 318064140
#      ... [offset:0 offset:-1 ]
#      "compileExternScan cost 1.456s"

grep -E 'ExternScanParallelReadWrite|compileExternScan cost' <mo-service log> | tail

# 5) CSV control on the same instance: 330MB CSV does shard correctly
grep -E 'ExternScanParallelReadWrite\, \[' <mo-service log> | tail

Observed on the reproducing instance (all measurements from the same mo-service process, same session):

jl 300m.jl (8M rows)     serial        8.09s / 8.4s
jl 300m.jl parallel 'true' -> [offset:0 offset:-1]      10.11s / 10.77s / 12.24s / 12.37s
jl_2c_270m.jl (10M rows, 2 cols) parallel 'true' -> [offset:0 offset:-1]
jl_1c_266m.jl (20M rows, 1 col)  parallel 'true' -> [offset:0 offset:-1], compileExternScan cost 3.117s
csv_big_313m.csv (24M rows, 3 cols, 330MiB) parallel 'true' -> [offset:0 offset:173207345 offset:173207345 offset:-1]  4.89s
set sql_mode='' + parallel 'true' (jl file) -> [offset:0 offset:-1]

Root cause analysis

The parallel split path is pkg/sql/compile/compile.go -> external.ReadFileOffset (pkg/sql/colexec/external/external.go:677). For each candidate boundary it opens the file at guess and calls getTailSizeStrict (external.go:1010) to find the end of the next record. That function reuses the CSV machinery literally:

  • JSONLINE loads build the CSV parser with FieldsTerminatedBy = "\t" (pkg/sql/plan/build_load.go:160-163) so one physical JSON line becomes a single field token.
  • getTailSizeStrict then requires len(fields) >= len(cols) at external.go:1044 (if len(fields) < len(cols) { continue }) - for any table with >= 2 columns 1 < n is always true, so the loop keeps reading.
  • Even for a 1-column table, isLegalLine (external.go:1047, 1053+) type-checks field.Val - which is the entire JSON line text (e.g. {"id": 10000001}) - as bigint/varchar per the column; parsing {"id": ...} as bigint fails, so it returns false and the loop keeps reading. Verified experimentally: the 1-column 324MiB file also ends [offset:0 offset:-1].
  • The reader therefore runs to EOF, returns io.EOF, and ReadFileOffset's strict branch (external.go:756-761) treats any probe error as break - the "parser-resynchronization fallback" - silently keeping only [FileStartOff, -1] and discarding the split. No warning is logged (the fallback comment documents the intent, but the JSONLINE case means the fallback fires always, not occasionally).
  • The non-strict branch does not save it: with Format != CSV, legacyBoundary (external.go:719) forces getNonStrictFallbackBoundary, which likewise fails to produce an offset for JSONLINE (set sql_mode='' repro above still yields [offset:0 offset:-1]).

So for the JSONLINE format the probe can never succeed, parallel 'true' degrades to whole-file serial reading for files >= 256MiB, and the failed probe has already streamed half the file (measured: 1.46s for a 150MB tail, 3.12s for a 162MB tail - matching the ~100MB/s parse rate), which is why explicit parallel actually measured 20-47% slower than omitting the option.

Note on related prior work: #30511 is a different defect (COS SDK Close() drains the HTTP body after a probe that succeeded; CSV shards were still produced, only preparation time exploded), and #29768/#29773 fixed JSONLINE null mapping semantics in parallel loads - both confirm this code path is actively used, neither covers "probe never succeeds for JSONLINE, parallel silently collapses to one shard with paid tail scan".

Additional information

Suggested fix directions (one root cause, pick the minimal):

  1. Teach readFileOffset/getTailSizeStrict the JSONLINE record framing: a JSONLINE record boundary is a bare \n that is not inside the JSON string of the current line - practically, for this format the CSV tokenizer's unquoted-newline rule already gives that, but the records are not per-column fields; the probe should split on \n and skip the len(fields)/isLegalLine column-shape check for param.Format == tree.JSONLINE (the reader side does not use per-column fields for JSONLINE either - it re-parses the line as JSON).
  2. Or share the CSV-style boundary search but validate a candidate boundary by "next non-space char is { or [ on a fresh line" for JSONLINE, which is what jsondata object/array guarantees.
  3. Independently, external.go:756-761's silent err-break fallback should at least log a WARN stating parallel was downgraded and why - the current silent behavior makes this invisible to users reading only LOAD output.
  4. The probe's Size: -1 policy (read-from-guess-to-EOF) is also what makes the wasted cost linear in tail size; bounding the probe to one record (read until the first line boundary, at most LargestEntryLimit) still bounds the failure cost if (1)/(2) are not taken.

Recommended: (1) as the primary fix - JSONLINE lines cannot contain a bare newline inside a record (a multi-line JSON document is not valid JSONLINE), so byte-offset splitting at \n is exact for this format, same as CSV.

Full reproduction, exactly as Loom ran it
#!/bin/bash
# MatrixOne JSONLINE parallel sharding degradation - minimal reproduction.
# Requires a running single-node mo-service and python3.
# NOTE: pass an explicit --default-character-set=utf8mb4 to the mysql client.
set -u
MO_PORT=6001   # adjust to your mo-service frontend port
MO=(mysql --connect-timeout=10 -h 127.0.0.1 -P "$MO_PORT" -uroot -p111 --default-character-set=utf8mb4 --protocol=tcp)

# 1) 303MiB JSONLINE file, 8M records, 3 columns (>= 2*128MiB so shard count = 2)
python3 - <<'EOF'
with open('/tmp/jl_300m.jl','w') as f:
    for i in range(8_000_000):
        f.write('{"id": %d, "v": "s%d", "pad": "%d"}\n' % (i, i%97, i%10))
# 318064140 bytes
EOF

"${MO[@]}" -e "drop database if exists jlrepro; create database jlrepro"
"${MO[@]}" -e "create table jlrepro.t (id bigint primary key, v varchar(16), pad int)"

# 2) serial baseline (time it)
"${MO[@]}" -e "load data infile {'filepath'='/tmp/jl_300m.jl','format'='jsonline','jsondata'='object'} into table jlrepro.t;"
"${MO[@]}" -e "select count(*), sum(id) from jlrepro.t"   # 8000000 / 31999996000000
"${MO[@]}" -e "truncate table jlrepro.t"

# 3) user asks for parallel — expect 2 shards, actually 1
"${MO[@]}" -e "load data infile {'filepath'='/tmp/jl_300m.jl','format'='jsonline','jsondata'='object'} into table jlrepro.t parallel 'true';"
"${MO[@]}" -e "select count(*) from jlrepro.t"             # 8000000 (correct, just slow)

# 4) mo-service log: single range despite mcpu=2, plus multi-second probe
#      compileExternScanParallelReadWrite, ... mcpu is 2, filepath is .../jl_300m.jl, file size is 318064140
#      compileExternScanParallelReadWrite, [offset:0 offset:-1 ]
#      "compileExternScan cost 1.456616167s"
grep -E 'ExternScanParallelReadWrite|compileExternScan cost' <mo-service log path> | tail

# 5) CSV control (330MiB) on the same instance shards correctly:
python3 - <<'EOF'
with open('/tmp/csv_big_313m.csv','w') as f:
    for i in range(1, 24_000_001):
        f.write(f'{i},s{i%97},{i%10}\n')
EOF
"${MO[@]}" -e "create table jlrepro.c (id bigint primary key, v varchar(16), pad int)"
"${MO[@]}" -e "load data infile '/tmp/csv_big_313m.csv' into table jlrepro.c (id,v,pad) parallel 'true';"
grep -E 'ExternScanParallelReadWrite\, \[' <mo-service log path> | tail
# CSV: [offset:0 offset:173207345 offset:173207345 offset:-1] in 0.17ms compile

# 6) optional: non-strict session behaves the same
"${MO[@]}" -e "set sql_mode=''; truncate table jlrepro.t; load data infile {'filepath'='/tmp/jl_300m.jl','format'='jsonline','jsondata'='object'} into table jlrepro.t parallel 'true';"
# still [offset:0 offset:-1]

Found by Loom bug hunt (module: custom-82082add71; tests: white-box, black-box, concurrency; severity: S1)

Activity

  1. added
    kind/bug
    severity/s1
    on Oct 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s1

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions