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.

297 lines
8.4 KiB

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