Skip to content

Commit

Permalink
Remove HTTP admin endpoints (#4754)
Browse files Browse the repository at this point in the history
These endpoints are now served via the GraphQL Admin API which is available at `/admin` and hence we are removing the HTTP endpoints.
  • Loading branch information
pawanrawal authored Feb 14, 2020
1 parent ebbb91d commit 8fa2fd9
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 227 deletions.
114 changes: 0 additions & 114 deletions dgraph/cmd/alpha/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,11 @@ package alpha

import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net"
"net/http"
"strconv"

"github.com/dgraph-io/dgraph/posting"
"github.com/dgraph-io/dgraph/worker"
"github.com/dgraph-io/dgraph/x"
"github.com/golang/glog"
)

// handlerInit does some standard checks. Returns false if something is wrong.
Expand All @@ -46,114 +40,6 @@ func handlerInit(w http.ResponseWriter, r *http.Request, allowedMethods map[stri
return true
}

func drainingHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodPut, http.MethodPost:
enableStr := r.URL.Query().Get("enable")

enable, err := strconv.ParseBool(enableStr)
if err != nil {
x.SetStatus(w, x.ErrorInvalidRequest,
"Found invalid value for the enable parameter")
return
}

x.UpdateDrainingMode(enable)
_, err = w.Write([]byte(fmt.Sprintf(`{"code": "Success",`+
`"message": "draining mode has been set to %v"}`, enable)))
if err != nil {
glog.Errorf("Failed to write response: %v", err)
}
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}

func shutDownHandler(w http.ResponseWriter, r *http.Request) {
if !handlerInit(w, r, map[string]bool{
http.MethodGet: true,
}) {
return
}

close(worker.ShutdownCh)
w.Header().Set("Content-Type", "application/json")
x.Check2(w.Write([]byte(`{"code": "Success", "message": "Server is shutting down"}`)))
}

func exportHandler(w http.ResponseWriter, r *http.Request) {
if !handlerInit(w, r, map[string]bool{
http.MethodGet: true,
}) {
return
}
if err := r.ParseForm(); err != nil {
x.SetHttpStatus(w, http.StatusBadRequest, "Parse of export request failed.")
return
}

format := worker.DefaultExportFormat
if vals, ok := r.Form["format"]; ok {
if len(vals) > 1 {
x.SetHttpStatus(w, http.StatusBadRequest,
"Only one export format may be specified.")
return
}
format = worker.NormalizeExportFormat(vals[0])
if format == "" {
x.SetHttpStatus(w, http.StatusBadRequest, "Invalid export format.")
return
}
}
if err := worker.ExportOverNetwork(context.Background(), format); err != nil {
x.SetStatus(w, err.Error(), "Export failed.")
return
}
w.Header().Set("Content-Type", "application/json")
x.Check2(w.Write([]byte(`{"code": "Success", "message": "Export completed."}`)))
}

func memoryLimitHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
memoryLimitGetHandler(w, r)
case http.MethodPut:
memoryLimitPutHandler(w, r)
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}

func memoryLimitPutHandler(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
memoryMB, err := strconv.ParseFloat(string(body), 64)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

if err := worker.UpdateLruMb(memoryMB); err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, err.Error())
return
}
w.WriteHeader(http.StatusOK)
}

func memoryLimitGetHandler(w http.ResponseWriter, r *http.Request) {
posting.Config.Lock()
memoryMB := posting.Config.AllottedMemory
posting.Config.Unlock()

if _, err := fmt.Fprintln(w, memoryMB); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}

