Skip to content

Commit 3ec4702

Browse files
authored
fix(backend): replace hardcoded Redis DB numbers with SSOT env vars (#2806) (#2933)
fix(backend): replace hardcoded Redis DB numbers with SSOT env vars (#2806)
1 parent 54d2dc8 commit 3ec4702

11 files changed

Lines changed: 69 additions & 25 deletions

autobot-infrastructure/shared/analysis/analytics_timeout_diagnostic.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
"""
33
Fix Redis Timeout in Analytics Indexing
44
5-
This script addresses Redis timeout issues in the analytics database (DB 8)
6-
by implementing connection pooling, batch operations, and timeout protection.
5+
This script addresses Redis timeout issues in the analytics database (DB 11 per
6+
redis-databases.yaml SSOT — #2806) by implementing connection pooling, batch
7+
operations, and timeout protection.
78
"""
89

910
import asyncio
@@ -23,6 +24,9 @@
2324
logging.basicConfig(level=logging.INFO)
2425
logger = logging.getLogger(__name__)
2526

27+
# DB number from redis-databases.yaml SSOT (#2806): analytics = 11
28+
_DB_ANALYTICS = int(os.getenv("AUTOBOT_REDIS_DB_ANALYTICS", "11"))
29+
2630

2731
class AnalyticsRedisOptimizer:
2832
"""Optimizes Redis operations for analytics database to prevent timeouts"""
@@ -44,7 +48,7 @@ async def initialize(self):
4448
self.connection_pool = redis.ConnectionPool(
4549
host=redis_host,
4650
port=redis_port,
47-
db=8,
51+
db=_DB_ANALYTICS,
4852
max_connections=10,
4953
socket_timeout=5, # Reduced from default 30s
5054
socket_connect_timeout=3, # Reduced connection timeout

autobot-infrastructure/shared/scripts/analysis/analyze_redis_db0.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"""
88

99
import logging
10+
import os
1011
from collections import defaultdict
1112
from typing import Any, Dict, List, Tuple
1213

@@ -15,6 +16,9 @@
1516
logging.basicConfig(level=logging.INFO)
1617
logger = logging.getLogger(__name__)
1718

19+
# DB number from redis-databases.yaml SSOT (#2806): main = 0
20+
_DB_MAIN = int(os.getenv("AUTOBOT_REDIS_DB_MAIN", "0"))
21+
1822
# Issue #338: Key pattern dispatch table for categorization
1923
KEY_PATTERN_PREFIXES = [
2024
("llama_index/vector", "llama_index/vector"),
@@ -172,7 +176,7 @@ def analyze_redis_db0():
172176
"""Analyze what's in Redis database 0"""
173177
# Issue #338: Refactored to use extracted helpers, reducing depth from 11 to 2
174178
try:
175-
client = redis.Redis(host="localhost", port=6379, db=0, decode_responses=False)
179+
client = redis.Redis(host="localhost", port=6379, db=_DB_MAIN, decode_responses=False)
176180

177181
logger.info("=== ANALYZING REDIS DATABASE 0 ===")
178182

autobot-infrastructure/shared/scripts/analysis/debug_redis_fields.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,22 @@
77
"""
88

99
import logging
10+
import os
1011

1112
import redis
1213

1314
logging.basicConfig(level=logging.INFO)
1415
logger = logging.getLogger(__name__)
1516

17+
# DB number from redis-databases.yaml SSOT (#2806): knowledge = 1
18+
_DB_KNOWLEDGE = int(os.getenv("AUTOBOT_REDIS_DB_KNOWLEDGE", "1"))
19+
1620

1721
def debug_redis_vector_fields():
1822
"""Debug what fields are available in Redis vector store"""
1923
try:
20-
# Connect to Redis
21-
client = redis.Redis(host="localhost", port=6379, db=2, decode_responses=True)
24+
# Connect to Redis (knowledge DB — llama_index vectors live in DB 1)
25+
client = redis.Redis(host="localhost", port=6379, db=_DB_KNOWLEDGE, decode_responses=True)
2226

2327
# Get index info
2428
logger.info("Getting Redis FT.INFO for llama_index...")

autobot-infrastructure/shared/scripts/analysis/redis_final_analysis.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import asyncio
1010
import json
1111
import logging
12+
import os
1213
import sys
1314
from typing import Any, Dict, List, Tuple
1415

@@ -21,6 +22,9 @@
2122
)
2223
logger = logging.getLogger(__name__)
2324

25+
# DB number from redis-databases.yaml SSOT (#2806): knowledge = 1
26+
_DB_KNOWLEDGE = int(os.getenv("AUTOBOT_REDIS_DB_KNOWLEDGE", "1"))
27+
2428

2529
def _build_fix_llamaindex_recommendation(data_analysis, llamaindex_fixes):
2630
"""Build recommendation for fixing LlamaIndex integration.
@@ -263,7 +267,7 @@ async def deep_analyze_existing_data(self) -> Dict[str, Any]:
263267
client = redis.Redis(
264268
host=NetworkConstants.LOCALHOST_NAME,
265269
port=NetworkConstants.REDIS_PORT,
266-
db=2,
270+
db=_DB_KNOWLEDGE,
267271
decode_responses=True,
268272
)
269273

autobot-infrastructure/shared/scripts/analysis/simple_redis_test.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@
66
Simple Redis hash test
77
"""
88

9+
import os
10+
911
import redis
1012

13+
# DB number from redis-databases.yaml SSOT (#2806): knowledge = 1
14+
_DB_KNOWLEDGE = int(os.getenv("AUTOBOT_REDIS_DB_KNOWLEDGE", "1"))
15+
1116

1217
def simple_redis_test():
13-
client = redis.Redis(host="localhost", port=6379, db=2, decode_responses=True)
18+
client = redis.Redis(host="localhost", port=6379, db=_DB_KNOWLEDGE, decode_responses=True)
1419

1520
# Get first key directly from FT.SEARCH
1621
result = client.execute_command("FT.SEARCH", "llama_index", "*", "LIMIT", "0", "1")

autobot-infrastructure/shared/scripts/analysis/test_data_layer_debug.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@
1212
import traceback
1313
from pathlib import Path
1414

15-
logger = logging.getLogger(__name__)
16-
1715
import logging
1816

1917
import aioredis
2018
import redis
2119

20+
logger = logging.getLogger(__name__)
21+
22+
# DB number from redis-databases.yaml SSOT (#2806): knowledge = 1
23+
_DB_KNOWLEDGE = int(os.getenv("AUTOBOT_REDIS_DB_KNOWLEDGE", "1"))
24+
2225

2326
def test_redis_basic():
2427
"""Test basic Redis connectivity to all databases"""
@@ -61,12 +64,12 @@ def test_redis_basic():
6164

6265

6366
def test_vector_database():
64-
"""Test vector database in Redis DB 8"""
65-
logger.info("\n=== VECTOR DATABASE TEST (Redis DB 8) ===")
67+
"""Test vector database in Redis knowledge DB (DB 1 per redis-databases.yaml SSOT)"""
68+
logger.info("\n=== VECTOR DATABASE TEST (Redis knowledge DB) ===")
6669

6770
try:
6871
client = redis.Redis(
69-
host="localhost", port=6379, db=8, decode_responses=False, socket_timeout=2
72+
host="localhost", port=6379, db=_DB_KNOWLEDGE, decode_responses=False, socket_timeout=2
7073
)
7174

7275
# Check for LlamaIndex keys

autobot-infrastructure/shared/scripts/analysis/test_redis_comparison.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@
99

1010
import asyncio
1111
import logging
12+
import os
1213
import sys
1314
import time
1415

1516
from constants import ServiceURLs
1617

18+
# DB number from redis-databases.yaml SSOT (#2806): knowledge = 1
19+
_DB_KNOWLEDGE = int(os.getenv("AUTOBOT_REDIS_DB_KNOWLEDGE", "1"))
20+
1721
# Add the project root to the Python path
1822
sys.path.insert(0, "/home/kali/Desktop/AutoBot")
1923

@@ -30,7 +34,7 @@ async def test_redis_connection():
3034
try:
3135
import redis
3236

33-
client = redis.Redis(host="localhost", port=6379, db=2, decode_responses=True)
37+
client = redis.Redis(host="localhost", port=6379, db=_DB_KNOWLEDGE, decode_responses=True)
3438
client.ping()
3539
logger.info("✅ Redis connection successful")
3640

@@ -288,7 +292,7 @@ async def analyze_existing_data():
288292
try:
289293
import redis
290294

291-
client = redis.Redis(host="localhost", port=6379, db=2, decode_responses=True)
295+
client = redis.Redis(host="localhost", port=6379, db=_DB_KNOWLEDGE, decode_responses=True)
292296

293297
# Get sample documents
294298
sample_keys = list(client.scan_iter(match="llama_index/vector*", count=5))

autobot-infrastructure/shared/scripts/analysis/test_redis_direct.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,21 @@
77
"""
88

99
import logging
10+
import os
1011

1112
import redis
1213

1314
logging.basicConfig(level=logging.INFO)
1415
logger = logging.getLogger(__name__)
1516

17+
# DB number from redis-databases.yaml SSOT (#2806): knowledge = 1
18+
_DB_KNOWLEDGE = int(os.getenv("AUTOBOT_REDIS_DB_KNOWLEDGE", "1"))
19+
1620

1721
def test_redis_direct_access():
1822
"""Test Redis direct access"""
1923
try:
20-
client = redis.Redis(host="localhost", port=6379, db=2, decode_responses=True)
24+
client = redis.Redis(host="localhost", port=6379, db=_DB_KNOWLEDGE, decode_responses=True)
2125

2226
# Test FT.SEARCH with explicit RETURN
2327
logger.info("Testing FT.SEARCH with explicit RETURN...")

autobot-infrastructure/shared/scripts/fix_kb_dimensions.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,26 @@
44
"""
55

66
import asyncio
7+
import logging
78
import os
89
import sys
910

10-
logger = logging.getLogger(__name__)
11-
12-
1311
import redis
1412

1513
# Add parent directory to path
1614
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
1715

16+
logger = logging.getLogger(__name__)
17+
18+
# DB number from redis-databases.yaml SSOT (#2806): knowledge = 1
19+
_DB_KNOWLEDGE = int(os.getenv("AUTOBOT_REDIS_DB_KNOWLEDGE", "1"))
20+
1821

1922
async def fix_dimensions():
2023
"""Drop all llama_index traces and let it recreate properly."""
2124

22-
# Connect to Redis
23-
r = redis.Redis(host="localhost", port=6379, db=0)
25+
# Connect to Redis knowledge DB (llama_index lives in DB 1 per redis-databases.yaml SSOT, #2806)
26+
r = redis.Redis(host="localhost", port=6379, db=_DB_KNOWLEDGE)
2427

2528
logger.info("Cleaning up Redis...")
2629

autobot-infrastructure/shared/scripts/fresh_kb_setup.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,24 @@
88
import os
99
import sys
1010

11-
logger = logging.getLogger(__name__)
11+
import redis # noqa: redis — standalone script, see Issue #1086
1212

1313
# Add parent directory to path
1414
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
1515

16+
logger = logging.getLogger(__name__)
17+
18+
# DB number from redis-databases.yaml SSOT (#2806): knowledge = 1
19+
_DB_KNOWLEDGE = int(os.getenv("AUTOBOT_REDIS_DB_KNOWLEDGE", "1"))
20+
1621

1722
def _clean_redis() -> None:
1823
"""Clean all Redis databases and indexes.
1924
2025
Helper for fresh_setup (Issue #825).
2126
"""
2227
logger.info("\n1. Cleaning Redis...")
23-
r = redis.Redis(host="localhost", port=6379, db=0)
28+
r = redis.Redis(host="localhost", port=6379, db=_DB_KNOWLEDGE)
2429

2530
try:
2631
indexes = r.execute_command("FT._LIST")
@@ -103,7 +108,7 @@ def _verify_index() -> None:
103108
Helper for fresh_setup (Issue #825).
104109
"""
105110
logger.info("\n5. Checking created index...")
106-
r = redis.Redis(host="localhost", port=6379, db=0)
111+
r = redis.Redis(host="localhost", port=6379, db=_DB_KNOWLEDGE)
107112

108113
indexes = r.execute_command("FT._LIST")
109114
logger.info(f" Indexes: {indexes}")

0 commit comments

Comments
 (0)