7 Commits

Author SHA1 Message Date
lash
4be5325df2 Renew backend every filter pass, allow race in backend 2022-04-30 18:32:10 +00:00
lash
96bdca20cc Filter out final states from upcoming in network exceptions 2022-04-30 16:44:37 +00:00
lash
14fce6f63f Upgrade chainsyncer 2022-04-30 07:51:59 +00:00
lash
9ed3bad0c4 Add upcoming throttling, tests 2022-04-30 05:45:02 +00:00
lash
e87ec0cd4c Upgrade chainqueue, chainsyncer 2022-04-29 06:26:43 +00:00
lash
b52d69c3f9 Correct chainlib version 2022-04-28 12:44:59 +00:00
lash
1d2656aaed Add settings module 2022-04-28 12:41:08 +00:00
13 changed files with 209 additions and 72 deletions

View File

@@ -1,3 +1,5 @@
- 0.1.2
* add settings object
- 0.1.0 - 0.1.0
* consume non chain-specific code * consume non chain-specific code
- 0.0.1 - 0.0.1

View File

@@ -1,5 +1,6 @@
# standard imports # standard imports
import logging import logging
import os
# external imports # external imports
from chainlib.error import RPCException from chainlib.error import RPCException
@@ -9,7 +10,9 @@ from chainqueue.store.fs import (
IndexStore, IndexStore,
CounterStore, CounterStore,
) )
from chainqueue.error import BackendIntegrityError
from shep.store.file import SimpleFileStoreFactory from shep.store.file import SimpleFileStoreFactory
from shep.error import StateInvalid
# local imports # local imports
from .base import ChaindAdapter from .base import ChaindAdapter
@@ -19,10 +22,11 @@ logg = logging.getLogger(__name__)
class ChaindFsAdapter(ChaindAdapter): class ChaindFsAdapter(ChaindAdapter):
def __init__(self, chain_spec, path, cache_adapter, dispatcher, cache=None, pending_retry_threshold=0, error_retry_threshold=0, digest_bytes=32): def __init__(self, chain_spec, path, cache_adapter, dispatcher, cache=None, pending_retry_threshold=0, error_retry_threshold=0, digest_bytes=32, event_callback=None):
factory = SimpleFileStoreFactory(path).add factory = SimpleFileStoreFactory(path).add
state_store = Status(factory) state_store = Status(factory, allow_invalid=True, event_callback=event_callback)
index_store = IndexStore(path, digest_bytes=digest_bytes) index_path = os.path.join(path, 'tx')
index_store = IndexStore(index_path, digest_bytes=digest_bytes)
counter_store = CounterStore(path) counter_store = CounterStore(path)
super(ChaindFsAdapter, self).__init__(chain_spec, state_store, index_store, counter_store, cache_adapter, dispatcher, cache=cache, pending_retry_threshold=pending_retry_threshold, error_retry_threshold=error_retry_threshold) super(ChaindFsAdapter, self).__init__(chain_spec, state_store, index_store, counter_store, cache_adapter, dispatcher, cache=cache, pending_retry_threshold=pending_retry_threshold, error_retry_threshold=error_retry_threshold)
@@ -33,12 +37,30 @@ class ChaindFsAdapter(ChaindAdapter):
def get(self, tx_hash): def get(self, tx_hash):
v = self.store.get(tx_hash) v = None
try:
v = self.store.get(tx_hash)
except StateInvalid as e:
logg.error('I am just a simple syncer and do not know how to handle the state which the tx {} is in: {}'.format(tx_hash, e))
return None
except FileNotFoundError:
pass
if v ==None:
raise BackendIntegrityError(tx_hash)
return v[1] return v[1]
def upcoming(self): def upcoming(self, limit=0):
return self.store.upcoming() real_limit = 0
in_flight = 0
if limit > 0:
in_flight = self.store.by_state(state=self.store.IN_NETWORK, not_state=self.store.FINAL)
real_limit = limit - len(in_flight)
if real_limit <= 0:
return []
r = self.store.upcoming(limit=real_limit)
logg.info('upcoming returning {} upcoming from limit {} less {} active in-flight txs'.format(len(r), limit, len(in_flight)))
return r
def pending(self): def pending(self):
@@ -49,7 +71,14 @@ class ChaindFsAdapter(ChaindAdapter):
return self.store.deferred() return self.store.deferred()
def failed(self):
return self.store.failed()
def succeed(self, block, tx): def succeed(self, block, tx):
if self.store.is_reserved(tx.hash):
raise QueueLockError(tx.hash)
return self.store.final(tx.hash, block, tx, error=False) return self.store.final(tx.hash, block, tx, error=False)
@@ -57,6 +86,10 @@ class ChaindFsAdapter(ChaindAdapter):
return self.store.final(tx.hash, block, tx, error=True) return self.store.final(tx.hash, block, tx, error=True)
def sendfail(self):
return self.store.fail(tx.hash)
def enqueue(self, tx_hash): def enqueue(self, tx_hash):
return self.store.enqueue(tx_hash) return self.store.enqueue(tx_hash)

