/
object.go
175 lines (149 loc) · 4.63 KB
/
object.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
// Copyright 2019 The OpenSDS Authors.
//
// Licensed 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 meta
import (
"context"
"strconv"
log "github.com/sirupsen/logrus"
. "github.com/opensds/multi-cloud/s3/error"
"github.com/opensds/multi-cloud/s3/pkg/helper"
tidbclient "github.com/opensds/multi-cloud/s3/pkg/meta/db/drivers/tidb"
"github.com/opensds/multi-cloud/s3/pkg/meta/redis"
. "github.com/opensds/multi-cloud/s3/pkg/meta/types"
)
const (
OBJECT_CACHE_PREFIX = "object:"
)
// Object will be updated to cache while willNeed is true
func (m *Meta) GetObject(ctx context.Context, bucketName string, objectName string, versionId string, willNeed bool) (object *Object, err error) {
getObject := func() (o helper.Serializable, err error) {
log.Info("GetObject CacheMiss. bucket:", bucketName, ", object:", objectName)
version := ""
if versionId != "" {
version = strconv.FormatUint(tidbclient.VersionStr2UInt64(versionId), 10)
}
object, err := m.Db.GetObject(ctx, bucketName, objectName, version)
if err != nil {
log.Errorln("get object failed, err:", err)
return
}
log.Infoln("GetObject object.Name:", objectName)
if object.ObjectKey != objectName {
err = ErrNoSuchKey
return
}
return object, nil
}
toObject := func(fields map[string]string) (interface{}, error) {
o := &Object{}
return o.Deserialize(fields)
}
o, err := m.Cache.Get(redis.ObjectTable, OBJECT_CACHE_PREFIX, bucketName+":"+objectName+":",
getObject, toObject, willNeed)
if err != nil {
return
}
object, ok := o.(*Object)
if !ok {
err = ErrInternalError
return
}
return object, nil
}
func (m *Meta) PutObject(ctx context.Context, object, deleteObj *Object, multipart *Multipart, objMap *ObjMap, updateUsage bool) error {
log.Debugf("PutObject begin, object=%+v, deleteObj:%+v\n", object, deleteObj)
tx, err := m.Db.NewTrans()
defer func() {
if err != nil {
m.Db.AbortTrans(tx)
}
}()
// if target object exist and it's location is different from new location, need to clean it
if deleteObj != nil {
if deleteObj.Location != object.Location {
log.Infoln("put gc, deleteObj:", deleteObj)
err = m.Db.PutGcobjRecord(ctx, deleteObj, tx)
if err != nil {
return err
}
}
log.Infoln("delete object metadata, deleteObj:", deleteObj)
err = m.Db.DeleteObject(ctx, deleteObj, tx)
if err != nil {
return err
}
}
err = m.Db.PutObject(ctx, object, tx)
if err != nil {
return err
}
if multipart != nil {
err = m.Db.DeleteMultipart(multipart, tx)
if err != nil {
return err
}
}
// TODO: usage need to be updated for charging, and it depends on redis, and the mechanism is:
// 1. Update usage in redis when each put happens.
// 2. Update usage in database periodically based on redis.
// see https://github.com/opensds/multi-cloud/issues/698 for redis related issue.
err = m.Db.CommitTrans(tx)
return nil
}
func (m *Meta) UpdateObjectMeta(object *Object) error {
err := m.Db.UpdateObjectMeta(object)
return err
}
func (m *Meta) DeleteObject(ctx context.Context, object *Object) error {
tx, err := m.Db.NewTrans()
defer func() {
if err != nil {
m.Db.AbortTrans(tx)
}
}()
err = m.Db.DeleteObject(ctx, object, tx)
if err != nil {
return err
}
// TODO: usage need to be updated for charging, it depends on redis, and the mechanism is:
// 1. Update usage in redis when each delete happens.
// 2. Update usage in database periodically based on redis.
// see https://github.com/opensds/multi-cloud/issues/698 for redis related issue.
err = m.Db.CommitTrans(tx)
return err
}
func (m *Meta) MarkObjectAsDeleted(ctx context.Context, object *Object) error {
return m.Db.SetObjectDeleteMarker(ctx, object, true)
}
func (m *Meta) UpdateObject4Lifecycle(ctx context.Context, old, new *Object, multipart *Multipart) (err error) {
log.Infof("update object from %v to %v\n", *old, *new)
tx, err := m.Db.NewTrans()
defer func() {
if err != nil {
m.Db.AbortTrans(tx)
}
}()
err = m.Db.UpdateObject4Lifecycle(ctx, old, new, tx)
if err != nil {
return err
}
if multipart != nil {
err = m.Db.DeleteMultipart(multipart, tx)
if err != nil {
return err
}
}
err = m.Db.CommitTrans(tx)
return err
}