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.

342 lines
10 KiB

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