View File

@@ -3,4 +3,3 @@ socket_path =
runtime_dir = runtime_dir =
id = id =
data_dir = data_dir =

View File

@@ -0,0 +1,2 @@
[token]
module =

View File

@@ -16,3 +16,7 @@ class ClientBlockError(BlockingIOError):
class ClientInputError(ValueError): class ClientInputError(ValueError):
pass pass
class QueueLockError(Exception):
pass

View File

@@ -1,31 +1,77 @@
# standard imports # standard imports
import logging import logging
import time
# external imports # external imports
from chainlib.status import Status as TxStatus from chainlib.status import Status as TxStatus
from chainsyncer.filter import SyncFilter from chainsyncer.filter import SyncFilter
from chainqueue.error import NotLocalTxError from chainqueue.error import (
NotLocalTxError,
BackendIntegrityError,
)
from chaind.adapters.fs import ChaindFsAdapter
# local imports
from .error import QueueLockError
logg = logging.getLogger(__name__) logg = logging.getLogger(__name__)
class StateFilter(SyncFilter): class StateFilter(SyncFilter):
def __init__(self, adapter, throttler=None): delay_limit = 3.0
self.adapter = adapter
def __init__(self, chain_spec, adapter_path, tx_adapter, throttler=None):
self.chain_spec = chain_spec
self.adapter_path = adapter_path
self.tx_adapter = tx_adapter
self.throttler = throttler self.throttler = throttler
def filter(self, conn, block, tx, session=None): def filter(self, conn, block, tx, session=None):
try: cache_tx = None
cache_tx = self.adapter.get(tx.hash) for i in range(3):
except NotLocalTxError: queue_adapter = None
logg.debug('skipping not local transaction {}'.format(tx.hash)) try:
return False queue_adapter = ChaindFsAdapter(
self.chain_spec,
self.adapter_path,
self.tx_adapter,
None,
)
except BackendIntegrityError as e:
logg.error('adapter instantiation failed: {}, one more try'.format(e))
continue
try:
cache_tx = queue_adapter.get(tx.hash)
except NotLocalTxError:
logg.debug('skipping not local transaction {}'.format(tx.hash))
return False
except BackendIntegrityError as e:
logg.error('adapter instantiation failed: {}, one more try'.format(e))
continue
break
if cache_tx == None:
raise NotLocalTxError(tx.hash)
delay = 0.01
while True:
if delay > self.delay_limit:
raise QueueLockError('The queue lock for tx {} seems to be stuck. Human meddling needed.'.format(tx.hash))
try:
if tx.status == TxStatus.SUCCESS:
queue_adapter.succeed(block, tx)
else:
queue_adapter.fail(block, tx)
break
except QueueLockError as e:
logg.debug('queue item {} is blocked, will retry: {}'.format(tx.hash, e))
time.sleep(delay)
delay *= 2
if tx.status == TxStatus.SUCCESS:
self.adapter.succeed(block, tx)
else:
self.adapter.fail(block, tx)
if self.throttler != None: if self.throttler != None:
self.throttler.dec(tx.hash) self.throttler.dec(tx.hash)

View File

@@ -104,7 +104,6 @@ class SessionController:
logg.error('invalid input "{}"'.format(data_in_str)) logg.error('invalid input "{}"'.format(data_in_str))
raise ClientInputError() raise ClientInputError()
logg.info('recv {} bytes'.format(len(data)))
return (srvs, data,) return (srvs, data,)

View File

