Skip to content

Run a study import's Java steps in one JVM - #203

Merged
jamesqo merged 11 commits into
mainfrom
codex/batch-import-runner
Nov 24, 2026
Merged

jamesqo merged 11 commits into
mainfrom
codex/batch-import-runner

Conversation

@jamesqo

@jamesqo jamesqo commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Depends on #168. This PR is based on its branch and retains DAO-assigned IDs across import steps; the four importer ID-allocation changes are supplied by #168.

Problem

Running each file of a study import in a separate JVM reloads the study, patient, sample, profile and clinical caches from the database between files. On ClickHouse Cloud, a replica may not yet contain rows written by the preceding step. Some importers skip a missing sample with only a warning, so an import can finish successfully with data missing.

Study and incremental directory imports now run their ordered Java steps in one JVM and retain the state written by earlier steps. Reduced JVM startup time is a secondary benefit.

Changes

  • Python prepares the complete step manifest before starting Java. If preparation fails, no import steps execute, preserving the existing study and its availability.
  • ImportStepRunner runs the allowed steps in order and stops at the first failure, reporting the failing step and exit status to Python.
  • Between steps, pending bulk buffers and progress state are cleared. ID counters are persisted without being reset.
  • Sample and patient deletion evicts only deleted rows from the DAO maps. Whole-study deletion also evicts its cached genetic profiles. Other studies remain cached, including samples whose stable IDs are shared across studies.
  • Study creation and case-list updates retain current caches instead of reloading them from the database. The runner no longer needs a cache-reload exception for removal commands.
  • Directory imports always use one JVM. Standalone commands use the single-step helper. Python helper names distinguish these paths; unused wrappers and the redundant exception-status alias are removed.

Limitations

This revision has not been tested against ClickHouse Cloud. Initial cache loading and concurrent independent importers remain outside the consistency guarantee provided by retaining state within one process.

@mandawilson

mandawilson commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

@jamesqo The Problem section should say why one JVM is required. Each file is its own Java process, and each process loads samples, patients, profiles, and clinical data into static maps with SELECT *. Lookups read those maps. On ClickHouse Cloud that read can hit a replica that does not yet have the rows the previous file wrote, so a later file skips data that is missing from its cache and the import can still exit successfully. Keeping the maps in one JVM is what prevents that. The sentence later in the description, that reloading the caches would "read from whichever replica answers," is this problem and belongs at the top. The startup time and the study_es_0 timing are a benefit of the same change.

UPDATE: This is done. The Problem section now leads with the reason one JVM is required: a later file can miss rows the previous file wrote, and the import can still succeed. Keeping that state in one JVM is the fix, and the startup time is a secondary benefit.

@mandawilson

mandawilson commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

@jamesqo Since the race can silently drop rows on a ClickHouse Cloud replica lag, and that can happen within a single study's import once it has more than one data file, this pull request should only have the single-JVM directory import. The multi-JVM directory import should not stay in this branch, even behind CBIOPORTAL_IMPORTER_SINGLE_JVM and even temporarily. That path is already on main. One-step commands can keep calling run_java once.

UPDATE: This is done. The multi-JVM directory-import path and CBIOPORTAL_IMPORTER_SINGLE_JVM are both removed (16f6949). Directory imports always run in one JVM now. Single-file subcommands still use run_java once, through run_single_step.

@jamesqo jamesqo changed the title Optionally run a study import's Java steps in one JVM Run a study import's Java steps in one JVM Oct 26, 2026

@mandawilson mandawilson left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Requesting changes because a single-JVM import still resets sequence counters and reloads DAO caches from the database between steps, so a later step can reuse an id or miss rows this same run just wrote.

Comment thread scripts/importer/cbioportal_common.py Resolved
Comment thread scripts/importer/cbioportal_common.py Resolved
Comment thread scripts/importer/cbioportalImporter.py Resolved
Comment thread scripts/importer/cbioportalImporter.py Resolved
Comment thread scripts/importer/cbioportalImporter.py Resolved
@mandawilson

Copy link
Copy Markdown
Contributor

Some reads can still see a stale replica during the single-JVM import, and each of these can drop or mis-attach data while the import exits 0.

1. IDs from SELECT max(...). ImportStepRunner.java#L45-L47 says ID counters stay current in memory, but four importers still start each file from a database max instead of ClickHouseAutoIncrement:

