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.

327 lines
9.9 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
  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: v.nm.FileCount(),
  131. DeleteCount: v.nm.DeletedCount(),
  132. DeletedByteCount: v.nm.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.nm.MaxFileKey() {
  160. maxFileKey = v.nm.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) Write(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. // TODO: count needle size ahead
  204. if MaxPossibleVolumeSize >= v.ContentSize()+uint64(size) {
  205. _, size, isUnchanged, err = v.writeNeedle(n)
  206. } else {
  207. err = fmt.Errorf("Volume Size Limit %d Exceeded! Current size is %d", s.GetVolumeSizeLimit(), v.ContentSize())
  208. }
  209. return
  210. }
  211. glog.V(0).Infoln("volume", i, "not found!")
  212. err = fmt.Errorf("volume %d not found on %s:%d", i, s.Ip, s.Port)
  213. return
  214. }
  215. func (s *Store) Delete(i needle.VolumeId, n *needle.Needle) (uint32, error) {
  216. if v := s.findVolume(i); v != nil && !v.readOnly {
  217. return v.deleteNeedle(n)
  218. }
  219. return 0, nil
  220. }
  221. func (s *Store) ReadVolumeNeedle(i needle.VolumeId, n *needle.Needle) (int, error) {
  222. if v := s.findVolume(i); v != nil {
  223. return v.readNeedle(n)
  224. }
  225. return 0, fmt.Errorf("volume %d not found", i)
  226. }
  227. func (s *Store) GetVolume(i needle.VolumeId) *Volume {
  228. return s.findVolume(i)
  229. }
  230. func (s *Store) HasVolume(i needle.VolumeId) bool {
  231. v := s.findVolume(i)
  232. return v != nil
  233. }
  234. func (s *Store) MountVolume(i needle.VolumeId) error {
  235. for _, location := range s.Locations {
  236. if found := location.LoadVolume(i, s.NeedleMapType); found == true {
  237. glog.V(0).Infof("mount volume %d", i)
  238. v := s.findVolume(i)
  239. s.NewVolumesChan <- master_pb.VolumeShortInformationMessage{
  240. Id: uint32(v.Id),
  241. Collection: v.Collection,
  242. ReplicaPlacement: uint32(v.ReplicaPlacement.Byte()),
  243. Version: uint32(v.Version()),
  244. Ttl: v.Ttl.ToUint32(),
  245. }
  246. return nil
  247. }
  248. }
  249. return fmt.Errorf("volume %d not found on disk", i)
  250. }
  251. func (s *Store) UnmountVolume(i needle.VolumeId) error {
  252. v := s.findVolume(i)
  253. if v == nil {
  254. return nil
  255. }
  256. message := master_pb.VolumeShortInformationMessage{
  257. Id: uint32(v.Id),
  258. Collection: v.Collection,
  259. ReplicaPlacement: uint32(v.ReplicaPlacement.Byte()),
  260. Version: uint32(v.Version()),
  261. Ttl: v.Ttl.ToUint32(),
  262. }
  263. for _, location := range s.Locations {
  264. if err := location.UnloadVolume(i); err == nil {
  265. glog.V(0).Infof("UnmountVolume %d", i)
  266. s.DeletedVolumesChan <- message
  267. return nil
  268. }
  269. }
  270. return fmt.Errorf("volume %d not found on disk", i)
  271. }
  272. func (s *Store) DeleteVolume(i needle.VolumeId) error {
  273. v := s.findVolume(i)
  274. if v == nil {
  275. return nil
  276. }
  277. message := master_pb.VolumeShortInformationMessage{
  278. Id: uint32(v.Id),
  279. Collection: v.Collection,
  280. ReplicaPlacement: uint32(v.ReplicaPlacement.Byte()),
  281. Version: uint32(v.Version()),
  282. Ttl: v.Ttl.ToUint32(),
  283. }
  284. for _, location := range s.Locations {
  285. if error := location.deleteVolumeById(i); error == nil {
  286. glog.V(0).Infof("DeleteVolume %d", i)
  287. s.DeletedVolumesChan <- message
  288. return nil
  289. }
  290. }
  291. return fmt.Errorf("volume %d not found on disk", i)
  292. }
  293. func (s *Store) SetVolumeSizeLimit(x uint64) {
  294. atomic.StoreUint64(&s.volumeSizeLimit, x)
  295. }
  296. func (s *Store) GetVolumeSizeLimit() uint64 {
  297. return atomic.LoadUint64(&s.volumeSizeLimit)
  298. }