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.

250 lines
6.8 KiB

6 years ago
6 years ago
6 years ago
6 years ago
  1. package weed_server
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/storage"
  6. "path/filepath"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  9. "github.com/chrislusf/seaweedfs/weed/stats"
  10. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  11. "github.com/chrislusf/seaweedfs/weed/storage/super_block"
  12. "github.com/chrislusf/seaweedfs/weed/storage/types"
  13. )
  14. func (vs *VolumeServer) DeleteCollection(ctx context.Context, req *volume_server_pb.DeleteCollectionRequest) (*volume_server_pb.DeleteCollectionResponse, error) {
  15. resp := &volume_server_pb.DeleteCollectionResponse{}
  16. err := vs.store.DeleteCollection(req.Collection)
  17. if err != nil {
  18. glog.Errorf("delete collection %s: %v", req.Collection, err)
  19. } else {
  20. glog.V(2).Infof("delete collection %v", req)
  21. }
  22. return resp, err
  23. }
  24. func (vs *VolumeServer) AllocateVolume(ctx context.Context, req *volume_server_pb.AllocateVolumeRequest) (*volume_server_pb.AllocateVolumeResponse, error) {
  25. resp := &volume_server_pb.AllocateVolumeResponse{}
  26. err := vs.store.AddVolume(
  27. needle.VolumeId(req.VolumeId),
  28. req.Collection,
  29. vs.needleMapKind,
  30. req.Replication,
  31. req.Ttl,
  32. req.Preallocate,
  33. req.MemoryMapMaxSizeMb,
  34. storage.DiskType(req.DiskType),
  35. )
  36. if err != nil {
  37. glog.Errorf("assign volume %v: %v", req, err)
  38. } else {
  39. glog.V(2).Infof("assign volume %v", req)
  40. }
  41. return resp, err
  42. }
  43. func (vs *VolumeServer) VolumeMount(ctx context.Context, req *volume_server_pb.VolumeMountRequest) (*volume_server_pb.VolumeMountResponse, error) {
  44. resp := &volume_server_pb.VolumeMountResponse{}
  45. err := vs.store.MountVolume(needle.VolumeId(req.VolumeId))
  46. if err != nil {
  47. glog.Errorf("volume mount %v: %v", req, err)
  48. } else {
  49. glog.V(2).Infof("volume mount %v", req)
  50. }
  51. return resp, err
  52. }
  53. func (vs *VolumeServer) VolumeUnmount(ctx context.Context, req *volume_server_pb.VolumeUnmountRequest) (*volume_server_pb.VolumeUnmountResponse, error) {
  54. resp := &volume_server_pb.VolumeUnmountResponse{}
  55. err := vs.store.UnmountVolume(needle.VolumeId(req.VolumeId))
  56. if err != nil {
  57. glog.Errorf("volume unmount %v: %v", req, err)
  58. } else {
  59. glog.V(2).Infof("volume unmount %v", req)
  60. }
  61. return resp, err
  62. }
  63. func (vs *VolumeServer) VolumeDelete(ctx context.Context, req *volume_server_pb.VolumeDeleteRequest) (*volume_server_pb.VolumeDeleteResponse, error) {
  64. resp := &volume_server_pb.VolumeDeleteResponse{}
  65. err := vs.store.DeleteVolume(needle.VolumeId(req.VolumeId))
  66. if err != nil {
  67. glog.Errorf("volume delete %v: %v", req, err)
  68. } else {
  69. glog.V(2).Infof("volume delete %v", req)
  70. }
  71. return resp, err
  72. }
  73. func (vs *VolumeServer) VolumeConfigure(ctx context.Context, req *volume_server_pb.VolumeConfigureRequest) (*volume_server_pb.VolumeConfigureResponse, error) {
  74. resp := &volume_server_pb.VolumeConfigureResponse{}
  75. // check replication format
  76. if _, err := super_block.NewReplicaPlacementFromString(req.Replication); err != nil {
  77. resp.Error = fmt.Sprintf("volume configure replication %v: %v", req, err)
  78. return resp, nil
  79. }
  80. // unmount
  81. if err := vs.store.UnmountVolume(needle.VolumeId(req.VolumeId)); err != nil {
  82. glog.Errorf("volume configure unmount %v: %v", req, err)
  83. resp.Error = fmt.Sprintf("volume configure unmount %v: %v", req, err)
  84. return resp, nil
  85. }
  86. // modify the volume info file
  87. if err := vs.store.ConfigureVolume(needle.VolumeId(req.VolumeId), req.Replication); err != nil {
  88. glog.Errorf("volume configure %v: %v", req, err)
  89. resp.Error = fmt.Sprintf("volume configure %v: %v", req, err)
  90. return resp, nil
  91. }
  92. // mount
  93. if err := vs.store.MountVolume(needle.VolumeId(req.VolumeId)); err != nil {
  94. glog.Errorf("volume configure mount %v: %v", req, err)
  95. resp.Error = fmt.Sprintf("volume configure mount %v: %v", req, err)
  96. return resp, nil
  97. }
  98. return resp, nil
  99. }
  100. func (vs *VolumeServer) VolumeMarkReadonly(ctx context.Context, req *volume_server_pb.VolumeMarkReadonlyRequest) (*volume_server_pb.VolumeMarkReadonlyResponse, error) {
  101. resp := &volume_server_pb.VolumeMarkReadonlyResponse{}
  102. err := vs.store.MarkVolumeReadonly(needle.VolumeId(req.VolumeId))
  103. if err != nil {
  104. glog.Errorf("volume mark readonly %v: %v", req, err)
  105. } else {
  106. glog.V(2).Infof("volume mark readonly %v", req)
  107. }
  108. return resp, err
  109. }
  110. func (vs *VolumeServer) VolumeMarkWritable(ctx context.Context, req *volume_server_pb.VolumeMarkWritableRequest) (*volume_server_pb.VolumeMarkWritableResponse, error) {
  111. resp := &volume_server_pb.VolumeMarkWritableResponse{}
  112. err := vs.store.MarkVolumeWritable(needle.VolumeId(req.VolumeId))
  113. if err != nil {
  114. glog.Errorf("volume mark writable %v: %v", req, err)
  115. } else {
  116. glog.V(2).Infof("volume mark writable %v", req)
  117. }
  118. return resp, err
  119. }
  120. func (vs *VolumeServer) VolumeStatus(ctx context.Context, req *volume_server_pb.VolumeStatusRequest) (*volume_server_pb.VolumeStatusResponse, error) {
  121. resp := &volume_server_pb.VolumeStatusResponse{}
  122. v := vs.store.GetVolume(needle.VolumeId(req.VolumeId))
  123. if v == nil {
  124. return nil, fmt.Errorf("not found volume id %d", req.VolumeId)
  125. }
  126. resp.IsReadOnly = v.IsReadOnly()
  127. return resp, nil
  128. }
  129. func (vs *VolumeServer) VolumeServerStatus(ctx context.Context, req *volume_server_pb.VolumeServerStatusRequest) (*volume_server_pb.VolumeServerStatusResponse, error) {
  130. resp := &volume_server_pb.VolumeServerStatusResponse{}
  131. for _, loc := range vs.store.Locations {
  132. if dir, e := filepath.Abs(loc.Directory); e == nil {
  133. resp.DiskStatuses = append(resp.DiskStatuses, stats.NewDiskStatus(dir))
  134. }
  135. }
  136. resp.MemoryStatus = stats.MemStat()
  137. return resp, nil
  138. }
  139. func (vs *VolumeServer) VolumeServerLeave(ctx context.Context, req *volume_server_pb.VolumeServerLeaveRequest) (*volume_server_pb.VolumeServerLeaveResponse, error) {
  140. resp := &volume_server_pb.VolumeServerLeaveResponse{}
  141. vs.StopHeartbeat()
  142. return resp, nil
  143. }
  144. func (vs *VolumeServer) VolumeNeedleStatus(ctx context.Context, req *volume_server_pb.VolumeNeedleStatusRequest) (*volume_server_pb.VolumeNeedleStatusResponse, error) {
  145. resp := &volume_server_pb.VolumeNeedleStatusResponse{}
  146. volumeId := needle.VolumeId(req.VolumeId)
  147. n := &needle.Needle{
  148. Id: types.NeedleId(req.NeedleId),
  149. }
  150. var count int
  151. var err error
  152. hasVolume := vs.store.HasVolume(volumeId)
  153. if !hasVolume {
  154. _, hasEcVolume := vs.store.FindEcVolume(volumeId)
  155. if !hasEcVolume {
  156. return nil, fmt.Errorf("volume not found %d", req.VolumeId)
  157. }
  158. count, err = vs.store.ReadEcShardNeedle(volumeId, n)
  159. } else {
  160. count, err = vs.store.ReadVolumeNeedle(volumeId, n, nil)
  161. }
  162. if err != nil {
  163. return nil, err
  164. }
  165. if count < 0 {
  166. return nil, fmt.Errorf("needle not found %d", n.Id)
  167. }
  168. resp.NeedleId = uint64(n.Id)
  169. resp.Cookie = uint32(n.Cookie)
  170. resp.Size = uint32(n.Size)
  171. resp.LastModified = n.LastModified
  172. resp.Crc = n.Checksum.Value()
  173. if n.HasTtl() {
  174. resp.Ttl = n.Ttl.String()
  175. }
  176. return resp, nil
  177. }