Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle too early request for healthcheck #402

Merged
merged 3 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions packages/packages.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
"contract/valory/staking_token/0.1.0": "bafybeiabkkhjpybqpzdfc4vcbiz4vefctfpvwetbmo7pqpiuxprmpvnti4",
"contract/valory/relayer/0.1.0": "bafybeibvqc3lwxtcnu6dgfkf7mzefdgtfyosyq2dow7ogyxsl25vkxjwea",
"skill/valory/market_manager_abci/0.1.0": "bafybeihmyqkzl3bm5zvjnc4auj32qjf3pk73scyq7mntmpsudqnisb4gey",
"skill/valory/decision_maker_abci/0.1.0": "bafybeifbu5ufyxout25e5qo4m4eh55ie52qsw6ejzx7vfbqenbqjzsejl4",
"skill/valory/trader_abci/0.1.0": "bafybeianhvowfav2hceteyru6smi6gfblih7ji7qqqoplapem2vgdgngk4",
"skill/valory/tx_settlement_multiplexer_abci/0.1.0": "bafybeifie37a5g5qf2w2b5zcbaq5z75mkhmq6b7l26xiuf46dg2ztmebme",
"skill/valory/decision_maker_abci/0.1.0": "bafybeigddrgyys4rqu3x6gwig7cpylesbpy6ioocmngremad373w54nlky",
"skill/valory/trader_abci/0.1.0": "bafybeidvi2fa26sbs3ig56rzfsewzcr2htgwkpxcdxzauyoys27byynoze",
"skill/valory/tx_settlement_multiplexer_abci/0.1.0": "bafybeicffb2hc2efvr33tvbbd5dp5sbj25fkar4fhgmqfbpy4fzxnzolsy",
"skill/valory/staking_abci/0.1.0": "bafybeial5xtgzf37e2khvdrc2q2wa2xhirjwioi2szwvcgtupidjxhg7tq",
"skill/valory/check_stop_trading_abci/0.1.0": "bafybeid7l74lmjnkeerkbvwhoo2l4cawb7c545rhcx3mjpsjux4zwy5wpm",
"agent/valory/trader/0.1.0": "bafybeibwwzx3spt3wi22jb3joejrywncbad32oatrrrfb6fprp63rssy64",
"service/valory/trader/0.1.0": "bafybeieiukh6zokn7bjgnyywmnb32n7gmvhngzjjiusxjsh72yfksw24bu",
"service/valory/trader_pearl/0.1.0": "bafybeidpwx2lx5he6l3lft2er7laaboxy5j6qcrginr3v7mimzyr5hu4jq"
"agent/valory/trader/0.1.0": "bafybeif4vgpi37dspburfa4nltd6rzi7muqgqx3ojliutibwpahln3wxbe",
"service/valory/trader/0.1.0": "bafybeigzal6ikcehpsitpi7g3qgv6vhigcp2uqwlqgs3askdzaknk5bcy4",
"service/valory/trader_pearl/0.1.0": "bafybeihgmbbjtkrlu62bkm3e4j2ehqipv5huqpifjiyttvjrk4sikwsfzu"
},
"third_party": {
"protocol/valory/acn_data_share/0.1.0": "bafybeih5ydonnvrwvy2ygfqgfabkr47s4yw3uqxztmwyfprulwfsoe7ipq",
Expand Down
6 changes: 3 additions & 3 deletions packages/valory/agents/trader/aea-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ skills:
- valory/reset_pause_abci:0.1.0:bafybeigebq46oqz2mx2iajupr6p5pgm6z5pvfye5w6zypsseuqtvta7b4a
- valory/termination_abci:0.1.0:bafybeieurwmfernodqyczj5ertsgfbjtjnrlgvte7sli4sajnbopty7inu
- valory/transaction_settlement_abci:0.1.0:bafybeifkftgkyzrxwxjdyqixpp7vk6aqmufikalmwx3kydtlg74tonu47u
- valory/tx_settlement_multiplexer_abci:0.1.0:bafybeifie37a5g5qf2w2b5zcbaq5z75mkhmq6b7l26xiuf46dg2ztmebme
- valory/tx_settlement_multiplexer_abci:0.1.0:bafybeicffb2hc2efvr33tvbbd5dp5sbj25fkar4fhgmqfbpy4fzxnzolsy
- valory/market_manager_abci:0.1.0:bafybeihmyqkzl3bm5zvjnc4auj32qjf3pk73scyq7mntmpsudqnisb4gey
- valory/decision_maker_abci:0.1.0:bafybeifbu5ufyxout25e5qo4m4eh55ie52qsw6ejzx7vfbqenbqjzsejl4
- valory/trader_abci:0.1.0:bafybeianhvowfav2hceteyru6smi6gfblih7ji7qqqoplapem2vgdgngk4
- valory/decision_maker_abci:0.1.0:bafybeigddrgyys4rqu3x6gwig7cpylesbpy6ioocmngremad373w54nlky
- valory/trader_abci:0.1.0:bafybeidvi2fa26sbs3ig56rzfsewzcr2htgwkpxcdxzauyoys27byynoze
- valory/staking_abci:0.1.0:bafybeial5xtgzf37e2khvdrc2q2wa2xhirjwioi2szwvcgtupidjxhg7tq
- valory/check_stop_trading_abci:0.1.0:bafybeid7l74lmjnkeerkbvwhoo2l4cawb7c545rhcx3mjpsjux4zwy5wpm
- valory/mech_interact_abci:0.1.0:bafybeif2tpz2zet6p4z4vi3b254oxzyyzoe5tehj3me3znzt7h7otkpd54
Expand Down
2 changes: 1 addition & 1 deletion packages/valory/services/trader/service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ license: Apache-2.0
fingerprint:
README.md: bafybeigtuothskwyvrhfosps2bu6suauycolj67dpuxqvnicdrdu7yhtvq
fingerprint_ignore_patterns: []
agent: valory/trader:0.1.0:bafybeibwwzx3spt3wi22jb3joejrywncbad32oatrrrfb6fprp63rssy64
agent: valory/trader:0.1.0:bafybeif4vgpi37dspburfa4nltd6rzi7muqgqx3ojliutibwpahln3wxbe
number_of_agents: 4
deployment:
agent:
Expand Down
2 changes: 1 addition & 1 deletion packages/valory/services/trader_pearl/service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license: Apache-2.0
fingerprint:
README.md: bafybeibg7bdqpioh4lmvknw3ygnllfku32oca4eq5pqtvdrdsgw6buko7e
fingerprint_ignore_patterns: []
agent: valory/trader:0.1.0:bafybeibwwzx3spt3wi22jb3joejrywncbad32oatrrrfb6fprp63rssy64
agent: valory/trader:0.1.0:bafybeif4vgpi37dspburfa4nltd6rzi7muqgqx3ojliutibwpahln3wxbe
number_of_agents: 1
deployment:
agent:
Expand Down
87 changes: 57 additions & 30 deletions packages/valory/skills/decision_maker_abci/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ def handle(self, message: IpfsMessage) -> None:
OK_CODE = 200
NOT_FOUND_CODE = 404
BAD_REQUEST_CODE = 400
TOO_EARLY_CODE = 425
AVERAGE_PERIOD_SECONDS = 10


Expand Down Expand Up @@ -272,6 +273,36 @@ def _handle_bad_request(
self.context.logger.info("Responding with: {}".format(http_response))
self.context.outbox.put_message(message=http_response)

def _has_transitioned(self) -> bool:
"""Check if the agent has transitioned."""
try:
return bool(self.round_sequence.last_round_transition_height)
except ValueError:
return False

def _handle_too_early(
self, http_msg: HttpMessage, http_dialogue: HttpDialogue
) -> None:
"""
Handle a request when the FSM's loop has not started yet.

:param http_msg: the http message
:param http_dialogue: the http dialogue
"""
http_response = http_dialogue.reply(
performative=HttpMessage.Performative.RESPONSE,
target_message=http_msg,
version=http_msg.version,
status_code=TOO_EARLY_CODE,
status_text="The state machine has not started yet! Please try again later...",
headers=http_msg.headers,
body=b"",
)

# Send response
self.context.logger.info("Responding with: {}".format(http_response))
self.context.outbox.put_message(message=http_response)

def _handle_get_health(
self, http_msg: HttpMessage, http_dialogue: HttpDialogue
) -> None:
Expand All @@ -281,45 +312,41 @@ def _handle_get_health(
:param http_msg: the http message
:param http_dialogue: the http dialogue
"""
is_transitioning_fast = (
seconds_since_last_transition
) = is_tm_unhealthy = rounds = None
if not self._has_transitioned():
self._handle_too_early(http_msg, http_dialogue)
return

has_required_funds = self._check_required_funds()
is_receiving_mech_responses = self._check_is_receiving_mech_responses()
is_staking_kpi_met = self.synchronized_data.is_staking_kpi_met
staking_status = self.synchronized_data.service_staking_state.name.lower()

round_sequence = self.round_sequence
is_tm_unhealthy = round_sequence.block_stall_deadline_expired

last_round_transition_timestamp = (
round_sequence._last_round_transition_timestamp
current_time = datetime.now().timestamp()
seconds_since_last_transition = current_time - datetime.timestamp(
round_sequence.last_round_transition_timestamp
)
if last_round_transition_timestamp:
is_tm_unhealthy = round_sequence.block_stall_deadline_expired

current_time = datetime.now().timestamp()
seconds_since_last_transition = current_time - datetime.timestamp(
last_round_transition_timestamp
)

previous_rounds = round_sequence.abci_app._previous_rounds
previous_round_cls = type(previous_rounds[-1])
previous_round_events = round_sequence.abci_app.transition_function.get(
previous_round_cls, {}
).keys()
previous_round_timeouts = {
round_sequence.abci_app.event_to_timeout.get(event, -1)
for event in previous_round_events
}
last_round_timeout = max(previous_round_timeouts)
is_transitioning_fast = (
not is_tm_unhealthy
and seconds_since_last_transition < 2 * last_round_timeout
)
abci_app = self.round_sequence.abci_app
previous_rounds = abci_app._previous_rounds
previous_round_cls = type(previous_rounds[-1])
previous_round_events = abci_app.transition_function.get(
previous_round_cls, {}
).keys()
previous_round_timeouts = {
abci_app.event_to_timeout.get(event, -1) for event in previous_round_events
}
last_round_timeout = max(previous_round_timeouts)
is_transitioning_fast = (
not is_tm_unhealthy
and seconds_since_last_transition < 2 * last_round_timeout
)

rounds = [r.round_id for r in previous_rounds[-FSM_REPR_MAX_DEPTH:]] + [
round_sequence.current_round_id
]
rounds = [r.round_id for r in previous_rounds[-FSM_REPR_MAX_DEPTH:]] + [
round_sequence.current_round_id
]

data = {
"seconds_since_last_transition": seconds_since_last_transition,
Expand Down Expand Up @@ -360,7 +387,7 @@ def _send_ok_response(
def _send_not_found_response(
self, http_msg: HttpMessage, http_dialogue: HttpDialogue
) -> None:
"""Send an not found response"""
"""Send a not found response"""
http_response = http_dialogue.reply(
performative=HttpMessage.Performative.RESPONSE,
target_message=http_msg,
Expand Down
2 changes: 1 addition & 1 deletion packages/valory/skills/decision_maker_abci/skill.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fingerprint:
behaviours/tool_selection.py: bafybeienlxcgjs3ogyofli3d7q3p5rst3mcxxcnwqf7qolqjeefjtixeke
dialogues.py: bafybeigpwuzku3we7axmxeamg7vn656maww6emuztau5pg3ebsoquyfdqm
fsm_specification.yaml: bafybeifvu7n6sjmrerogkzsftjrw2l6w5ppuq3f43ouj2rwbomdr5glp2e
handlers.py: bafybeidnrhmpngeobpv76nuyg7vdjstostrez3jiu4d4vqgk2467t6gq4i
handlers.py: bafybeiherm5a2kjmf46ffjoyvhh3tsja5au5i677oasiutdxdojwdtwqla
io_/__init__.py: bafybeifxgmmwjqzezzn3e6keh2bfo4cyo7y5dq2ept3stfmgglbrzfl5rq
io_/loader.py: bafybeih3sdsx5dhe4kzhtoafexjgkutsujwqy3zcdrlrkhtdks45bc7exa
models.py: bafybeiasqxrhudkisjohjdgbkmmj6esyw52sh66xflwkatd5dyradxb37q
Expand Down
4 changes: 2 additions & 2 deletions packages/valory/skills/trader_abci/skill.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ skills:
- valory/transaction_settlement_abci:0.1.0:bafybeifkftgkyzrxwxjdyqixpp7vk6aqmufikalmwx3kydtlg74tonu47u
- valory/termination_abci:0.1.0:bafybeieurwmfernodqyczj5ertsgfbjtjnrlgvte7sli4sajnbopty7inu
- valory/market_manager_abci:0.1.0:bafybeihmyqkzl3bm5zvjnc4auj32qjf3pk73scyq7mntmpsudqnisb4gey
- valory/decision_maker_abci:0.1.0:bafybeifbu5ufyxout25e5qo4m4eh55ie52qsw6ejzx7vfbqenbqjzsejl4
- valory/tx_settlement_multiplexer_abci:0.1.0:bafybeifie37a5g5qf2w2b5zcbaq5z75mkhmq6b7l26xiuf46dg2ztmebme
- valory/decision_maker_abci:0.1.0:bafybeigddrgyys4rqu3x6gwig7cpylesbpy6ioocmngremad373w54nlky
- valory/tx_settlement_multiplexer_abci:0.1.0:bafybeicffb2hc2efvr33tvbbd5dp5sbj25fkar4fhgmqfbpy4fzxnzolsy
- valory/staking_abci:0.1.0:bafybeial5xtgzf37e2khvdrc2q2wa2xhirjwioi2szwvcgtupidjxhg7tq
- valory/check_stop_trading_abci:0.1.0:bafybeid7l74lmjnkeerkbvwhoo2l4cawb7c545rhcx3mjpsjux4zwy5wpm
- valory/mech_interact_abci:0.1.0:bafybeif2tpz2zet6p4z4vi3b254oxzyyzoe5tehj3me3znzt7h7otkpd54
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protocols:
- valory/ledger_api:1.0.0:bafybeihmqzcbj6t7vxz2aehd5726ofnzsfjs5cwlf42ro4tn6i34cbfrc4
skills:
- valory/abstract_round_abci:0.1.0:bafybeia27qmw6w5ds5fcrpj2475brnz742aampe3sgochloijs2l7jovai
- valory/decision_maker_abci:0.1.0:bafybeifbu5ufyxout25e5qo4m4eh55ie52qsw6ejzx7vfbqenbqjzsejl4
- valory/decision_maker_abci:0.1.0:bafybeigddrgyys4rqu3x6gwig7cpylesbpy6ioocmngremad373w54nlky
- valory/staking_abci:0.1.0:bafybeial5xtgzf37e2khvdrc2q2wa2xhirjwioi2szwvcgtupidjxhg7tq
- valory/mech_interact_abci:0.1.0:bafybeif2tpz2zet6p4z4vi3b254oxzyyzoe5tehj3me3znzt7h7otkpd54
behaviours:
Expand Down
Loading