|
| 1 | +package coordinator |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/chroma-core/chroma/go/pkg/common" |
| 8 | + "github.com/chroma-core/chroma/go/pkg/proto/coordinatorpb" |
| 9 | + "github.com/chroma-core/chroma/go/pkg/sysdb/coordinator/model" |
| 10 | + "github.com/chroma-core/chroma/go/pkg/sysdb/metastore/db/dbmodel" |
| 11 | + "github.com/chroma-core/chroma/go/pkg/types" |
| 12 | + "github.com/google/uuid" |
| 13 | + "github.com/pingcap/log" |
| 14 | + "go.uber.org/zap" |
| 15 | + "google.golang.org/protobuf/proto" |
| 16 | +) |
| 17 | + |
| 18 | +// CreateTask creates a new task in the database |
| 19 | +func (s *Coordinator) CreateTask(ctx context.Context, req *coordinatorpb.CreateTaskRequest) (*coordinatorpb.CreateTaskResponse, error) { |
| 20 | + // Generate new task UUID |
| 21 | + taskID := uuid.New() |
| 22 | + var outputCollectionID string |
| 23 | + |
| 24 | + // Look up database_id from databases table using database name and tenant |
| 25 | + databases, err := s.catalog.metaDomain.DatabaseDb(ctx).GetDatabases(req.TenantId, req.Database) |
| 26 | + if err != nil { |
| 27 | + log.Error("CreateTask: failed to get database", zap.Error(err)) |
| 28 | + return nil, err |
| 29 | + } |
| 30 | + if len(databases) == 0 { |
| 31 | + log.Error("CreateTask: database not found") |
| 32 | + return nil, common.ErrDatabaseNotFound |
| 33 | + } |
| 34 | + |
| 35 | + // Look up operator by name from the operators table |
| 36 | + operator, err := s.catalog.metaDomain.OperatorDb(ctx).GetByName(req.OperatorName) |
| 37 | + if err != nil { |
| 38 | + log.Error("CreateTask: failed to get operator", zap.Error(err)) |
| 39 | + return nil, err |
| 40 | + } |
| 41 | + if operator == nil { |
| 42 | + log.Error("CreateTask: operator not found", zap.String("operator_name", req.OperatorName)) |
| 43 | + return nil, common.ErrOperatorNotFound |
| 44 | + } |
| 45 | + operatorID := operator.OperatorID |
| 46 | + |
| 47 | + // Generate UUIDv7 for time-ordered nonce |
| 48 | + nextNonce, err := uuid.NewV7() |
| 49 | + if err != nil { |
| 50 | + return nil, err |
| 51 | + } |
| 52 | + |
| 53 | + // Create output collection and task transactionally |
| 54 | + err = s.catalog.txImpl.Transaction(ctx, func(txCtx context.Context) error { |
| 55 | + // Create the output collection |
| 56 | + createCollection := &model.CreateCollection{ |
| 57 | + ID: types.NewUniqueID(), |
| 58 | + Name: req.OutputCollectionName, |
| 59 | + TenantID: req.TenantId, |
| 60 | + DatabaseName: req.Database, |
| 61 | + GetOrCreate: false, // Don't get existing, must be new |
| 62 | + Ts: types.Timestamp(time.Now().Unix()), |
| 63 | + } |
| 64 | + |
| 65 | + collection, _, err := s.catalog.CreateCollection(txCtx, createCollection, createCollection.Ts) |
| 66 | + if err != nil { |
| 67 | + log.Error("CreateTask: failed to create output collection", zap.Error(err)) |
| 68 | + return err |
| 69 | + } |
| 70 | + outputCollectionID = collection.ID.String() |
| 71 | + |
| 72 | + // Create the task model |
| 73 | + now := time.Now() |
| 74 | + task := &dbmodel.Task{ |
| 75 | + ID: taskID, |
| 76 | + Name: req.Name, |
| 77 | + TenantID: req.TenantId, |
| 78 | + DatabaseID: databases[0].ID, |
| 79 | + InputCollectionID: req.InputCollectionId, |
| 80 | + OutputCollectionID: outputCollectionID, |
| 81 | + OperatorID: operatorID, |
| 82 | + OperatorParams: req.Params, |
| 83 | + CompletionOffset: 0, |
| 84 | + LastRun: nil, |
| 85 | + NextRun: nil, // Will be scheduled by task scheduler |
| 86 | + MinRecordsForTask: int64(req.MinRecordsForTask), |
| 87 | + CurrentAttempts: 0, |
| 88 | + CreatedAt: now, |
| 89 | + UpdatedAt: now, |
| 90 | + NextNonce: nextNonce, |
| 91 | + OldestWrittenNonce: nil, |
| 92 | + } |
| 93 | + |
| 94 | + // Insert task into database |
| 95 | + err = s.catalog.metaDomain.TaskDb(txCtx).Insert(task) |
| 96 | + if err != nil { |
| 97 | + log.Error("CreateTask: failed to insert task", zap.Error(err)) |
| 98 | + return err |
| 99 | + } |
| 100 | + |
| 101 | + return nil |
| 102 | + }) |
| 103 | + |
| 104 | + if err != nil { |
| 105 | + return nil, err |
| 106 | + } |
| 107 | + |
| 108 | + log.Info("Task created", zap.String("task_id", taskID.String()), zap.String("name", req.Name)) |
| 109 | + |
| 110 | + return &coordinatorpb.CreateTaskResponse{ |
| 111 | + TaskId: taskID.String(), |
| 112 | + }, nil |
| 113 | +} |
| 114 | + |
| 115 | +// GetTaskByName retrieves a task by name from the database |
| 116 | +func (s *Coordinator) GetTaskByName(ctx context.Context, req *coordinatorpb.GetTaskByNameRequest) (*coordinatorpb.GetTaskByNameResponse, error) { |
| 117 | + task, err := s.catalog.metaDomain.TaskDb(ctx).GetByName(req.InputCollectionId, req.TaskName) |
| 118 | + if err != nil { |
| 119 | + return nil, err |
| 120 | + } |
| 121 | + |
| 122 | + // If task not found, return empty response |
| 123 | + if task == nil { |
| 124 | + return &coordinatorpb.GetTaskByNameResponse{}, nil |
| 125 | + } |
| 126 | + |
| 127 | + // Look up operator name from operators table |
| 128 | + operator, err := s.catalog.metaDomain.OperatorDb(ctx).GetByID(task.OperatorID) |
| 129 | + if err != nil { |
| 130 | + log.Error("GetTaskByName: failed to get operator", zap.Error(err)) |
| 131 | + return nil, err |
| 132 | + } |
| 133 | + if operator == nil { |
| 134 | + log.Error("GetTaskByName: operator not found", zap.String("operator_id", task.OperatorID.String())) |
| 135 | + return nil, common.ErrOperatorNotFound |
| 136 | + } |
| 137 | + |
| 138 | + // Debug logging |
| 139 | + log.Info("Found task", zap.String("task_id", task.ID.String()), zap.String("name", task.Name), zap.String("input_collection_id", task.InputCollectionID), zap.String("output_collection_id", task.OutputCollectionID)) |
| 140 | + |
| 141 | + // Convert task to response |
| 142 | + return &coordinatorpb.GetTaskByNameResponse{ |
| 143 | + TaskId: proto.String(task.ID.String()), |
| 144 | + Name: proto.String(task.Name), |
| 145 | + OperatorName: proto.String(operator.OperatorName), |
| 146 | + InputCollectionId: proto.String(task.InputCollectionID), |
| 147 | + OutputCollectionName: proto.String(task.OutputCollectionID), |
| 148 | + Params: proto.String(task.OperatorParams), |
| 149 | + CompletionOffset: proto.Int64(task.CompletionOffset), |
| 150 | + MinRecordsForTask: proto.Uint64(uint64(task.MinRecordsForTask)), |
| 151 | + }, nil |
| 152 | +} |
| 153 | + |
| 154 | +// DeleteTask soft deletes a task by name |
| 155 | +func (s *Coordinator) DeleteTask(ctx context.Context, req *coordinatorpb.DeleteTaskRequest) (*coordinatorpb.DeleteTaskResponse, error) { |
| 156 | + // lookup database_id from databases table using database name and tenant |
| 157 | + databases, err := s.catalog.metaDomain.DatabaseDb(ctx).GetDatabases(req.TenantId, req.Database) |
| 158 | + if err != nil { |
| 159 | + log.Error("DeleteTask failed", zap.Error(err)) |
| 160 | + return nil, err |
| 161 | + } |
| 162 | + if len(databases) == 0 { |
| 163 | + log.Error("DeleteTask failed: database not found") |
| 164 | + return nil, common.ErrDatabaseNotFound |
| 165 | + } |
| 166 | + err = s.catalog.metaDomain.TaskDb(ctx).SoftDelete(req.TenantId, databases[0].ID, req.TaskName) |
| 167 | + if err != nil { |
| 168 | + log.Error("DeleteTask failed", zap.Error(err)) |
| 169 | + return nil, err |
| 170 | + } |
| 171 | + |
| 172 | + log.Info("Task deleted", zap.String("tenant", req.TenantId), zap.String("database", req.Database), zap.String("task_name", req.TaskName)) |
| 173 | + |
| 174 | + return &coordinatorpb.DeleteTaskResponse{ |
| 175 | + Success: true, |
| 176 | + }, nil |
| 177 | +} |
0 commit comments