feat(library): 整库发布身份、写集合与收尾边界
build / build (push) Successful in 1m45s

执行时真实时钟与持久授权回读(撤销/过期即停并保留预备份)、写集合写前落盘、
租约与 attempt 身份、恢复队列与原发送身份分离、成功收尾必须释放维护后再登记终态。
This commit is contained in:
姚章浩
2026-11-01 11:35:10 +08:00
parent 5afd8c0140
commit 8d838d9cb4
82 changed files with 4809 additions and 551 deletions
+32 -4
View File
@@ -101,7 +101,7 @@ func (exporter *SearchExporter) PruneScope(ctx context.Context, identity SearchI
end = len(stale)
}
payload := map[string]any{
"query": map[string]any{"ids": map[string]any{"values": stale[start:end]}},
"query": map[string]any{"bool": map[string]any{"filter": append(scopeFilterFor(inspection.fields, identity), map[string]any{"ids": map[string]any{"values": stale[start:end]}})}},
}
var response struct {
Deleted int `json:"deleted"`
@@ -126,6 +126,10 @@ func scopeFilterFor(fields searchFieldNames, identity SearchIdentity) []any {
// BulkIndex 把归档里的原始文档与向量直接写入生产索引(不解析、不重新 Embedding)。
// 写入前补齐 tenant_id/knowledge_base_id,避免归档文档缺字段时落到别的库。
func (exporter *SearchExporter) BulkIndex(ctx context.Context, documents []librarysnapshot.IndexDocument, identity SearchIdentity) error {
return exporter.bulkIndex(ctx, documents, identity, nil)
}
func (exporter *SearchExporter) bulkIndex(ctx context.Context, documents []librarysnapshot.IndexDocument, identity SearchIdentity, guards map[string]indexWriteGuard) error {
if len(documents) == 0 {
return nil
}
@@ -146,9 +150,21 @@ func (exporter *SearchExporter) BulkIndex(ctx context.Context, documents []libra
return err
}
payload = encoded
action, err := json.Marshal(map[string]any{"index": map[string]string{
"_index": exporter.index, "_id": document.ID,
}})
operation := "index"
metadata := map[string]any{"_index": exporter.index, "_id": document.ID}
if guards != nil {
guard, ok := guards[document.ID]
if !ok {
return errors.New("missing index write guard")
}
if !guard.Found {
operation = "create"
} else {
metadata["if_seq_no"] = *guard.SeqNo
metadata["if_primary_term"] = *guard.PrimaryTerm
}
}
action, err := json.Marshal(map[string]any{operation: metadata})
if err != nil {
return err
}
@@ -189,6 +205,18 @@ func (exporter *SearchExporter) BulkIndex(ctx context.Context, documents []libra
if err := json.Unmarshal(raw, &parsed); err != nil {
return errors.New("Elasticsearch bulk response invalid")
}
if len(parsed.Items) == len(documents) {
for i, item := range parsed.Items {
if len(item) != 1 {
return errors.New("invalid bulk item result")
}
for operation, result := range item {
if (operation != "index" && operation != "create") || result.ID != documents[i].ID || result.Status < 200 || result.Status >= 300 || result.Error != nil {
return fmt.Errorf("Elasticsearch item outcome not confirmed: %s status %d", documents[i].ID, result.Status)
}
}
}
}
if parsed.Errors {
for _, item := range parsed.Items {
for _, result := range item {