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

finality-exporter: add ws exception handling #39

Closed
wants to merge 2 commits into from
Closed
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
14 changes: 5 additions & 9 deletions exporters/common/finality_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import threading
import logging
import traceback
import random
from functions import SUBSTRATE_INTERFACE, get_chain_info, get_keys, ss58_convert
from _thread import interrupt_main
from collections import deque
Expand Down Expand Up @@ -87,6 +88,8 @@ def get_votes(url, substrate_interface):
while True:
try:
d = substrate_interface.rpc_request(method='grandpa_roundState')
if url in q_outaged:
q_outaged.remove(url)
d = d['result']['best']
if d['round'] != rd and len(r) != 0:
q_votes_raw.append(r)
Expand All @@ -97,14 +100,11 @@ def get_votes(url, substrate_interface):
r[rd]['prevotes'] = d['prevotes']['missing']
r[rd]['precommits'] = d['precommits']['missing']

if url in q_outaged:
q_outaged.remove(url)
except Exception:
if url not in q_outaged:
q_outaged.append(url)
time.sleep(1)
substrate_interface.connect_websocket()
pass


def construct_metrics(active_validators, grandpa_keys, votes_threshold, current_session):
Expand Down Expand Up @@ -176,6 +176,7 @@ def construct_metrics(active_validators, grandpa_keys, votes_threshold, current_


def main():
substrate_interface = SUBSTRATE_INTERFACE(random.choice(rpc_endpoints), chain)
while True:
try:
for i in q_outaged:
Expand Down Expand Up @@ -212,19 +213,14 @@ def main():
rpc_count = len(rpc_endpoints)
thread_count = 3

substrate_interface = SUBSTRATE_INTERFACE(rpc_endpoints[0], chain)

q_metrics = deque([])
q_votes_raw = deque([], maxlen=rpc_count * thread_count * 15)
q_rounds_processed = deque([], maxlen=100)
q_outaged = deque([], maxlen=30)

for url in rpc_endpoints:
for i in range(thread_count):
if url == rpc_endpoints[0]:
th = threading.Thread(target=get_votes, args=(url, substrate_interface))
else:
th = threading.Thread(target=get_votes, args=(url, SUBSTRATE_INTERFACE(url, chain)))
th = threading.Thread(target=get_votes, args=(url, SUBSTRATE_INTERFACE(url, chain)))
th.daemon = True
th.start()
time.sleep(0.2)
Expand Down
11 changes: 6 additions & 5 deletions exporters/common/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,20 @@ def __init__(self, ws_endpoint, chain):

def request(self, module: str, function: str, params: str = None):
try:
r = self.substrate.query(
return self.substrate.query(
module=module,
storage_function=function,
params=params)

return r
except (WebSocketConnectionClosedException, ConnectionRefusedError, SubstrateRequestException) as e:
self.substrate.connect_websocket()
logging.critical('The substrate api call failed with error ' + str(e))
r = None

def rpc_request(self, method: str, params: str = None):
return self.substrate.rpc_request(method=method, params=params)
try:
return self.substrate.rpc_request(method=method, params=params)
except Exception as e:
self.substrate.connect_websocket()
logging.critical('The substrate api request failed with error ' + str(e))


def get_era_points(data):
Expand Down
7 changes: 5 additions & 2 deletions exporters/events/cmd/events-exporter/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Config struct {
NewHeadBy string `json:"NEW_HEAD_BY" envconfig:"NEW_HEAD_BY" default:"poll"`
ExposeIdentities bool `json:"EXPOSE_ID" envconfig:"EXPOSE_ID"`
KnownValidatorCfg []string `json:"VALIDATORS_CFG" envconfig:"VALIDATORS_CFG" default:""`
ExposeParaVotes bool `json:"EXPOSE_PARAVOTES" envconfig:"EXPOSE_PARAVOTES" default:"true"`
}

/*
Expand Down Expand Up @@ -291,8 +292,10 @@ func (reader *HeadReader) Read(ctx context.Context) error {
if err := reader.ProcessBlockEvents(callCtx, h); err != nil {
return err
}
if err := reader.ProcessBlockParaVotes(callCtx, h); err != nil {
return err
if reader.cfg.ExposeParaVotes {
if err := reader.ProcessBlockParaVotes(callCtx, h); err != nil {
return err
}
}
case <-ctx.Done():
return ctx.Err()
Expand Down