|
15 | 15 |
|
16 | 16 | app = Flask(__name__) |
17 | 17 |
|
18 | | -# Initialize Redis client |
| 18 | +# Initialize Redis client (standalone) |
19 | 19 | redis_client = redis.Redis( |
20 | 20 | host=os.getenv("REDIS_HOST", "redis"), port=int(os.getenv("REDIS_PORT", "6379")), db=0, decode_responses=True |
21 | 21 | ) |
22 | 22 |
|
| 23 | +# Lazy-initialized RedisCluster client. |
| 24 | +# Initialized on first use so that cluster connection failures don't prevent |
| 25 | +# the rest of the app from starting (avoids breaking non-cluster tests). |
| 26 | +_cluster_client = None |
| 27 | + |
| 28 | + |
| 29 | +def get_cluster_client(): |
| 30 | + global _cluster_client |
| 31 | + if _cluster_client is None: |
| 32 | + _cluster_client = redis.RedisCluster( |
| 33 | + host=os.getenv("REDIS_CLUSTER_HOST", "redis-node-1"), |
| 34 | + port=int(os.getenv("REDIS_CLUSTER_PORT", "6379")), |
| 35 | + decode_responses=True, |
| 36 | + ) |
| 37 | + return _cluster_client |
| 38 | + |
23 | 39 |
|
24 | 40 | @app.route("/health") |
25 | 41 | def health(): |
@@ -245,6 +261,85 @@ def test_transaction_watch(): |
245 | 261 | return jsonify({"error": str(e)}), 500 |
246 | 262 |
|
247 | 263 |
|
| 264 | +@app.route("/test/cluster-set-get", methods=["GET"]) |
| 265 | +def test_cluster_set_get(): |
| 266 | + """Test SET/GET through RedisCluster. |
| 267 | +
|
| 268 | + RedisCluster.execute_command is a separate method from Redis.execute_command |
| 269 | + """ |
| 270 | + try: |
| 271 | + cluster = get_cluster_client() |
| 272 | + cluster.set("test:cluster:key1", "cluster_value1") |
| 273 | + cluster.set("test:cluster:key2", "cluster_value2") |
| 274 | + val1 = cluster.get("test:cluster:key1") |
| 275 | + val2 = cluster.get("test:cluster:key2") |
| 276 | + cluster.delete("test:cluster:key1", "test:cluster:key2") |
| 277 | + return jsonify({"success": True, "val1": val1, "val2": val2}) |
| 278 | + except Exception as e: |
| 279 | + return jsonify({"error": str(e)}), 500 |
| 280 | + |
| 281 | + |
| 282 | +@app.route("/test/cluster-incr", methods=["GET"]) |
| 283 | +def test_cluster_incr(): |
| 284 | + """Test INCR through RedisCluster. |
| 285 | +
|
| 286 | + Exercises the cluster's execute_command path for a simple write operation. |
| 287 | + """ |
| 288 | + try: |
| 289 | + cluster = get_cluster_client() |
| 290 | + cluster.set("test:cluster:counter", "0") |
| 291 | + cluster.incr("test:cluster:counter") |
| 292 | + cluster.incr("test:cluster:counter") |
| 293 | + cluster.incr("test:cluster:counter") |
| 294 | + val = cluster.get("test:cluster:counter") |
| 295 | + cluster.delete("test:cluster:counter") |
| 296 | + return jsonify({"success": True, "value": int(val)}) |
| 297 | + except Exception as e: |
| 298 | + return jsonify({"error": str(e)}), 500 |
| 299 | + |
| 300 | + |
| 301 | +@app.route("/test/cluster-pipeline", methods=["GET"]) |
| 302 | +def test_cluster_pipeline(): |
| 303 | + """Test pipeline through RedisCluster (ClusterPipeline). |
| 304 | +
|
| 305 | + ClusterPipeline.execute_command is also a separate path. |
| 306 | + All keys use the same hash tag {cp} to ensure they land on the same slot, |
| 307 | + which is required for cluster pipelines. |
| 308 | + """ |
| 309 | + try: |
| 310 | + cluster = get_cluster_client() |
| 311 | + pipe = cluster.pipeline() |
| 312 | + pipe.set("{cp}:key1", "pipe_val1") |
| 313 | + pipe.set("{cp}:key2", "pipe_val2") |
| 314 | + pipe.get("{cp}:key1") |
| 315 | + pipe.get("{cp}:key2") |
| 316 | + results = pipe.execute() |
| 317 | + cluster.delete("{cp}:key1", "{cp}:key2") |
| 318 | + return jsonify({"success": True, "results": results}) |
| 319 | + except Exception as e: |
| 320 | + return jsonify({"error": str(e)}), 500 |
| 321 | + |
| 322 | + |
| 323 | +@app.route("/test/cluster-pipeline-transaction", methods=["GET"]) |
| 324 | +def test_cluster_pipeline_transaction(): |
| 325 | + """Test ClusterPipeline with transaction mode. |
| 326 | +
|
| 327 | + Uses TransactionStrategy internally. All keys must be on the same slot. |
| 328 | + """ |
| 329 | + try: |
| 330 | + cluster = get_cluster_client() |
| 331 | + pipe = cluster.pipeline(transaction=True) |
| 332 | + pipe.set("{tx}:key1", "txval1") |
| 333 | + pipe.set("{tx}:key2", "txval2") |
| 334 | + pipe.get("{tx}:key1") |
| 335 | + pipe.get("{tx}:key2") |
| 336 | + results = pipe.execute() |
| 337 | + cluster.delete("{tx}:key1", "{tx}:key2") |
| 338 | + return jsonify({"success": True, "results": results}) |
| 339 | + except Exception as e: |
| 340 | + return jsonify({"error": str(e)}), 500 |
| 341 | + |
| 342 | + |
248 | 343 | if __name__ == "__main__": |
249 | 344 | sdk.mark_app_as_ready() |
250 | 345 | app.run(host="0.0.0.0", port=8000, debug=False) |
0 commit comments