@@ -4,32 +4,27 @@ import os
import uuid import uuid
# external imports # external imports
from chainlib.chain import ChainSpec
from chainsyncer.settings import ChainsyncerSettings from chainsyncer.settings import ChainsyncerSettings
from chainqueue.settings import ChainqueueSettings
logg = logging.getLogger(__name__) logg = logging.getLogger(__name__)
class ChaindSettings(ChainsyncerSettings): class ChaindSettings(ChainsyncerSettings, ChainqueueSettings):
def __init__(self, include_sync=False, include_queue=False): def __init__(self, include_sync=False, include_queue=False):
self.o = {} super(ChaindSettings, self).__init__()
self.get = self.o.get
self.include_sync = include_sync self.include_sync = include_sync
self.include_queue = include_queue self.include_queue = include_queue
def process_common(self, config):
self.o['CHAIN_SPEC'] = ChainSpec.from_chain_str(config.get('CHAIN_SPEC'))
def process_session(self, config): def process_session(self, config):
session_id = config.get('SESSION_ID') session_id = config.get('SESSION_ID')
base_dir = os.getcwd() base_dir = os.getcwd()
data_dir = config.get('SESSION_DATA_DIR') data_dir = config.get('SESSION_DATA_DIR')
if data_dir == None: if data_dir == None:
data_dir = os.path.join(base_dir, '.chaind', 'chaind') data_dir = os.path.join(base_dir, '.chaind', 'chaind', self.o.get('CHAIND_BACKEND'))
data_engine_dir = os.path.join(data_dir, config.get('CHAIND_ENGINE')) data_engine_dir = os.path.join(data_dir, config.get('CHAIND_ENGINE'))
os.makedirs(data_engine_dir, exist_ok=True) os.makedirs(data_engine_dir, exist_ok=True)
@@ -55,15 +50,17 @@ class ChaindSettings(ChainsyncerSettings):
fp = os.path.join(data_engine_dir, 'default') fp = os.path.join(data_engine_dir, 'default')
os.symlink(session_dir, fp) os.symlink(session_dir, fp)
data_dir = os.path.join(session_dir, config.get('CHAIND_COMPONENT')) #data_dir = os.path.join(session_dir, config.get('CHAIND_COMPONENT'))
data_dir = session_dir
os.makedirs(data_dir, exist_ok=True) os.makedirs(data_dir, exist_ok=True)
# create volatile dir # create volatile dir
uid = os.getuid() uid = os.getuid()
runtime_dir = config.get('SESSION_RUNTIME_DIR') runtime_dir = config.get('SESSION_RUNTIME_DIR')
if runtime_dir == None: if runtime_dir == None:
runtime_dir = os.path.join('/run', 'user', str(uid), 'chaind') runtime_dir = os.path.join('/run', 'user', str(uid), 'chaind', self.o.get('CHAIND_BACKEND'))
runtime_dir = os.path.join(runtime_dir, config.get('CHAIND_ENGINE'), session_id, config.get('CHAIND_COMPONENT')) #runtime_dir = os.path.join(runtime_dir, config.get('CHAIND_ENGINE'), session_id, config.get('CHAIND_COMPONENT'))
runtime_dir = os.path.join(runtime_dir, config.get('CHAIND_ENGINE'), session_id)
os.makedirs(runtime_dir, exist_ok=True) os.makedirs(runtime_dir, exist_ok=True)
self.o['SESSION_RUNTIME_DIR'] = runtime_dir self.o['SESSION_RUNTIME_DIR'] = runtime_dir
@@ -91,25 +88,38 @@ class ChaindSettings(ChainsyncerSettings):
def process_dispatch(self, config): def process_dispatch(self, config):
self.o['SESSION_DISPATCH_DELAY'] = 0.01 self.o['SESSION_DISPATCH_DELAY'] = 0.01
def process_token(self, config):
self.o['TOKEN_MODULE'] = config.get('TOKEN_MODULE')
def process_backend(self, config):
if self.include_sync and self.include_queue:
if self.o['QUEUE_BACKEND'] != self.o['SYNCER_BACKEND']:
raise ValueError('queue and syncer backends must match. queue "{}" != syncer "{}"'.format(self.o['QUEUE_BACKEND'], self.o['SYNCER_BACKEND']))
self.o['CHAIND_BACKEND'] = self.o['SYNCER_BACKEND']
elif self.include_sync:
self.o['CHAIND_BACKEND'] = self.o['SYNCER_BACKEND']
elif self.include_queue:
self.o['CHAIND_BACKEND'] = self.o['QUEUE_BACKEND']
else:
raise ValueError('at least one backend must be set')
def process(self, config): def process(self, config):
self.process_common(config) super(ChaindSettings, self).process(config)
self.process_session(config)
self.process_socket(config)
if self.include_sync: if self.include_sync:
self.process_sync(config) self.process_sync(config)
self.process_sync_backend(config)
if self.include_queue: if self.include_queue:
self.process_queue_backend(config)
self.process_dispatch(config) self.process_dispatch(config)
self.process_token(config)
self.process_backend(config)
self.process_session(config)
self.process_socket(config)
def dir_for(self, k): def dir_for(self, k):
return os.path.join(self.o['SESSION_DIR'], k) return os.path.join(self.o['SESSION_DIR'], k)
def __str__(self):
ks = list(self.o.keys())
ks.sort()
s = ''
for k in ks:
s += '{}: {}\n'.format(k, self.o.get(k))
return s

View File

