Skip to content

Commit

Permalink
feat: snapshotter active status check at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
anomit committed Sep 1, 2023
1 parent cf4a9a8 commit ee1e4ac
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 4 deletions.
19 changes: 16 additions & 3 deletions snapshotter/core_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from snapshotter.utils.redis.redis_keys import epoch_id_project_to_state_mapping
from snapshotter.utils.redis.redis_keys import epoch_process_report_cached_key
from snapshotter.utils.redis.redis_keys import project_last_finalized_epoch_key
from snapshotter.utils.redis.redis_keys import active_status_key
from snapshotter.utils.rpc import RpcHelper


Expand Down Expand Up @@ -97,11 +98,23 @@ async def startup_boilerplate():
await app.state.ipfs_singleton.init_sessions()
app.state.ipfs_reader_client = app.state.ipfs_singleton._ipfs_read_client

# Health check endpoint that returns 200 OK


# Health check endpoint
@app.get('/health')
async def health_check():
async def health_check(
request: Request,
response: Response,
):
redis_conn: aioredis.Redis = request.app.state.redis_pool
_ = await redis_conn.get(active_status_key)
if _:
active_status = bool(int(_))
if not active_status:
response.status_code = 503
return {
'status': 'error',
'message': 'Snapshotter is not active',
}
return {'status': 'OK'}

# get current epoch
Expand Down
11 changes: 10 additions & 1 deletion snapshotter/processor_distributor.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from snapshotter.utils.models.data_models import PreloaderAsyncFutureDetails
from snapshotter.utils.models.data_models import SnapshotterStates
from snapshotter.utils.models.data_models import SnapshotterStateUpdate
from snapshotter.utils.models.data_models import SnapshottersUpdatedEvent
from snapshotter.utils.models.message_models import EpochBase
from snapshotter.utils.models.message_models import PayloadCommitFinalizedMessage
from snapshotter.utils.models.message_models import PowerloomCalculateAggregateMessage
Expand All @@ -54,6 +55,7 @@
from snapshotter.utils.redis.redis_keys import epoch_id_project_to_state_mapping
from snapshotter.utils.redis.redis_keys import project_finalized_data_zset
from snapshotter.utils.redis.redis_keys import snapshot_submission_window_key
from snapshotter.utils.redis.redis_keys import active_status_key
from snapshotter.utils.rpc import RpcHelper


Expand Down Expand Up @@ -674,7 +676,14 @@ async def _on_rabbitmq_message(self, message: IncomingMessage):
)
elif message_type == 'ProjectsUpdated':
await self._update_all_projects(message)

elif message_type == 'SnapshottersUpdated':
msg_cast = SnapshottersUpdatedEvent.parse_raw(message.body)
if msg_cast.snapshotterAddress == settings.instance_id:
if self._redis_conn:
await self._redis_conn.set(
active_status_key,
int(msg_cast.allowed)
)
else:
self._logger.error(
(
Expand Down
5 changes: 5 additions & 0 deletions snapshotter/snapshotter_id_ping.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from snapshotter.auth.helpers.redis_conn import RedisPoolCache
from snapshotter.settings.config import settings
from snapshotter.utils.file_utils import read_json_file
from snapshotter.utils.redis.redis_keys import active_status_key
from snapshotter.utils.rpc import RpcHelper


Expand All @@ -28,6 +29,10 @@ async def main():
allowed_snapshotters = snapshotters_arr_query[0]
if settings.instance_id in allowed_snapshotters:
print('Snapshotting allowed...')
await redis_conn.set(
active_status_key,
int(True)
)
sys.exit(0)
else:
print('Snapshotting not allowed...')
Expand Down
11 changes: 11 additions & 0 deletions snapshotter/system_event_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from snapshotter.utils.models.data_models import EventBase
from snapshotter.utils.models.data_models import ProjectsUpdatedEvent
from snapshotter.utils.models.data_models import SnapshotFinalizedEvent
from snapshotter.utils.models.data_models import SnapshottersUpdatedEvent
from snapshotter.utils.rabbitmq_helpers import RabbitmqThreadedSelectLoopInteractor
from snapshotter.utils.redis.redis_conn import RedisPoolCache
from snapshotter.utils.redis.redis_keys import event_detector_last_processed_block
Expand Down Expand Up @@ -119,12 +120,15 @@ def __init__(self, name, **kwargs):
'EpochReleased': self.contract.events.EpochReleased._get_event_abi(),
'SnapshotFinalized': self.contract.events.SnapshotFinalized._get_event_abi(),
'ProjectsUpdated': self.contract.events.ProjectsUpdated._get_event_abi(),
'SnapshottersUpdated': self.contract.events.SnapshottersUpdated._get_event_abi(),
}

EVENT_SIGS = {
'EpochReleased': 'EpochReleased(uint256,uint256,uint256,uint256)',
'SnapshotFinalized': 'SnapshotFinalized(uint256,uint256,string,string,uint256)',
'ProjectsUpdated': 'ProjectsUpdated(string,bool,uint256)',
'SnapshottersUpdated': 'SnapshottersUpdated(address,bool)',

}

self.event_sig, self.event_abi = get_event_sig_and_abi(
Expand Down Expand Up @@ -187,6 +191,13 @@ async def get_events(self, from_block: int, to_block: int):
timestamp=int(time.time()),
)
events.append((log.event, event))
elif log.event == 'SnapshottersUpdated':
event = SnapshottersUpdatedEvent(
snapshotterAddress=log.args.snapshotterAddress,
allowed=log.args.allowed,
timestamp=int(time.time()),
)
events.append((log.event, event))

self._logger.info('Events: {}', events)
return events
Expand Down
5 changes: 5 additions & 0 deletions snapshotter/utils/models/data_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ class ProjectsUpdatedEvent(EventBase):
enableEpochId: int


class SnapshottersUpdatedEvent(EventBase):
snapshotterAddress: str
allowed: bool


class SnapshotSubmittedEvent(EventBase):
snapshotCid: str
epochId: int
Expand Down
2 changes: 2 additions & 0 deletions snapshotter/utils/redis/redis_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@

snapshot_submission_window_key = 'snapshotSubmissionWindow'

active_status_key = f'snapshotterActiveStatus:{settings.namespace}'

# project finalzed data zset


Expand Down

0 comments on commit ee1e4ac

Please sign in to comment.