/
kv_dao.go
253 lines (239 loc) · 7.78 KB
/
kv_dao.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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kv
import (
"context"
"fmt"
"time"
"github.com/apache/servicecomb-kie/pkg/model"
"github.com/apache/servicecomb-kie/server/service"
"github.com/apache/servicecomb-kie/server/service/mongo/counter"
"github.com/apache/servicecomb-kie/server/service/mongo/history"
"github.com/apache/servicecomb-kie/server/service/mongo/session"
"github.com/go-mesh/openlogging"
uuid "github.com/satori/go.uuid"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
//createKey get latest revision from history
//and increase revision of label
//and insert key
func createKey(ctx context.Context, kv *model.KVDoc) (*model.KVDoc, error) {
collection := session.GetDB().Collection(session.CollectionKV)
var err error
kv.ID = uuid.NewV4().String()
revision, err := counter.ApplyRevision(ctx, kv.Domain)
if err != nil {
openlogging.Error(err.Error())
return nil, err
}
kv.UpdateRevision = revision
kv.CreateRevision = revision
now := time.Now().Unix()
kv.CreateTime = now
kv.UpdateTime = now
_, err = collection.InsertOne(ctx, kv)
if err != nil {
openlogging.Error("create error", openlogging.WithTags(openlogging.Tags{
"err": err.Error(),
"kv": kv,
}))
return nil, err
}
err = history.AddHistory(ctx, kv)
if err != nil {
openlogging.Warn(
fmt.Sprintf("can not updateKeyValue version for [%s] [%s] in [%s]",
kv.Key, kv.Labels, kv.Domain))
}
openlogging.Debug(fmt.Sprintf("create %s with labels %s value [%s]", kv.Key, kv.Labels, kv.Value))
return kv, nil
}
//updateKeyValue update key value and add new revision
func updateKeyValue(ctx context.Context, kv *model.KVDoc) error {
var err error
kv.UpdateTime = time.Now().Unix()
kv.UpdateRevision, err = counter.ApplyRevision(ctx, kv.Domain)
if err != nil {
return err
}
collection := session.GetDB().Collection(session.CollectionKV)
ur, err := collection.UpdateOne(ctx, bson.M{"key": kv.Key, "label_id": kv.LabelID}, bson.D{
{"$set", bson.D{
{"value", kv.Value},
{"status", kv.Status},
{"checker", kv.Checker},
{"update_time", kv.UpdateTime},
{"update_revision", kv.UpdateRevision},
}},
})
if err != nil {
return err
}
openlogging.Debug(
fmt.Sprintf("updateKeyValue %s with labels %s value [%s] %d ",
kv.Key, kv.Labels, kv.Value, ur.ModifiedCount))
err = history.AddHistory(ctx, kv)
if err != nil {
openlogging.Error(
fmt.Sprintf("can not add revision for [%s] [%s] in [%s],err: %s",
kv.Key, kv.Labels, kv.Domain, err))
}
openlogging.Debug(
fmt.Sprintf("add history %s with labels %s value [%s] %d ",
kv.Key, kv.Labels, kv.Value, ur.ModifiedCount))
return nil
}
func findKV(ctx context.Context, domain string, project string, opts service.FindOptions) (*mongo.Cursor, int, error) {
collection := session.GetDB().Collection(session.CollectionKV)
ctx, _ = context.WithTimeout(ctx, opts.Timeout)
filter := bson.M{"domain": domain, "project": project}
if opts.Key != "" {
filter["key"] = opts.Key
}
if len(opts.Labels) != 0 {
for k, v := range opts.Labels {
filter["labels."+k] = v
}
}
opt := options.Find()
if opts.Offset != 0 && opts.Limit != 0 {
opt = opt.SetLimit(opts.Limit)
opt = opt.SetSkip(opts.Offset)
}
curTotal, err := collection.CountDocuments(ctx, filter)
if err != nil {
if err.Error() == context.DeadlineExceeded.Error() {
openlogging.Error(MsgFindKvFailed, openlogging.WithTags(openlogging.Tags{
"timeout": opts.Timeout,
}))
return nil, 0, fmt.Errorf(FmtErrFindKvFailed, opts.Timeout)
}
return nil, 0, err
}
if opts.Status != "" {
filter["status"] = opts.Status
}
cur, err := collection.Find(ctx, filter, opt)
if err != nil {
if err.Error() == context.DeadlineExceeded.Error() {
openlogging.Error(MsgFindKvFailed, openlogging.WithTags(openlogging.Tags{
"timeout": opts.Timeout,
}))
return nil, 0, fmt.Errorf(FmtErrFindKvFailed, opts.Timeout)
}
return nil, 0, err
}
return cur, int(curTotal), err
}
func findOneKey(ctx context.Context, filter bson.M) ([]*model.KVDoc, error) {
collection := session.GetDB().Collection(session.CollectionKV)
sr := collection.FindOne(ctx, filter)
if sr.Err() != nil {
if sr.Err() == mongo.ErrNoDocuments {
return nil, service.ErrKeyNotExists
}
return nil, sr.Err()
}
curKV := &model.KVDoc{}
err := sr.Decode(curKV)
if err != nil {
openlogging.Error("decode error: " + err.Error())
return nil, err
}
return []*model.KVDoc{curKV}, nil
}
//deleteKV by kvID
func deleteKV(ctx context.Context, kvID, project, domain string) error {
if _, err := counter.ApplyRevision(ctx, domain); err != nil {
openlogging.Error(fmt.Sprintf("increase revision failed, the kv [%s] not deleted: [%s]", kvID, err))
return err
}
collection := session.GetDB().Collection(session.CollectionKV)
dr, err := collection.DeleteOne(ctx, bson.M{"id": kvID, "project": project, "domain": domain})
//check error and delete number
if err != nil {
openlogging.Error(fmt.Sprintf("delete [%s] failed : [%s]", kvID, err))
return err
}
if dr.DeletedCount != 1 {
openlogging.Warn(fmt.Sprintf("failed, may have been deleted,kvID=%s", kvID))
} else {
openlogging.Info(fmt.Sprintf("delete success,kvID=%s", kvID))
}
err = history.AddDeleteTime(ctx, kvID, project, domain)
if err != nil {
openlogging.Error(fmt.Sprintf("add delete time to [%s] failed : [%s]", kvID, err))
}
return err
}
func findKeys(ctx context.Context, filter bson.M, withoutLabel bool) ([]*model.KVDoc, error) {
collection := session.GetDB().Collection(session.CollectionKV)
cur, err := collection.Find(ctx, filter)
if err != nil {
if err.Error() == context.DeadlineExceeded.Error() {
openlogging.Error("find kvs failed, dead line exceeded", openlogging.WithTags(openlogging.Tags{
"timeout": session.Timeout,
}))
return nil, fmt.Errorf("can not find keys due to timout")
}
return nil, err
}
defer cur.Close(ctx)
if cur.Err() != nil {
return nil, err
}
kvs := make([]*model.KVDoc, 0)
for cur.Next(ctx) {
curKV := &model.KVDoc{}
if err := cur.Decode(curKV); err != nil {
openlogging.Error("decode to KVs error: " + err.Error())
return nil, err
}
if withoutLabel {
curKV.Labels = nil
}
kvs = append(kvs, curKV)
}
if len(kvs) == 0 {
return nil, service.ErrKeyNotExists
}
return kvs, nil
}
//findKVByLabelID get kvs by key and label id
//key can be empty, then it will return all key values
//if key is given, will return 0-1 key value
func findKVByLabelID(ctx context.Context, domain, labelID, key string, project string) ([]*model.KVDoc, error) {
filter := bson.M{"label_id": labelID, "domain": domain, "project": project}
if key != "" {
filter["key"] = key
openlogging.Debug(MsgFindOneKey)
return findOneKey(ctx, filter)
}
return findKeys(ctx, filter, true)
}
//findKVDocByID get kvs by kv id
func findKVDocByID(ctx context.Context, domain, project, kvID string) (*model.KVDoc, error) {
filter := bson.M{"id": kvID, "domain": domain, "project": project}
kvs, err := findOneKey(ctx, filter)
if err != nil {
openlogging.Error(err.Error())
return nil, err
}
return kvs[0], nil
}