Skip to content

fix: Enable configuration for Redis usage#141

Merged
pallakartheekreddy merged 3 commits into
Sunbird-Knowlg:developfrom
aimansharief:disable-redis
Mar 25, 2026
Merged

fix: Enable configuration for Redis usage#141
pallakartheekreddy merged 3 commits into
Sunbird-Knowlg:developfrom
aimansharief:disable-redis

Conversation

@aimansharief

@aimansharief aimansharief commented Mar 20, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR makes Redis fully optional for the DIAL code service. When redis.cache.enabled=false (the new default), the service operates entirely on Cassandra with no dependency on a running Redis instance, while preserving correctness and concurrency safety.


Problem

Previously Redis was a hard runtime requirement for DIAL code generation. The dialcode_max_index counter lived primarily in Redis, and the service would throw an error if Redis was unavailable or its counter had drifted behind Cassandra. There was no supported path to run the service without Redis.


Changes

app/commons/JedisFactory.java

  • Reads redis.cache.enabled config flag (default false) at startup.
  • Only creates the Redis connection pool when the flag is true.
  • isEnabled() is the single gate used by all other classes — no other code touches Redis directly without checking this first.

app/utils/RedisStoreUtil.java

  • All methods now guard with JedisFactory.isEnabled() and return safely (null / no-op) when Redis is disabled, removing any risk of NPE if called when Redis is off.

app/dbstore/SystemConfigStore.java — new method getAndIncrementDialCodeIndex()

  • Implements a Cassandra Lightweight Transaction (CAS) loop to atomically increment dialcode_max_index.
  • Uses UPDATE … SET prop_value = N+1 WHERE prop_key = 'dialcode_max_index' IF prop_value = N executed directly via CassandraConnector.getSession() (not the inherited PreparedStatement helper, which cannot return the [applied] column).
  • On a CAS conflict (concurrent request won the race), re-reads and retries — up to 10 times — before throwing.
  • This replaces Redis INCRBY as the atomic counter when Redis is disabled, making Cassandra the single authoritative counter.

app/utils/DialCodeGenerator.java

  • getMaxIndex(): when Redis is disabled, delegates each index allocation to getAndIncrementDialCodeIndex() (CAS-based) instead of Redis INCRBY.
  • generate(): guards the final setMaxIndex() (the Redis→Cassandra sync write) behind JedisFactory.isEnabled().
    When Redis is disabled this write is intentionally skipped — Cassandra already holds the committed index from the CAS loop, and a plain write at the end would race with concurrent requests and could overwrite a higher index.

app/managers/HealthCheckManager.java & IHealthCheckManager.java

  • The "redis cache" and "DIAL Max Index" health checks are now gated behind JedisFactory.isEnabled().
  • When Redis is disabled, neither check appears in the /health response — only elasticsearch and cassandra db are reported.
  • IHealthCheckManager.checkRedisHealth() returns true immediately when Redis is disabled (no false-positive failures).

conf/application.conf

  • Added redis.cache.enabled=false as the explicit default, making the no-Redis path the safe out-of-the-box configuration.

test/resources/application.conf

  • Added redis.cache.enabled=false explicitly so test intent is self-documenting and tests are protected from future default changes.

test/manager/DialCodeManagerImplTest.java

  • Added generateDialCodesProduceUniqueIndicesWhenRedisDisabledTest: generates two sequential batches of 3 DIAL codes each and asserts no code appears in both batches, proving the CAS allocator never re-uses an index.
  • Fixed missing import static org.junit.Assert.assertFalse (compilation error).

test/manager/HealthCheckManagerTest.java

  • Added getAllServicesHealthWithRedisDisabledTest: asserts that elasticsearch and cassandra db checks are present, and that redis cache and DIAL Max Index checks are absent when Redis is disabled.

build/build.sh

  • Added --platform linux/amd64 to the Docker build command to produce the correct image architecture when building on Apple Silicon machines.

How index allocation works — before vs after

Redis enabled (before) Redis disabled (after)
Live counter Redis INCRBY Cassandra LWT (IF prop_value = N)
Cassandra role Checkpoint / backup Authoritative source of truth
setMaxIndex() after generate Called — syncs Redis → Cassandra Skipped — Cassandra already holds the committed value
Health checks ES + Cassandra + Redis Cache + DIAL Max Index ES + Cassandra only
Failure if counter store restarts Yes — Redis restart resets counter, causes duplicates No — Cassandra is durable

Test plan

  • Run full test suite: sbt test (or mvn test) passes with redis.cache.enabled=false.
  • generateDialCodesProduceUniqueIndicesWhenRedisDisabledTest passes — no index reuse across sequential generation requests.
  • getAllServicesHealthWithRedisDisabledTest passes — only ES and Cassandra checks appear in /health.
  • Existing getAllServicesHealthTest, checkCassandraHealth, checkElasticSearchHealth, checkRedisHealth all continue to pass.
  • Manual: start service with redis.cache.enabled=false and no Redis running — POST /v1/dialcode/generate succeeds and returns unique codes.
  • Manual: start service with redis.cache.enabled=true and Redis running — existing Redis-backed flow is unchanged.

🤖 Generated with Claude Code

Copilot AI 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 a configuration flag to enable/disable Redis usage and gates Redis-dependent code paths (RedisStoreUtil, DIAL max-index allocation, and health checks) based on that flag.

Changes:

  • Introduced redis.cache.enabled configuration flag (defaulted to false in application.conf).
  • Updated Redis utilities/factory to short-circuit or throw when Redis is disabled.
  • Conditioned Redis and DIAL max-index health checks on Redis being enabled.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
conf/application.conf Adds redis.cache.enabled flag to control Redis usage.
app/commons/JedisFactory.java Initializes Redis pool only when enabled; adds isEnabled(); guards connection/return methods.
app/utils/RedisStoreUtil.java Skips Redis reads/writes when Redis is disabled.
app/utils/DialCodeGenerator.java Falls back to Cassandra-based index increment when Redis is disabled.
app/managers/IHealthCheckManager.java Treats Redis as healthy when Redis is disabled.
app/managers/HealthCheckManager.java Skips Redis-related checks (and DIAL max index check) when Redis is disabled.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread conf/application.conf
Comment thread app/utils/DialCodeGenerator.java Outdated
Comment thread app/commons/JedisFactory.java
Comment thread app/managers/HealthCheckManager.java
@sonarqubecloud

Copy link
Copy Markdown

@pallakartheekreddy pallakartheekreddy merged commit fd1a5da into Sunbird-Knowlg:develop Mar 25, 2026
2 of 4 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