-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
collection_service.go
281 lines (255 loc) · 9.59 KB
/
collection_service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package grpc
import (
"context"
"encoding/json"
"errors"
"github.com/chroma-core/chroma/go/pkg/grpcutils"
"github.com/chroma-core/chroma/go/pkg/common"
"github.com/chroma-core/chroma/go/pkg/model"
"github.com/chroma-core/chroma/go/pkg/proto/coordinatorpb"
"github.com/chroma-core/chroma/go/pkg/types"
"github.com/pingcap/log"
"go.uber.org/zap"
"google.golang.org/protobuf/types/known/emptypb"
)
const errorCode = 500
const successCode = 200
const success = "ok"
func (s *Server) ResetState(context.Context, *emptypb.Empty) (*coordinatorpb.ResetStateResponse, error) {
log.Info("reset state")
res := &coordinatorpb.ResetStateResponse{}
err := s.coordinator.ResetState(context.Background())
if err != nil {
res.Status = failResponseWithError(err, errorCode)
return res, err
}
setResponseStatus(successCode)
return res, nil
}
// Cases for get_or_create
// Case 0
// new_metadata is none, coll is an existing collection
// get_or_create should return the existing collection with existing metadata
// Essentially - an update with none is a no-op
// Case 1
// new_metadata is none, coll is a new collection
// get_or_create should create a new collection with the metadata of None
// Case 2
// new_metadata is not none, coll is an existing collection
// get_or_create should return the existing collection with updated metadata
// Case 3
// new_metadata is not none, coll is a new collection
// get_or_create should create a new collection with the new metadata, ignoring
// the metdata of in the input coll.
// The fact that we ignore the metadata of the generated collections is a
// bit weird, but it is the easiest way to excercise all cases
func (s *Server) CreateCollection(ctx context.Context, req *coordinatorpb.CreateCollectionRequest) (*coordinatorpb.CreateCollectionResponse, error) {
res := &coordinatorpb.CreateCollectionResponse{}
createCollection, err := convertToCreateCollectionModel(req)
if err != nil {
log.Error("error converting to create collection model", zap.Error(err))
res.Collection = &coordinatorpb.Collection{
Id: req.Id,
Name: req.Name,
Dimension: req.Dimension,
Metadata: req.Metadata,
Tenant: req.Tenant,
Database: req.Database,
}
res.Created = false
res.Status = failResponseWithError(err, successCode)
return res, nil
}
collection, err := s.coordinator.CreateCollection(ctx, createCollection)
if err != nil {
log.Error("error creating collection", zap.Error(err))
res.Collection = &coordinatorpb.Collection{
Id: req.Id,
Name: req.Name,
Dimension: req.Dimension,
Metadata: req.Metadata,
Tenant: req.Tenant,
Database: req.Database,
}
res.Created = false
if err == common.ErrCollectionUniqueConstraintViolation {
res.Status = failResponseWithError(err, 409)
} else {
res.Status = failResponseWithError(err, errorCode)
}
return res, nil
}
res.Collection = convertCollectionToProto(collection)
res.Status = setResponseStatus(successCode)
return res, nil
}
func (s *Server) GetCollections(ctx context.Context, req *coordinatorpb.GetCollectionsRequest) (*coordinatorpb.GetCollectionsResponse, error) {
collectionID := req.Id
collectionName := req.Name
tenantID := req.Tenant
databaseName := req.Database
limit := req.Limit
offset := req.Offset
res := &coordinatorpb.GetCollectionsResponse{}
parsedCollectionID, err := types.ToUniqueID(collectionID)
if err != nil {
log.Error("collection id format error", zap.String("collectionpd.id", *collectionID))
res.Status = failResponseWithError(common.ErrCollectionIDFormat, errorCode)
return res, nil
}
collections, err := s.coordinator.GetCollections(ctx, parsedCollectionID, collectionName, tenantID, databaseName, limit, offset)
if err != nil {
log.Error("error getting collections", zap.Error(err))
res.Status = failResponseWithError(err, errorCode)
return res, nil
}
res.Collections = make([]*coordinatorpb.Collection, 0, len(collections))
for _, collection := range collections {
collectionpb := convertCollectionToProto(collection)
res.Collections = append(res.Collections, collectionpb)
}
log.Info("collection service collections", zap.Any("collections", res.Collections))
res.Status = setResponseStatus(successCode)
return res, nil
}
func (s *Server) DeleteCollection(ctx context.Context, req *coordinatorpb.DeleteCollectionRequest) (*coordinatorpb.DeleteCollectionResponse, error) {
collectionID := req.GetId()
res := &coordinatorpb.DeleteCollectionResponse{}
parsedCollectionID, err := types.Parse(collectionID)
if err != nil {
log.Error(err.Error(), zap.String("collectionpd.id", collectionID))
res.Status = failResponseWithError(common.ErrCollectionIDFormat, errorCode)
return res, nil
}
deleteCollection := &model.DeleteCollection{
ID: parsedCollectionID,
TenantID: req.GetTenant(),
DatabaseName: req.GetDatabase(),
}
err = s.coordinator.DeleteCollection(ctx, deleteCollection)
if err != nil {
if errors.Is(err, common.ErrCollectionDeleteNonExistingCollection) {
log.Error("ErrCollectionDeleteNonExistingCollection", zap.String("collectionpd.id", collectionID))
res.Status = failResponseWithError(err, 404)
} else {
log.Error(err.Error(), zap.String("collectionpd.id", collectionID))
res.Status = failResponseWithError(err, errorCode)
}
return res, nil
}
res.Status = setResponseStatus(successCode)
return res, nil
}
func (s *Server) UpdateCollection(ctx context.Context, req *coordinatorpb.UpdateCollectionRequest) (*coordinatorpb.UpdateCollectionResponse, error) {
res := &coordinatorpb.UpdateCollectionResponse{}
collectionID := req.Id
parsedCollectionID, err := types.ToUniqueID(&collectionID)
if err != nil {
log.Error("collection id format error", zap.String("collectionpd.id", collectionID))
res.Status = failResponseWithError(common.ErrCollectionIDFormat, errorCode)
return res, nil
}
updateCollection := &model.UpdateCollection{
ID: parsedCollectionID,
Name: req.Name,
Dimension: req.Dimension,
}
resetMetadata := req.GetResetMetadata()
updateCollection.ResetMetadata = resetMetadata
metadata := req.GetMetadata()
// Case 1: if resetMetadata is true, then delete all metadata for the collection
// Case 2: if resetMetadata is true and metadata is not nil -> THIS SHOULD NEVER HAPPEN
// Case 3: if resetMetadata is false, and the metadata is not nil - set the metadata to the value in metadata
// Case 4: if resetMetadata is false and metadata is nil, then leave the metadata as is
if resetMetadata {
if metadata != nil {
log.Error("reset metadata is true and metadata is not nil", zap.Any("metadata", metadata))
res.Status = failResponseWithError(common.ErrInvalidMetadataUpdate, errorCode)
return res, nil
} else {
updateCollection.Metadata = nil
}
} else {
if metadata != nil {
modelMetadata, err := convertCollectionMetadataToModel(metadata)
if err != nil {
log.Error("error converting collection metadata to model", zap.Error(err))
res.Status = failResponseWithError(err, errorCode)
return res, nil
}
updateCollection.Metadata = modelMetadata
} else {
updateCollection.Metadata = nil
}
}
_, err = s.coordinator.UpdateCollection(ctx, updateCollection)
if err != nil {
log.Error("error updating collection", zap.Error(err))
if err == common.ErrCollectionUniqueConstraintViolation {
res.Status = failResponseWithError(err, 409)
} else {
res.Status = failResponseWithError(err, errorCode)
}
return res, nil
}
res.Status = setResponseStatus(successCode)
return res, nil
}
func (s *Server) FlushCollectionCompaction(ctx context.Context, req *coordinatorpb.FlushCollectionCompactionRequest) (*coordinatorpb.FlushCollectionCompactionResponse, error) {
blob, err := json.Marshal(req)
if err != nil {
return nil, err
}
log.Info("flush collection compaction", zap.String("request", string(blob)))
collectionID, err := types.ToUniqueID(&req.CollectionId)
err = grpcutils.BuildErrorForUUID(collectionID, "collection", err)
if err != nil {
return nil, err
}
segmentCompactionInfo := make([]*model.FlushSegmentCompaction, 0, len(req.SegmentCompactionInfo))
for _, flushSegmentCompaction := range req.SegmentCompactionInfo {
segmentID, err := types.ToUniqueID(&flushSegmentCompaction.SegmentId)
err = grpcutils.BuildErrorForUUID(segmentID, "segment", err)
if err != nil {
return nil, err
}
filePaths := make(map[string][]string)
for key, filePath := range flushSegmentCompaction.FilePaths {
filePaths[key] = filePath.Paths
}
segmentCompactionInfo = append(segmentCompactionInfo, &model.FlushSegmentCompaction{
ID: segmentID,
FilePaths: filePaths,
})
}
FlushCollectionCompaction := &model.FlushCollectionCompaction{
ID: collectionID,
TenantID: req.TenantId,
LogPosition: req.LogPosition,
CurrentCollectionVersion: req.CollectionVersion,
FlushSegmentCompactions: segmentCompactionInfo,
}
flushCollectionInfo, err := s.coordinator.FlushCollectionCompaction(ctx, FlushCollectionCompaction)
if err != nil {
log.Error("error FlushCollectionCompaction", zap.Error(err))
return nil, grpcutils.BuildInternalGrpcError(err.Error())
}
res := &coordinatorpb.FlushCollectionCompactionResponse{
CollectionId: flushCollectionInfo.ID,
CollectionVersion: flushCollectionInfo.CollectionVersion,
LastCompactionTime: flushCollectionInfo.TenantLastCompactionTime,
}
return res, nil
}
func failResponseWithError(err error, code int32) *coordinatorpb.Status {
return &coordinatorpb.Status{
Reason: err.Error(),
Code: code,
}
}
func setResponseStatus(code int32) *coordinatorpb.Status {
return &coordinatorpb.Status{
Reason: success,
Code: code,
}
}