The trace deployed in round5 located the cost: batches_ms carried 80% of every work-buffer poll (1400-3900ms of 1600-4100ms total). The poller's enablement check called ListHumanBatches for the project, which — because the legacy migration created one batch per task — materialized 12,009 full batch documents on every 3s poll, walking an organizationId-only index over 17,334 docs. - HasRawHumanAnnotationBatch (Memory + Mongo): projects only the stages field and stops at the first three-role raw_human match (105ms on the production dataset, was 2000ms+). - New humanBatches index organizationId_projectId_pluginId backs both the enablement query and ListHumanBatches' project-scoped reads. - store.Store interface gains the method; both store implementations keep identical semantics (mid-test profile flips stay visible).
118 lines
4.1 KiB
Go
118 lines
4.1 KiB
Go
package api
|
|
|
|
import (
|
|
"droboticflow/internal/domain"
|
|
"log"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func (s *server) handleAnnotationWorkBuffer(w http.ResponseWriter, r *http.Request) {
|
|
started := time.Now()
|
|
var authMs, batchesMs, storeMs, enrichMs int64
|
|
defer func() {
|
|
userID := ""
|
|
if principal, ok := currentPrincipal(r); ok && principal != nil {
|
|
userID = principal.User.ID
|
|
}
|
|
log.Printf("[api] work_buffer_latency operation=handleAnnotationWorkBuffer user=%s total_ms=%d auth_ms=%d batches_ms=%d store_ms=%d enrich_ms=%d",
|
|
userID, time.Since(started).Milliseconds(), authMs, batchesMs, storeMs, enrichMs)
|
|
}()
|
|
p, _ := currentPrincipal(r)
|
|
if !randomClaimAnnotator(p) {
|
|
renderJSON(w, 403, map[string]string{"error": "仅标注员使用个人任务池"})
|
|
return
|
|
}
|
|
projectID := strings.TrimSpace(r.URL.Query().Get("projectId"))
|
|
project, err := s.store.GetProject(r.Context(), projectID)
|
|
if err != nil {
|
|
renderError(w, 500, err)
|
|
return
|
|
}
|
|
if project == nil || project.OrganizationID != p.User.OrganizationID || project.DeletionTaskID != "" || strings.EqualFold(string(project.Status), "archived") {
|
|
renderJSON(w, 403, map[string]string{"error": "项目不可用"})
|
|
return
|
|
}
|
|
members, err := s.annotationMembershipsForUser(r.Context(), p.User.OrganizationID, projectID, p.User.ID)
|
|
if err != nil {
|
|
renderError(w, 500, err)
|
|
return
|
|
}
|
|
allowed := false
|
|
for _, m := range members {
|
|
if m.Role == domain.AnnotationRoleAnnotator && m.SupplierID != "" {
|
|
supplier, e := s.store.GetSupplier(r.Context(), m.SupplierID)
|
|
if e != nil {
|
|
renderError(w, 500, e)
|
|
return
|
|
}
|
|
if supplier != nil && supplier.OrganizationID == p.User.OrganizationID && strings.EqualFold(supplier.Status, "active") {
|
|
allowed = true
|
|
}
|
|
}
|
|
}
|
|
if !allowed {
|
|
renderJSON(w, 403, map[string]string{"error": "没有该项目的有效标注权限"})
|
|
return
|
|
}
|
|
// Scope comes from runner-created batches, including empty active batches;
|
|
// merely using annotation-qa or a template version never enables buffering.
|
|
// The projected existence query bounds the scan with the
|
|
// (organizationId, projectId, pluginId) index and stops at the first
|
|
// three-role match, so a 12k-batch legacy project no longer pays a full
|
|
// batch-document materialization per poll.
|
|
segment := time.Now()
|
|
enabled, enablementErr := s.store.HasRawHumanAnnotationBatch(r.Context(), p.User.OrganizationID, projectID)
|
|
batchesMs = time.Since(segment).Milliseconds()
|
|
if enablementErr != nil {
|
|
renderError(w, 500, enablementErr)
|
|
return
|
|
}
|
|
if !enabled {
|
|
if r.Method == http.MethodPut {
|
|
renderJSON(w, 409, map[string]string{"code": "annotation_buffer_not_supported", "error": "该项目流程不使用个人任务缓冲"})
|
|
return
|
|
}
|
|
renderJSON(w, 200, map[string]any{"enabled": false, "capacity": 2, "occupied": 0, "items": []domain.HumanItemRecord{}, "processing": map[string]any{}})
|
|
return
|
|
}
|
|
capacity := 0
|
|
if r.Method == http.MethodPut {
|
|
var body struct {
|
|
Capacity int `json:"capacity"`
|
|
}
|
|
if !decodeJSON(w, r, &body) {
|
|
return
|
|
}
|
|
capacity = body.Capacity
|
|
if capacity < 2 || capacity > 8 {
|
|
renderValidationError(w, validationIssue{Field: "capacity", Message: "容量必须是2至8的整数"})
|
|
return
|
|
}
|
|
}
|
|
segment = time.Now()
|
|
buffer, err := s.store.AnnotationWorkBuffer(r.Context(), p.User.OrganizationID, p.User.ID, projectID, capacity)
|
|
storeMs = time.Since(segment).Milliseconds()
|
|
if err != nil {
|
|
renderError(w, 500, err)
|
|
return
|
|
}
|
|
resolver := newHumanDisplayResolver(s)
|
|
segment = time.Now()
|
|
for index := range buffer.Items {
|
|
if err := resolver.enrichItem(r.Context(), &buffer.Items[index]); err != nil {
|
|
renderError(w, http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
}
|
|
enrichMs = time.Since(segment).Milliseconds()
|
|
processing := map[string]any{}
|
|
for _, item := range buffer.Items {
|
|
if job := item.ProcessingJob; job != nil {
|
|
processing[item.ID] = map[string]string{"status": job.Status, "kind": job.Kind, "error": job.Error}
|
|
}
|
|
}
|
|
renderJSON(w, 200, map[string]any{"enabled": true, "capacity": buffer.Capacity, "occupied": buffer.Occupied, "items": buffer.Items, "processing": processing})
|
|
}
|