2014-04-14 07:23:52 +00:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
2018-10-15 07:03:55 +00:00
|
|
|
"context"
|
2015-01-08 08:19:32 +00:00
|
|
|
"fmt"
|
2020-12-13 08:58:58 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage"
|
2019-04-19 04:43:36 +00:00
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
2019-09-12 13:18:21 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/operation"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
|
2019-10-25 06:41:32 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage/backend/memory_map"
|
2019-09-12 13:18:21 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage/needle"
|
2019-12-23 20:48:20 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage/super_block"
|
2019-09-12 13:18:21 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/topology"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
2014-04-14 07:23:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (ms *MasterServer) collectionDeleteHandler(w http.ResponseWriter, r *http.Request) {
|
2019-10-31 06:25:05 +00:00
|
|
|
collectionName := r.FormValue("collection")
|
|
|
|
collection, ok := ms.Topo.FindCollection(collectionName)
|
2014-04-14 07:23:52 +00:00
|
|
|
if !ok {
|
2019-10-31 06:25:05 +00:00
|
|
|
writeJsonError(w, r, http.StatusBadRequest, fmt.Errorf("collection %s does not exist", collectionName))
|
2014-04-14 07:23:52 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, server := range collection.ListVolumeServers() {
|
2020-02-26 05:50:12 +00:00
|
|
|
err := operation.WithVolumeServerClient(server.Url(), ms.grpcDialOption, func(client volume_server_pb.VolumeServerClient) error {
|
|
|
|
_, deleteErr := client.DeleteCollection(context.Background(), &volume_server_pb.DeleteCollectionRequest{
|
2018-10-15 07:03:55 +00:00
|
|
|
Collection: collection.Name,
|
|
|
|
})
|
|
|
|
return deleteErr
|
|
|
|
})
|
2014-04-14 07:23:52 +00:00
|
|
|
if err != nil {
|
2015-01-08 08:19:32 +00:00
|
|
|
writeJsonError(w, r, http.StatusInternalServerError, err)
|
2014-04-14 07:23:52 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2019-10-31 06:25:05 +00:00
|
|
|
ms.Topo.DeleteCollection(collectionName)
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
return
|
2014-04-14 07:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *MasterServer) dirStatusHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
m := make(map[string]interface{})
|
2020-06-02 07:10:35 +00:00
|
|
|
m["Version"] = util.Version()
|
2014-04-14 07:23:52 +00:00
|
|
|
m["Topology"] = ms.Topo.ToMap()
|
2015-01-08 08:19:32 +00:00
|
|
|
writeJsonQuiet(w, r, http.StatusOK, m)
|
2014-04-14 07:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *MasterServer) volumeVacuumHandler(w http.ResponseWriter, r *http.Request) {
|
2018-10-15 06:12:43 +00:00
|
|
|
gcString := r.FormValue("garbageThreshold")
|
2019-06-23 10:08:27 +00:00
|
|
|
gcThreshold := ms.option.GarbageThreshold
|
2018-10-15 06:12:43 +00:00
|
|
|
if gcString != "" {
|
|
|
|
var err error
|
|
|
|
gcThreshold, err = strconv.ParseFloat(gcString, 32)
|
|
|
|
if err != nil {
|
|
|
|
glog.V(0).Infof("garbageThreshold %s is not a valid float number: %v", gcString, err)
|
2019-10-31 12:43:20 +00:00
|
|
|
writeJsonError(w, r, http.StatusNotAcceptable, fmt.Errorf("garbageThreshold %s is not a valid float number", gcString))
|
2018-10-15 06:12:43 +00:00
|
|
|
return
|
|
|
|
}
|
2014-04-14 07:23:52 +00:00
|
|
|
}
|
2020-03-10 05:29:02 +00:00
|
|
|
// glog.Infoln("garbageThreshold =", gcThreshold)
|
2019-10-29 13:28:28 +00:00
|
|
|
ms.Topo.Vacuum(ms.grpcDialOption, gcThreshold, ms.preallocateSize)
|
2014-04-14 07:23:52 +00:00
|
|
|
ms.dirStatusHandler(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *MasterServer) volumeGrowHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
count := 0
|
|
|
|
option, err := ms.getVolumeGrowOption(r)
|
|
|
|
if err != nil {
|
2015-01-08 08:19:32 +00:00
|
|
|
writeJsonError(w, r, http.StatusNotAcceptable, err)
|
2014-04-14 07:23:52 +00:00
|
|
|
return
|
|
|
|
}
|
2019-10-22 05:36:41 +00:00
|
|
|
|
|
|
|
if count, err = strconv.Atoi(r.FormValue("count")); err == nil {
|
2020-12-14 03:44:57 +00:00
|
|
|
if ms.Topo.AvailableSpaceFor(option) < int64(count*option.ReplicaPlacement.GetCopyCount()) {
|
2019-10-22 05:36:41 +00:00
|
|
|
err = fmt.Errorf("only %d volumes left, not enough for %d", ms.Topo.FreeSpace(), count*option.ReplicaPlacement.GetCopyCount())
|
2014-04-14 07:23:52 +00:00
|
|
|
} else {
|
2019-10-29 13:28:28 +00:00
|
|
|
count, err = ms.vg.GrowByCountAndType(ms.grpcDialOption, count, option, ms.Topo)
|
2014-04-14 07:23:52 +00:00
|
|
|
}
|
2019-10-22 05:36:41 +00:00
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("can not parse parameter count %s", r.FormValue("count"))
|
2014-04-14 07:23:52 +00:00
|
|
|
}
|
2019-10-22 05:36:41 +00:00
|
|
|
|
2014-04-14 07:23:52 +00:00
|
|
|
if err != nil {
|
2015-01-08 08:19:32 +00:00
|
|
|
writeJsonError(w, r, http.StatusNotAcceptable, err)
|
2014-04-14 07:23:52 +00:00
|
|
|
} else {
|
2015-01-08 08:19:32 +00:00
|
|
|
writeJsonQuiet(w, r, http.StatusOK, map[string]interface{}{"count": count})
|
2014-04-14 07:23:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *MasterServer) volumeStatusHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
m := make(map[string]interface{})
|
2020-06-02 07:10:35 +00:00
|
|
|
m["Version"] = util.Version()
|
2014-04-14 07:23:52 +00:00
|
|
|
m["Volumes"] = ms.Topo.ToVolumeMap()
|
2015-01-08 08:19:32 +00:00
|
|
|
writeJsonQuiet(w, r, http.StatusOK, m)
|
2014-04-14 07:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *MasterServer) redirectHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vid, _, _, _, _ := parseURLPath(r.URL.Path)
|
2016-05-23 10:21:38 +00:00
|
|
|
collection := r.FormValue("collection")
|
2019-07-28 10:58:13 +00:00
|
|
|
location := ms.findVolumeLocation(collection, vid)
|
|
|
|
if location.Error == "" {
|
|
|
|
loc := location.Locations[rand.Intn(len(location.Locations))]
|
2015-12-25 08:32:50 +00:00
|
|
|
var url string
|
|
|
|
if r.URL.RawQuery != "" {
|
2019-07-28 10:58:13 +00:00
|
|
|
url = util.NormalizeUrl(loc.PublicUrl) + r.URL.Path + "?" + r.URL.RawQuery
|
2015-12-25 08:32:50 +00:00
|
|
|
} else {
|
2019-07-28 10:58:13 +00:00
|
|
|
url = util.NormalizeUrl(loc.PublicUrl) + r.URL.Path
|
2015-12-25 08:32:50 +00:00
|
|
|
}
|
2020-09-10 05:36:10 +00:00
|
|
|
http.Redirect(w, r, url, http.StatusPermanentRedirect)
|
2014-04-14 07:23:52 +00:00
|
|
|
} else {
|
2019-07-28 10:58:13 +00:00
|
|
|
writeJsonError(w, r, http.StatusNotFound, fmt.Errorf("volume id %s not found: %s", vid, location.Error))
|
2014-04-14 07:23:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-24 16:50:41 +00:00
|
|
|
func (ms *MasterServer) selfUrl(r *http.Request) string {
|
|
|
|
if r.Host != "" {
|
|
|
|
return r.Host
|
|
|
|
}
|
2019-06-23 10:08:27 +00:00
|
|
|
return "localhost:" + strconv.Itoa(ms.option.Port)
|
2015-06-24 16:50:41 +00:00
|
|
|
}
|
2014-04-14 07:23:52 +00:00
|
|
|
func (ms *MasterServer) submitFromMasterServerHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if ms.Topo.IsLeader() {
|
2019-10-29 13:28:28 +00:00
|
|
|
submitForClientHandler(w, r, ms.selfUrl(r), ms.grpcDialOption)
|
2014-04-14 07:23:52 +00:00
|
|
|
} else {
|
2015-04-19 11:00:20 +00:00
|
|
|
masterUrl, err := ms.Topo.Leader()
|
|
|
|
if err != nil {
|
|
|
|
writeJsonError(w, r, http.StatusInternalServerError, err)
|
|
|
|
} else {
|
2019-10-29 13:28:28 +00:00
|
|
|
submitForClientHandler(w, r, masterUrl, ms.grpcDialOption)
|
2015-04-19 11:00:20 +00:00
|
|
|
}
|
2014-04-14 07:23:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-26 06:45:31 +00:00
|
|
|
func (ms *MasterServer) HasWritableVolume(option *topology.VolumeGrowOption) bool {
|
2020-12-13 19:59:32 +00:00
|
|
|
vl := ms.Topo.GetVolumeLayout(option.Collection, option.ReplicaPlacement, option.Ttl, option.DiskType)
|
2014-04-14 07:23:52 +00:00
|
|
|
return vl.GetActiveVolumeCount(option) > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *MasterServer) getVolumeGrowOption(r *http.Request) (*topology.VolumeGrowOption, error) {
|
|
|
|
replicationString := r.FormValue("replication")
|
|
|
|
if replicationString == "" {
|
2019-06-23 10:08:27 +00:00
|
|
|
replicationString = ms.option.DefaultReplicaPlacement
|
2014-04-14 07:23:52 +00:00
|
|
|
}
|
2019-12-23 20:48:20 +00:00
|
|
|
replicaPlacement, err := super_block.NewReplicaPlacementFromString(replicationString)
|
2014-04-14 07:23:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-19 04:43:36 +00:00
|
|
|
ttl, err := needle.ReadTTL(r.FormValue("ttl"))
|
2014-09-20 19:38:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-22 07:49:42 +00:00
|
|
|
memoryMapMaxSizeMb, err := memory_map.ReadMemoryMapMaxSizeMb(r.FormValue("memoryMapMaxSizeMb"))
|
2019-09-04 14:27:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-13 20:06:21 +00:00
|
|
|
diskType, err := storage.ToDiskType(r.FormValue("disk"))
|
2020-12-13 08:58:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-04 14:27:14 +00:00
|
|
|
|
2019-06-23 10:08:27 +00:00
|
|
|
preallocate := ms.preallocateSize
|
2017-01-08 19:01:46 +00:00
|
|
|
if r.FormValue("preallocate") != "" {
|
|
|
|
preallocate, err = strconv.ParseInt(r.FormValue("preallocate"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to parse int64 preallocate = %s: %v", r.FormValue("preallocate"), err)
|
|
|
|
}
|
|
|
|
}
|
2014-04-14 07:23:52 +00:00
|
|
|
volumeGrowOption := &topology.VolumeGrowOption{
|
2019-09-04 14:27:14 +00:00
|
|
|
Collection: r.FormValue("collection"),
|
|
|
|
ReplicaPlacement: replicaPlacement,
|
|
|
|
Ttl: ttl,
|
2020-12-13 19:59:32 +00:00
|
|
|
DiskType: diskType,
|
2019-09-04 14:27:14 +00:00
|
|
|
Prealloacte: preallocate,
|
|
|
|
DataCenter: r.FormValue("dataCenter"),
|
|
|
|
Rack: r.FormValue("rack"),
|
|
|
|
DataNode: r.FormValue("dataNode"),
|
2019-10-22 05:57:01 +00:00
|
|
|
MemoryMapMaxSizeMb: memoryMapMaxSizeMb,
|
2014-04-14 07:23:52 +00:00
|
|
|
}
|
|
|
|
return volumeGrowOption, nil
|
|
|
|
}
|