-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
ceph.go
250 lines (222 loc) · 6.42 KB
/
ceph.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
// Package cephbackupstorage implements the BackupStorage interface
// for Ceph Cloud Storage.
package cephbackupstorage
import (
"encoding/json"
"flag"
"fmt"
"io"
"os"
"sort"
"strings"
"sync"
minio "github.com/minio/minio-go"
"github.com/youtube/vitess/go/vt/concurrency"
"github.com/youtube/vitess/go/vt/mysqlctl/backupstorage"
)
var (
// bucket is where the backups will go.
bucket string
// configFilePath is where the configs/credentials for backups will be stored.
configFilePath = flag.String("ceph_backup_storage_config", "ceph_backup_config.json",
"Path to JSON config file for ceph backup storage")
)
var storageConfig struct {
AccessKey string `json:"accessKey"`
SecretKey string `json:"secretKey"`
EndPoint string `json:"endPoint"`
Bucket string `json:"bucket"`
}
// CephBackupHandle implements BackupHandle for Ceph Cloud Storage.
type CephBackupHandle struct {
client *minio.Client
bs *CephBackupStorage
dir string
name string
readOnly bool
errors concurrency.AllErrorRecorder
waitGroup sync.WaitGroup
}
// Directory implements BackupHandle.
func (bh *CephBackupHandle) Directory() string {
return bh.dir
}
// Name implements BackupHandle.
func (bh *CephBackupHandle) Name() string {
return bh.name
}
// AddFile implements BackupHandle.
func (bh *CephBackupHandle) AddFile(filename string) (io.WriteCloser, error) {
if bh.readOnly {
return nil, fmt.Errorf("AddFile cannot be called on read-only backup")
}
reader, writer := io.Pipe()
bh.waitGroup.Add(1)
go func() {
defer bh.waitGroup.Done()
// Give PutObject() the read end of the pipe.
object := objName(bh.dir, bh.name, filename)
_, err := bh.client.PutObject(bucket, object, reader, "application/octet-stream")
if err != nil {
// Signal the writer that an error occurred, in case it's not done writing yet.
reader.CloseWithError(err)
// In case the error happened after the writer finished, we need to remember it.
bh.errors.RecordError(err)
}
}()
// Give our caller the write end of the pipe.
return writer, nil
}
// EndBackup implements BackupHandle.
func (bh *CephBackupHandle) EndBackup() error {
if bh.readOnly {
return fmt.Errorf("EndBackup cannot be called on read-only backup")
}
bh.waitGroup.Wait()
// Return the saved PutObject() errors, if any.
return bh.errors.Error()
}
// AbortBackup implements BackupHandle.
func (bh *CephBackupHandle) AbortBackup() error {
if bh.readOnly {
return fmt.Errorf("AbortBackup cannot be called on read-only backup")
}
return bh.bs.RemoveBackup(bh.dir, bh.name)
}
// ReadFile implements BackupHandle.
func (bh *CephBackupHandle) ReadFile(filename string) (io.ReadCloser, error) {
if !bh.readOnly {
return nil, fmt.Errorf("ReadFile cannot be called on read-write backup")
}
object := objName(bh.dir, bh.name, filename)
return bh.client.GetObject(bucket, object)
}
// CephBackupStorage implements BackupStorage for Ceph Cloud Storage.
type CephBackupStorage struct {
// client is the instance of the Ceph Cloud Storage Go client.
// Once this field is set, it must not be written again/unset to nil.
_client *minio.Client
// mu guards all fields.
mu sync.Mutex
}
// ListBackups implements BackupStorage.
func (bs *CephBackupStorage) ListBackups(dir string) ([]backupstorage.BackupHandle, error) {
c, err := bs.client()
if err != nil {
return nil, err
}
// List prefixes that begin with dir (i.e. list subdirs).
var subdirs []string
searchPrefix := objName(dir, "")
doneCh := make(chan struct{})
for object := range c.ListObjects(bucket, searchPrefix, false, doneCh) {
if object.Err != nil {
return nil, object.Err
}
subdir := strings.TrimPrefix(object.Key, searchPrefix)
subdir = strings.TrimSuffix(subdir, "/")
subdirs = append(subdirs, subdir)
}
// Backups must be returned in order, oldest first.
sort.Strings(subdirs)
result := make([]backupstorage.BackupHandle, 0, len(subdirs))
for _, subdir := range subdirs {
result = append(result, &CephBackupHandle{
client: c,
bs: bs,
dir: dir,
name: subdir,
readOnly: true,
})
}
return result, nil
}
// StartBackup implements BackupStorage.
func (bs *CephBackupStorage) StartBackup(dir, name string) (backupstorage.BackupHandle, error) {
c, err := bs.client()
if err != nil {
return nil, err
}
return &CephBackupHandle{
client: c,
bs: bs,
dir: dir,
name: name,
readOnly: false,
}, nil
}
// RemoveBackup implements BackupStorage.
func (bs *CephBackupStorage) RemoveBackup(dir, name string) error {
c, err := bs.client()
if err != nil {
return err
}
fullName := objName(dir, name, "")
// err = c.RemoveObject(bucket, fullName)
// if err != nil {
// return err
// }
// return nil
var arr []string
doneCh := make(chan struct{})
defer close(doneCh)
for object := range c.ListObjects(bucket, fullName, true, doneCh) {
if object.Err != nil {
return object.Err
}
arr = append(arr, object.Key)
}
for _, obj := range arr {
err = c.RemoveObject(bucket, obj)
if err != nil {
return err
}
}
return nil
}
// Close implements BackupStorage.
func (bs *CephBackupStorage) Close() error {
bs.mu.Lock()
defer bs.mu.Unlock()
if bs._client != nil {
// a new client the next time one is needed.
bs._client = nil
}
return nil
}
// client returns the Ceph Storage client instance.
// If there isn't one yet, it tries to create one.
func (bs *CephBackupStorage) client() (*minio.Client, error) {
bs.mu.Lock()
defer bs.mu.Unlock()
if bs._client == nil {
configFile, err := os.Open(*configFilePath)
if err != nil {
return nil, fmt.Errorf("file not present : %v", err)
}
defer configFile.Close()
jsonParser := json.NewDecoder(configFile)
if err = jsonParser.Decode(&storageConfig); err != nil {
return nil, fmt.Errorf("Error parsing the json file : %v", err)
}
bucket = storageConfig.Bucket
accessKey := storageConfig.AccessKey
secretKey := storageConfig.SecretKey
url := storageConfig.EndPoint
client, err := minio.NewV2(url, accessKey, secretKey, true)
if err != nil {
return nil, err
}
bs._client = client
}
return bs._client, nil
}
func init() {
backupstorage.BackupStorageMap["ceph"] = &CephBackupStorage{}
}
// objName joins path parts into an object name.
// Unlike path.Join, it doesn't collapse ".." or strip trailing slashes.
// It also adds the value of the -gcs_backup_storage_root flag if set.
func objName(parts ...string) string {
return strings.Join(parts, "/")
}