Skip to content

HIVE-29697: Add support for Persistable Sessions - #6687

Open
ayushtkn wants to merge 9 commits into
apache:masterfrom
ayushtkn:HIVE-29697
Open

HIVE-29697: Add support for Persistable Sessions#6687
ayushtkn wants to merge 9 commits into
apache:masterfrom
ayushtkn:HIVE-29697

Conversation

@ayushtkn

@ayushtknayushtkn commented Aug 10, 2026

Copy link
Copy Markdown
Member

What changes were proposed in this pull request?

Add support for Persistable Sessions in Hive

Why are the changes needed?

In case of HS2 failures, the Session should be preserved for better HS2 HA

Does this PR introduce any user-facing change?

In case there is a HS2 crash, the other HS2 can reload the session, so session is preserved and the query retry doesn't fail with Invalid SessionHandle like

Error: Invalid SessionHandle: SessionHandle [5929b974-c356-4ebb-8ec4-e99071f6319d] (state=,code=0)

How was this patch tested?

UT, Manually for REDIS via K8s Operator
Killed the HS2 pod which had the session with

kubectl delete pod hive-hiveserver2-768f66c44d-tntnj --grace-period=0 --force

Checked running a query and if the temp table and configs set survived
image

CopilotAI 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.

Pull request overview

Adds an initial implementation of “Persistable Sessions” for HiveServer2, allowing HS2 to recover session state (configs, jars, temp tables) from an external shared store (ZooKeeper/Redis) after failover.

Changes:

  • Introduces a new service-session-store module with SessionStateStore plus ZooKeeper and Redis implementations and a JSON-serializable HiveSessionSnapshot.
  • Integrates snapshot save/delete + session recovery logic into HS2 SessionManager, with a configurable fetch strategy.
  • Adds utilities + hooks (PersistableSessionUtils, HiveSessionImpl notifications) and new unit/integration tests covering recovery and strategies.

Reviewed changes

Copilot reviewed 19 out of 19 changed files in this pull request and generated 7 comments.

Show a summary per file
FileDescription
service/src/java/org/apache/hive/service/cli/session/SessionManager.javaInitializes the store, saves/deletes snapshots, and attempts session recovery/sync from the remote store.
service/src/java/org/apache/hive/service/cli/session/PersistableSessionUtils.javaSnapshot capture/hydration helpers and store save/delete helpers.
service/src/java/org/apache/hive/service/cli/session/HiveSessionProxy.javaExposes the base session for unwrapping during snapshot capture.
service/src/java/org/apache/hive/service/cli/session/HiveSessionImpl.javaCalls snapshot save notifications on detected “state-changing” commands and exposes captureSnapshot().
service/pom.xmlAdds dependency on the new session-store module.
service-session-store/src/main/java/org/apache/hive/service/cli/session/store/SessionStateStore.javaDefines the SPI for snapshot persistence.
service-session-store/src/main/java/org/apache/hive/service/cli/session/store/HiveSessionSnapshot.javaDTO for persisted session state (Jackson-serializable).
service-session-store/src/main/java/org/apache/hive/service/cli/session/store/ZooKeeperSessionStateStore.javaZooKeeper-backed persistence implementation.
service-session-store/src/main/java/org/apache/hive/service/cli/session/store/RedisSessionStateStore.javaRedis-backed persistence implementation (TTL-based).
service-session-store/src/test/java/org/apache/hive/service/cli/session/store/TestSessionStateStoreBase.javaBase unit tests for store save/get/delete behavior.
service-session-store/src/test/java/org/apache/hive/service/cli/session/store/TestZooKeeperSessionStateStore.javaZooKeeper store unit tests using Curator TestingServer.
service-session-store/src/test/java/org/apache/hive/service/cli/session/store/TestRedisSessionStateStore.javaRedis store unit tests using Testcontainers.
service-session-store/pom.xmlNew module POM and dependencies (Jackson/Curator/Jedis).
pom.xmlAdds module + jedis.version property.
itests/hive-unit/src/test/java/org/apache/hive/service/cli/session/TestPersistableSessionBase.javaIntegration tests that validate failover recovery + fetch strategies.
itests/hive-unit/src/test/java/org/apache/hive/service/cli/session/TestPersistableSessionWithZooKeeper.javaZooKeeper-backed integration test wiring.
itests/hive-unit/src/test/java/org/apache/hive/service/cli/session/TestPersistableSessionWithRedis.javaRedis-backed integration test wiring.
itests/hive-unit/pom.xmlAdds test dependencies for the new module and Jedis.
common/src/java/org/apache/hadoop/hive/conf/HiveConf.javaAdds new ConfVars for store class, strategy, and TTL.
Suppressed comments (1)

service/src/java/org/apache/hive/service/cli/session/PersistableSessionUtils.java:190

  • When restoring temp tables during hydration, the warn log drops the exception stack trace (only logs e.getMessage()). That makes restore failures hard to debug, especially when a DDL fails due to missing jars/serde/permissions. Logging the exception object preserves useful context.
 for (Map.Entry<String, String> entry : snapshot.getTempTableDefinitions().entrySet()) {
try {
session.executeStatement(entry.getValue(), null);
} catch (Exception e) {
LOG.warn("Failed to restore temporary table {}: {}", entry.getKey(), e.getMessage());
}

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment threadservice/src/java/org/apache/hive/service/cli/session/SessionManager.java Outdated
Comment threadservice/src/java/org/apache/hive/service/cli/session/HiveSessionImpl.java Outdated
Comment on lines +546 to +547
LOG.info("Operation lost after failover, reconnecting and re-executing (attempt {} of {}): {}",
failoverRetries, maxFailoverRetries, lastSql, e);

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.

Can we remove the error being logged here? On the console output, it initially appears as some error post query re-attempt.

@ayushtknayushtkn changed the title WIP: HIVE-29697: Add support for Persistable SessionsHIVE-29697: Add support for Persistable SessionsAug 31, 2026
@sonarqubecloud

Copy link
Copy Markdown

@tanishq-chughtanishq-chugh 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.

Tried it out and works as expected 🚀

With ZK:

Initial Cluster state:
Image

Created Temporary tables and changed session level configs:
Image

HS2 to which session was connected crashes and new HS2 spins up in its place:
Image

Same session accessible with temporary tables + session-configs:
Image

With Redis:
New HS2 spins up after session-connected HS2 crashed:
Image

Same session accessible with temporary tables + session-configs:
Image

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@ayushtkn@tanishq-chugh@asf-ci-hive