Skip to content

Commit

Permalink
Merge branch 'master' of github.com:dgraph-io/dgraph into jatin/GRAPH…
Browse files Browse the repository at this point in the history
…QL-924
  • Loading branch information
JatinDev543 committed Jan 4, 2021
2 parents 5296787 + 2f10119 commit 3864368
Show file tree
Hide file tree
Showing 51 changed files with 2,283 additions and 247 deletions.
118 changes: 18 additions & 100 deletions dgraph/cmd/alpha/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func runGzipWithRetry(contentType, url string, buf io.Reader, gzReq, gzResp bool
return nil, err
}
req.Header.Add("Content-Type", contentType)
req.Header.Set("X-Dgraph-AccessToken", token.AccessJwt)
req.Header.Set("X-Dgraph-AccessToken", token.getAccessJWTToken())

if gzReq {
req.Header.Set("Content-Encoding", "gzip")
Expand All @@ -79,15 +79,10 @@ func runGzipWithRetry(contentType, url string, buf io.Reader, gzReq, gzResp bool

resp, err = client.Do(req)
if err != nil && strings.Contains(err.Error(), "Token is expired") {
newToken, err := testutil.HttpLogin(&testutil.LoginParams{
Endpoint: addr + "/admin",
RefreshJwt: token.RefreshToken,
})
err := token.refreshToken()
if err != nil {
return nil, err
}
token.AccessJwt = newToken.AccessJwt
token.RefreshToken = newToken.RefreshToken
continue
} else if err != nil {
return nil, err
Expand Down Expand Up @@ -168,33 +163,8 @@ func queryWithGz(queryText, contentType, debug, timeout string, gzReq, gzResp bo
}

func queryWithTs(queryText, contentType, debug string, ts uint64) (string, uint64, error) {
params := make([]string, 0, 2)
if debug != "" {
params = append(params, "debug="+debug)
}
if ts != 0 {
params = append(params, fmt.Sprintf("startTs=%v", strconv.FormatUint(ts, 10)))
}
url := addr + "/query?" + strings.Join(params, "&")

_, body, err := runWithRetries("POST", contentType, url, queryText)
if err != nil {
return "", 0, err
}

var r res
if err := json.Unmarshal(body, &r); err != nil {
return "", 0, err
}
startTs := r.Extensions.Txn.StartTs

// Remove the extensions.
r2 := res{
Data: r.Data,
}
output, err := json.Marshal(r2)

return string(output), startTs, err
out, startTs, _, err := queryWithTsForResp(queryText, contentType, debug, ts)
return out, startTs, err
}

// queryWithTsForResp query the dgraph and returns it's http response and result.
Expand Down Expand Up @@ -296,57 +266,34 @@ func createRequest(method, contentType, url string, body string) (*http.Request,

func runWithRetries(method, contentType, url string, body string) (
*x.QueryResWithData, []byte, error) {

req, err := createRequest(method, contentType, url, body)
if err != nil {
return nil, nil, err
}

qr, respBody, err := runRequest(req)
if err != nil && strings.Contains(err.Error(), "Token is expired") {
token, err = testutil.HttpLogin(&testutil.LoginParams{
Endpoint: addr + "/admin",
RefreshJwt: token.RefreshToken,
})
if err != nil {
return nil, nil, err
}

// create a new request since the previous request would have been closed upon the err
retryReq, err := createRequest(method, contentType, url, body)
if err != nil {
return nil, nil, err
}

return runRequest(retryReq)
}
qr, respBody, _, err := runWithRetriesForResp(method, contentType, url, body)
return qr, respBody, err
}

// attach the grootAccessJWT to the request and sends the http request
func runRequest(req *http.Request) (*x.QueryResWithData, []byte, error) {
func runRequest(req *http.Request) (*x.QueryResWithData, []byte, *http.Response, error) {
client := &http.Client{}
req.Header.Set("X-Dgraph-AccessToken", token.AccessJwt)
req.Header.Set("X-Dgraph-AccessToken", token.getAccessJWTToken())
resp, err := client.Do(req)
if err != nil {
return nil, nil, err
return nil, nil, resp, err
}
if status := resp.StatusCode; status != http.StatusOK {
return nil, nil, errors.Errorf("Unexpected status code: %v", status)
return nil, nil, resp, errors.Errorf("Unexpected status code: %v", status)
}

defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, nil, errors.Errorf("unable to read from body: %v", err)
return nil, nil, resp, errors.Errorf("unable to read from body: %v", err)
}

qr := new(x.QueryResWithData)
json.Unmarshal(body, qr) // Don't check error.
if len(qr.Errors) > 0 {
return nil, nil, errors.New(qr.Errors[0].Message)
return nil, nil, resp, errors.New(qr.Errors[0].Message)
}
return qr, body, nil
return qr, body, resp, nil
}

func runWithRetriesForResp(method, contentType, url string, body string) (
Expand All @@ -357,12 +304,9 @@ func runWithRetriesForResp(method, contentType, url string, body string) (
return nil, nil, nil, err
}

qr, respBody, resp, err := runRequestForResp(req)
qr, respBody, resp, err := runRequest(req)
if err != nil && strings.Contains(err.Error(), "Token is expired") {
token, err = testutil.HttpLogin(&testutil.LoginParams{
Endpoint: addr + "/admin",
RefreshJwt: token.RefreshToken,
})
err = token.refreshToken()
if err != nil {
return nil, nil, nil, err
}
Expand All @@ -373,37 +317,11 @@ func runWithRetriesForResp(method, contentType, url string, body string) (
return nil, nil, resp, err
}

return runRequestForResp(retryReq)
return runRequest(retryReq)
}
return qr, respBody, resp, err
}

// attach the grootAccessJWT to the request and sends the http request
func runRequestForResp(req *http.Request) (*x.QueryResWithData, []byte, *http.Response, error) {
client := &http.Client{}
req.Header.Set("X-Dgraph-AccessToken", token.AccessJwt)
resp, err := client.Do(req)
if err != nil {
return nil, nil, resp, err
}
if status := resp.StatusCode; status != http.StatusOK {
return nil, nil, resp, errors.Errorf("Unexpected status code: %v", status)
}

defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, nil, resp, errors.Errorf("unable to read from body: %v", err)
}

qr := new(x.QueryResWithData)
json.Unmarshal(body, qr) // Don't check error.
if len(qr.Errors) > 0 {
return nil, nil, resp, errors.New(qr.Errors[0].Message)
}
return qr, body, resp, nil
}

func commitWithTs(keys, preds []string, ts uint64) error {
url := addr + "/commit"
if ts != 0 {
Expand All @@ -421,7 +339,7 @@ func commitWithTs(keys, preds []string, ts uint64) error {
if err != nil {
return err
}
_, _, err = runRequest(req)
_, _, _, err = runRequest(req)
return err
}

Expand All @@ -439,7 +357,7 @@ func commitWithTsKeysOnly(keys []string, ts uint64) error {
if err != nil {
return err
}
_, _, err = runRequest(req)
_, _, _, err = runRequest(req)
return err
}

Expand Down Expand Up @@ -633,7 +551,7 @@ func TestTransactionBasicOldCommitFormat(t *testing.T) {
url := fmt.Sprintf("%s/commit?startTs=%d&abort=true", addr, ts)
req, err := http.NewRequest("POST", url, nil)
require.NoError(t, err)
_, _, err = runRequest(req)
_, _, _, err = runRequest(req)
require.NoError(t, err)
}

Expand Down
4 changes: 2 additions & 2 deletions dgraph/cmd/alpha/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func fetchMetric(t *testing.T) int {
requiredMetric := "dgraph_txn_aborts_total"
req, err := http.NewRequest("GET", addr+"/debug/prometheus_metrics", nil)
require.NoError(t, err)
_, body, err := runRequest(req)
_, body, _, err := runRequest(req)
require.NoError(t, err)
metricsMap, err := extractMetrics(string(body))
require.NoError(t, err)
Expand All @@ -93,7 +93,7 @@ func TestMetrics(t *testing.T) {
req, err := http.NewRequest("GET", addr+"/debug/prometheus_metrics", nil)
require.NoError(t, err)

_, body, err := runRequest(req)
_, body, _, err := runRequest(req)
require.NoError(t, err)
metricsMap, err := extractMetrics(string(body))
require.NoError(t, err, "Unable to get the metrics map: %v", err)
Expand Down
40 changes: 35 additions & 5 deletions dgraph/cmd/alpha/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"net"
"os"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
Expand Down Expand Up @@ -1655,9 +1656,35 @@ func TestGeoValidWkbData(t *testing.T) {

var addr string

// the grootAccessJWT stores the access JWT extracted from the response
// of http login
var token *testutil.HttpToken
type Token struct {
token *testutil.HttpToken
sync.RWMutex
}

//// the grootAccessJWT stores the access JWT extracted from the response
//// of http login
var token *Token

func (t *Token) getAccessJWTToken() string {
t.RLock()
defer t.RUnlock()
return t.token.AccessJwt
}

func (t *Token) refreshToken() error {
t.Lock()
defer t.Unlock()
newToken, err := testutil.HttpLogin(&testutil.LoginParams{
Endpoint: addr + "/admin",
RefreshJwt: t.token.RefreshToken,
})
if err != nil {
return err
}
t.token.AccessJwt = newToken.AccessJwt
t.token.RefreshToken = newToken.RefreshToken
return nil
}

func TestMain(m *testing.M) {
addr = "http://" + testutil.SockAddrHttp
Expand All @@ -1670,8 +1697,11 @@ func TestMain(m *testing.M) {
if _, err := zc.AssignUids(context.Background(), &pb.Num{Val: 1e6}); err != nil {
log.Fatal(err)
}
token = testutil.GrootHttpLogin(addr + "/admin")

httpToken := testutil.GrootHttpLogin(addr + "/admin")
token = &Token{
token: httpToken,
RWMutex: sync.RWMutex{},
}
r := m.Run()
os.Exit(r)
}
6 changes: 3 additions & 3 deletions edgraph/access_ee.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (s *Server) authenticateLogin(ctx context.Context, request *api.LoginReques
}

if user == nil {
return nil, errors.Errorf("unable to authenticate through refresh token: "+
return nil, errors.Errorf("unable to authenticate: "+
"invalid username or password")
}

Expand All @@ -154,8 +154,8 @@ func (s *Server) authenticateLogin(ctx context.Context, request *api.LoginReques
}

if user == nil {
return nil, errors.Errorf("unable to authenticate through password: "+
"invalid username or passowrd")
return nil, errors.Errorf("unable to authenticate: "+
"invalid username or password")
}
if !user.PasswordMatch {
return nil, x.ErrorInvalidLogin
Expand Down
8 changes: 8 additions & 0 deletions netlify.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@
HUGO_VERSION = "0.74.3"
LOOP = "false"

[context.deploy-preview]
command = "./scripts/local.sh --preview $DEPLOY_PRIME_URL"

[context.deploy-preview.environment]
HUGO_VERSION = "0.74.3"
LOOP = "false"
HOST = "/"

[context.branch-deploy.environment]
HUGO_VERSION = "0.74.3"
LOOP = "false"
HOST = "/"
2 changes: 1 addition & 1 deletion systest/1million/1million_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9271,7 +9271,7 @@ func Test1Million(t *testing.T) {
}

for _, tt := range tc {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
resp, err := dg.NewTxn().Query(ctx, tt.query)
cancel()

Expand Down
2 changes: 1 addition & 1 deletion systest/21million/common/run_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestQueriesFor21Million(t *testing.T) {
for retry := 0; retry < 3; retry++ {
// If a query takes too long to run, it probably means dgraph is stuck and there's
// no point in waiting longer or trying more tests.
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
resp, err := dg.NewTxn().Query(ctx, bodies[0])
cancel()

Expand Down
10 changes: 5 additions & 5 deletions systest/mutations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1715,7 +1715,7 @@ func CountIndexConcurrentSetDelUIDList(t *testing.T, c *dgo.Dgraph) {
err := c.Alter(ctx, op)
require.NoError(t, err)

r := rand.New(rand.NewSource(time.Now().Unix()))
rand.Seed(time.Now().Unix())
maxUID := 100
txnTotal := uint64(1000)
txnCur := uint64(0)
Expand All @@ -1733,7 +1733,7 @@ func CountIndexConcurrentSetDelUIDList(t *testing.T, c *dgo.Dgraph) {
if atomic.AddUint64(&txnCur, 1) > txnTotal {
break
}
id := 2 + int(r.Int31n(int32(maxUID))) // 1 id subject id.
id := 2 + int(rand.Int31n(int32(maxUID))) // 1 id subject id.
mu := &api.Mutation{
CommitNow: true,
}
Expand Down Expand Up @@ -1784,7 +1784,7 @@ func CountIndexConcurrentSetDelUIDList(t *testing.T, c *dgo.Dgraph) {
if atomic.AddUint64(&txnCur, 1) > txnTotal {
break
}
id := insertedUids[r.Intn(len(insertedUids))]
id := insertedUids[rand.Intn(len(insertedUids))]
mu := &api.Mutation{
CommitNow: true,
}
Expand Down Expand Up @@ -1836,7 +1836,7 @@ func CountIndexConcurrentSetDelScalarPredicate(t *testing.T, c *dgo.Dgraph) {
err := c.Alter(ctx, op)
require.NoError(t, err)

r := rand.New(rand.NewSource(time.Now().Unix()))
rand.Seed(time.Now().Unix())
txnTotal := uint64(100)
txnCur := uint64(0)

Expand All @@ -1850,7 +1850,7 @@ func CountIndexConcurrentSetDelScalarPredicate(t *testing.T, c *dgo.Dgraph) {
if atomic.AddUint64(&txnCur, 1) > txnTotal {
break
}
id := int(r.Int31n(int32(10000)))
id := int(rand.Int31n(int32(10000)))
mu := &api.Mutation{
CommitNow: true,
}
Expand Down
Loading

0 comments on commit 3864368

Please sign in to comment.