func ipInIPWhitelistRanges(ipString string) bool {
ip := net.ParseIP(ipString)

Expand Down
64 changes: 0 additions & 64 deletions dgraph/cmd/alpha/admin_backup.go

This file was deleted.

31 changes: 23 additions & 8 deletions dgraph/cmd/alpha/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -736,14 +736,29 @@ func TestHealth(t *testing.T) {
}

func setDrainingMode(t *testing.T, enable bool) {
url := fmt.Sprintf("%s/admin/draining?enable=%v", addr, enable)
req, err := http.NewRequest("POST", url, nil)
require.NoError(t, err, "Error while creating post request for %s", url)
client := &http.Client{}
resp, err := client.Do(req)
require.NoError(t, err, "Error while sending post request to %s", url)
status := resp.StatusCode
require.Equal(t, http.StatusOK, status, "Unexpected status code: %v", status)
drainingRequest := `mutation drain($enable: Boolean) {
draining(input: {enable: $enable}) {
response {
code
}
}
}`
adminUrl := fmt.Sprintf("%s/admin", addr)
params := testutil.GraphQLParams{
Query: drainingRequest,
Variables: map[string]interface{}{"enable": enable},
}
b, err := json.Marshal(params)
require.NoError(t, err)

resp, err := http.Post(adminUrl, "application/json", bytes.NewBuffer(b))
require.NoError(t, err)

defer resp.Body.Close()
b, err = ioutil.ReadAll(resp.Body)
require.NoError(t, err)
require.JSONEq(t, `{"data":{"draining":{"response":{"code":"Success"}}}}`,
string(b))
}

func TestDrainingMode(t *testing.T) {
Expand Down
5 changes: 0 additions & 5 deletions dgraph/cmd/alpha/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,11 +447,6 @@ func setupServer(closer *y.Closer) {
// TODO: Figure out what this is for?
http.HandleFunc("/debug/store", storeStatsHandler)

http.HandleFunc("/admin/shutdown", shutDownHandler)
http.HandleFunc("/admin/draining", drainingHandler)
http.HandleFunc("/admin/export", exportHandler)
http.HandleFunc("/admin/config/lru_mb", memoryLimitHandler)

introspection := Alpha.Conf.GetBool("graphql_introspection")
mainServer, adminServer := admin.NewServers(introspection, closer)
http.Handle("/graphql", mainServer.HTTPHandler())
Expand Down
2 changes: 1 addition & 1 deletion dgraph/cmd/bulk/systest/test-bulk-schema.sh
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ function QuerySchema
function DoExport
{
INFO "running export"
docker exec alpha1 curl -Ss localhost:$HTTP_PORT/admin/export &>/dev/null
docker exec alpha1 curl -Ss -H "Content-Type: application/json" localhost:$HTTP_PORT/admin -XPOST -d '{ "query": "mutation { export(input: {format: \"rdf\"}) { response { code message } }}" }' &>/dev/null
sleep 2
docker cp alpha1:/data/alpha1/export .
sleep 1
Expand Down
29 changes: 21 additions & 8 deletions ee/backup/tests/filesystem/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
package main

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"math"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -210,15 +211,27 @@ func runBackup(t *testing.T, numExpectedFiles, numExpectedDirs int) []string {

func runBackupInternal(t *testing.T, forceFull bool, numExpectedFiles,
numExpectedDirs int) []string {
forceFullStr := "false"
if forceFull {
forceFullStr = "true"
backupRequest := `mutation backup($dst: String!, $ff: Boolean!) {
backup(input: {destination: $dst, forceFull: $ff}) {
response {
code
message
}
}
}`

adminUrl := "http://localhost:8180/admin"
params := testutil.GraphQLParams{
Query: backupRequest,
Variables: map[string]interface{}{
"dst": alphaBackupDir,
"ff": forceFull,
},
}
b, err := json.Marshal(params)
require.NoError(t, err)

resp, err := http.PostForm("http://localhost:8180/admin/backup", url.Values{
"destination": []string{alphaBackupDir},
"force_full": []string{forceFullStr},
})
resp, err := http.Post(adminUrl, "application/json", bytes.NewBuffer(b))
require.NoError(t, err)
defer resp.Body.Close()
buf, err := ioutil.ReadAll(resp.Body)
Expand Down
26 changes: 22 additions & 4 deletions ee/backup/tests/minio-large/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
package main

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"math"
"net/http"
"net/url"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -139,9 +140,26 @@ func addTriples(t *testing.T, dg *dgo.Dgraph, numTriples int) {
}

func runBackup(t *testing.T) {
resp, err := http.PostForm("http://localhost:8180/admin/backup", url.Values{
"destination": []string{backupDestination},
})
backupRequest := `mutation backup($dst: String!) {
backup(input: {destination: $dst}) {
response {
code
message
}
}
}`

adminUrl := "http://localhost:8180/admin"
params := testutil.GraphQLParams{
Query: backupRequest,
Variables: map[string]interface{}{
"dst": backupDestination,
},
}
b, err := json.Marshal(params)
require.NoError(t, err)

resp, err := http.Post(adminUrl, "application/json", bytes.NewBuffer(b))
require.NoError(t, err)
defer resp.Body.Close()
buf, err := ioutil.ReadAll(resp.Body)
Expand Down
Loading

0 comments on commit 8fa2fd9

Please sign in to comment.