diff --git a/examples/postgres-compose.yml b/examples/postgres-compose.yml index dfdc24c7..266ce078 100644 --- a/examples/postgres-compose.yml +++ b/examples/postgres-compose.yml @@ -2,6 +2,7 @@ version: '3.7' x-common-variables: &common-variables CHAIN_ID: mainnet + REDIS_URL: redis://redis:6379/ DATABASE_URL: postgres:5432 DATABASE_USER: postgres DATABASE_PASSWORD: password @@ -29,6 +30,7 @@ services: - 8000:8000 depends_on: - postgres + - redis restart: on-failure state-indexer: @@ -45,6 +47,7 @@ services: - 8080:8080 depends_on: - postgres + - redis restart: on-failure tx-indexer: @@ -61,6 +64,7 @@ services: - 8081:8081 depends_on: - postgres + - redis restart: on-failure postgres: @@ -72,3 +76,9 @@ services: POSTGRES_PASSWORD: password ports: - 5432:5432 + + redis: + image: redis/redis-stack-server:latest + restart: always + ports: + - 6379:6379 diff --git a/examples/rightsizing-compose.yml b/examples/rightsizing-compose.yml index e1b16671..e1d97ee4 100644 --- a/examples/rightsizing-compose.yml +++ b/examples/rightsizing-compose.yml @@ -2,6 +2,7 @@ version: '3.7' x-common-variables: &common-variables CHAIN_ID: mainnet + REDIS_URL: redis://redis:6379/ DATABASE_URL: postgres:5432 DATABASE_USER: postgres DATABASE_PASSWORD: password @@ -31,6 +32,7 @@ services: - 8000:8000 depends_on: - postgres + - redis state-indexer: build: @@ -46,6 +48,7 @@ services: - 8080:8080 depends_on: - postgres + - redis restart: on-failure tx-indexer: @@ -57,12 +60,12 @@ services: environment: <<: *common-variables TX_INDEXER_ID: tx-indexer-local - command: [ "from-interruption" ] ports: - 8081:8081 depends_on: - postgres + - redis restart: on-failure postgres: @@ -74,3 +77,10 @@ services: POSTGRES_PASSWORD: password ports: - 5432:5432 + + redis: + image: redis/redis-stack-server:latest + container_name: redis-stack-server + ports: + - 6379:6379 + restart: always diff --git a/rpc-server/README.md b/rpc-server/README.md index 02e65a28..ba2c408c 100644 --- a/rpc-server/README.md +++ b/rpc-server/README.md @@ -89,6 +89,10 @@ This feature flag enables the shadow data consistency checks. With this feature We encountered a problem with the design of the table `account_access_keys` and overall design of the logic around it. We had to disable the table and proxy the calls of the `query.access_key_list` method to the real NEAR RPC. However, we still aren't ready to get rid of the code and that's why we hid it under the feature flag. We are planning to remove the code in the future and remove the feature flag. +## Redis (Optional) + +Redis is used as an optional dependency when paired with the near_state_indexer feature. To use Redis, you need to specify the REDIS_URL in the .env file and set up a server using docker or `redis-server` command. Redis can be used to cache the state data indexed by the near_state_indexer for faster retrieval and reduced load on the database. + ## Metrics (Prometheus) The read-rpc-server exposes Prometheus-compatible metrics at the `/metrics` endpoint. @@ -113,4 +117,4 @@ For example, method `block` will have these metrics: - `BLOCK_ERROR_1` - `BLOCK_ERROR_2` - `BLOCK_ERROR_3` -- `BLOCK_ERROR_4` \ No newline at end of file +- `BLOCK_ERROR_4` diff --git a/rpc-server/src/main.rs b/rpc-server/src/main.rs index 0142af2d..ac1c8c30 100644 --- a/rpc-server/src/main.rs +++ b/rpc-server/src/main.rs @@ -44,25 +44,32 @@ async fn main() -> anyhow::Result<()> { let blocks_info_by_finality_clone = std::sync::Arc::clone(&server_context.blocks_info_by_finality); let near_rpc_client_clone = near_rpc_client.clone(); + let redis_client = redis::Client::open(rpc_server_config.general.redis_url.clone())? .get_connection_manager() - .await?; - let redis_client_clone = redis_client.clone(); + .await + .map_err(|err| { + crate::metrics::OPTIMISTIC_UPDATING.set_not_working(); + tracing::warn!("Failed to connect to Redis: {:?}", err); + }) + .ok(); // We need to update final block from Redis and Lake // Because we can't be sure that Redis has the latest block // And Lake can be used as a backup source - // Update final block from Redis - tokio::spawn(async move { - utils::update_final_block_regularly_from_redis( - blocks_cache_clone, - blocks_info_by_finality_clone, - redis_client_clone, - near_rpc_client_clone, - ) - .await - }); + // Update final block from Redis if Redis is available + if let Some(redis_client) = redis_client.clone() { + tokio::spawn(async move { + utils::update_final_block_regularly_from_redis( + blocks_cache_clone, + blocks_info_by_finality_clone, + redis_client, + near_rpc_client_clone, + ) + .await + }); + } // Update final block from Lake let blocks_cache_clone = std::sync::Arc::clone(&server_context.blocks_cache); @@ -78,11 +85,14 @@ async fn main() -> anyhow::Result<()> { .await }); - let blocks_info_by_finality = std::sync::Arc::clone(&server_context.blocks_info_by_finality); - tokio::spawn(async move { - utils::update_optimistic_block_regularly(blocks_info_by_finality, redis_client.clone()) - .await - }); + // Update optimistic block from Redis if Redis is available + if let Some(redis_client) = redis_client { + let blocks_info_by_finality = + std::sync::Arc::clone(&server_context.blocks_info_by_finality); + tokio::spawn(async move { + utils::update_optimistic_block_regularly(blocks_info_by_finality, redis_client).await + }); + } let rpc = Server::new() .with_data(Data::new(server_context.clone())) diff --git a/rpc-server/src/utils.rs b/rpc-server/src/utils.rs index 1df95040..3a0259a8 100644 --- a/rpc-server/src/utils.rs +++ b/rpc-server/src/utils.rs @@ -278,7 +278,7 @@ pub async fn update_final_block_regularly_from_redis( } } Err(err) => { - tracing::error!("Error to get final block from redis: {:?}", err); + tracing::warn!("Error to get final block from redis: {:?}", err); } } } @@ -319,7 +319,7 @@ pub async fn update_optimistic_block_regularly( } } Err(err) => { - tracing::error!("Error to get optimistic block from redis: {:?}", err); + tracing::warn!("Error to get optimistic block from redis: {:?}", err); } }