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.

314 lines
9.3 KiB

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