2018-10-15 15:03:55 +08:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-02-03 07:37:23 +08:00
|
|
|
"fmt"
|
2022-08-24 14:18:21 +08:00
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
2022-11-14 16:19:27 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage"
|
|
|
|
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/cluster"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
2018-10-15 15:03:55 +08:00
|
|
|
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/stats"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/super_block"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/types"
|
2018-10-15 15:03:55 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func (vs *VolumeServer) DeleteCollection(ctx context.Context, req *volume_server_pb.DeleteCollectionRequest) (*volume_server_pb.DeleteCollectionResponse, error) {
|
|
|
|
|
|
|
|
resp := &volume_server_pb.DeleteCollectionResponse{}
|
|
|
|
|
|
|
|
err := vs.store.DeleteCollection(req.Collection)
|
|
|
|
|
|
|
|
if err != nil {
|
2018-10-15 15:40:46 +08:00
|
|
|
glog.Errorf("delete collection %s: %v", req.Collection, err)
|
|
|
|
} else {
|
|
|
|
glog.V(2).Infof("delete collection %v", req)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-04-11 12:41:17 +08:00
|
|
|
func (vs *VolumeServer) AllocateVolume(ctx context.Context, req *volume_server_pb.AllocateVolumeRequest) (*volume_server_pb.AllocateVolumeResponse, error) {
|
2018-10-15 15:40:46 +08:00
|
|
|
|
2019-04-11 12:41:17 +08:00
|
|
|
resp := &volume_server_pb.AllocateVolumeResponse{}
|
2018-10-15 15:40:46 +08:00
|
|
|
|
|
|
|
err := vs.store.AddVolume(
|
2019-04-19 12:43:36 +08:00
|
|
|
needle.VolumeId(req.VolumeId),
|
2018-10-15 15:40:46 +08:00
|
|
|
req.Collection,
|
|
|
|
vs.needleMapKind,
|
|
|
|
req.Replication,
|
|
|
|
req.Ttl,
|
|
|
|
req.Preallocate,
|
2019-10-22 13:57:01 +08:00
|
|
|
req.MemoryMapMaxSizeMb,
|
2021-02-16 19:03:00 +08:00
|
|
|
types.ToDiskType(req.DiskType),
|
2022-11-14 16:19:27 +08:00
|
|
|
vs.ldbTimout,
|
2018-10-15 15:40:46 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("assign volume %v: %v", req, err)
|
|
|
|
} else {
|
|
|
|
glog.V(2).Infof("assign volume %v", req)
|
2018-10-15 15:03:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp, err
|
|
|
|
|
|
|
|
}
|
2018-10-15 16:48:15 +08:00
|
|
|
|
|
|
|
func (vs *VolumeServer) VolumeMount(ctx context.Context, req *volume_server_pb.VolumeMountRequest) (*volume_server_pb.VolumeMountResponse, error) {
|
|
|
|
|
|
|
|
resp := &volume_server_pb.VolumeMountResponse{}
|
|
|
|
|
2019-04-19 12:43:36 +08:00
|
|
|
err := vs.store.MountVolume(needle.VolumeId(req.VolumeId))
|
2018-10-15 16:48:15 +08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("volume mount %v: %v", req, err)
|
|
|
|
} else {
|
|
|
|
glog.V(2).Infof("volume mount %v", req)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vs *VolumeServer) VolumeUnmount(ctx context.Context, req *volume_server_pb.VolumeUnmountRequest) (*volume_server_pb.VolumeUnmountResponse, error) {
|
|
|
|
|
|
|
|
resp := &volume_server_pb.VolumeUnmountResponse{}
|
|
|
|
|
2019-04-19 12:43:36 +08:00
|
|
|
err := vs.store.UnmountVolume(needle.VolumeId(req.VolumeId))
|
2018-10-15 16:48:15 +08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("volume unmount %v: %v", req, err)
|
|
|
|
} else {
|
|
|
|
glog.V(2).Infof("volume unmount %v", req)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, err
|
|
|
|
|
|
|
|
}
|
2018-12-29 16:03:30 +08:00
|
|
|
|
|
|
|
func (vs *VolumeServer) VolumeDelete(ctx context.Context, req *volume_server_pb.VolumeDeleteRequest) (*volume_server_pb.VolumeDeleteResponse, error) {
|
|
|
|
|
|
|
|
resp := &volume_server_pb.VolumeDeleteResponse{}
|
|
|
|
|
2023-06-13 01:42:44 +08:00
|
|
|
err := vs.store.DeleteVolume(needle.VolumeId(req.VolumeId), req.OnlyEmpty)
|
2018-12-29 16:03:30 +08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("volume delete %v: %v", req, err)
|
|
|
|
} else {
|
|
|
|
glog.V(2).Infof("volume delete %v", req)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, err
|
|
|
|
|
|
|
|
}
|
2019-06-27 14:02:22 +08:00
|
|
|
|
2020-02-03 07:37:23 +08:00
|
|
|
func (vs *VolumeServer) VolumeConfigure(ctx context.Context, req *volume_server_pb.VolumeConfigureRequest) (*volume_server_pb.VolumeConfigureResponse, error) {
|
|
|
|
|
|
|
|
resp := &volume_server_pb.VolumeConfigureResponse{}
|
|
|
|
|
|
|
|
// check replication format
|
|
|
|
if _, err := super_block.NewReplicaPlacementFromString(req.Replication); err != nil {
|
|
|
|
resp.Error = fmt.Sprintf("volume configure replication %v: %v", req, err)
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// unmount
|
|
|
|
if err := vs.store.UnmountVolume(needle.VolumeId(req.VolumeId)); err != nil {
|
|
|
|
glog.Errorf("volume configure unmount %v: %v", req, err)
|
|
|
|
resp.Error = fmt.Sprintf("volume configure unmount %v: %v", req, err)
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// modify the volume info file
|
|
|
|
if err := vs.store.ConfigureVolume(needle.VolumeId(req.VolumeId), req.Replication); err != nil {
|
|
|
|
glog.Errorf("volume configure %v: %v", req, err)
|
|
|
|
resp.Error = fmt.Sprintf("volume configure %v: %v", req, err)
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// mount
|
|
|
|
if err := vs.store.MountVolume(needle.VolumeId(req.VolumeId)); err != nil {
|
|
|
|
glog.Errorf("volume configure mount %v: %v", req, err)
|
|
|
|
resp.Error = fmt.Sprintf("volume configure mount %v: %v", req, err)
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-27 14:02:22 +08:00
|
|
|
func (vs *VolumeServer) VolumeMarkReadonly(ctx context.Context, req *volume_server_pb.VolumeMarkReadonlyRequest) (*volume_server_pb.VolumeMarkReadonlyResponse, error) {
|
|
|
|
|
|
|
|
resp := &volume_server_pb.VolumeMarkReadonlyResponse{}
|
|
|
|
|
2022-09-15 14:05:30 +08:00
|
|
|
v := vs.store.GetVolume(needle.VolumeId(req.VolumeId))
|
|
|
|
if v == nil {
|
|
|
|
return nil, fmt.Errorf("volume %d not found", req.VolumeId)
|
|
|
|
}
|
|
|
|
|
|
|
|
// step 1: stop master from redirecting traffic here
|
|
|
|
if err := vs.notifyMasterVolumeReadonly(v, true); err != nil {
|
|
|
|
return resp, err
|
2022-09-12 10:29:10 +08:00
|
|
|
}
|
|
|
|
|
2022-09-15 14:05:30 +08:00
|
|
|
// rare case 1.5: it will be unlucky if heartbeat happened between step 1 and 2.
|
|
|
|
|
|
|
|
// step 2: mark local volume as readonly
|
2019-06-27 14:02:22 +08:00
|
|
|
err := vs.store.MarkVolumeReadonly(needle.VolumeId(req.VolumeId))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("volume mark readonly %v: %v", req, err)
|
|
|
|
} else {
|
|
|
|
glog.V(2).Infof("volume mark readonly %v", req)
|
|
|
|
}
|
|
|
|
|
2022-09-15 14:05:30 +08:00
|
|
|
// step 3: tell master from redirecting traffic here again, to prevent rare case 1.5
|
|
|
|
if err := vs.notifyMasterVolumeReadonly(v, true); err != nil {
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
2019-06-27 14:02:22 +08:00
|
|
|
return resp, err
|
2020-08-19 23:42:56 +08:00
|
|
|
}
|
|
|
|
|
2022-09-15 14:05:30 +08:00
|
|
|
func (vs *VolumeServer) notifyMasterVolumeReadonly(v *storage.Volume, isReadOnly bool) error {
|
2024-06-15 02:40:34 +08:00
|
|
|
if grpcErr := pb.WithMasterClient(false, vs.GetMaster(context.Background()), vs.grpcDialOption, false, func(client master_pb.SeaweedClient) error {
|
2022-09-15 14:05:30 +08:00
|
|
|
_, err := client.VolumeMarkReadonly(context.Background(), &master_pb.VolumeMarkReadonlyRequest{
|
|
|
|
Ip: vs.store.Ip,
|
|
|
|
Port: uint32(vs.store.Port),
|
|
|
|
VolumeId: uint32(v.Id),
|
|
|
|
Collection: v.Collection,
|
|
|
|
ReplicaPlacement: uint32(v.ReplicaPlacement.Byte()),
|
|
|
|
Ttl: v.Ttl.ToUint32(),
|
|
|
|
DiskType: string(v.DiskType()),
|
|
|
|
IsReadonly: isReadOnly,
|
2022-09-12 10:29:10 +08:00
|
|
|
})
|
|
|
|
if err != nil {
|
2022-09-15 14:05:30 +08:00
|
|
|
return fmt.Errorf("set volume %d to read only on master: %v", v.Id, err)
|
2022-09-12 10:29:10 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); grpcErr != nil {
|
2024-06-15 02:40:34 +08:00
|
|
|
glog.V(0).Infof("connect to %s: %v", vs.GetMaster(context.Background()), grpcErr)
|
|
|
|
return fmt.Errorf("grpc VolumeMarkReadonly with master %s: %v", vs.GetMaster(context.Background()), grpcErr)
|
2022-09-15 14:05:30 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vs *VolumeServer) VolumeMarkWritable(ctx context.Context, req *volume_server_pb.VolumeMarkWritableRequest) (*volume_server_pb.VolumeMarkWritableResponse, error) {
|
|
|
|
|
|
|
|
resp := &volume_server_pb.VolumeMarkWritableResponse{}
|
|
|
|
|
|
|
|
v := vs.store.GetVolume(needle.VolumeId(req.VolumeId))
|
|
|
|
if v == nil {
|
|
|
|
return nil, fmt.Errorf("volume %d not found", req.VolumeId)
|
2022-09-12 10:29:10 +08:00
|
|
|
}
|
|
|
|
|
2020-08-19 23:42:56 +08:00
|
|
|
err := vs.store.MarkVolumeWritable(needle.VolumeId(req.VolumeId))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("volume mark writable %v: %v", req, err)
|
|
|
|
} else {
|
|
|
|
glog.V(2).Infof("volume mark writable %v", req)
|
|
|
|
}
|
|
|
|
|
2022-09-15 14:05:30 +08:00
|
|
|
// enable master to redirect traffic here
|
|
|
|
if err := vs.notifyMasterVolumeReadonly(v, false); err != nil {
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
2020-08-19 23:42:56 +08:00
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vs *VolumeServer) VolumeStatus(ctx context.Context, req *volume_server_pb.VolumeStatusRequest) (*volume_server_pb.VolumeStatusResponse, error) {
|
|
|
|
|
|
|
|
resp := &volume_server_pb.VolumeStatusResponse{}
|
|
|
|
|
|
|
|
v := vs.store.GetVolume(needle.VolumeId(req.VolumeId))
|
|
|
|
if v == nil {
|
|
|
|
return nil, fmt.Errorf("not found volume id %d", req.VolumeId)
|
|
|
|
}
|
2024-03-25 23:19:31 +08:00
|
|
|
if v.DataBackend == nil {
|
|
|
|
return nil, fmt.Errorf("volume %d data backend not found", req.VolumeId)
|
|
|
|
}
|
2020-08-19 23:42:56 +08:00
|
|
|
|
2023-06-06 01:17:21 +08:00
|
|
|
volumeSize, _, _ := v.DataBackend.GetStat()
|
2020-08-19 23:42:56 +08:00
|
|
|
resp.IsReadOnly = v.IsReadOnly()
|
2023-06-06 01:17:21 +08:00
|
|
|
resp.VolumeSize = uint64(volumeSize)
|
2023-10-10 00:57:26 +08:00
|
|
|
resp.FileCount = v.FileCount()
|
|
|
|
resp.FileDeletedCount = v.DeletedCount()
|
2020-08-19 23:42:56 +08:00
|
|
|
|
|
|
|
return resp, nil
|
2019-06-27 14:02:22 +08:00
|
|
|
}
|
2020-02-22 13:45:03 +08:00
|
|
|
|
|
|
|
func (vs *VolumeServer) VolumeServerStatus(ctx context.Context, req *volume_server_pb.VolumeServerStatusRequest) (*volume_server_pb.VolumeServerStatusResponse, error) {
|
|
|
|
|
2022-06-13 02:56:23 +08:00
|
|
|
resp := &volume_server_pb.VolumeServerStatusResponse{
|
|
|
|
MemoryStatus: stats.MemStat(),
|
|
|
|
Version: util.Version(),
|
|
|
|
DataCenter: vs.dataCenter,
|
|
|
|
Rack: vs.rack,
|
|
|
|
}
|
2020-02-22 13:45:03 +08:00
|
|
|
|
|
|
|
for _, loc := range vs.store.Locations {
|
|
|
|
if dir, e := filepath.Abs(loc.Directory); e == nil {
|
|
|
|
resp.DiskStatuses = append(resp.DiskStatuses, stats.NewDiskStatus(dir))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
|
|
|
|
}
|
2020-07-23 03:02:21 +08:00
|
|
|
|
2020-09-14 12:25:51 +08:00
|
|
|
func (vs *VolumeServer) VolumeServerLeave(ctx context.Context, req *volume_server_pb.VolumeServerLeaveRequest) (*volume_server_pb.VolumeServerLeaveResponse, error) {
|
|
|
|
|
|
|
|
resp := &volume_server_pb.VolumeServerLeaveResponse{}
|
|
|
|
|
|
|
|
vs.StopHeartbeat()
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-07-23 03:02:21 +08:00
|
|
|
func (vs *VolumeServer) VolumeNeedleStatus(ctx context.Context, req *volume_server_pb.VolumeNeedleStatusRequest) (*volume_server_pb.VolumeNeedleStatusResponse, error) {
|
|
|
|
|
|
|
|
resp := &volume_server_pb.VolumeNeedleStatusResponse{}
|
|
|
|
|
|
|
|
volumeId := needle.VolumeId(req.VolumeId)
|
|
|
|
|
|
|
|
n := &needle.Needle{
|
|
|
|
Id: types.NeedleId(req.NeedleId),
|
|
|
|
}
|
|
|
|
|
|
|
|
var count int
|
|
|
|
var err error
|
|
|
|
hasVolume := vs.store.HasVolume(volumeId)
|
|
|
|
if !hasVolume {
|
|
|
|
_, hasEcVolume := vs.store.FindEcVolume(volumeId)
|
|
|
|
if !hasEcVolume {
|
|
|
|
return nil, fmt.Errorf("volume not found %d", req.VolumeId)
|
|
|
|
}
|
2021-08-09 14:25:16 +08:00
|
|
|
count, err = vs.store.ReadEcShardNeedle(volumeId, n, nil)
|
2020-07-23 03:02:21 +08:00
|
|
|
} else {
|
2021-08-09 14:25:16 +08:00
|
|
|
count, err = vs.store.ReadVolumeNeedle(volumeId, n, nil, nil)
|
2020-07-23 03:02:21 +08:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if count < 0 {
|
|
|
|
return nil, fmt.Errorf("needle not found %d", n.Id)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.NeedleId = uint64(n.Id)
|
|
|
|
resp.Cookie = uint32(n.Cookie)
|
2020-08-19 08:04:28 +08:00
|
|
|
resp.Size = uint32(n.Size)
|
2020-07-23 03:02:21 +08:00
|
|
|
resp.LastModified = n.LastModified
|
|
|
|
resp.Crc = n.Checksum.Value()
|
|
|
|
if n.HasTtl() {
|
|
|
|
resp.Ttl = n.Ttl.String()
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
|
|
|
|
}
|
2022-04-02 07:37:06 +08:00
|
|
|
|
|
|
|
func (vs *VolumeServer) Ping(ctx context.Context, req *volume_server_pb.PingRequest) (resp *volume_server_pb.PingResponse, pingErr error) {
|
2022-04-17 03:45:49 +08:00
|
|
|
resp = &volume_server_pb.PingResponse{
|
|
|
|
StartTimeNs: time.Now().UnixNano(),
|
|
|
|
}
|
2022-04-02 07:55:26 +08:00
|
|
|
if req.TargetType == cluster.FilerType {
|
2023-01-20 17:48:12 +08:00
|
|
|
pingErr = pb.WithFilerClient(false, 0, pb.ServerAddress(req.Target), vs.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
|
2022-04-17 03:45:49 +08:00
|
|
|
pingResp, err := client.Ping(ctx, &filer_pb.PingRequest{})
|
|
|
|
if pingResp != nil {
|
|
|
|
resp.RemoteTimeNs = pingResp.StartTimeNs
|
|
|
|
}
|
2022-04-02 07:44:58 +08:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
2022-04-02 07:55:26 +08:00
|
|
|
if req.TargetType == cluster.VolumeServerType {
|
2022-04-02 07:37:06 +08:00
|
|
|
pingErr = pb.WithVolumeServerClient(false, pb.ServerAddress(req.Target), vs.grpcDialOption, func(client volume_server_pb.VolumeServerClient) error {
|
2022-04-17 03:45:49 +08:00
|
|
|
pingResp, err := client.Ping(ctx, &volume_server_pb.PingRequest{})
|
|
|
|
if pingResp != nil {
|
|
|
|
resp.RemoteTimeNs = pingResp.StartTimeNs
|
|
|
|
}
|
2022-04-02 07:37:06 +08:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
2022-04-02 07:55:26 +08:00
|
|
|
if req.TargetType == cluster.MasterType {
|
2022-08-24 14:18:21 +08:00
|
|
|
pingErr = pb.WithMasterClient(false, pb.ServerAddress(req.Target), vs.grpcDialOption, false, func(client master_pb.SeaweedClient) error {
|
2022-04-17 03:45:49 +08:00
|
|
|
pingResp, err := client.Ping(ctx, &master_pb.PingRequest{})
|
|
|
|
if pingResp != nil {
|
|
|
|
resp.RemoteTimeNs = pingResp.StartTimeNs
|
|
|
|
}
|
2022-04-02 07:50:58 +08:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if pingErr != nil {
|
|
|
|
pingErr = fmt.Errorf("ping %s %s: %v", req.TargetType, req.Target, pingErr)
|
|
|
|
}
|
2022-04-17 03:45:49 +08:00
|
|
|
resp.StopTimeNs = time.Now().UnixNano()
|
2022-04-02 07:37:06 +08:00
|
|
|
return
|
|
|
|
}
|