You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

355 lines
11 KiB

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