-
Notifications
You must be signed in to change notification settings - Fork 1
/
metaserver_assign.go
46 lines (43 loc) · 1.23 KB
/
metaserver_assign.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
package metaserver
import (
"errors"
"sort"
"github.com/rfeverything/rfs/internal/constant"
"github.com/rfeverything/rfs/internal/logger"
"github.com/rfeverything/rfs/internal/proto/rfs"
vpb "github.com/rfeverything/rfs/internal/proto/volume_server"
"go.uber.org/zap"
"golang.org/x/exp/slices"
)
func (ms *MetaServer) assignVolumeForChunks(volumes []*vpb.VolumeStatus, chunks []*rfs.FileChunk) error {
logger.Global().Debug("assignVolumeForChunks", zap.Any("volumes", volumes), zap.Any("chunks", chunks))
for _, chunk := range chunks {
sort.Slice(volumes, func(i, j int) bool {
return volumes[i].Free < volumes[j].Free
})
if chunk.VolumeIds == nil {
chunk.VolumeIds = make([]string, 0)
}
isassigned := false
replicationCount := constant.Replication
for _, volume := range volumes {
if slices.Contains(chunk.VolumeIds, volume.VolumeId) {
continue
}
if volume.Free >= chunk.Size {
chunk.VolumeIds = append(chunk.VolumeIds, volume.VolumeId)
volume.Free -= chunk.Size
replicationCount--
if replicationCount == 0 {
//TODO: volumeservers less than replication count
isassigned = true
}
break
}
}
if !isassigned {
return errors.New("no enough space")
}
}
return nil
}