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.

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