This repository has been archived by the owner on Jul 6, 2023. It is now read-only.
/
volume_entry.go
1146 lines (990 loc) · 31.8 KB
/
volume_entry.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright (c) 2015 The heketi Authors
//
// This file is licensed to you under your choice of the GNU Lesser
// General Public License, version 3 or any later version (LGPLv3 or
// later), or the GNU General Public License, version 2 (GPLv2), in all
// cases as published by the Free Software Foundation.
//
package glusterfs
import (
"bytes"
"encoding/gob"
"errors"
"fmt"
"sort"
"strconv"
"strings"
"github.com/boltdb/bolt"
"github.com/heketi/heketi/v10/executors"
wdb "github.com/heketi/heketi/v10/pkg/db"
"github.com/heketi/heketi/v10/pkg/glusterfs/api"
"github.com/heketi/heketi/v10/pkg/idgen"
"github.com/heketi/heketi/v10/pkg/paths"
"github.com/heketi/heketi/v10/pkg/sortedstrings"
"github.com/lpabon/godbc"
)
const (
// Byte values in KB
KB = 1
MB = KB * 1024
GB = MB * 1024
TB = GB * 1024
// Default values
DEFAULT_REPLICA = 2
DEFAULT_EC_DATA = 4
DEFAULT_EC_REDUNDANCY = 2
DEFAULT_THINP_SNAPSHOT_FACTOR = 1.5
HEKETI_ID_KEY = "user.heketi.id"
HEKETI_ARBITER_KEY = "user.heketi.arbiter"
HEKETI_AVERAGE_FILE_SIZE_KEY = "user.heketi.average-file-size"
HEKETI_ZONE_CHECKING_KEY = "user.heketi.zone-checking"
HEKETI_TAG_MATCH_KEY = "user.heketi.device-tag-match"
)
var (
// Average size of files on a volume, currently used only for arbiter sizing.
// Might be used for other purposes later.
averageFileSize uint64 = 64 * KB
)
// VolumeEntry struct represents a volume in heketi. Serialization is done using
// gob when written to db and using json package when exportdb/importdb is used
// There are two reasons I skip Durability field for json pkg
// 1. Durability is used in some places in code, however, it represents the
// same info that is in Info.Durability.
// 2. I wasn't able to serialize interface type to json in a straightfoward
// way.
// Chose to skip writing redundant data than adding kludgy code
type VolumeEntry struct {
Info api.VolumeInfo
Bricks sort.StringSlice
Durability VolumeDurability `json:"-"`
GlusterVolumeOptions []string
Pending PendingItem
}
func VolumeList(tx *bolt.Tx) ([]string, error) {
list := EntryKeys(tx, BOLTDB_BUCKET_VOLUME)
if list == nil {
return nil, ErrAccessList
}
return list, nil
}
func NewVolumeEntry() *VolumeEntry {
entry := &VolumeEntry{}
entry.Bricks = make(sort.StringSlice, 0)
return entry
}
func NewVolumeEntryFromRequest(req *api.VolumeCreateRequest) *VolumeEntry {
godbc.Require(req != nil)
vol := NewVolumeEntry()
vol.Info.Gid = req.Gid
vol.Info.Id = idgen.GenUUID()
vol.Info.Durability = req.Durability
vol.Info.Snapshot = req.Snapshot
vol.Info.Size = req.Size
vol.Info.Block = req.Block
// Set default durability values
durability := vol.Info.Durability.Type
switch {
case durability == api.DurabilityReplicate:
logger.Debug("[%v] Replica %v",
vol.Info.Id,
vol.Info.Durability.Replicate.Replica)
vol.Durability = NewVolumeReplicaDurability(&vol.Info.Durability.Replicate)
case durability == api.DurabilityEC:
logger.Debug("[%v] EC %v + %v ",
vol.Info.Id,
vol.Info.Durability.Disperse.Data,
vol.Info.Durability.Disperse.Redundancy)
vol.Durability = NewVolumeDisperseDurability(&vol.Info.Durability.Disperse)
case durability == api.DurabilityDistributeOnly || durability == "":
logger.Debug("[%v] Distributed", vol.Info.Id)
vol.Durability = NewNoneDurability()
default:
panic(fmt.Sprintf("BUG: Unknown type: %v\n", vol.Info.Durability))
}
// Set the default values accordingly
vol.Durability.SetDurability()
// Set default name
if req.Name == "" {
vol.Info.Name = "vol_" + vol.Info.Id
} else {
vol.Info.Name = req.Name
}
// Set default thinp factor
if vol.Info.Snapshot.Enable && vol.Info.Snapshot.Factor == 0 {
vol.Info.Snapshot.Factor = DEFAULT_THINP_SNAPSHOT_FACTOR
} else if !vol.Info.Snapshot.Enable {
vol.Info.Snapshot.Factor = 1
}
// If it is zero, then no volume options are set.
vol.GlusterVolumeOptions = req.GlusterVolumeOptions
if vol.Info.Block {
if err := vol.SetRawCapacity(req.Size); err != nil {
logger.Err(err)
// we can either panic here or return nil. We panic because
// returning nil is most likely going to lead to nil
// dereference panics anyway
panic(err)
}
// prepend the gluster-block group option,
// so that the user-specified options can take precedence
blockoptions := strings.Split(BlockHostingVolumeOptions, ",")
vol.GlusterVolumeOptions = append(
blockoptions,
vol.GlusterVolumeOptions...)
}
// Add volume options using PreRequestVolumeOptions, this must be
// set before volume options from the request are set.
preReqVolumeOptions := strings.Split(PreReqVolumeOptions, ",")
vol.GlusterVolumeOptions = append(preReqVolumeOptions,
vol.GlusterVolumeOptions...)
// Add volume options using PostRequestVolumeOptions, this must be
// set after volume options from the request are set.
postReqVolumeOptions := strings.Split(PostReqVolumeOptions, ",")
vol.GlusterVolumeOptions = append(vol.GlusterVolumeOptions,
postReqVolumeOptions...)
// If it is zero, then it will be assigned during volume creation
vol.Info.Clusters = req.Clusters
return vol
}
func NewVolumeEntryFromId(tx *bolt.Tx, id string) (*VolumeEntry, error) {
godbc.Require(tx != nil)
entry := NewVolumeEntry()
err := EntryLoad(tx, entry, id)
if err != nil {
return nil, err
}
return entry, nil
}
func NewVolumeEntryFromClone(v *VolumeEntry, name string) *VolumeEntry {
entry := NewVolumeEntry()
entry.Info.Id = idgen.GenUUID()
if name == "" {
entry.Info.Name = "vol_" + entry.Info.Id
} else {
entry.Info.Name = name
}
entry.GlusterVolumeOptions = v.GlusterVolumeOptions
entry.Info.Cluster = v.Info.Cluster
entry.Info.Durability = v.Info.Durability
entry.Info.Durability.Type = v.Info.Durability.Type
entry.Info.Gid = v.Info.Gid
entry.Info.Mount = v.Info.Mount
entry.Info.Size = v.Info.Size
entry.Info.Snapshot = v.Info.Snapshot
copy(entry.Info.Mount.GlusterFS.Hosts, v.Info.Mount.GlusterFS.Hosts)
entry.Info.Mount.GlusterFS.MountPoint = v.Info.Mount.GlusterFS.Hosts[0] + ":" + entry.Info.Name
entry.Info.Mount.GlusterFS.Options = v.Info.Mount.GlusterFS.Options
entry.Info.BlockInfo.FreeSize = v.Info.BlockInfo.FreeSize
copy(entry.Info.BlockInfo.BlockVolumes, v.Info.BlockInfo.BlockVolumes)
// entry.Bricks is still empty, these need to be filled by the caller
return entry
}
func (v *VolumeEntry) BucketName() string {
return BOLTDB_BUCKET_VOLUME
}
func (v *VolumeEntry) Save(tx *bolt.Tx) error {
godbc.Require(tx != nil)
godbc.Require(len(v.Info.Id) > 0)
return EntrySave(tx, v, v.Info.Id)
}
func (v *VolumeEntry) Delete(tx *bolt.Tx) error {
return EntryDelete(tx, v, v.Info.Id)
}
func (v *VolumeEntry) NewInfoResponse(tx *bolt.Tx) (*api.VolumeInfoResponse, error) {
godbc.Require(tx != nil)
info := api.NewVolumeInfoResponse()
info.Id = v.Info.Id
info.Cluster = v.Info.Cluster
if err := v.updateMountInfo(wdb.WrapTx(tx), &info.VolumeInfo); err != nil {
return nil, err
}
info.Snapshot = v.Info.Snapshot
info.Size = v.Info.Size
info.Durability = v.Info.Durability
info.Name = v.Info.Name
info.GlusterVolumeOptions = v.GlusterVolumeOptions
info.Block = v.Info.Block
info.BlockInfo = v.Info.BlockInfo
info.Gid = v.Info.Gid
for _, brickid := range v.BricksIds() {
brick, err := NewBrickEntryFromId(tx, brickid)
if err != nil {
return nil, err
}
brickinfo, err := brick.NewInfoResponse(tx)
if err != nil {
return nil, err
}
info.Bricks = append(info.Bricks, *brickinfo)
}
return info, nil
}
func (v *VolumeEntry) Marshal() ([]byte, error) {
var buffer bytes.Buffer
enc := gob.NewEncoder(&buffer)
err := enc.Encode(*v)
return buffer.Bytes(), err
}
func (v *VolumeEntry) Unmarshal(buffer []byte) error {
dec := gob.NewDecoder(bytes.NewReader(buffer))
err := dec.Decode(v)
if err != nil {
return err
}
// Make sure to setup arrays if nil
if v.Bricks == nil {
v.Bricks = make(sort.StringSlice, 0)
}
return nil
}
// volOptsMap returns the volume options as a map of space separated
// key-value pairs. Keys that are found later in the list will overwrite
// the same key if it occurred earlier in the list. Strings that lack a
// space separating key from value will be treated as a key with a
// value of "".
func (v *VolumeEntry) volOptsMap() map[string]string {
om := map[string]string{}
for _, s := range v.GlusterVolumeOptions {
r := strings.SplitN(s, " ", 2)
if len(r) == 2 {
om[r[0]] = r[1]
} else {
om[r[0]] = ""
}
}
return om
}
// HasArbiterOption returns true if this volume is flagged for
// arbiter support.
func (v *VolumeEntry) HasArbiterOption() bool {
value := v.volOptsMap()[HEKETI_ARBITER_KEY]
if b, e := strconv.ParseBool(value); e == nil {
return b
}
return false
}
// GetAverageFileSize returns averageFileSize provided by user or default averageFileSize
func (v *VolumeEntry) GetAverageFileSize() uint64 {
value := v.volOptsMap()[HEKETI_AVERAGE_FILE_SIZE_KEY]
if size, e := strconv.ParseUint(value, 10, 64); e == nil {
if size == 0 {
logger.LogError("Average File Size cannot be zero, using default file size %v", averageFileSize)
return averageFileSize
}
return size
}
return averageFileSize
}
// GetZoneCheckingStrategy returns a ZoneCheckingStrategy based on
// the volume's options.
func (v *VolumeEntry) GetZoneCheckingStrategy() ZoneCheckingStrategy {
value := v.volOptsMap()[HEKETI_ZONE_CHECKING_KEY]
if value != "" {
return ZoneCheckingStrategy(value)
}
return ZONE_CHECKING_UNSET
}
func (v *VolumeEntry) GetTagMatchingRule() (*TagMatchingRule, error) {
value := v.volOptsMap()[HEKETI_TAG_MATCH_KEY]
if value == "" {
return nil, nil
}
return ParseTagMatchingRule(value)
}
func (v *VolumeEntry) BrickAdd(id string) {
godbc.Require(!sortedstrings.Has(v.Bricks, id))
v.Bricks = append(v.Bricks, id)
v.Bricks.Sort()
}
func (v *VolumeEntry) BrickDelete(id string) {
v.Bricks = sortedstrings.Delete(v.Bricks, id)
}
func (v *VolumeEntry) Create(db wdb.DB,
executor executors.Executor) (e error) {
return RunOperation(
NewVolumeCreateOperation(v, db),
executor)
}
// ModifyFreeSize adjusts the free size of a block hosting volume.
// When taking space from the volume the value must be negative (on
// block volume add) and positive when the space is being "freed."
func (v *VolumeEntry) ModifyFreeSize(delta int) error {
logger.Debug("Modifying free size: FreeSize=[%v] delta=[%v]",
v.Info.BlockInfo.FreeSize, delta)
v.Info.BlockInfo.FreeSize += delta
if v.Info.BlockInfo.FreeSize < 0 {
return logger.Err(fmt.Errorf(
"Volume %v free size may not be set less than zero", v.Info.Id))
}
if v.Info.BlockInfo.FreeSize+v.Info.BlockInfo.ReservedSize > v.Info.Size {
return logger.Err(fmt.Errorf(
"Volume %v free size may not be set greater than %v",
v.Info.Id, v.Info.Size))
}
return nil
}
func (v *VolumeEntry) ModifyReservedSize(delta int) error {
logger.Debug("Modifying reserved size: ReservedSize=[%v] delta=[%v]",
v.Info.BlockInfo.ReservedSize, delta)
v.Info.BlockInfo.ReservedSize += delta
if v.Info.BlockInfo.ReservedSize < 0 {
return logger.Err(fmt.Errorf(
"Volume %v reserved size may not be set less than zero", v.Info.Id))
}
if v.Info.BlockInfo.ReservedSize+v.Info.BlockInfo.FreeSize > v.Info.Size {
return logger.Err(fmt.Errorf(
"Volume %v reserved size may not be set greater than %v",
v.Info.Id, v.Info.Size))
}
return nil
}
//ReduceRawSize reserves 2% of size for block volume creation
func ReduceRawSize(size int) int {
return size * 98 / 100
}
// AddRawCapacity adds raw capacity to the BlockInfo
// FreeSize tracking, reserving one percent of the
// raw capacity for the filesystem.
func (v *VolumeEntry) AddRawCapacity(delta int) error {
var freeDelta int
var reservedDelta int
freeDelta = ReduceRawSize(delta)
reservedDelta = delta - freeDelta
if err := v.ModifyFreeSize(freeDelta); err != nil {
return err
}
if err := v.ModifyReservedSize(reservedDelta); err != nil {
return err
}
return nil
}
func (v *VolumeEntry) SetRawCapacity(delta int) error {
v.Info.BlockInfo.FreeSize = 0
v.Info.BlockInfo.ReservedSize = 0
return v.AddRawCapacity(delta)
}
// TotalSizeBlockVolumes returns the total size of the block volumes that
// the given volume is hosting. This function iterates over the block
// volumes in the db to calculate the total.
func (v *VolumeEntry) TotalSizeBlockVolumes(tx *bolt.Tx) (int, error) {
if !v.Info.Block {
return 0, fmt.Errorf(
"Volume %v is not a block hosting volume", v.Info.Id)
}
bvsum := 0
for _, bvid := range v.Info.BlockInfo.BlockVolumes {
bvol, err := NewBlockVolumeEntryFromId(tx, bvid)
if err != nil {
return 0, err
}
bvsum += bvol.Info.Size
}
return bvsum, nil
}
// blockHostingSizeIsCorrect returns true if the total size of the volume
// is equal to the sum of the used, free and reserved block hosting size values.
// The used size must be provided and should be calculated based on the sizes
// of the block volumes.
func (v *VolumeEntry) blockHostingSizeIsCorrect(used int) bool {
logger.Debug("volume [%v]: comparing %v == %v + %v + %v",
v.Info.Id, v.Info.Size,
used, v.Info.BlockInfo.FreeSize, v.Info.BlockInfo.ReservedSize)
unused := v.Info.BlockInfo.FreeSize + v.Info.BlockInfo.ReservedSize
if v.Info.Size != (used + unused) {
logger.Warning("detected volume [%v] has size %v != %v + %v + %v",
v.Info.Id, v.Info.Size,
used, v.Info.BlockInfo.FreeSize, v.Info.BlockInfo.ReservedSize)
return false
}
return true
}
func (v *VolumeEntry) tryAllocateBricks(
db wdb.DB,
possibleClusters []string) (brick_entries []*BrickEntry, err error) {
cerr := ClusterErrorMap{}
for _, cluster := range possibleClusters {
// Check this cluster for space
brick_entries, err = v.allocBricksInCluster(db, cluster, v.Info.Size)
if err == nil {
v.Info.Cluster = cluster
logger.Debug("Volume to be created on cluster %v", cluster)
break
} else if err == ErrNoSpace ||
err == ErrMaxBricks ||
err == ErrMinimumBrickSize {
logger.Debug("Cluster %v can not accommodate volume "+
"(%v), trying next cluster", cluster, err)
// Map these errors to NoSpace here as that is what heketi
// traditionally did. Its not particularly helpful but it
// is more backwards compatible.
cerr.Add(cluster, ErrNoSpace)
} else if err == ErrEmptyCluster ||
err == ErrNoStorage {
logger.Debug("Issue on cluster %v: %v", cluster, err)
cerr.Add(cluster, err)
} else {
// A genuine error occurred - bail out
logger.LogError("Error calling v.allocBricksInCluster: %v", err)
return
}
}
// if our last attempt failed and we collected at least one error
// return the short form all the errors we collected
if err != nil && len(cerr) > 0 {
err = cerr.ToError("Unable to create volume on any cluster:")
}
return
}
// checkDestroyVolumeFromHost returns an error if the volume may
// not be destroyed at this time. Nil is returned if the volume
// may be destroyed.
func (v *VolumeEntry) checkDestroyVolumeFromHost(
executor executors.Executor, h string) error {
err := executor.VolumeDestroyCheck(h, v.Info.Name)
if _, ok := err.(*executors.VolumeDoesNotExistErr); ok {
logger.Warning("volume %v not present in gluster", v.Info.Id)
return nil
}
return err
}
func (v *VolumeEntry) destroyVolumeFromHost(
executor executors.Executor, h string) error {
err := executor.VolumeDestroy(h, v.Info.Name)
switch {
case err == nil:
// no errors, so we just deleted the volume from gluster
return nil
case strings.Contains(err.Error(), "does not exist"):
// we asked gluster to delete a volume that already does not exist
return nil
default:
logger.Warning("failed to delete volume %v via %v: %v",
v.Info.Id, h, err)
return err
}
}
func (v *VolumeEntry) destroyGlusterVolume(
db wdb.RODB, executor executors.Executor) error {
hosts, err := v.hosts(db)
if err != nil {
return err
}
err = newTryOnHosts(hosts).run(func(h string) error {
return v.destroyVolumeFromHost(executor, h)
})
if err != nil {
logger.LogError("failed to delete volume in cleanup: %v", err)
return fmt.Errorf("failed to clean up volume: %v", v.Info.Id)
}
return nil
}
func (v *VolumeEntry) cleanupCreateVolume(db wdb.DB,
executor executors.Executor,
brick_entries []*BrickEntry) error {
if err := v.destroyGlusterVolume(db, executor); err != nil {
return err
}
// from a quick read its "safe" to unconditionally try to delete
// bricks. TODO: find out if that is true with functional tests
reclaimed, err := DestroyBricks(db, executor, brick_entries)
if err != nil {
logger.LogError("failed to destroy bricks during cleanup: %v", err)
}
return v.teardown(db, brick_entries, reclaimed)
}
func (v *VolumeEntry) createVolumeComponents(
db wdb.DB) ([]*BrickEntry, error) {
// Get list of clusters
var possibleClusters []string
if len(v.Info.Clusters) == 0 {
err := db.View(func(tx *bolt.Tx) error {
var err error
possibleClusters, err = ClusterList(tx)
return err
})
if err != nil {
return nil, err
}
} else {
possibleClusters = v.Info.Clusters
}
cr := clusterReq{
allowBlock: v.Info.Block,
allowName: v.Info.Name,
allowCreate: true,
}
possibleClusters, err := eligibleClusters(db, cr, possibleClusters)
if err != nil {
return nil, err
}
logger.Debug("Using the following clusters: %+v", possibleClusters)
return v.saveCreateVolume(db, possibleClusters)
}
func (v *VolumeEntry) createVolumeExec(db wdb.DB,
executor executors.Executor,
brick_entries []*BrickEntry) (e error) {
// Create the bricks on the nodes
e = CreateBricks(db, executor, brick_entries)
if e != nil {
return
}
// Create GlusterFS volume
return v.createVolume(db, executor, brick_entries)
}
func (v *VolumeEntry) saveCreateVolume(db wdb.DB,
possibleClusters []string) (brick_entries []*BrickEntry, err error) {
err = db.Update(func(tx *bolt.Tx) error {
txdb := wdb.WrapTx(tx)
// For each cluster look for storage space for this volume
brick_entries, err = v.tryAllocateBricks(txdb, possibleClusters)
if err != nil {
return err
}
if brick_entries == nil {
// Map all 'valid' errors to NoSpace here:
// Only the last such error could get propagated down,
// so it does not make sense to hand the granularity on.
// But for other callers (Expand), we keep it.
return ErrNoSpace
}
err = v.updateMountInfo(txdb, &v.Info)
if err != nil {
return err
}
// Save volume information
if v.Info.Block {
if err := v.SetRawCapacity(v.Info.Size); err != nil {
return err
}
}
err := v.Save(tx)
if err != nil {
return err
}
// Save cluster
cluster, err := NewClusterEntryFromId(tx, v.Info.Cluster)
if err != nil {
return err
}
cluster.VolumeAdd(v.Info.Id)
return cluster.Save(tx)
})
return
}
func (v *VolumeEntry) deleteVolumeExec(db wdb.RODB,
executor executors.Executor,
brick_entries []*BrickEntry,
sshhost string) (map[string]bool, error) {
if err := v.checkDestroyVolumeFromHost(executor, sshhost); err != nil {
return nil, err
}
if err := v.destroyVolumeFromHost(executor, sshhost); err != nil {
logger.LogError("Unable to delete volume: %v", err)
return nil, err
}
// Destroy bricks
space_reclaimed, err := DestroyBricks(db, executor, brick_entries)
if err != nil {
logger.LogError("Unable to delete bricks: %v", err)
return nil, err
}
return space_reclaimed, nil
}
// teardown removes a volume and the volume's bricks from the database.
// It updates related objects meta-data such as cluster contents and
// device used/free sizes.
func (v *VolumeEntry) teardown(
db wdb.DB, brick_entries []*BrickEntry, reclaimed ReclaimMap) error {
return db.Update(func(tx *bolt.Tx) error {
for _, brick := range brick_entries {
err := brick.removeAndFree(tx, v, reclaimed[brick.Info.DeviceId])
if err != nil {
return err
}
}
if v.Info.Cluster != "" {
cluster, err := NewClusterEntryFromId(tx, v.Info.Cluster)
if err != nil {
return err
}
cluster.VolumeDelete(v.Info.Id)
err = cluster.Save(tx)
if err != nil {
return err
}
}
return v.Delete(tx)
})
}
func (v *VolumeEntry) manageHostFromBricks(db wdb.DB,
brick_entries []*BrickEntry) (sshhost string, err error) {
err = db.View(func(tx *bolt.Tx) error {
for _, brick := range brick_entries {
node, err := NewNodeEntryFromId(tx, brick.Info.NodeId)
if err != nil {
return err
}
sshhost = node.ManageHostName()
return nil
}
return fmt.Errorf("Unable to get management host from bricks")
})
return
}
func (v *VolumeEntry) deleteVolumeComponents(
db wdb.RODB) (brick_entries []*BrickEntry, e error) {
e = db.View(func(tx *bolt.Tx) error {
for _, id := range v.BricksIds() {
brick, err := NewBrickEntryFromId(tx, id)
if err != nil {
logger.LogError("Brick %v not found in db: %v", id, err)
return err
}
brick_entries = append(brick_entries, brick)
}
return nil
})
return
}
func (v *VolumeEntry) Destroy(db wdb.DB, executor executors.Executor) error {
logger.Info("Destroying volume %v", v.Info.Id)
return RunOperation(
NewVolumeDeleteOperation(v, db),
executor)
}
func (v *VolumeEntry) expandVolumeComponents(db wdb.DB,
sizeGB int,
setSize bool) (brick_entries []*BrickEntry, e error) {
e = db.Update(func(tx *bolt.Tx) error {
// Allocate new bricks in the cluster
txdb := wdb.WrapTx(tx)
var err error
brick_entries, err = v.allocBricksInCluster(txdb, v.Info.Cluster, sizeGB)
if err != nil {
return err
}
// Increase the recorded volume size
if setSize {
v.Info.Size += sizeGB
}
// Save brick entries
for _, brick := range brick_entries {
err := brick.Save(tx)
if err != nil {
return err
}
}
return v.Save(tx)
})
return
}
func (v *VolumeEntry) expandVolumeExec(db wdb.DB,
executor executors.Executor,
brick_entries []*BrickEntry) (e error) {
// Create bricks
err := CreateBricks(db, executor, brick_entries)
if err != nil {
return err
}
// Create a volume request to send to executor
// so that it can add the new bricks
vr, host, err := v.createVolumeRequest(db, brick_entries)
if err != nil {
return err
}
// Expand the volume
_, err = executor.VolumeExpand(host, vr)
if err != nil {
return err
}
return err
}
func (v *VolumeEntry) Expand(db wdb.DB,
executor executors.Executor,
sizeGB int) (e error) {
return RunOperation(
NewVolumeExpandOperation(v, db, sizeGB),
executor)
}
func (v *VolumeEntry) BricksIds() sort.StringSlice {
ids := make(sort.StringSlice, len(v.Bricks))
copy(ids, v.Bricks)
return ids
}
func VolumeEntryUpgrade(tx *bolt.Tx) error {
return nil
}
func (v *VolumeEntry) BlockVolumeAdd(id string) {
v.Info.BlockInfo.BlockVolumes = append(v.Info.BlockInfo.BlockVolumes, id)
v.Info.BlockInfo.BlockVolumes.Sort()
}
func (v *VolumeEntry) BlockVolumeDelete(id string) {
v.Info.BlockInfo.BlockVolumes = sortedstrings.Delete(v.Info.BlockInfo.BlockVolumes, id)
}
// Visible returns true if this volume is meant to be visible to
// API calls.
func (v *VolumeEntry) Visible() bool {
return v.Pending.Id == ""
}
func volumeNameExistsInCluster(tx *bolt.Tx, cluster *ClusterEntry,
name string) (found bool, e error) {
for _, volumeId := range cluster.Info.Volumes {
volume, err := NewVolumeEntryFromId(tx, volumeId)
if err != nil {
return false, err
}
if name == volume.Info.Name {
found = true
return
}
}
return
}
type clusterReq struct {
allowBlock bool
allowName string
allowCreate bool
}
func eligibleClusters(db wdb.RODB, req clusterReq,
possibleClusters []string) ([]string, error) {
//
// If the request carries the Block flag, consider only
// those clusters that carry the Block flag if there are
// any, otherwise consider all clusters.
// If the request does *not* carry the Block flag, consider
// only those clusters that do not carry the Block flag.
//
if len(possibleClusters) == 0 {
return nil, fmt.Errorf("No clusters configured")
}
candidateClusters := []string{}
cerr := ClusterErrorMap{}
err := db.View(func(tx *bolt.Tx) error {
for _, clusterId := range possibleClusters {
c, err := NewClusterEntryFromId(tx, clusterId)
if err != nil {
return err
}
switch {
case req.allowBlock && c.Info.Block:
case !req.allowBlock && c.Info.File:
case !(c.Info.Block || c.Info.File):
// possibly bad cluster config
logger.Info("Cluster %v lacks both block and file flags",
clusterId)
cerr.Add(
c.Info.Id,
fmt.Errorf("Cluster has disabled all volume types"))
continue
default:
cerr.Add(
c.Info.Id,
fmt.Errorf("Cluster does not support requested volume type"))
continue
}
if req.allowName != "" {
found, err := volumeNameExistsInCluster(tx, c, req.allowName)
if err != nil {
return err
}
if found {
logger.LogError("Name %v already in use in cluster %v",
req.allowName, clusterId)
cerr.Add(
c.Info.Id,
fmt.Errorf("Volume name '%v' already in use", req.allowName))
continue
}
}
if req.allowCreate && c.volumeCount() >= maxVolumesPerCluster {
cerr.Add(
c.Info.Id,
fmt.Errorf("Cluster has %v volumes and limit is %v", c.volumeCount(), maxVolumesPerCluster))
continue
}
candidateClusters = append(candidateClusters, clusterId)
}
return nil
})
if err == nil && len(candidateClusters) == 0 {
logger.LogError("No clusters eligible to satisfy create volume request")
// use generic "no space" error if cluster errors is empty
err = ErrNoSpace
if len(cerr) > 0 {
err = cerr.ToError("No eligible cluster for volume")
}
}
return candidateClusters, err
}
// hosts returns a node-to-host mapping for all nodes in the
// volume's cluster. These hosts can be used as destinations
// for gluster commands.
func (v *VolumeEntry) hosts(db wdb.RODB) (nodeHosts, error) {
var hosts nodeHosts
err := db.View(func(tx *bolt.Tx) error {
vol, err := NewVolumeEntryFromId(tx, v.Info.Id)
if err != nil {
return err
}
cluster, err := NewClusterEntryFromId(tx, vol.Info.Cluster)
if err != nil {
return err
}
hosts, err = cluster.hosts(wdb.WrapTx(tx))
return err
})
return hosts, err
}
func (v *VolumeEntry) prepareVolumeClone(tx *bolt.Tx, clonename string) (
*VolumeEntry, []*BrickEntry, []*DeviceEntry, error) {
if v.Info.Block {
return nil, nil, nil, ErrCloneBlockVol
}
bricks := []*BrickEntry{}
devices := []*DeviceEntry{}
cvol := NewVolumeEntryFromClone(v, clonename)
for _, brickId := range v.Bricks {
brick, err := CloneBrickEntryFromId(tx, brickId)
if err != nil {
return nil, nil, nil, err
}
device, err := NewDeviceEntryFromId(tx, brick.Info.DeviceId)
if err != nil {
return nil, nil, nil, err
}
brick.Info.VolumeId = cvol.Info.Id
cvol.Bricks = append(cvol.Bricks, brick.Id())
bricks = append(bricks, brick)
// Add the cloned brick to the device (clones do not take extra storage space)
device.BrickAdd(brick.Id())
devices = append(devices, device)
}
return cvol, bricks, devices, nil
}