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.

279 lines
8.0 KiB

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