Skip to content

[KYUUBI #7874][SPARK] Apply session configs before initializing catalogs on session open - #7875

Open
capacman wants to merge 2 commits into
apache:masterfrom
capacman:kyuubi-7874-session-conf-before-catalog-init
Open

capacman wants to merge 2 commits into
apache:masterfrom
capacman:kyuubi-7874-session-conf-before-catalog-init

Conversation

@capacman

Copy link
Copy Markdown

Why are the changes needed?

Closes #7874.

A Kyuubi session's Spark session is created via newSession(), so it starts with the configs of the connection that launched the engine. Catalog plugins read spark.sql.catalog.<name>.* only once, in initialize. On session open, the initialize SQLs (kyuubi.engine.session.initialize.sql) and use:catalog/use:database ran before the session configs were applied. So a catalog touched by them, e.g. by USE iceberg, got the stale configs (such as the first connection's credentials) and ignored the ones the client passed.

  • SparkSQLSessionManager.newSparkSession now applies the session configs before it runs the initialize SQLs.
  • SparkSessionImpl.open now applies the session configs before switching catalog/database. It also makes the session active while switching, because CatalogManager reads configs via SQLConf.get.
  • setModifiableConfig moves to KyuubiSparkUtil so both places can call it.

How was this patch tested?

Added SessionConfBeforeCatalogInitSuite. It registers a catalog plugin that records the config it is initialized with, and checks that a session's own value wins over the engine-launch value, both through the initialize SQL and through use:catalog.

build/mvn test -pl externals/kyuubi-spark-sql-engine -am -Dtest=none \
  -DwildcardSuites=org.apache.kyuubi.engine.spark.session

Was this patch assisted by generative AI tooling?

Assisted-by: Claude:claude-opus-5-5

🤖 Generated with Claude Code

… catalogs on session open

A new Spark session is created via `newSession()`, so it starts from the
SparkContext configs, i.e. the ones of the session which launched the engine.
The session initialization SQLs and `use:catalog`/`use:database` ran before
the session configs were applied, so catalog plugins touched by them (e.g.
`USE iceberg`) were initialized with stale `spark.sql.catalog.<name>.*`
configs, such as credentials of the first connection, and never re-read them.

- Apply session configs to the new session before the initialization SQLs.
- Apply session configs before switching catalog/database in
  `SparkSessionImpl.open`, and switch with the session set as active, since
  `CatalogManager` reads configs via `SQLConf.get`.
@wangzhigang1999

Copy link
Copy Markdown
Contributor

Thanks @capacman for tracking this down, especially the detail that CatalogManager reads through SQLConf.get and needs the new Spark session to be active when switching catalogs.

I tested master (dfd0f7aab) with and without this patch on Spark 3.5.9 / Scala 2.12 / JDK 17. The JDBC client passed spark.sql.shuffle.partitions=7 on every connection. Across four ABBA rounds, I observed the following values during initialization and after the connection opened.

Case master (during init → after open) master + patch (during init → after open)
Engine default is 19, init SQL reads the setting 19 → 7 7 → 7
Init SQL runs SET spark.sql.shuffle.partitions=11, then reads it 11 → 7 11 → 7

The first case confirms that init SQL can now see the client’s setting. The second shows why it makes sense to keep applying the client config in SparkSessionImpl.open(). The explicit SET takes effect during initialization, and the client’s value still wins after open, just as it does on master.

Both tests in SessionConfBeforeCatalogInitSuite passed. The three targeted JDBC workloads also completed with zero errors, and active session and operation counts returned to zero after every round.

Overall LGTM. Three non-blocking nits:

  1. In SparkSessionImpl.open(), could we use USE_DATABASE instead of the literal "use:database", for consistency with the partition above?
  2. In SessionConfBeforeCatalogInitSuite.openSession(), could we check status.getStatusCode === TStatusCode.SUCCESS_STATUS directly and explicitly close the session with TCloseSessionReq after a successful open? withThriftClient closes the underlying socket.
  3. SparkSessionImpl.withActiveSession and SparkOperation.withLocalProperties both save and restore the active Spark session. A small helper in KyuubiSparkUtil could keep that logic in one place.

- Use USE_DATABASE instead of the literal key in SparkSessionImpl.open.
- Close the opened session and check TStatusCode in
  SessionConfBeforeCatalogInitSuite.
- Extract KyuubiSparkUtil.withActiveSession and reuse it in
  SparkOperation.withLocalProperties.
@capacman

Copy link
Copy Markdown
Author

Thanks @wangzhigang1999 for the review and the ABBA verification. All three nits are addressed in a6e9f4e:

  1. SparkSessionImpl.open() now uses USE_DATABASE instead of the literal key.
  2. SessionConfBeforeCatalogInitSuite.openSession() checks TStatusCode.SUCCESS_STATUS and closes the session with TCloseSessionReq after a successful open.
  3. Extracted KyuubiSparkUtil.withActiveSession, now used by both SparkSessionImpl.open() and SparkOperation.withLocalProperties. The cleanup order in withLocalProperties is unchanged: the active session is still restored last.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Spark engine applies session configs after catalogs are initialized on session open

2 participants