feat: persist verification tasks inputs and attempts
This commit is contained in:
1 parent
0406028a02
commit
f5d39f6261
3 files changed
+270
No files matched your search
@@ -65,6 +65,8 @@ class StoreSchema:
|
|||||||
from .execution_journal import create_execution_schema
|
from .execution_journal import create_execution_schema
|
||||||
create_history_schema(self._store._connection)
|
create_history_schema(self._store._connection)
|
||||||
create_execution_schema(self._store._connection)
|
create_execution_schema(self._store._connection)
|
||||||
|
from .verification.store import create_schema as create_verification_schema
|
||||||
|
create_verification_schema(self._store._connection)
|
||||||
|
|
||||||
def _create_schema_v1(self) -> None:
|
def _create_schema_v1(self) -> None:
|
||||||
statements = (
|
statements = (
|
||||||
|
|||||||
@@ -0,0 +1,185 @@
|
|||||||
|
"""Transactional verification records; replacement receipts are never modified."""
|
||||||
|
from datetime import datetime, timezone
|
||||||
|
import json
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
from .contracts import ACTIVE, TERMINAL, digest, fail, identifier, validate_input
|
||||||
|
from ..errors import TaskCenterError
|
||||||
|
|
||||||
|
|
||||||
|
def now():
|
||||||
|
return datetime.now(timezone.utc).isoformat()
|
||||||
|
|
||||||
|
|
||||||
|
def encode(value):
|
||||||
|
return json.dumps(value, ensure_ascii=False, sort_keys=True, allow_nan=False)
|
||||||
|
|
||||||
|
|
||||||
|
def create_schema(c):
|
||||||
|
statements = (
|
||||||
|
'''CREATE TABLE IF NOT EXISTS verification_tasks (
|
||||||
|
task_id TEXT PRIMARY KEY, name TEXT NOT NULL, created TEXT NOT NULL,
|
||||||
|
view_revision INTEGER NOT NULL DEFAULT 0)''',
|
||||||
|
'''CREATE TABLE IF NOT EXISTS verification_items (
|
||||||
|
item_id TEXT PRIMARY KEY, task_id TEXT NOT NULL REFERENCES verification_tasks(task_id),
|
||||||
|
item_key TEXT NOT NULL, project_key TEXT NOT NULL, before_id TEXT,
|
||||||
|
display_name TEXT NOT NULL, current_version INTEGER NOT NULL,
|
||||||
|
UNIQUE(task_id,item_key))''',
|
||||||
|
'''CREATE TABLE IF NOT EXISTS verification_input_revisions (
|
||||||
|
item_id TEXT NOT NULL REFERENCES verification_items(item_id), input_version INTEGER NOT NULL,
|
||||||
|
payload_json TEXT NOT NULL, payload_digest TEXT NOT NULL, created TEXT NOT NULL,
|
||||||
|
PRIMARY KEY(item_id,input_version))''',
|
||||||
|
'''CREATE TABLE IF NOT EXISTS verification_attempts (
|
||||||
|
attempt_id TEXT PRIMARY KEY, item_id TEXT NOT NULL, input_version INTEGER NOT NULL,
|
||||||
|
generation INTEGER NOT NULL, state TEXT NOT NULL, owner_id TEXT, run_id TEXT,
|
||||||
|
result_json TEXT, reason TEXT NOT NULL DEFAULT '', resource_state TEXT NOT NULL DEFAULT 'not_started',
|
||||||
|
created TEXT NOT NULL, updated TEXT NOT NULL,
|
||||||
|
FOREIGN KEY(item_id,input_version) REFERENCES verification_input_revisions(item_id,input_version))''',
|
||||||
|
'''CREATE UNIQUE INDEX IF NOT EXISTS verification_active_attempt ON verification_attempts(item_id,input_version)
|
||||||
|
WHERE state IN ('queued','running','publishing','stopping')''',
|
||||||
|
'''CREATE TABLE IF NOT EXISTS verification_runs (
|
||||||
|
run_id TEXT PRIMARY KEY, task_id TEXT NOT NULL REFERENCES verification_tasks(task_id),
|
||||||
|
members_json TEXT NOT NULL, stop_requested INTEGER NOT NULL DEFAULT 0,
|
||||||
|
state TEXT NOT NULL DEFAULT 'queued', created TEXT NOT NULL)''',
|
||||||
|
'''CREATE TABLE IF NOT EXISTS verification_requests (
|
||||||
|
kind TEXT NOT NULL, request_id TEXT NOT NULL, request_hash TEXT NOT NULL, result_json TEXT NOT NULL,
|
||||||
|
PRIMARY KEY(kind,request_id))''',
|
||||||
|
)
|
||||||
|
for statement in statements: c.execute(statement)
|
||||||
|
|
||||||
|
|
||||||
|
class VerificationRepository:
|
||||||
|
def __init__(self, task_store):
|
||||||
|
self.store = task_store
|
||||||
|
with task_store.transaction(): create_schema(task_store._connection)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def connection(self): return self.store._connection
|
||||||
|
|
||||||
|
def _request(self, kind, request_id, body):
|
||||||
|
identifier(request_id)
|
||||||
|
row = self.connection.execute('SELECT * FROM verification_requests WHERE kind=? AND request_id=?',
|
||||||
|
(kind,request_id)).fetchone()
|
||||||
|
if row:
|
||||||
|
if row['request_hash'] != digest(body): fail('相同请求标识对应不同内容。','verification_conflict')
|
||||||
|
return json.loads(row['result_json'])
|
||||||
|
|
||||||
|
def _remember(self, kind, request_id, body, result):
|
||||||
|
self.connection.execute('INSERT INTO verification_requests VALUES (?,?,?,?)',
|
||||||
|
(kind,request_id,digest(body),encode(result)))
|
||||||
|
return result
|
||||||
|
|
||||||
|
def _touch(self, task_id):
|
||||||
|
self.connection.execute('UPDATE verification_tasks SET view_revision=view_revision+1 WHERE task_id=?',(task_id,))
|
||||||
|
|
||||||
|
def _one(self, table, key, value):
|
||||||
|
with self.store._lock:
|
||||||
|
row = self.connection.execute(f'SELECT * FROM {table} WHERE {key}=?',(value,)).fetchone()
|
||||||
|
if row is None: fail('找不到校验记录。','verification_not_found')
|
||||||
|
return dict(row)
|
||||||
|
|
||||||
|
def task(self, task_id): return self._one('verification_tasks','task_id',task_id)
|
||||||
|
def item(self, item_id): return self._one('verification_items','item_id',item_id)
|
||||||
|
|
||||||
|
def revision(self, item_id, version):
|
||||||
|
with self.store._lock:
|
||||||
|
row = self.connection.execute('SELECT * FROM verification_input_revisions WHERE item_id=? AND input_version=?',
|
||||||
|
(item_id,version)).fetchone()
|
||||||
|
if row is None: fail('输入版本不存在。','verification_not_found')
|
||||||
|
return dict(row) | {'payload':json.loads(row['payload_json'])}
|
||||||
|
|
||||||
|
def attempt(self, attempt_id):
|
||||||
|
row = self._one('verification_attempts','attempt_id',attempt_id)
|
||||||
|
row['result'] = json.loads(row['result_json']) if row['result_json'] else None
|
||||||
|
return row
|
||||||
|
|
||||||
|
def latest_attempt(self, item_id, version):
|
||||||
|
with self.store._lock:
|
||||||
|
row = self.connection.execute('SELECT attempt_id FROM verification_attempts WHERE item_id=? AND input_version=? ORDER BY generation DESC LIMIT 1',
|
||||||
|
(item_id,version)).fetchone()
|
||||||
|
return self.attempt(row[0]) if row else None
|
||||||
|
|
||||||
|
def create_task(self, name, *, request_id):
|
||||||
|
name = identifier(name).strip()
|
||||||
|
with self.store.transaction():
|
||||||
|
old = self._request('create',request_id,name)
|
||||||
|
if old is not None: return old
|
||||||
|
task_id = uuid4().hex
|
||||||
|
self.connection.execute('INSERT INTO verification_tasks(task_id,name,created) VALUES (?,?,?)',(task_id,name,now()))
|
||||||
|
return self._remember('create',request_id,name,self.task(task_id))
|
||||||
|
|
||||||
|
def publish(self, task_id, inputs, *, submission_id):
|
||||||
|
body = {'task_id':task_id,'inputs':inputs}
|
||||||
|
with self.store.transaction():
|
||||||
|
self.task(task_id)
|
||||||
|
old = self._request('publish',submission_id,body)
|
||||||
|
if old is not None: return old
|
||||||
|
result = {key:[] for key in ('accepted','duplicates','stale_ignored','rejected')}
|
||||||
|
for index,value in enumerate(inputs):
|
||||||
|
try:
|
||||||
|
row = validate_input(value)
|
||||||
|
item = self.connection.execute('SELECT * FROM verification_items WHERE task_id=? AND item_key=?',
|
||||||
|
(task_id,row['item_key'])).fetchone()
|
||||||
|
before_id = row['before_ref']['manifest_id'] if row['before_ref'] else None
|
||||||
|
if item:
|
||||||
|
if item['project_key'] != row['project_key'] or (item['before_id'] and item['before_id'] != before_id):
|
||||||
|
fail('工程身份或原始基线变化,请新增比较项。','verification_conflict')
|
||||||
|
item_id = item['item_id']
|
||||||
|
existing = self.connection.execute('SELECT payload_digest FROM verification_input_revisions WHERE item_id=? AND input_version=?',
|
||||||
|
(item_id,row['input_version'])).fetchone()
|
||||||
|
ref = {'item_id':item_id,'input_version':row['input_version']}
|
||||||
|
if existing:
|
||||||
|
if existing[0] != row['payload_digest']: fail('同一版本内容冲突。','verification_conflict')
|
||||||
|
result['duplicates'].append(ref); continue
|
||||||
|
if row['input_version'] < item['current_version']:
|
||||||
|
result['stale_ignored'].append(ref); continue
|
||||||
|
else:
|
||||||
|
item_id = uuid4().hex
|
||||||
|
self.connection.execute('INSERT INTO verification_items VALUES (?,?,?,?,?,?,?)',
|
||||||
|
(item_id,task_id,row['item_key'],row['project_key'],before_id,row['display_name'],row['input_version']))
|
||||||
|
self.connection.execute('INSERT INTO verification_input_revisions VALUES (?,?,?,?,?)',
|
||||||
|
(item_id,row['input_version'],encode(row),row['payload_digest'],now()))
|
||||||
|
self.connection.execute('UPDATE verification_items SET current_version=?,before_id=COALESCE(before_id,?),display_name=? WHERE item_id=?',
|
||||||
|
(row['input_version'],before_id,row['display_name'],item_id))
|
||||||
|
result['accepted'].append({'item_id':item_id,'input_version':row['input_version']})
|
||||||
|
self._touch(task_id)
|
||||||
|
except TaskCenterError as error:
|
||||||
|
result['rejected'].append({'index':index,'code':error.code,'reason':error.message})
|
||||||
|
return self._remember('publish',submission_id,body,result)
|
||||||
|
|
||||||
|
def create_attempt(self, item_id, version, *, request_id, run_id=None):
|
||||||
|
body = [item_id,version]
|
||||||
|
with self.store.transaction():
|
||||||
|
old = self._request('attempt',request_id,body)
|
||||||
|
if old is not None: return old
|
||||||
|
item = self.item(item_id)
|
||||||
|
revision = self.revision(item_id,version)
|
||||||
|
if revision['payload']['input_state'] != 'ready': fail('前后输入不可用。')
|
||||||
|
current = self.latest_attempt(item_id,version)
|
||||||
|
if current and current['state'] in ACTIVE:
|
||||||
|
return self._remember('attempt',request_id,body,current)
|
||||||
|
generation = (current['generation'] + 1) if current else 1
|
||||||
|
attempt_id, stamp = uuid4().hex, now()
|
||||||
|
self.connection.execute('''INSERT INTO verification_attempts
|
||||||
|
(attempt_id,item_id,input_version,generation,state,run_id,created,updated)
|
||||||
|
VALUES (?,?,?,?,'queued',?,?,?)''',(attempt_id,item_id,version,generation,run_id,stamp,stamp))
|
||||||
|
self._touch(item['task_id'])
|
||||||
|
return self._remember('attempt',request_id,body,self.attempt(attempt_id))
|
||||||
|
|
||||||
|
def claim(self, attempt_id, *, owner_id):
|
||||||
|
with self.store.transaction():
|
||||||
|
changed = self.connection.execute("UPDATE verification_attempts SET state='running',owner_id=?,updated=? WHERE attempt_id=? AND state='queued'",
|
||||||
|
(identifier(owner_id),now(),attempt_id)).rowcount
|
||||||
|
return self.attempt(attempt_id) if changed else None
|
||||||
|
|
||||||
|
def finish(self, attempt_id, *, generation, result):
|
||||||
|
with self.store.transaction():
|
||||||
|
attempt = self.attempt(attempt_id)
|
||||||
|
if attempt['generation'] != generation or attempt['state'] not in {'running','publishing'}: return False
|
||||||
|
if result.get('resource_state') != 'released': fail('资源退出尚未确认。','verification_resources')
|
||||||
|
state = result.get('attempt_state','succeeded')
|
||||||
|
if state not in TERMINAL: fail('无效校验终态。')
|
||||||
|
self.connection.execute('UPDATE verification_attempts SET state=?,result_json=?,resource_state=?,updated=? WHERE attempt_id=?',
|
||||||
|
(state,encode(result),'released',now(),attempt_id))
|
||||||
|
self._touch(self.item(attempt['item_id'])['task_id'])
|
||||||
|
return True
|
||||||
@@ -0,0 +1,83 @@
|
|||||||
|
import pytest
|
||||||
|
from dx_productivity.trial_task_center.store import TaskStore
|
||||||
|
from dx_productivity.trial_task_center.errors import TaskCenterError
|
||||||
|
from test_independent_verification_contracts import payload
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def repo(tmp_path):
|
||||||
|
from dx_productivity.trial_task_center.verification.store import VerificationRepository
|
||||||
|
store = TaskStore(tmp_path/'tasks.db')
|
||||||
|
try: yield VerificationRepository(store)
|
||||||
|
finally: store.close()
|
||||||
|
|
||||||
|
|
||||||
|
def test_tasks_are_independent_and_create_is_idempotent(repo):
|
||||||
|
task = repo.create_task('校验', request_id='one')
|
||||||
|
assert repo.create_task('校验',request_id='one') == task
|
||||||
|
assert repo.create_task('校验',request_id='two')['task_id'] != task['task_id']
|
||||||
|
with pytest.raises(TaskCenterError): repo.create_task('changed',request_id='one')
|
||||||
|
assert repo.store.list_tasks() == ()
|
||||||
|
|
||||||
|
|
||||||
|
def test_versions_are_immutable_and_delivery_is_idempotent(repo):
|
||||||
|
task = repo.create_task('校验',request_id='a')['task_id']
|
||||||
|
first = repo.publish(task,(payload(),),submission_id='s1')
|
||||||
|
item = first['accepted'][0]['item_id']
|
||||||
|
assert repo.publish(task,(payload(),),submission_id='s1') == first
|
||||||
|
assert len(repo.publish(task,(payload(),),submission_id='s2')['duplicates']) == 1
|
||||||
|
newer = payload(); newer['input_version'] = 3; newer['after_ref']['sha256'] = '3'*64
|
||||||
|
repo.publish(task,(newer,),submission_id='s3')
|
||||||
|
older = payload(); older['input_version'] = 2
|
||||||
|
assert repo.publish(task,(older,),submission_id='s4')['stale_ignored']
|
||||||
|
assert repo.item(item)['current_version'] == 3
|
||||||
|
bad = payload(); bad['after_ref']['sha256'] = '5'*64
|
||||||
|
assert repo.publish(task,(bad,),submission_id='s5')['rejected']
|
||||||
|
assert repo.revision(item,1)['payload']['after_ref']['sha256'] == '2'*64
|
||||||
|
|
||||||
|
|
||||||
|
def test_bad_item_does_not_erase_other_input_and_bad_task_does_not_publish(repo):
|
||||||
|
task = repo.create_task('校验',request_id='a')['task_id']
|
||||||
|
bad = payload(); bad['input_version'] = -1
|
||||||
|
result = repo.publish(task,(bad,payload()),submission_id='mixed')
|
||||||
|
assert len(result['accepted']) == len(result['rejected']) == 1
|
||||||
|
with pytest.raises(TaskCenterError): repo.publish('missing',(payload(),),submission_id='x')
|
||||||
|
|
||||||
|
|
||||||
|
def test_single_active_attempt_and_late_result_cannot_override_new_attempt(repo):
|
||||||
|
task = repo.create_task('校验',request_id='a')['task_id']
|
||||||
|
item = repo.publish(task,(payload(),),submission_id='s')['accepted'][0]['item_id']
|
||||||
|
first = repo.create_attempt(item,1,request_id='run-1')
|
||||||
|
assert repo.create_attempt(item,1,request_id='run-2')['attempt_id'] == first['attempt_id']
|
||||||
|
claimed = repo.claim(first['attempt_id'],owner_id='owner')
|
||||||
|
assert repo.claim(first['attempt_id'],owner_id='other') is None
|
||||||
|
assert not repo.finish(first['attempt_id'],generation=99,result={'resource_state':'released'})
|
||||||
|
assert repo.finish(first['attempt_id'],generation=claimed['generation'],
|
||||||
|
result={'resource_state':'released','verdict':'DIFFERENT'})
|
||||||
|
second = repo.create_attempt(item,1,request_id='run-3')
|
||||||
|
assert second['generation'] > first['generation']
|
||||||
|
assert not repo.finish(first['attempt_id'],generation=first['generation'],result={'resource_state':'released'})
|
||||||
|
assert repo.attempt(second['attempt_id'])['state'] == 'queued'
|
||||||
|
|
||||||
|
|
||||||
|
def test_old_input_attempt_completion_does_not_move_current_revision(repo):
|
||||||
|
task = repo.create_task('校验',request_id='a')['task_id']
|
||||||
|
item = repo.publish(task,(payload(),),submission_id='s')['accepted'][0]['item_id']
|
||||||
|
attempt = repo.create_attempt(item,1,request_id='run'); repo.claim(attempt['attempt_id'],owner_id='owner')
|
||||||
|
newer = payload(); newer['input_version'] = 2
|
||||||
|
repo.publish(task,(newer,),submission_id='s2')
|
||||||
|
assert repo.finish(attempt['attempt_id'],generation=attempt['generation'],result={'resource_state':'released','verdict':'CONSISTENT'})
|
||||||
|
assert repo.item(item)['current_version'] == 2
|
||||||
|
assert repo.latest_attempt(item,2) is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_schema_survives_reopen_and_does_not_change_execution_tables(tmp_path):
|
||||||
|
from dx_productivity.trial_task_center.verification.store import VerificationRepository
|
||||||
|
path = tmp_path/'tasks.db'
|
||||||
|
store = TaskStore(path); repo = VerificationRepository(store)
|
||||||
|
task = repo.create_task('持久任务',request_id='p')
|
||||||
|
store.close(); store = TaskStore(path)
|
||||||
|
try:
|
||||||
|
assert VerificationRepository(store).task(task['task_id'])['name'] == '持久任务'
|
||||||
|
assert store._connection.execute('SELECT COUNT(*) FROM execution_operations').fetchone()[0] == 0
|
||||||
|
finally: store.close()
|
||||||
Reference in new issue
Block a user