2014-04-14 07:23:52 +00:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2015-01-08 08:19:32 +00:00
|
|
|
"fmt"
|
2014-04-21 09:11:10 +00:00
|
|
|
"io/ioutil"
|
2015-02-12 22:34:05 +00:00
|
|
|
"math/rand"
|
2014-04-14 07:23:52 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2014-10-26 18:34:55 +00:00
|
|
|
|
2016-06-03 01:09:14 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/operation"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/topology"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
2014-12-26 08:59:53 +00:00
|
|
|
"github.com/golang/protobuf/proto"
|
2014-04-14 07:23:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (ms *MasterServer) collectionDeleteHandler(w http.ResponseWriter, r *http.Request) {
|
2016-05-30 19:30:26 +00:00
|
|
|
collection, ok := ms.Topo.FindCollection(r.FormValue("collection"))
|
2014-04-14 07:23:52 +00:00
|
|
|
if !ok {
|
2015-03-10 07:20:31 +00:00
|
|
|
writeJsonError(w, r, http.StatusBadRequest, fmt.Errorf("collection %s does not exist", r.FormValue("collection")))
|
2014-04-14 07:23:52 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, server := range collection.ListVolumeServers() {
|
|
|
|
_, err := util.Get("http://" + server.Ip + ":" + strconv.Itoa(server.Port) + "/admin/delete_collection?collection=" + r.FormValue("collection"))
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ms.Topo.DeleteCollection(r.FormValue("collection"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *MasterServer) dirJoinHandler(w http.ResponseWriter, r *http.Request) {
|
2014-04-21 09:11:10 +00:00
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
2015-01-08 08:19:32 +00:00
|
|
|
writeJsonError(w, r, http.StatusBadRequest, err)
|
2014-04-21 09:11:10 +00:00
|
|
|
return
|
2014-04-14 07:23:52 +00:00
|
|
|
}
|
2014-04-21 09:11:10 +00:00
|
|
|
joinMessage := &operation.JoinMessage{}
|
|
|
|
if err = proto.Unmarshal(body, joinMessage); err != nil {
|
2015-01-08 08:19:32 +00:00
|
|
|
writeJsonError(w, r, http.StatusBadRequest, err)
|
2014-04-14 07:23:52 +00:00
|
|
|
return
|
|
|
|
}
|
2014-04-21 09:11:10 +00:00
|
|
|
if *joinMessage.Ip == "" {
|
2016-03-08 16:46:46 +00:00
|
|
|
*joinMessage.Ip = r.RemoteAddr[0:strings.LastIndex(r.RemoteAddr, ":")]
|
2014-04-21 09:11:10 +00:00
|
|
|
}
|
|
|
|
if glog.V(4) {
|
|
|
|
if jsonData, jsonError := json.Marshal(joinMessage); jsonError != nil {
|
|
|
|
glog.V(0).Infoln("json marshaling error: ", jsonError)
|
2015-01-08 08:19:32 +00:00
|
|
|
writeJsonError(w, r, http.StatusBadRequest, jsonError)
|
2014-04-21 09:11:10 +00:00
|
|
|
return
|
|
|
|
} else {
|
|
|
|
glog.V(4).Infoln("Proto size", len(body), "json size", len(jsonData), string(jsonData))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-20 19:38:59 +00:00
|
|
|
ms.Topo.ProcessJoinMessage(joinMessage)
|
2015-02-07 23:35:28 +00:00
|
|
|
writeJsonQuiet(w, r, http.StatusOK, operation.JoinResult{
|
|
|
|
VolumeSizeLimit: uint64(ms.volumeSizeLimitMB) * 1024 * 1024,
|
|
|
|
SecretKey: string(ms.guard.SecretKey),
|
|
|
|
})
|
2014-04-14 07:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 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) {
|
|
|
|
gcThreshold := r.FormValue("garbageThreshold")
|
|
|
|
if gcThreshold == "" {
|
|
|
|
gcThreshold = ms.garbageThreshold
|
|
|
|
}
|
2015-07-11 19:20:09 +00:00
|
|
|
glog.Infoln("garbageThreshold =", gcThreshold)
|
2014-04-14 07:23:52 +00:00
|
|
|
ms.Topo.Vacuum(gcThreshold)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
if count, err = strconv.Atoi(r.FormValue("count")); err == nil {
|
|
|
|
if ms.Topo.FreeSpace() < count*option.ReplicaPlacement.GetCopyCount() {
|
|
|
|
err = errors.New("Only " + strconv.Itoa(ms.Topo.FreeSpace()) + " volumes left! Not enough for " + strconv.Itoa(count*option.ReplicaPlacement.GetCopyCount()))
|
|
|
|
} else {
|
|
|
|
count, err = ms.vg.GrowByCountAndType(count, option, ms.Topo)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = errors.New("parameter count is not found")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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{})
|
|
|
|
m["Version"] = util.VERSION
|
|
|
|
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)
|
|
|
|
volumeId, err := storage.NewVolumeId(vid)
|
|
|
|
if err != nil {
|
|
|
|
debug("parsing error:", err, r.URL.Path)
|
|
|
|
return
|
|
|
|
}
|
2016-05-23 10:21:38 +00:00
|
|
|
collection := r.FormValue("collection")
|
|
|
|
machines := ms.Topo.Lookup(collection, volumeId)
|
2014-04-14 07:23:52 +00:00
|
|
|
if machines != nil && len(machines) > 0 {
|
2015-12-25 08:32:50 +00: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 07:23:52 +00:00
|
|
|
} else {
|
2016-05-23 10:21:38 +00:00
|
|
|
writeJsonError(w, r, http.StatusNotFound, fmt.Errorf("volume id %d or collection %s not found", volumeId, collection))
|
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
|
|
|
|
}
|
|
|
|
return "localhost:" + strconv.Itoa(ms.port)
|
|
|
|
}
|
2014-04-14 07:23:52 +00:00
|
|
|
func (ms *MasterServer) submitFromMasterServerHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if ms.Topo.IsLeader() {
|
2015-06-24 16:50:41 +00:00
|
|
|
submitForClientHandler(w, r, ms.selfUrl(r))
|
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 {
|
|
|
|
submitForClientHandler(w, r, masterUrl)
|
|
|
|
}
|
2014-04-14 07:23:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *MasterServer) deleteFromMasterServerHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if ms.Topo.IsLeader() {
|
2015-06-24 16:50:41 +00:00
|
|
|
deleteForClientHandler(w, r, ms.selfUrl(r))
|
2014-04-14 07:23:52 +00:00
|
|
|
} else {
|
|
|
|
deleteForClientHandler(w, r, ms.Topo.RaftServer.Leader())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-26 06:45:31 +00:00
|
|
|
func (ms *MasterServer) HasWritableVolume(option *topology.VolumeGrowOption) bool {
|
2014-09-20 19:38:59 +00:00
|
|
|
vl := ms.Topo.GetVolumeLayout(option.Collection, option.ReplicaPlacement, option.Ttl)
|
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 == "" {
|
|
|
|
replicationString = ms.defaultReplicaPlacement
|
|
|
|
}
|
|
|
|
replicaPlacement, err := storage.NewReplicaPlacementFromString(replicationString)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-09-20 19:38:59 +00:00
|
|
|
ttl, err := storage.ReadTTL(r.FormValue("ttl"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-04-14 07:23:52 +00:00
|
|
|
volumeGrowOption := &topology.VolumeGrowOption{
|
|
|
|
Collection: r.FormValue("collection"),
|
|
|
|
ReplicaPlacement: replicaPlacement,
|
2014-09-20 19:38:59 +00:00
|
|
|
Ttl: ttl,
|
2014-04-14 07:23:52 +00:00
|
|
|
DataCenter: r.FormValue("dataCenter"),
|
|
|
|
Rack: r.FormValue("rack"),
|
|
|
|
DataNode: r.FormValue("dataNode"),
|
|
|
|
}
|
|
|
|
return volumeGrowOption, nil
|
|
|
|
}
|