store.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. package storage
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "strings"
  6. "sync/atomic"
  7. "google.golang.org/grpc"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/pb"
  10. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  11. "github.com/chrislusf/seaweedfs/weed/stats"
  12. "github.com/chrislusf/seaweedfs/weed/storage/erasure_coding"
  13. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  14. "github.com/chrislusf/seaweedfs/weed/storage/super_block"
  15. . "github.com/chrislusf/seaweedfs/weed/storage/types"
  16. "github.com/chrislusf/seaweedfs/weed/util"
  17. )
  18. const (
  19. MAX_TTL_VOLUME_REMOVAL_DELAY = 10 // 10 minutes
  20. )
  21. /*
  22. * A VolumeServer contains one Store
  23. */
  24. type Store struct {
  25. MasterAddress string
  26. grpcDialOption grpc.DialOption
  27. volumeSizeLimit uint64 // read from the master
  28. Ip string
  29. Port int
  30. PublicUrl string
  31. Locations []*DiskLocation
  32. dataCenter string // optional informaton, overwriting master setting if exists
  33. rack string // optional information, overwriting master setting if exists
  34. connected bool
  35. NeedleMapType NeedleMapType
  36. NewVolumesChan chan master_pb.VolumeShortInformationMessage
  37. DeletedVolumesChan chan master_pb.VolumeShortInformationMessage
  38. NewEcShardsChan chan master_pb.VolumeEcShardInformationMessage
  39. DeletedEcShardsChan chan master_pb.VolumeEcShardInformationMessage
  40. }
  41. func (s *Store) String() (str string) {
  42. str = fmt.Sprintf("Ip:%s, Port:%d, PublicUrl:%s, dataCenter:%s, rack:%s, connected:%v, volumeSizeLimit:%d", s.Ip, s.Port, s.PublicUrl, s.dataCenter, s.rack, s.connected, s.GetVolumeSizeLimit())
  43. return
  44. }
  45. func NewStore(grpcDialOption grpc.DialOption, port int, ip, publicUrl string, dirnames []string, maxVolumeCounts []int, minFreeSpacePercents []float32, needleMapKind NeedleMapType) (s *Store) {
  46. s = &Store{grpcDialOption: grpcDialOption, Port: port, Ip: ip, PublicUrl: publicUrl, NeedleMapType: needleMapKind}
  47. s.Locations = make([]*DiskLocation, 0)
  48. for i := 0; i < len(dirnames); i++ {
  49. location := NewDiskLocation(util.ResolvePath(dirnames[i]), maxVolumeCounts[i], minFreeSpacePercents[i])
  50. location.loadExistingVolumes(needleMapKind)
  51. s.Locations = append(s.Locations, location)
  52. stats.VolumeServerMaxVolumeCounter.Add(float64(maxVolumeCounts[i]))
  53. }
  54. s.NewVolumesChan = make(chan master_pb.VolumeShortInformationMessage, 3)
  55. s.DeletedVolumesChan = make(chan master_pb.VolumeShortInformationMessage, 3)
  56. s.NewEcShardsChan = make(chan master_pb.VolumeEcShardInformationMessage, 3)
  57. s.DeletedEcShardsChan = make(chan master_pb.VolumeEcShardInformationMessage, 3)
  58. return
  59. }
  60. func (s *Store) AddVolume(volumeId needle.VolumeId, collection string, needleMapKind NeedleMapType, replicaPlacement string, ttlString string, preallocate int64, MemoryMapMaxSizeMb uint32) error {
  61. rt, e := super_block.NewReplicaPlacementFromString(replicaPlacement)
  62. if e != nil {
  63. return e
  64. }
  65. ttl, e := needle.ReadTTL(ttlString)
  66. if e != nil {
  67. return e
  68. }
  69. e = s.addVolume(volumeId, collection, needleMapKind, rt, ttl, preallocate, MemoryMapMaxSizeMb)
  70. return e
  71. }
  72. func (s *Store) DeleteCollection(collection string) (e error) {
  73. for _, location := range s.Locations {
  74. e = location.DeleteCollectionFromDiskLocation(collection)
  75. if e != nil {
  76. return
  77. }
  78. // let the heartbeat send the list of volumes, instead of sending the deleted volume ids to DeletedVolumesChan
  79. }
  80. return
  81. }
  82. func (s *Store) findVolume(vid needle.VolumeId) *Volume {
  83. for _, location := range s.Locations {
  84. if v, found := location.FindVolume(vid); found {
  85. return v
  86. }
  87. }
  88. return nil
  89. }
  90. func (s *Store) FindFreeLocation() (ret *DiskLocation) {
  91. max := 0
  92. for _, location := range s.Locations {
  93. currentFreeCount := location.MaxVolumeCount - location.VolumesLen()
  94. currentFreeCount *= erasure_coding.DataShardsCount
  95. currentFreeCount -= location.EcVolumesLen()
  96. currentFreeCount /= erasure_coding.DataShardsCount
  97. if currentFreeCount > max {
  98. max = currentFreeCount
  99. ret = location
  100. }
  101. }
  102. return ret
  103. }
  104. func (s *Store) addVolume(vid needle.VolumeId, collection string, needleMapKind NeedleMapType, replicaPlacement *super_block.ReplicaPlacement, ttl *needle.TTL, preallocate int64, memoryMapMaxSizeMb uint32) error {
  105. if s.findVolume(vid) != nil {
  106. return fmt.Errorf("Volume Id %d already exists!", vid)
  107. }
  108. if location := s.FindFreeLocation(); location != nil {
  109. glog.V(0).Infof("In dir %s adds volume:%v collection:%s replicaPlacement:%v ttl:%v",
  110. location.Directory, vid, collection, replicaPlacement, ttl)
  111. if volume, err := NewVolume(location.Directory, collection, vid, needleMapKind, replicaPlacement, ttl, preallocate, memoryMapMaxSizeMb); err == nil {
  112. location.SetVolume(vid, volume)
  113. glog.V(0).Infof("add volume %d", vid)
  114. s.NewVolumesChan <- master_pb.VolumeShortInformationMessage{
  115. Id: uint32(vid),
  116. Collection: collection,
  117. ReplicaPlacement: uint32(replicaPlacement.Byte()),
  118. Version: uint32(volume.Version()),
  119. Ttl: ttl.ToUint32(),
  120. }
  121. return nil
  122. } else {
  123. return err
  124. }
  125. }
  126. return fmt.Errorf("No more free space left")
  127. }
  128. func (s *Store) VolumeInfos() (allStats []*VolumeInfo) {
  129. for _, location := range s.Locations {
  130. stats := collectStatsForOneLocation(location)
  131. allStats = append(allStats, stats...)
  132. }
  133. sortVolumeInfos(allStats)
  134. return allStats
  135. }
  136. func collectStatsForOneLocation(location *DiskLocation) (stats []*VolumeInfo) {
  137. location.volumesLock.RLock()
  138. defer location.volumesLock.RUnlock()
  139. for k, v := range location.volumes {
  140. s := collectStatForOneVolume(k, v)
  141. stats = append(stats, s)
  142. }
  143. return stats
  144. }
  145. func collectStatForOneVolume(vid needle.VolumeId, v *Volume) (s *VolumeInfo) {
  146. s = &VolumeInfo{
  147. Id: vid,
  148. Collection: v.Collection,
  149. ReplicaPlacement: v.ReplicaPlacement,
  150. Version: v.Version(),
  151. ReadOnly: v.IsReadOnly(),
  152. Ttl: v.Ttl,
  153. CompactRevision: uint32(v.CompactionRevision),
  154. }
  155. s.RemoteStorageName, s.RemoteStorageKey = v.RemoteStorageNameKey()
  156. v.dataFileAccessLock.RLock()
  157. defer v.dataFileAccessLock.RUnlock()
  158. if v.nm == nil {
  159. return
  160. }
  161. s.FileCount = v.nm.FileCount()
  162. s.DeleteCount = v.nm.DeletedCount()
  163. s.DeletedByteCount = v.nm.DeletedSize()
  164. s.Size = v.nm.ContentSize()
  165. return
  166. }
  167. func (s *Store) SetDataCenter(dataCenter string) {
  168. s.dataCenter = dataCenter
  169. }
  170. func (s *Store) SetRack(rack string) {
  171. s.rack = rack
  172. }
  173. func (s *Store) CollectHeartbeat() *master_pb.Heartbeat {
  174. var volumeMessages []*master_pb.VolumeInformationMessage
  175. maxVolumeCount := 0
  176. var maxFileKey NeedleId
  177. collectionVolumeSize := make(map[string]uint64)
  178. for _, location := range s.Locations {
  179. var deleteVids []needle.VolumeId
  180. maxVolumeCount = maxVolumeCount + location.MaxVolumeCount
  181. location.volumesLock.RLock()
  182. for _, v := range location.volumes {
  183. if maxFileKey < v.MaxFileKey() {
  184. maxFileKey = v.MaxFileKey()
  185. }
  186. if !v.expired(s.GetVolumeSizeLimit()) {
  187. volumeMessages = append(volumeMessages, v.ToVolumeInformationMessage())
  188. } else {
  189. if v.expiredLongEnough(MAX_TTL_VOLUME_REMOVAL_DELAY) {
  190. deleteVids = append(deleteVids, v.Id)
  191. } else {
  192. glog.V(0).Infoln("volume", v.Id, "is expired.")
  193. }
  194. }
  195. fileSize, _, _ := v.FileStat()
  196. collectionVolumeSize[v.Collection] += fileSize
  197. }
  198. location.volumesLock.RUnlock()
  199. if len(deleteVids) > 0 {
  200. // delete expired volumes.
  201. location.volumesLock.Lock()
  202. for _, vid := range deleteVids {
  203. found, err := location.deleteVolumeById(vid)
  204. if found {
  205. if err == nil {
  206. glog.V(0).Infof("volume %d is deleted", vid)
  207. } else {
  208. glog.V(0).Infof("delete volume %d: %v", vid, err)
  209. }
  210. }
  211. }
  212. location.volumesLock.Unlock()
  213. }
  214. }
  215. for col, size := range collectionVolumeSize {
  216. stats.VolumeServerDiskSizeGauge.WithLabelValues(col, "normal").Set(float64(size))
  217. }
  218. return &master_pb.Heartbeat{
  219. Ip: s.Ip,
  220. Port: uint32(s.Port),
  221. PublicUrl: s.PublicUrl,
  222. MaxVolumeCount: uint32(maxVolumeCount),
  223. MaxFileKey: NeedleIdToUint64(maxFileKey),
  224. DataCenter: s.dataCenter,
  225. Rack: s.rack,
  226. Volumes: volumeMessages,
  227. HasNoVolumes: len(volumeMessages) == 0,
  228. }
  229. }
  230. func (s *Store) Close() {
  231. for _, location := range s.Locations {
  232. location.Close()
  233. }
  234. }
  235. func (s *Store) WriteVolumeNeedle(i needle.VolumeId, n *needle.Needle, fsync bool) (isUnchanged bool, err error) {
  236. if v := s.findVolume(i); v != nil {
  237. if v.IsReadOnly() {
  238. err = fmt.Errorf("volume %d is read only", i)
  239. return
  240. }
  241. _, _, isUnchanged, err = v.writeNeedle2(n, fsync)
  242. return
  243. }
  244. glog.V(0).Infoln("volume", i, "not found!")
  245. err = fmt.Errorf("volume %d not found on %s:%d", i, s.Ip, s.Port)
  246. return
  247. }
  248. func (s *Store) DeleteVolumeNeedle(i needle.VolumeId, n *needle.Needle) (uint32, error) {
  249. if v := s.findVolume(i); v != nil {
  250. if v.noWriteOrDelete {
  251. return 0, fmt.Errorf("volume %d is read only", i)
  252. }
  253. return v.deleteNeedle2(n)
  254. }
  255. return 0, fmt.Errorf("volume %d not found on %s:%d", i, s.Ip, s.Port)
  256. }
  257. func (s *Store) ReadVolumeNeedle(i needle.VolumeId, n *needle.Needle) (int, error) {
  258. if v := s.findVolume(i); v != nil {
  259. return v.readNeedle(n)
  260. }
  261. return 0, fmt.Errorf("volume %d not found", i)
  262. }
  263. func (s *Store) GetVolume(i needle.VolumeId) *Volume {
  264. return s.findVolume(i)
  265. }
  266. func (s *Store) HasVolume(i needle.VolumeId) bool {
  267. v := s.findVolume(i)
  268. return v != nil
  269. }
  270. func (s *Store) MarkVolumeReadonly(i needle.VolumeId) error {
  271. v := s.findVolume(i)
  272. if v == nil {
  273. return fmt.Errorf("volume %d not found", i)
  274. }
  275. v.noWriteOrDelete = true
  276. return nil
  277. }
  278. func (s *Store) MountVolume(i needle.VolumeId) error {
  279. for _, location := range s.Locations {
  280. if found := location.LoadVolume(i, s.NeedleMapType); found == true {
  281. glog.V(0).Infof("mount volume %d", i)
  282. v := s.findVolume(i)
  283. s.NewVolumesChan <- master_pb.VolumeShortInformationMessage{
  284. Id: uint32(v.Id),
  285. Collection: v.Collection,
  286. ReplicaPlacement: uint32(v.ReplicaPlacement.Byte()),
  287. Version: uint32(v.Version()),
  288. Ttl: v.Ttl.ToUint32(),
  289. }
  290. return nil
  291. }
  292. }
  293. return fmt.Errorf("volume %d not found on disk", i)
  294. }
  295. func (s *Store) UnmountVolume(i needle.VolumeId) error {
  296. v := s.findVolume(i)
  297. if v == nil {
  298. return nil
  299. }
  300. message := master_pb.VolumeShortInformationMessage{
  301. Id: uint32(v.Id),
  302. Collection: v.Collection,
  303. ReplicaPlacement: uint32(v.ReplicaPlacement.Byte()),
  304. Version: uint32(v.Version()),
  305. Ttl: v.Ttl.ToUint32(),
  306. }
  307. for _, location := range s.Locations {
  308. if err := location.UnloadVolume(i); err == nil {
  309. glog.V(0).Infof("UnmountVolume %d", i)
  310. s.DeletedVolumesChan <- message
  311. return nil
  312. }
  313. }
  314. return fmt.Errorf("volume %d not found on disk", i)
  315. }
  316. func (s *Store) DeleteVolume(i needle.VolumeId) error {
  317. v := s.findVolume(i)
  318. if v == nil {
  319. return fmt.Errorf("delete volume %d not found on disk", i)
  320. }
  321. message := master_pb.VolumeShortInformationMessage{
  322. Id: uint32(v.Id),
  323. Collection: v.Collection,
  324. ReplicaPlacement: uint32(v.ReplicaPlacement.Byte()),
  325. Version: uint32(v.Version()),
  326. Ttl: v.Ttl.ToUint32(),
  327. }
  328. for _, location := range s.Locations {
  329. if found, error := location.deleteVolumeById(i); found && error == nil {
  330. glog.V(0).Infof("DeleteVolume %d", i)
  331. s.DeletedVolumesChan <- message
  332. return nil
  333. }
  334. }
  335. return fmt.Errorf("volume %d not found on disk", i)
  336. }
  337. func (s *Store) ConfigureVolume(i needle.VolumeId, replication string) error {
  338. for _, location := range s.Locations {
  339. fileInfo, found := location.LocateVolume(i)
  340. if !found {
  341. continue
  342. }
  343. // load, modify, save
  344. baseFileName := strings.TrimSuffix(fileInfo.Name(), filepath.Ext(fileInfo.Name()))
  345. vifFile := filepath.Join(location.Directory, baseFileName+".vif")
  346. volumeInfo, _, err := pb.MaybeLoadVolumeInfo(vifFile)
  347. if err != nil {
  348. return fmt.Errorf("volume %d fail to load vif", i)
  349. }
  350. volumeInfo.Replication = replication
  351. err = pb.SaveVolumeInfo(vifFile, volumeInfo)
  352. if err != nil {
  353. return fmt.Errorf("volume %d fail to save vif", i)
  354. }
  355. return nil
  356. }
  357. return fmt.Errorf("volume %d not found on disk", i)
  358. }
  359. func (s *Store) SetVolumeSizeLimit(x uint64) {
  360. atomic.StoreUint64(&s.volumeSizeLimit, x)
  361. }
  362. func (s *Store) GetVolumeSizeLimit() uint64 {
  363. return atomic.LoadUint64(&s.volumeSizeLimit)
  364. }
  365. func (s *Store) MaybeAdjustVolumeMax() (hasChanges bool) {
  366. volumeSizeLimit := s.GetVolumeSizeLimit()
  367. for _, diskLocation := range s.Locations {
  368. if diskLocation.MaxVolumeCount == 0 {
  369. diskStatus := stats.NewDiskStatus(diskLocation.Directory)
  370. unusedSpace := diskLocation.UnUsedSpace(volumeSizeLimit)
  371. unclaimedSpaces := int64(diskStatus.Free) - int64(unusedSpace)
  372. volCount := diskLocation.VolumesLen()
  373. maxVolumeCount := volCount
  374. if unclaimedSpaces > int64(volumeSizeLimit) {
  375. maxVolumeCount += int(uint64(unclaimedSpaces)/volumeSizeLimit) - 1
  376. }
  377. diskLocation.MaxVolumeCount = maxVolumeCount
  378. glog.V(0).Infof("disk %s max %d unclaimedSpace:%dMB, unused:%dMB volumeSizeLimit:%dMB",
  379. diskLocation.Directory, maxVolumeCount, unclaimedSpaces/1024/1024, unusedSpace/1024/1024, volumeSizeLimit/1024/1024)
  380. hasChanges = true
  381. }
  382. }
  383. return
  384. }