Skip to content

Commit

Permalink
fix: clarified Redis connection errors and added Redis dependency to …
Browse files Browse the repository at this point in the history
…docker-compose files (#278)

* feat: provided better error message in case "Connection refused" caused by Redis

* feat: added redis dependency to docker compose files

* feat: made redis an optional dependency

* chore: downgraded block fetching error logs to the warn level

* chore: set `OPTIMISTIC_UPDATING` to not working

* docs: added info about Redis
  • Loading branch information
frolvanya committed Jun 11, 2024
1 parent c121d72 commit dc74efe
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 21 deletions.
10 changes: 10 additions & 0 deletions examples/postgres-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -29,6 +30,7 @@ services:
- 8000:8000
depends_on:
- postgres
- redis
restart: on-failure

state-indexer:
Expand All @@ -45,6 +47,7 @@ services:
- 8080:8080
depends_on:
- postgres
- redis
restart: on-failure

tx-indexer:
Expand All @@ -61,6 +64,7 @@ services:
- 8081:8081
depends_on:
- postgres
- redis
restart: on-failure

postgres:
Expand All @@ -72,3 +76,9 @@ services:
POSTGRES_PASSWORD: password
ports:
- 5432:5432

redis:
image: redis/redis-stack-server:latest
restart: always
ports:
- 6379:6379
12 changes: 11 additions & 1 deletion examples/rightsizing-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -31,6 +32,7 @@ services:
- 8000:8000
depends_on:
- postgres
- redis

state-indexer:
build:
Expand All @@ -46,6 +48,7 @@ services:
- 8080:8080
depends_on:
- postgres
- redis
restart: on-failure

tx-indexer:
Expand All @@ -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:
Expand All @@ -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
6 changes: 5 additions & 1 deletion rpc-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -113,4 +117,4 @@ For example, method `block` will have these metrics:
- `BLOCK_ERROR_1`
- `BLOCK_ERROR_2`
- `BLOCK_ERROR_3`
- `BLOCK_ERROR_4`
- `BLOCK_ERROR_4`
44 changes: 27 additions & 17 deletions rpc-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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()))
Expand Down
4 changes: 2 additions & 2 deletions rpc-server/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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);
}
}

Expand Down

0 comments on commit dc74efe

Please sign in to comment.