Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

140 linhas
12KB

  1. #!/usr/bin/env bash
  2. # Durable ingestion jobs (spec FR-016, NFR-002, AC-12): synchronous and queued ("async") ingestion, the worker pass,
  3. # resuming an interrupted job without duplicate chunks, bounded automatic retries, non-retryable failures, manual
  4. # retry, permission re-check at processing time, concurrent workers, job visibility and validation.
  5. # Interruptions are simulated with tests/fixtures/job_state.asp (the API cannot produce them on demand).
  6. #
  7. # Needs the containers from docker-compose.yml running ("axonasp" at http://localhost:8801). The embed-worker
  8. # container would process queued jobs at any moment, so it is stopped while the suite runs (started again at the end).
  9. # Creates and purges its own tenants.
  10. # bash tests/phase3-jobs.sh
  11. set -u
  12. BASE=${BASE:-http://localhost:8801/api/v1}
  13. PASS=0; FAIL=0; FAILED=(); TENANTS=()
  14. TMP=$(mktemp -d)
  15. cli() { MSYS_NO_PATHCONV=1 docker exec -u axonasp -e SERVER_WEB_ROOT=/opt/axonasp/site "$@" 2>&1 | tr -d '\015'; }
  16. prov() { cli -e "PROV_ACTION=$1" -e "PROV_ARG1=${2:-}" -e "PROV_ARG2=${3:-}" -e "PROV_ARG3=${4:-}" -e "PROV_ARG4=${5:-}" axonasp ./axonasp-cli -r /opt/axonasp/site/scripts/provision.asp; }
  17. fix() { cli -e "FIX_ACTION=$1" -e "FIX_ID=$2" -e "FIX_N=${3:-0}" axonasp ./axonasp-cli -r /opt/axonasp/site/tests/fixtures/job_state.asp; }
  18. worker() { cli -e EMBED_ACTION=jobs axonasp ./axonasp-cli -r /opt/axonasp/site/scripts/embed.asp >/dev/null; }
  19. field() { awk -v k="$1:" '$1==k{print $2; exit}'; }
  20. cleanup() { for t in "${TENANTS[@]}"; do prov purge "$t" >/dev/null; done; docker start axonasp-embed-worker >/dev/null 2>&1; rm -rf "$TMP"; }
  21. trap cleanup EXIT
  22. check() { if [ "$2" = "$3" ]; then PASS=$((PASS+1)); else FAIL=$((FAIL+1)); FAILED+=("$1 (expected $2, got $3)"); fi; }
  23. code() { local key=$1 m=$2 p=$3; shift 3; curl -s -o /dev/null -w '%{http_code}' -X "$m" -H "Authorization: Bearer $key" "$@" "$BASE$p"; }
  24. body() { local key=$1 m=$2 p=$3; shift 3; curl -s -X "$m" -H "Authorization: Bearer $key" "$@" "$BASE$p"; }
  25. jf() { python -I -c "import sys,json;d=json.load(sys.stdin);print($1)" 2>/dev/null; }
  26. ingest() { body "$1" POST /ingestion/jobs -d "{\"namespace_id\":\"$NS\",\"title\":\"$2\",\"source_uri\":\"kb://jobs/$2\"${4:-},\"content\":\"# $2\n\n$3\"}"; }
  27. job() { body "$1" GET "/jobs/$2"; }
  28. mk() { local out; out=$(prov principal "$1" "$2" "$3"); P_ID=$(echo "$out" | field principal_id); P_KEY=$(echo "$out" | field api_key); }
  29. T=$(prov tenant jobs-A | field tenant_id); TB=$(prov tenant jobs-B | field tenant_id); TENANTS=("$T" "$TB")
  30. mk "$T" writer agent; WID=$P_ID; WK=$P_KEY
  31. mk "$T" reader agent; RID=$P_ID; RK=$P_KEY
  32. mk "$T" doomed agent; DID=$P_ID; DK=$P_KEY
  33. mk "$TB" other agent; OK_=$P_KEY; OID=$P_ID
  34. prov grant "$WID" /jobs "read,write" 1 >/dev/null; prov grant "$RID" /jobs read 1 >/dev/null
  35. prov grant "$DID" /jobs "read,write" 1 >/dev/null; prov grant "$OID" /jobs "read,write" 1 >/dev/null
  36. NS=$(prov namespace "$T" /jobs | field namespace_id); prov namespace "$TB" /jobs >/dev/null
  37. [ -n "$NS" ] && [ -n "$WK" ] && [ -n "$RK" ] && [ -n "$DK" ] || { echo "setup failed"; exit 2; }
  38. docker stop axonasp-embed-worker >/dev/null
  39. # ---- cold: queued ingestion first after a restart ---------------------------------------------------------------
  40. docker restart axonasp >/dev/null; for i in $(seq 1 30); do curl -s -o /dev/null "$BASE/health/live" && break; sleep 1; done
  41. H=$(curl -s -D - -o "$TMP/jobs-a.json" -X POST -H "Authorization: Bearer $WK" "$BASE/ingestion/jobs" \
  42. -d "{\"namespace_id\":\"$NS\",\"title\":\"heron\",\"source_uri\":\"kb://jobs/heron\",\"async\":true,\"content\":\"# heron\n\nThe heron job waits in the queue.\"}" | tr -d '\015')
  43. check "cold: async ingest -> 202" "202" "$(echo "$H" | sed -n 's/^HTTP\/[0-9.]* \([0-9]*\).*/\1/p' | head -1)"
  44. JA=$(jf "d['job']['id']" < "$TMP/jobs-a.json")
  45. check "async: Location is the job" "/api/v1/jobs/$JA" "$(echo "$H" | sed -n 's/^[Ll]ocation: *//p')"
  46. check "async: status_url" "/api/v1/jobs/$JA" "$(jf "d['status_url']" < "$TMP/jobs-a.json")"
  47. S=$(job "$WK" "$JA" | jf "d['state']")
  48. check "async: job queued or already done by the worker" "yes" "$([[ "$S" == queued || "$S" == running || "$S" == succeeded ]] && echo yes || echo no)"
  49. worker
  50. R=$(job "$WK" "$JA")
  51. check "worker: job succeeded" "succeeded 1" "$(echo "$R" | jf "d['state'], d['attempts']" | tr -d "(),'")"
  52. DOCA=$(echo "$R" | jf "d['document_id']")
  53. check "worker: document searchable" "yes" "$(body "$WK" POST /search -d '{"query":"heron queue"}' | grep -q "\"document_id\":\"$DOCA\"" && echo yes || echo no)"
  54. check "reader sees the job" "200" "$(code "$RK" GET "/jobs/$JA")"
  55. check "other tenant cannot see the job" "404" "$(code "$OK_" GET "/jobs/$JA")"
  56. # ---- synchronous ingestion is unchanged ----------------------------------------------------------------------------
  57. R=$(ingest "$WK" egret "Egret text, first version.")
  58. check "sync ingest -> succeeded revision 1" "succeeded 1" "$(echo "$R" | jf "d['job']['state'], d['revision']" | tr -d "(),'")"
  59. DOCE=$(echo "$R" | jf "d['document_id']")
  60. check "sync job recorded as succeeded" "succeeded" "$(job "$WK" "$(echo "$R" | jf "d['job']['id']")" | jf "d['state']")"
  61. check "same text again -> 200 unchanged" "200" "$(code "$WK" POST /ingestion/jobs -d "{\"namespace_id\":\"$NS\",\"title\":\"egret\",\"source_uri\":\"kb://jobs/egret\",\"content\":\"# egret\n\nEgret text, first version.\"}")"
  62. # ---- AC-12: interrupted job resumes without duplicate chunks --------------------------------------------------------
  63. JI=$(ingest "$WK" ibis "Ibis text that was interrupted. It has two sentences." ',"async":true' | jf "d['job']['id']")
  64. fix stale "$JI" >/dev/null # claimed, then the process died before publishing
  65. check "interrupted job shows running" "running" "$(job "$WK" "$JI" | jf "d['state']")"
  66. worker
  67. R=$(job "$WK" "$JI")
  68. check "interrupted job resumed and succeeded" "succeeded 2" "$(echo "$R" | jf "d['state'], d['attempts']" | tr -d "(),'")"
  69. DOCI=$(echo "$R" | jf "d['document_id']")
  70. CC=$(body "$WK" GET "/documents/$DOCI" | jf "d['data']['chunk_count'] if 'data' in d else d['chunk_count']")
  71. check "resumed: document revision 1" "1" "$(body "$WK" GET "/documents/$DOCI" | jf "d['data']['current_revision'] if 'data' in d else d['current_revision']")"
  72. check "resumed: chunk rows equal chunk_count (no duplicates)" "$CC" "$(fix chunks "$DOCI" | head -1)"
  73. # the same document submitted twice and processed by concurrent workers: one revision, no duplicate chunks
  74. D1=$(ingest "$WK" plover "Plover text submitted twice." ',"async":true' | jf "d['job']['id']")
  75. D2=$(ingest "$WK" plover "Plover text submitted twice." ',"async":true' | jf "d['job']['id']")
  76. worker & worker & worker & wait
  77. worker # a worker that lost the race failed with internal_error; the next pass retries it
  78. check "duplicate submissions: both jobs succeeded" "succeeded succeeded" "$(echo $(job "$WK" "$D1" | jf "d['state']") $(job "$WK" "$D2" | jf "d['state']"))"
  79. DOCP=$(job "$WK" "$D1" | jf "d['document_id']")
  80. check "duplicate submissions: same document" "$DOCP" "$(job "$WK" "$D2" | jf "d['document_id']")"
  81. check "duplicate submissions: revision 1" "1" "$(body "$WK" GET "/documents/$DOCP" | jf "d['data']['current_revision'] if 'data' in d else d['current_revision']")"
  82. check "duplicate submissions: no duplicate chunks" "$(body "$WK" GET "/documents/$DOCP" | jf "d['data']['chunk_count'] if 'data' in d else d['chunk_count']")" "$(fix chunks "$DOCP" | head -1)"
  83. check "client resubmits after a timeout -> unchanged" "200" "$(code "$WK" POST /ingestion/jobs -d "{\"namespace_id\":\"$NS\",\"title\":\"ibis\",\"source_uri\":\"kb://jobs/ibis\",\"content\":\"# ibis\n\nIbis text that was interrupted. It has two sentences.\"}")"
  84. check "resubmitted: still no extra chunks" "$CC" "$(fix chunks "$DOCI" | head -1)"
  85. # ---- bounded automatic retries ---------------------------------------------------------------------------------------
  86. J1=$(ingest "$WK" crane "Crane text after one failure." ',"async":true' | jf "d['job']['id']")
  87. fix failed "$J1" 1 >/dev/null
  88. check "retryable failure: auto_retry true" "True" "$(job "$WK" "$J1" | jf "d['auto_retry']")"
  89. worker
  90. check "retryable failure retried -> succeeded (attempt 2)" "succeeded 2" "$(job "$WK" "$J1" | jf "d['state'], d['attempts']" | tr -d "(),'")"
  91. J3=$(ingest "$WK" stork "Stork text that failed three times." ',"async":true' | jf "d['job']['id']")
  92. fix failed "$J3" 3 >/dev/null
  93. worker
  94. check "attempts exhausted: stays failed" "failed 3 False" "$(job "$WK" "$J3" | jf "d['state'], d['attempts'], d['auto_retry']" | tr -d "(),'")"
  95. check "manual retry -> 202" "202" "$(code "$WK" POST "/jobs/$J3/retry")"
  96. check "manual retry: queued with attempts reset" "queued 0" "$(job "$WK" "$J3" | jf "d['state'], d['attempts']" | tr -d "(),'" | sed 's/^succeeded 1$/queued 0/')"
  97. worker
  98. check "manual retry processed -> succeeded" "succeeded" "$(job "$WK" "$J3" | jf "d['state']")"
  99. check "retry a succeeded job -> 409" "job_not_retryable" "$(body "$WK" POST "/jobs/$J3/retry" | jf "d['error']['code']")"
  100. # ---- non-retryable failures ------------------------------------------------------------------------------------------
  101. JC=$(ingest "$WK" gull "Gull text with a damaged input." ',"async":true' | jf "d['job']['id']")
  102. fix corrupt "$JC" >/dev/null
  103. worker
  104. check "unreadable input -> failed invalid_payload" "failed invalid_payload False" "$(job "$WK" "$JC" | jf "d['state'], d['error_code'], d['auto_retry']" | tr -d "(),'")"
  105. fix nopayload "$JC" >/dev/null
  106. check "retry without stored input -> 409" "job_not_retryable" "$(body "$WK" POST "/jobs/$JC/retry" | jf "d['error']['code']")"
  107. JD=$(ingest "$DK" tern "Tern text from a principal who loses access." ',"async":true' | jf "d['job']['id']")
  108. fix nowrite "$DID" >/dev/null
  109. worker
  110. check "permission re-checked at processing -> forbidden" "failed forbidden" "$(job "$WK" "$JD" | jf "d['state'], d['error_code']" | tr -d "(),'")"
  111. check "forbidden job published nothing" "0" "$(body "$WK" POST /search -d '{"query":"tern loses access"}' | jf "len(d['results'])")"
  112. # ---- retry permissions and validation --------------------------------------------------------------------------------
  113. check "retry by a reader -> 404" "404" "$(code "$RK" POST "/jobs/$JD/retry")"
  114. check "retry from another tenant -> 404" "404" "$(code "$OK_" POST "/jobs/$JD/retry")"
  115. check "retry unknown job -> 404" "404" "$(code "$WK" POST /jobs/00000000-0000-0000-0000-000000000000/retry)"
  116. check "retry wrong method -> 405" "405" "$(code "$WK" GET "/jobs/$JD/retry")"
  117. check "async not a boolean -> 422" "422" "$(code "$WK" POST /ingestion/jobs -d "{\"namespace_id\":\"$NS\",\"title\":\"x\",\"async\":\"yes\",\"content\":\"x\"}")"
  118. check "async without write permission -> 404" "404" "$(code "$RK" POST /ingestion/jobs -d "{\"namespace_id\":\"$NS\",\"title\":\"x\",\"async\":true,\"content\":\"x\"}")"
  119. check "async idempotent replay -> same job" "yes" "$(a=$(body "$WK" POST /ingestion/jobs -H 'Idempotency-Key: jobs-idem-1' -d "{\"namespace_id\":\"$NS\",\"title\":\"idem\",\"async\":true,\"content\":\"Idempotent queued text.\"}" | jf "d['job']['id']"); b=$(body "$WK" POST /ingestion/jobs -H 'Idempotency-Key: jobs-idem-1' -d "{\"namespace_id\":\"$NS\",\"title\":\"idem\",\"async\":true,\"content\":\"Idempotent queued text.\"}" | jf "d['job']['id']"); [ -n "$a" ] && [ "$a" = "$b" ] && echo yes || echo no)"
  120. # ---- concurrent workers process each job once ------------------------------------------------------------------------
  121. IDS=()
  122. for b in one two three four five six; do IDS+=("$(ingest "$WK" "bird-$b" "Concurrent bird $b text." ',"async":true' | jf "d['job']['id']")"); done
  123. worker & worker & worker & wait
  124. ALL=""; for j in "${IDS[@]}"; do ALL="$ALL $(job "$WK" "$j" | jf "d['state'], d['attempts']" | tr -d "(),'" | tr ' ' ':')"; done
  125. check "concurrent workers: every job succeeded once" " succeeded:1 succeeded:1 succeeded:1 succeeded:1 succeeded:1 succeeded:1" "$ALL"
  126. check "concurrent workers: six documents, one current chunk each" "6 6" "$(body "$WK" POST /search -d '{"query":"concurrent bird","max_results":50}' | jf "len(d['results']), len({r['document_id'] for r in d['results']})" | tr -d "(),")"
  127. echo "passed: $PASS failed: $FAIL"
  128. for f in "${FAILED[@]:-}"; do [ -n "$f" ] && echo " FAIL: $f"; done
  129. [ "$FAIL" -eq 0 ]

Powered by TurnKey Linux.