A second file of the same type can read an old max and reuse the first file's IDs. These tables have no unique key, so the duplicates are kept. The sequences are already registered in ClickHouseAutoIncrement.java#L86-L92. Please call nextId for each row and remove the four getLargest...Id() methods.

2. Generic-assay entities are reread right after they are inserted. ImportProfileData.java#L113 inserts the missing genetic_entity rows. The data import then reads that table again at ImportTabDelimData.java#L291-L294 and ImportGenericAssayPatientLevelData.java#L170. On a stale replica the new entities are missing, and their rows are skipped with a warning at ImportTabDelimData.java#L747-L751 and ImportGenericAssayPatientLevelData.java#L248-L251. The step fails only if no row at all was stored (#L381-L384). Please keep the entity map built during the insert and pass it to the data import instead of rereading the table.

3. Full reload of genetic profiles. GeneticProfileReader.java#L171-L176 calls DaoGeneticProfile.reCache() when source_stable_id is not in the cache. That replaces the profiles kept from earlier steps with a read that can come from a stale replica. Please remove the fallback and fail when the referenced profile is not in the cache.

4. A deleted _all list can be kept. AddCaseList.java#L88-L91 looks up {study}_all in the database and keeps it if it is found. During a reimport the old list is deleted at DaoCancerStudy.java#L559. A stale replica can still return the deleted list, so the new _all is never written and the import succeeds. Please make this step not depend on that read, or fail as ImportSampleList does when the stable id already exists.

@jamesqo

jamesqo commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

@mandawilson Addressed all four points from your later review comment in 72eb631.

  • Timeline, mutation-event, structural-variant, and copy-number-segment imports now allocate IDs through ClickHouseAutoIncrement.nextId, preserving the sequence across files in the same JVM. Removed the four per-file maximum-ID DAO helpers.
  • Generic assay metadata import returns its updated entity-ID map and passes it directly to both sample-level and patient-level data import, avoiding a read immediately after inserting entities.
  • Removed the missing-source-profile reCache() fallback. A missing source now fails without replacing the retained profile cache.
  • AddCaseList now fails explicitly when a stable ID already exists, including _all, instead of silently keeping a potentially stale list.

Validation: 132 unit tests and 16 targeted local ClickHouse integration tests passed. Added stale-read regression coverage for sequence retention across files, entity-map reuse, missing-source handling, and existing case lists. The integration checks cover all four ID importers, both generic assay paths, and genetic-profile loading. These were local ClickHouse tests; this revision has not been validated against ClickHouse Cloud.

@sheridancbio

Copy link
Copy Markdown
Contributor

This is the beginning of a readthrough of this PR. My first comment is about the PR description:

Running each file of a study import in a separate JVM reloads the study, patient, sample, profile and clinical caches from the database between files. On ClickHouse Cloud, a replica may not yet contain rows written by the preceding step. Some importers skip a missing sample with only a warning, so an import can finish successfully with data missing.

Study and incremental directory imports now run their ordered Java steps in one JVM and retain the state written by earlier steps. Reduced JVM startup time is a secondary benefit.

This seems to combine multiple problems ... so perhaps this PR could be split into smaller pieces. One piece could be to focus on the asynchronous nature of the import process --- the "a replica may not yet contain rows written by the preceding step" issue. Trying to fix that problem at the same time that we try to maintain state and speed up the running of the JVM probably blends together two different efforts. (I have not yet seen the changes yet though)

The validation reports probably don't belong in the PR description. These results are regenerated on every revision of the PR and can be seen in the validation reports under the "Checks" tab. So I would drop all that as noise. But maybe one good thing about this noise is this:

"Local ClickHouse 24.3: all six integration tests passed. Covers full study import, reimport in the same JVM, stopping/recovering after a failed step, sample and patient removal followed by another step, and rejection of an invalid manifest. Cache assertions run without a test-only reload."

Why are we running tests under Clickhouse 24.3 ? The standard Clickhouse version we released under was 24.10 --- so I'm confused why we would downgrade Clickhouse in order to run tests. Please comment on this.

@sheridancbio sheridancbio left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I've got the gist of this PR now, but not the details yet. Here are some shallow issues I saw which we can discuss. Deeper review needed.

