master_grpc_server_collection.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package weed_server
  2. import (
  3. "context"
  4. "github.com/chrislusf/raft"
  5. "github.com/chrislusf/seaweedfs/weed/operation"
  6. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  7. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  8. )
  9. func (ms *MasterServer) CollectionList(ctx context.Context, req *master_pb.CollectionListRequest) (*master_pb.CollectionListResponse, error) {
  10. if !ms.Topo.IsLeader() {
  11. return nil, raft.NotLeaderError
  12. }
  13. resp := &master_pb.CollectionListResponse{}
  14. collections := ms.Topo.ListCollections(req.IncludeNormalVolumes, req.IncludeEcVolumes)
  15. for _, c := range collections {
  16. resp.Collections = append(resp.Collections, &master_pb.Collection{
  17. Name: c,
  18. })
  19. }
  20. return resp, nil
  21. }
  22. func (ms *MasterServer) CollectionDelete(ctx context.Context, req *master_pb.CollectionDeleteRequest) (*master_pb.CollectionDeleteResponse, error) {
  23. if !ms.Topo.IsLeader() {
  24. return nil, raft.NotLeaderError
  25. }
  26. resp := &master_pb.CollectionDeleteResponse{}
  27. err := ms.doDeleteNormalCollection(req.Name)
  28. if err != nil {
  29. return nil, err
  30. }
  31. err = ms.doDeleteEcCollection(req.Name)
  32. if err != nil {
  33. return nil, err
  34. }
  35. return resp, nil
  36. }
  37. func (ms *MasterServer) doDeleteNormalCollection(collectionName string) error {
  38. collection, ok := ms.Topo.FindCollection(collectionName)
  39. if !ok {
  40. return nil
  41. }
  42. for _, server := range collection.ListVolumeServers() {
  43. err := operation.WithVolumeServerClient(false, server.ServerAddress(), ms.grpcDialOption, func(client volume_server_pb.VolumeServerClient) error {
  44. _, deleteErr := client.DeleteCollection(context.Background(), &volume_server_pb.DeleteCollectionRequest{
  45. Collection: collectionName,
  46. })
  47. return deleteErr
  48. })
  49. if err != nil {
  50. return err
  51. }
  52. }
  53. ms.Topo.DeleteCollection(collectionName)
  54. return nil
  55. }
  56. func (ms *MasterServer) doDeleteEcCollection(collectionName string) error {
  57. listOfEcServers := ms.Topo.ListEcServersByCollection(collectionName)
  58. for _, server := range listOfEcServers {
  59. err := operation.WithVolumeServerClient(false, server, ms.grpcDialOption, func(client volume_server_pb.VolumeServerClient) error {
  60. _, deleteErr := client.DeleteCollection(context.Background(), &volume_server_pb.DeleteCollectionRequest{
  61. Collection: collectionName,
  62. })
  63. return deleteErr
  64. })
  65. if err != nil {
  66. return err
  67. }
  68. }
  69. ms.Topo.DeleteEcCollection(collectionName)
  70. return nil
  71. }