2014-04-14 15:23:52 +08:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
2018-10-15 15:03:55 +08:00
|
|
|
"context"
|
2014-04-14 15:23:52 +08:00
|
|
|
"errors"
|
2015-01-08 16:19:32 +08:00
|
|
|
"fmt"
|
2019-04-19 12:43:36 +08:00
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
2016-06-03 09:09:14 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2018-10-15 15:03:55 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/operation"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
|
2016-06-03 09:09:14 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage"
|
2019-04-19 12:43:36 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage/needle"
|
2016-06-03 09:09:14 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/topology"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
2014-04-14 15:23:52 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func (ms *MasterServer) collectionDeleteHandler(w http.ResponseWriter, r *http.Request) {
|
2016-05-31 03:30:26 +08:00
|
|
|
collection, ok := ms.Topo.FindCollection(r.FormValue("collection"))
|
2014-04-14 15:23:52 +08:00
|
|
|
if !ok {
|
2015-03-10 15:20:31 +08:00
|
|
|
writeJsonError(w, r, http.StatusBadRequest, fmt.Errorf("collection %s does not exist", r.FormValue("collection")))
|
2014-04-14 15:23:52 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, server := range collection.ListVolumeServers() {
|
2019-02-19 04:11:52 +08:00
|
|
|
err := operation.WithVolumeServerClient(server.Url(), ms.grpcDialOpiton, func(client volume_server_pb.VolumeServerClient) error {
|
2019-02-20 17:01:01 +08:00
|
|
|
_, deleteErr := client.DeleteCollection(context.Background(), &volume_server_pb.DeleteCollectionRequest{
|
2018-10-15 15:03:55 +08:00
|
|
|
Collection: collection.Name,
|
|
|
|
})
|
|
|
|
return deleteErr
|
|
|
|
})
|
2014-04-14 15:23:52 +08:00
|
|
|
if err != nil {
|
2015-01-08 16:19:32 +08:00
|
|
|
writeJsonError(w, r, http.StatusInternalServerError, err)
|
2014-04-14 15:23:52 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ms.Topo.DeleteCollection(r.FormValue("collection"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *MasterServer) dirStatusHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
m := make(map[string]interface{})
|
|
|
|
m["Version"] = util.VERSION
|
|
|
|
m["Topology"] = ms.Topo.ToMap()
|
2015-01-08 16:19:32 +08:00
|
|
|
writeJsonQuiet(w, r, http.StatusOK, m)
|
2014-04-14 15:23:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *MasterServer) volumeVacuumHandler(w http.ResponseWriter, r *http.Request) {
|
2018-10-15 14:12:43 +08:00
|
|
|
gcString := r.FormValue("garbageThreshold")
|
2019-06-23 18:08:27 +08:00
|
|
|
gcThreshold := ms.option.GarbageThreshold
|
2018-10-15 14:12:43 +08: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)
|
|
|
|
return
|
|
|
|
}
|
2014-04-14 15:23:52 +08:00
|
|
|
}
|
2015-07-12 03:20:09 +08:00
|
|
|
glog.Infoln("garbageThreshold =", gcThreshold)
|
2019-06-23 18:08:27 +08:00
|
|
|
ms.Topo.Vacuum(ms.grpcDialOpiton, gcThreshold, ms.preallocateSize)
|
2014-04-14 15:23:52 +08: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 16:19:32 +08:00
|
|
|
writeJsonError(w, r, http.StatusNotAcceptable, err)
|
2014-04-14 15:23:52 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
if count, err = strconv.Atoi(r.FormValue("count")); err == nil {
|
2019-04-05 10:27:00 +08:00
|
|
|
if ms.Topo.FreeSpace() < int64(count*option.ReplicaPlacement.GetCopyCount()) {
|
|
|
|
err = fmt.Errorf("only %d volumes left, not enough for %d", ms.Topo.FreeSpace(), count*option.ReplicaPlacement.GetCopyCount())
|
2014-04-14 15:23:52 +08:00
|
|
|
} else {
|
2019-02-19 04:11:52 +08:00
|
|
|
count, err = ms.vg.GrowByCountAndType(ms.grpcDialOpiton, count, option, ms.Topo)
|
2014-04-14 15:23:52 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = errors.New("parameter count is not found")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
2015-01-08 16:19:32 +08:00
|
|
|
writeJsonError(w, r, http.StatusNotAcceptable, err)
|
2014-04-14 15:23:52 +08:00
|
|
|
} else {
|
2015-01-08 16:19:32 +08:00
|
|
|
writeJsonQuiet(w, r, http.StatusOK, map[string]interface{}{"count": count})
|
2014-04-14 15:23:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *MasterServer) volumeStatusHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
m := make(map[string]interface{})
|
|
|
|
m["Version"] = util.VERSION
|
|
|
|
m["Volumes"] = ms.Topo.ToVolumeMap()
|
2015-01-08 16:19:32 +08:00
|
|
|
writeJsonQuiet(w, r, http.StatusOK, m)
|
2014-04-14 15:23:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *MasterServer) redirectHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vid, _, _, _, _ := parseURLPath(r.URL.Path)
|
2019-04-19 12:43:36 +08:00
|
|
|
volumeId, err := needle.NewVolumeId(vid)
|
2014-04-14 15:23:52 +08:00
|
|
|
if err != nil {
|
|
|
|
debug("parsing error:", err, r.URL.Path)
|
|
|
|
return
|
|
|
|
}
|
2016-05-23 18:21:38 +08:00
|
|
|
collection := r.FormValue("collection")
|
|
|
|
machines := ms.Topo.Lookup(collection, volumeId)
|
2014-04-14 15:23:52 +08:00
|
|
|
if machines != nil && len(machines) > 0 {
|
2015-12-25 16:32:50 +08:00
|
|
|
var url string
|
|
|
|
if r.URL.RawQuery != "" {
|
|
|
|
url = util.NormalizeUrl(machines[rand.Intn(len(machines))].PublicUrl) + r.URL.Path + "?" + r.URL.RawQuery
|
|
|
|
} else {
|
|
|
|
url = util.NormalizeUrl(machines[rand.Intn(len(machines))].PublicUrl) + r.URL.Path
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, url, http.StatusMovedPermanently)
|
2014-04-14 15:23:52 +08:00
|
|
|
} else {
|
2016-05-23 18:21:38 +08:00
|
|
|
writeJsonError(w, r, http.StatusNotFound, fmt.Errorf("volume id %d or collection %s not found", volumeId, collection))
|
2014-04-14 15:23:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-25 00:50:41 +08:00
|
|
|
func (ms *MasterServer) selfUrl(r *http.Request) string {
|
|
|
|
if r.Host != "" {
|
|
|
|
return r.Host
|
|
|
|
}
|
2019-06-23 18:08:27 +08:00
|
|
|
return "localhost:" + strconv.Itoa(ms.option.Port)
|
2015-06-25 00:50:41 +08:00
|
|
|
}
|
2014-04-14 15:23:52 +08:00
|
|
|
func (ms *MasterServer) submitFromMasterServerHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if ms.Topo.IsLeader() {
|
2019-02-19 04:11:52 +08:00
|
|
|
submitForClientHandler(w, r, ms.selfUrl(r), ms.grpcDialOpiton)
|
2014-04-14 15:23:52 +08:00
|
|
|
} else {
|
2015-04-19 19:00:20 +08:00
|
|
|
masterUrl, err := ms.Topo.Leader()
|
|
|
|
if err != nil {
|
|
|
|
writeJsonError(w, r, http.StatusInternalServerError, err)
|
|
|
|
} else {
|
2019-02-19 04:11:52 +08:00
|
|
|
submitForClientHandler(w, r, masterUrl, ms.grpcDialOpiton)
|
2015-04-19 19:00:20 +08:00
|
|
|
}
|
2014-04-14 15:23:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-26 14:45:31 +08:00
|
|
|
func (ms *MasterServer) HasWritableVolume(option *topology.VolumeGrowOption) bool {
|
2014-09-21 03:38:59 +08:00
|
|
|
vl := ms.Topo.GetVolumeLayout(option.Collection, option.ReplicaPlacement, option.Ttl)
|
2014-04-14 15:23:52 +08: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 18:08:27 +08:00
|
|
|
replicationString = ms.option.DefaultReplicaPlacement
|
2014-04-14 15:23:52 +08:00
|
|
|
}
|
|
|
|
replicaPlacement, err := storage.NewReplicaPlacementFromString(replicationString)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-19 12:43:36 +08:00
|
|
|
ttl, err := needle.ReadTTL(r.FormValue("ttl"))
|
2014-09-21 03:38:59 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-23 18:08:27 +08:00
|
|
|
preallocate := ms.preallocateSize
|
2017-01-09 03:01:46 +08: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 15:23:52 +08:00
|
|
|
volumeGrowOption := &topology.VolumeGrowOption{
|
|
|
|
Collection: r.FormValue("collection"),
|
|
|
|
ReplicaPlacement: replicaPlacement,
|
2014-09-21 03:38:59 +08:00
|
|
|
Ttl: ttl,
|
2017-01-09 03:01:46 +08:00
|
|
|
Prealloacte: preallocate,
|
2014-04-14 15:23:52 +08:00
|
|
|
DataCenter: r.FormValue("dataCenter"),
|
|
|
|
Rack: r.FormValue("rack"),
|
|
|
|
DataNode: r.FormValue("dataNode"),
|
|
|
|
}
|
|
|
|
return volumeGrowOption, nil
|
|
|
|
}
|