@@ -1,5 +1,4 @@
# standard imports # standard imports
import unittest
import hashlib import hashlib
import tempfile import tempfile
@@ -9,9 +8,6 @@ from chainlib.status import Status as TxStatus
from chainlib.chain import ChainSpec from chainlib.chain import ChainSpec
from chainlib.error import RPCException from chainlib.error import RPCException
# local imports
from chaind.adapters.fs import ChaindFsAdapter
class MockCacheAdapter(CacheTokenTx): class MockCacheAdapter(CacheTokenTx):
@@ -33,7 +29,9 @@ class MockDispatcher:
def send(self, v): def send(self, v):
if v not in self.fails: import sys
sys.stderr.write('susu v {} {}\n'.format(v, self.fails))
if v in self.fails:
raise RPCException('{} is in fails'.format(v)) raise RPCException('{} is in fails'.format(v))
pass pass
@@ -43,12 +41,3 @@ class MockTx:
def __init__(self, tx_hash, status=TxStatus.SUCCESS): def __init__(self, tx_hash, status=TxStatus.SUCCESS):
self.hash = tx_hash self.hash = tx_hash
self.status = status self.status = status
class TestChaindFsBase(unittest.TestCase):
def setUp(self):
self.chain_spec = ChainSpec('foo', 'bar', 42, 'baz')
self.path = tempfile.mkdtemp()
self.adapter = ChaindFsAdapter(self.chain_spec, self.path, self.cache_adapter, self.dispatcher)

31
chaind/unittest/fs.py Normal file
View File

@@ -0,0 +1,31 @@
# standard imports
import unittest
import tempfile
import logging
# external imports
from chainlib.chain import ChainSpec
# local imports
from chaind.adapters.fs import ChaindFsAdapter
logging.STATETRACE = 5
logg = logging.getLogger(__name__)
logg.setLevel(logging.STATETRACE)
class TestChaindFsBase(unittest.TestCase):
def setUp(self):
self.chain_spec = ChainSpec('foo', 'bar', 42, 'baz')
self.path = tempfile.mkdtemp()
self.adapter = ChaindFsAdapter(self.chain_spec, self.path, self.cache_adapter, self.dispatcher, event_callback=self.log_state)
def log_state(self, k, from_state, to_state):
logg.log(logging.STATETRACE, 'state change {}: {} -> {}'.format(
k,
from_state,
to_state,
)
)

View File

@@ -1,6 +1,6 @@
chainlib~=0.1.0 chainlib~=0.1.1
chainqueue~=0.1.2 chainqueue~=0.1.6
chainsyncer~=0.3.6 chainsyncer~=0.4.3
confini~=0.6.0 confini~=0.6.0
funga~=0.5.2 funga~=0.5.2
pyxdg~=0.26 pyxdg~=0.26

View File

@@ -1,7 +1,7 @@
[metadata] [metadata]
name = chaind name = chaind
version = 0.1.0 version = 0.2.0
description = Base package for chain queue servicek description = Base package for chain queue service
author = Louis Holbrook author = Louis Holbrook
author_email = dev@holbrook.no author_email = dev@holbrook.no
url = https://gitlab.com/chaintool/chaind url = https://gitlab.com/chaintool/chaind
@@ -23,7 +23,7 @@ licence_files =
LICENSE LICENSE
[options] [options]
python_requires = >= 3.6 python_requires = >= 3.7
include_package_data = True include_package_data = True
packages = packages =
chaind chaind

View File

@@ -15,15 +15,14 @@ from chaind.filter import StateFilter
from chaind.unittest.common import ( from chaind.unittest.common import (
MockTx, MockTx,
MockCacheAdapter, MockCacheAdapter,
TestChaindFsBase, MockDispatcher,
) )
from chaind.unittest.fs import TestChaindFsBase
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger() logg = logging.getLogger()
class TestChaindFs(TestChaindFsBase): class TestChaindFs(TestChaindFsBase):
def setUp(self): def setUp(self):
@@ -43,12 +42,15 @@ class TestChaindFs(TestChaindFsBase):
self.assertEqual(data, v) self.assertEqual(data, v)
def test_fs_defer(self): def test_fs_fail(self):
data = os.urandom(128).hex() data = os.urandom(128).hex()
hsh = self.adapter.put(data) hsh = self.adapter.put(data)
self.dispatcher.add_fail(hsh) self.dispatcher.add_fail(data)
self.adapter.dispatch(hsh)
txs = self.adapter.deferred() r = self.adapter.dispatch(hsh)
self.assertFalse(r)
txs = self.adapter.failed()
self.assertEqual(len(txs), 1) self.assertEqual(len(txs), 1)
@@ -88,5 +90,25 @@ class TestChaindFs(TestChaindFsBase):
fltr.filter(None, None, tx) fltr.filter(None, None, tx)
def test_upcoming(self):
drv = QueueDriver(self.adapter)
txs = []
for i in range(10):
data = os.urandom(128).hex()
hsh = self.adapter.put(data)
txs.append(hsh)
self.adapter.enqueue(hsh)
r = self.adapter.upcoming(limit=5)
self.assertEqual(len(r), 5)
r = self.adapter.dispatch(txs[0])
self.assertTrue(r)
r = self.adapter.upcoming(limit=5)
self.assertEqual(len(r), 4)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()