Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tablet move and group removal #2880

Merged
merged 7 commits into from
Jan 15, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions dgraph/cmd/zero/zero.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,10 @@ func (s *Server) removeNode(ctx context.Context, nodeId uint64, groupId uint32)
if _, ok := s.state.Groups[groupId].Members[nodeId]; !ok {
return x.Errorf("No node with nodeId %d found in group %d", nodeId, groupId)
}
if len(s.state.Groups[groupId].Members) == 1 && len(s.state.Groups[groupId].Tablets) > 0 {
return x.Errorf("Move all tablets from group %d before removing the last node", groupId)
}

return s.Node.proposeAndWait(ctx, zp)
}

Expand Down
2 changes: 1 addition & 1 deletion ee/backup/systest/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func BackupSetup(t *testing.T, c *dgo.Dgraph) {
require.NoError(t, err)
require.NoError(t, c.Alter(ctx, &api.Operation{Schema: string(schema)}))

fp, err := os.Open(`data/goldendata_export.rdf.gz`)
fp, err := os.Open(`../../../systest/data/goldendata_export.rdf.gz`)
x.Check(err)
defer fp.Close()

Expand Down
251 changes: 251 additions & 0 deletions systest/nodes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
/*
* Copyright 2018 Dgraph Labs, Inc. and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package main

import (
"bufio"
"bytes"
"compress/gzip"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"testing"
"time"

"github.com/dgraph-io/dgo"
"github.com/dgraph-io/dgo/protos/api"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
)

func TestNodes(t *testing.T) {
wrap := func(fn func(*testing.T, *dgo.Dgraph)) func(*testing.T) {
return func(t *testing.T) {
conn, err := grpc.Dial("localhost:9180", grpc.WithInsecure())
require.NoError(t, err)
dg := dgo.NewDgraphClient(api.NewDgraphClient(conn))
fn(t, dg)
}
}

tests := []struct {
name string
fn func(*testing.T, *dgo.Dgraph)
}{
{name: "setup test data", fn: NodesSetup},
{name: "move tablets from 3", fn: NodesMoveTablets3},
{name: "test query 1", fn: NodesTestQuery},
{name: "move tablets from 2", fn: NodesMoveTablets2},
{name: "test query 2", fn: NodesTestQuery},
}
for _, tc := range tests {
if !t.Run(tc.name, wrap(tc.fn)) {
break
}
}
t.Run("cleanup", wrap(NodesCleanup))
}

func NodesSetup(t *testing.T, c *dgo.Dgraph) {
ctx := context.Background()

require.NoError(t, c.Alter(ctx, &api.Operation{DropAll: true}))

schema, err := ioutil.ReadFile(`data/goldendata.schema`)
require.NoError(t, err)
require.NoError(t, c.Alter(ctx, &api.Operation{Schema: string(schema)}))

fp, err := os.Open(`data/goldendata_export.rdf.gz`)
require.NoError(t, err)
defer fp.Close()

gz, err := gzip.NewReader(fp)
require.NoError(t, err)
defer gz.Close()

var (
cnt int
bb bytes.Buffer
)

reader := bufio.NewReader(gz)
for {
b, err := reader.ReadBytes('\n')
if err != nil {
if err == io.EOF {
break
}
require.NoError(t, err)
}
bb.Write(b)
cnt++
if cnt%100 == 0 {
_, err = c.NewTxn().Mutate(ctx, &api.Mutation{
CommitNow: true,
SetNquads: bb.Bytes(),
})
require.NoError(t, err)
bb.Reset()
}
}
}

func NodesCleanup(t *testing.T, c *dgo.Dgraph) {
require.NoError(t, c.Alter(context.Background(), &api.Operation{DropAll: true}))
}

type response struct {
Groups map[string]struct {
Members map[string]interface{} `json:"members"`
Tablets map[string]struct {
GroupID int `json:"groupId"`
Predicate string `json:"predicate"`
} `json:"tablets"`
} `json:"groups"`
}

func getState() (*response, error) {
resp, err := http.Get("http://localhost:6080/state")
if err != nil {
return nil, err
}
defer resp.Body.Close()

b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}

if bytes.Contains(b, []byte("Error")) {
return nil, fmt.Errorf("Failed to get state: %s", string(b))
}

var st response
if err := json.Unmarshal(b, &st); err != nil {
return nil, err
}
return &st, nil
}

func getError(rc io.ReadCloser) error {
defer rc.Close()
b, err := ioutil.ReadAll(rc)
if err != nil {
return fmt.Errorf("Read failed: %v", err)
}
if bytes.Contains(b, []byte("Error")) {
return fmt.Errorf("%s", string(b))
}
return nil
}

func NodesMoveTablets3(t *testing.T, c *dgo.Dgraph) {
state1, err := getState()
require.NoError(t, err)

for pred := range state1.Groups["3"].Tablets {
url := fmt.Sprintf("http://localhost:6080/moveTablet?tablet=%s&group=2", pred)
resp, err := http.Get(url)
require.NoError(t, err)
require.NoError(t, getError(resp.Body))
time.Sleep(time.Second)
}

state2, err := getState()
require.NoError(t, err)

if len(state2.Groups["3"].Tablets) > 0 {
t.Errorf("moving tablets failed")
}

resp, err := http.Get("http://localhost:6080/removeNode?group=3&id=3")
require.NoError(t, err)
require.NoError(t, getError(resp.Body))

state2, err = getState()
require.NoError(t, err)

if _, ok := state2.Groups["3"]; ok {
t.Errorf("node removal failed")
}
}

func NodesMoveTablets2(t *testing.T, c *dgo.Dgraph) {
state1, err := getState()
require.NoError(t, err)

for pred := range state1.Groups["2"].Tablets {
url := fmt.Sprintf("http://localhost:6080/moveTablet?tablet=%s&group=1", pred)
resp, err := http.Get(url)
require.NoError(t, err)
require.NoError(t, getError(resp.Body))
time.Sleep(time.Second)
}

state2, err := getState()
require.NoError(t, err)

if len(state2.Groups["2"].Tablets) > 0 {
t.Errorf("moving tablets failed")
}

resp, err := http.Get("http://localhost:6080/removeNode?group=2&id=2")
require.NoError(t, err)
require.NoError(t, getError(resp.Body))

state2, err = getState()
require.NoError(t, err)

if _, ok := state2.Groups["2"]; ok {
t.Errorf("node removal failed")
}
}

func NodesTestQuery(t *testing.T, c *dgo.Dgraph) {
resp, err := c.NewTxn().Query(context.Background(), `
{
q(func:anyofterms(name@en, "good bad"), first: -5) {
name@en
}
}`)
require.NoError(t, err)

CompareJSON(t, `
{
"q": [
{
"name@en": "Good Grief"
},
{
"name@en": "Half Good Killer"
},
{
"name@en": "Bad Friend"
},
{
"name@en": "Ace of Spades: Bad Destiny"
},
{
"name@en": "Bad Girls 6"
}
]
}`, string(resp.GetJson()))
}