James Ko added 8 commits November 11, 2026 18:33
cbioportalImporter.py starts a JVM for every file it imports. With
CBIOPORTAL_IMPORTER_SINGLE_JVM=1, a study or data directory import instead
writes the same steps, in the same order, to a JSON manifest and runs them
with ImportStepRunner in one JVM. The version check becomes the first step.
Python keeps the ordering logic; without the variable nothing changes.

ImportStepRunner runs each ConsoleRunnable in-process and stops at the first
failed step with that step's exit status, writing the failed step's index
to a result file so Python can report it as before. Between steps it resets
what a fresh JVM would start without: pending bulk loads and deletes, the
bulk load and relaxed mode flags, sequence counters (persisted first, as the
shutdown hook would) and ProgressMonitor, and it reloads the study caches.
The gene cache is kept.

Importing study_es_0 with cbioportalImporter.py took about 28 s with a JVM
per step and about 5.5 s in one JVM, with identical contents in all 60
tables.

Also import JavaRunException in cbioportalImporter and give it the
java_process_status attribute its error handlers already read.
The DAOs keep the study, patient, sample, profile and clinical attribute
caches current as they insert rows, so reloading every cache from the
database before each step only repeated work and read from whichever
replica answered. Reload them only after RemoveSamples or RemovePatients,
which clear the sample and patient caches without reloading them.
The per-step path reloads the study, patient and sample caches in every
step's JVM. On ClickHouse Cloud that reload can miss rows the previous step
wrote, and several importers then skip the missing sample or patient with
only a warning. Make the single JVM the default; CBIOPORTAL_IMPORTER_SINGLE_JVM=0
(or false/no) keeps one JVM per step as a temporary opt-out until the per-step
path is removed.

The existing per-step tests now set the opt-out explicitly, and new tests
cover the default and the opt-out values.
Study and data directory imports now always run their steps in one JVM,
and CBIOPORTAL_IMPORTER_SINGLE_JVM is gone. Single-file subcommands still
run their one step in its own JVM. Drop the process_case_lists,
import_incremental_data and update_case_lists_from_folder wrappers, which
nothing calls any more.

The directory import tests now read the step manifest instead of separate
java calls, and the single-JVM tests move into system_tests_import_data.py.
AddCaseList now refuses to keep an existing case list, so the tests that
run it after RemoveSamples or RemovePatients delete study_es_0_all first
and check that it is rebuilt from the remaining cached samples.
@jamesqo
jamesqo force-pushed the codex/batch-import-runner branch from 350d951 to 051f0b3 Compare November 11, 2026 17:37
@jamesqo
jamesqo changed the base branch from main to eliminate-clinical-event-id-pregen November 11, 2026 17:37

@sheridancbio sheridancbio left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

python review part

Comment thread scripts/importer/cbioportalImporter.py Resolved
Comment thread scripts/importer/cbioportalImporter.py Resolved
Comment thread scripts/importer/cbioportalImporter.py Resolved

@sheridancbio sheridancbio left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Java main code review (not including test code).

Overall, looks very good.

@jamesqo

jamesqo commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

In response to Rob’s feedback:

The single JVM is how this change retains the state written by earlier files, so the correctness fix and the process change are coupled

@jamesqo

jamesqo commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

In response to Rob’s feedback:

Thanks. I have separated the specific changes and open questions below so each can be reviewed on its own.

@sheridancbio sheridancbio left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I've quickly read over the generated tests, and tests seem to be testing reasonable conditions. The only think that jumped out for me is the license reference header containing references to Affero, but we switched to Apache for the cbioportal codebase (which this is really a part of)

@sheridancbio sheridancbio left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This requested change is in the base branch for the work in this PR. See comment (which is placed in a class which references the altered class just to have a placeholder)

@sheridancbio

Copy link
Copy Markdown
Contributor

The Clickhouse version for running integration tests has been updated now to 24.10, which matches our currently recommended default deployment of cBioPortal. ✅

@sheridancbio sheridancbio left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

all of my requested changes have been made, so I am approving this PR which will be system/regression tested before merge

@jamesqo
jamesqo changed the base branch from eliminate-clinical-event-id-pregen to main November 24, 2026 06:15
@jamesqo
jamesqo dismissed mandawilson’s stale review November 24, 2026 20:29

feedback has been applied

@jamesqo
jamesqo merged commit dc996c5 into main Nov 24, 2026
4 checks passed
@jamesqo
jamesqo deleted the codex/batch-import-runner branch November 24, 2026 20:30
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