/
minio.go
209 lines (169 loc) · 5.09 KB
/
minio.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
package storage
import (
"context"
"sync"
"time"
"github.com/Filecoin-Titan/titan/api"
"github.com/Filecoin-Titan/titan/api/types"
"github.com/Filecoin-Titan/titan/node/config"
"github.com/ipfs/go-cid"
"github.com/minio/madmin-go/v3"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"bytes"
"io"
"github.com/ipfs/go-unixfsnode/data/builder"
"github.com/ipld/go-ipld-prime"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
)
const (
timeInterval = 3 * time.Second
eventObjectCreate = "s3:ObjectCreated:*"
)
type IMinioService interface {
GetMinioStat(ctx context.Context) (totalSpace, usage float64)
}
type minioService struct {
adminClient *madmin.AdminClient
s3Client *minio.Client
schedulerAPI api.Scheduler
objects []*object
lock *sync.Mutex
}
type object struct {
name string
bucket string
}
func newMinioService(config *config.MinioConfig, schedulerAPI api.Scheduler) (IMinioService, error) {
if len(config.AccessKeyID) == 0 || len(config.Endpoint) == 0 || len(config.SecretAccessKey) == 0 {
return nil, nil
}
// Initialize minio client object.
adminClient, err := madmin.New(config.Endpoint, config.AccessKeyID, config.SecretAccessKey, false)
if err != nil {
return nil, err
}
minClient, err := minio.New(config.Endpoint, &minio.Options{
Creds: credentials.NewStaticV4(config.AccessKeyID, config.SecretAccessKey, ""),
Secure: false,
})
if err != nil {
return nil, err
}
client := &minioService{
adminClient: adminClient,
s3Client: minClient,
schedulerAPI: schedulerAPI,
objects: make([]*object, 0),
lock: &sync.Mutex{},
}
go client.ListenObjectCreate()
go client.run()
return client, nil
}
func (mc *minioService) ListenObjectCreate() {
log.Debugf("listen minio object create")
// TODO recovery panic
for {
notificationInfos := mc.s3Client.ListenNotification(context.Background(), "", "", []string{
eventObjectCreate,
})
for info := range notificationInfos {
if info.Err != nil {
log.Error("ListenNotification error ", info.Err.Error())
continue
}
for _, event := range info.Records {
mc.addObject(&object{bucket: event.S3.Bucket.Name, name: event.S3.Object.Key})
log.Debugf("listen event %s, bucket %s, object %s,", event.EventName, event.S3.Bucket.Name, event.S3.Object.Key)
}
}
}
}
func (mc *minioService) run() {
for {
time.Sleep(timeInterval)
for object := mc.nextObject(); object != nil; {
if err := mc.calculateCidAndSend(object.bucket, object.name); err != nil {
log.Errorf("calculateCidAndSend error %s", err.Error())
}
object = mc.nextObject()
}
}
}
func (mc *minioService) nextObject() *object {
mc.lock.Lock()
defer mc.lock.Unlock()
if len(mc.objects) == 0 {
return nil
}
obj := mc.objects[0]
mc.objects = mc.objects[1:]
return obj
}
func (mc *minioService) addObject(obj *object) {
mc.lock.Lock()
defer mc.lock.Unlock()
mc.objects = append(mc.objects, obj)
}
func (mc *minioService) calculateCidAndSend(bucket, object string) error {
obj, err := mc.s3Client.GetObject(context.Background(), bucket, object, minio.GetObjectOptions{})
if err != nil {
return err
}
defer obj.Close()
cid, err := mc.calculateCid(obj)
if err != nil {
return err
}
objInfo, err := mc.s3Client.StatObject(context.Background(), bucket, object, minio.StatObjectOptions{})
if err != nil {
return err
}
event := &types.MinioUploadFileEvent{
AssetCID: cid.String(),
Size: objInfo.Size,
CreateTime: objInfo.LastModified,
Expiration: objInfo.Expiration,
}
if event.Expiration.IsZero() {
event.Expiration = time.Now().AddDate(20, 0, 0)
}
log.Debugf("cid %s, size %d, CreateTime %s, Expiration:%s", cid.String(), event.Size, event.CreateTime, event.Expiration)
return mc.schedulerAPI.MinioUploadFileEvent(context.Background(), event)
}
func (mc *minioService) calculateCid(r io.Reader) (cid.Cid, error) {
ls := cidlink.DefaultLinkSystem()
ls.TrustedStorage = true
ls.StorageReadOpener = func(_ ipld.LinkContext, l ipld.Link) (io.Reader, error) {
return nil, nil
}
ls.StorageWriteOpener = func(_ ipld.LinkContext) (io.Writer, ipld.BlockWriteCommitter, error) {
buf := bytes.NewBuffer(nil)
return buf, func(l ipld.Link) error {
return nil
}, nil
}
link, _, err := builder.BuildUnixFSFile(r, "", &ls)
if err != nil {
return cid.Cid{}, err
}
root := link.(cidlink.Link)
return root.Cid, nil
}
func (mc *minioService) GetMinioStat(ctx context.Context) (totalSpace, usage float64) {
info, err := mc.adminClient.ServerInfo(ctx)
if err != nil {
log.Errorf("minio admin get server info error %s", err.Error())
return 0, 0
}
rawCapacity := uint64(0)
rawUsage := uint64(0)
for _, pool := range info.Pools {
for _, erasureSet := range pool {
rawCapacity += erasureSet.RawCapacity
rawUsage += erasureSet.RawUsage
}
}
return float64(rawCapacity), float64(rawUsage) / float64(rawCapacity)
}