3e7fcce0b1
build / build (push) Successful in 1m11s
新增迁移50与每5分钟/手动持久同步,API只读本地;同步失败保留、权限阻断、租约与质量失效原子激活。 保留全量稳定分页/固定列,已存质量批量读取,元数据不反复解压长答案;详情安全Markdown与原文。 已运行整仓 make verify、源与副本隔离PG -race、真实只读源及隔离浏览器。 实际同步2525条;固定窗口640条本地中位1.46秒,对照直接源33.10秒。 停worker、缺源凭据、重启API后仍可查询;未更新8088,未修改生产原问答。
164 lines
6.0 KiB
Go
164 lines
6.0 KiB
Go
// Package qaopsmirror serves an explicitly synchronized local copy, never an implicit source fallback.
|
|
package qaopsmirror
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
"knowledge-governance/internal/dobotaiops"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
const Interval = 5 * time.Minute
|
|
const localBaseSQL = `WITH records AS (SELECT id,asked_at,question,status,identity,quality_hash,payload FROM qa_ops_mirror_records WHERE tenant_key=$1)`
|
|
|
|
type Store struct {
|
|
pool *pgxpool.Pool
|
|
tenant string
|
|
mu sync.RWMutex
|
|
safety dobotaiops.SourceSafety
|
|
}
|
|
|
|
func NewStore(pool *pgxpool.Pool, tenant string) *Store { return &Store{pool: pool, tenant: tenant} }
|
|
|
|
type Status struct {
|
|
Mode string `json:"mode"`
|
|
Ready bool `json:"ready"`
|
|
Blocked bool `json:"blocked"`
|
|
Revision int64 `json:"revision"`
|
|
Records int64 `json:"records"`
|
|
SourceObservedAt *time.Time `json:"source_observed_at"`
|
|
LastSuccessAt *time.Time `json:"last_success_at"`
|
|
NextSyncAt *time.Time `json:"next_sync_at"`
|
|
ErrorCode string `json:"error_code"`
|
|
IntervalSeconds int `json:"interval_seconds"`
|
|
WorkerReady bool `json:"worker_ready"`
|
|
Job *SyncJob `json:"job"`
|
|
Safety dobotaiops.SourceSafety `json:"-"`
|
|
}
|
|
type SyncJob struct {
|
|
ID string `json:"id"`
|
|
State string `json:"state"`
|
|
Loaded int `json:"loaded"`
|
|
Expected int `json:"expected"`
|
|
Changed int `json:"changed"`
|
|
ErrorCode string `json:"error_code"`
|
|
}
|
|
|
|
func (s *Store) valid() bool {
|
|
return s != nil && s.pool != nil && strings.TrimSpace(s.tenant) == s.tenant && len(s.tenant) > 0 && len(s.tenant) <= 128 && !strings.ContainsRune(s.tenant, 0)
|
|
}
|
|
func (s *Store) readState(ctx context.Context, q dobotaiops.RecordQuerier) (Status, error) {
|
|
out := Status{Mode: "local", IntervalSeconds: int(Interval / time.Second)}
|
|
var safety []byte
|
|
err := q.QueryRow(ctx, `SELECT ready,blocked,revision,record_count,source_observed_at,synced_at,next_sync_at,last_error,source_safety FROM qa_ops_mirror_state WHERE tenant_key=$1`, s.tenant).Scan(&out.Ready, &out.Blocked, &out.Revision, &out.Records, &out.SourceObservedAt, &out.LastSuccessAt, &out.NextSyncAt, &out.ErrorCode, &safety)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
out.ErrorCode = "initial_sync_pending"
|
|
return out, nil
|
|
}
|
|
if err != nil || json.Unmarshal(safety, &out.Safety) != nil {
|
|
return Status{}, dobotaiops.ErrUnavailable
|
|
}
|
|
s.mu.Lock()
|
|
s.safety = out.Safety
|
|
s.mu.Unlock()
|
|
return out, nil
|
|
}
|
|
|
|
type txQuery struct{ pgx.Tx }
|
|
|
|
func (q txQuery) QueryRow(ctx context.Context, sql string, args ...any) dobotaiops.RecordRow {
|
|
return q.Tx.QueryRow(ctx, sql, args...)
|
|
}
|
|
|
|
type poolQuery struct{ *pgxpool.Pool }
|
|
|
|
func (q poolQuery) QueryRow(ctx context.Context, sql string, args ...any) dobotaiops.RecordRow {
|
|
return q.Pool.QueryRow(ctx, sql, args...)
|
|
}
|
|
func (s *Store) Available(ctx context.Context) error {
|
|
if !s.valid() {
|
|
return dobotaiops.ErrUnavailable
|
|
}
|
|
state, e := s.readState(ctx, poolQuery{s.pool})
|
|
if e != nil {
|
|
return e
|
|
}
|
|
if state.Blocked {
|
|
return dobotaiops.ErrNotReadOnly
|
|
}
|
|
if !state.Ready {
|
|
return dobotaiops.ErrUnavailable
|
|
}
|
|
return nil
|
|
}
|
|
func (s *Store) Safety() dobotaiops.SourceSafety { s.mu.RLock(); defer s.mu.RUnlock(); return s.safety }
|
|
func (s *Store) repository(revision *int64) *dobotaiops.RecordsRepository {
|
|
return dobotaiops.NewRecordsRepository(dobotaiops.RecordsRepositoryOptions{
|
|
BaseSQL: localBaseSQL, SummarySQL: localSummarySQL, QualityScopeSQL: localQualityScopeSQL, TenantKey: s.tenant,
|
|
ValidateWindow: func(w dobotaiops.Window) error {
|
|
if w.TenantKey != s.tenant {
|
|
return dobotaiops.ErrInvalidQuery
|
|
}
|
|
_, e := dobotaiops.NewWindow(w.TenantKey, w.Start.Format(time.RFC3339Nano), w.End.Format(time.RFC3339Nano), 90*24*time.Hour, time.Now())
|
|
return e
|
|
},
|
|
ReadAsOf: func(ctx context.Context, q dobotaiops.RecordQuerier, t *time.Time) error {
|
|
return q.QueryRow(ctx, `SELECT source_observed_at FROM qa_ops_mirror_state WHERE tenant_key=$1`, s.tenant).Scan(t)
|
|
},
|
|
Transaction: func(ctx context.Context, fn func(context.Context, dobotaiops.RecordQuerier) error) error {
|
|
if !s.valid() {
|
|
return dobotaiops.ErrUnavailable
|
|
}
|
|
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
|
|
defer cancel()
|
|
tx, e := s.pool.BeginTx(ctx, pgx.TxOptions{IsoLevel: pgx.RepeatableRead, AccessMode: pgx.ReadOnly})
|
|
if e != nil {
|
|
return dobotaiops.ErrUnavailable
|
|
}
|
|
defer func() { _ = tx.Rollback(context.Background()) }()
|
|
q := txQuery{tx}
|
|
state, e := s.readState(ctx, q)
|
|
if e != nil {
|
|
return e
|
|
}
|
|
if state.Blocked {
|
|
return dobotaiops.ErrNotReadOnly
|
|
}
|
|
if !state.Ready {
|
|
return dobotaiops.ErrUnavailable
|
|
}
|
|
if revision != nil {
|
|
*revision = state.Revision
|
|
}
|
|
if e = fn(ctx, q); e != nil {
|
|
return e
|
|
}
|
|
if tx.Commit(ctx) != nil {
|
|
return dobotaiops.ErrUnavailable
|
|
}
|
|
return nil
|
|
},
|
|
})
|
|
}
|
|
func (s *Store) ListRecords(ctx context.Context, q dobotaiops.RecordQuery) (dobotaiops.RecordPage, error) {
|
|
var rev int64
|
|
p, e := s.repository(&rev).ListRecords(ctx, q)
|
|
p.LocalRevision = rev
|
|
p.Columns = []string{"question_chinese", "answer", "coverage", "is_correct", "product_category", "problem_type", "root_cause", "asked_at", "asker", "question", "language", "trigger_person", "department", "region", "feedback"}
|
|
return p, e
|
|
}
|
|
func (s *Store) GetRecord(ctx context.Context, tenant, id string) (dobotaiops.Record, error) {
|
|
return s.repository(nil).GetRecord(ctx, tenant, id)
|
|
}
|
|
func (s *Store) ReadUsageGroups(ctx context.Context, q dobotaiops.RecordQuery) (dobotaiops.UsageGroups, error) {
|
|
return s.repository(nil).ReadUsageGroups(ctx, q)
|
|
}
|
|
func (s *Store) ReadQualityScope(ctx context.Context, q dobotaiops.RecordQuery) (dobotaiops.QualityScope, error) {
|
|
return s.repository(nil).ReadQualityScope(ctx, q)
|
|
}
|