Skip to content

Commit

Permalink
fix(live): fix usage of force-namespace parameter in export (#7526)
Browse files Browse the repository at this point in the history
This PR fixes the behaviour of --force-namespace flag in live loader.
Earlier, we used to silently ignore this flag when passed by the non-galaxy user. Now, we throw an error if the force-namespace flag is used by the non-galaxy user.
  • Loading branch information
NamanJain8 authored and aman-bansal committed Mar 9, 2021
1 parent aab3598 commit 5629ca1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
14 changes: 8 additions & 6 deletions dgraph/cmd/live/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,8 @@ func init() {
"be used to store blank nodes as an xid")
flag.String("tmp", "t", "Directory to store temporary buffers.")
flag.Int64("force-namespace", 0, "Namespace onto which to load the data."+
"This flag will be ignored when not logging into galaxy namespace."+
"Only guardian of galaxy should use this for loading data into multiple namespaces."+
"Setting it to negative value will preserve the namespace.")
"Only guardian of galaxy should use this for loading data into multiple namespaces or some"+
"specific namespace. Setting it to negative value will preserve the namespace.")
}

func getSchema(ctx context.Context, dgraphClient *dgo.Dgraph, ns uint64) (*schema, error) {
Expand Down Expand Up @@ -714,16 +713,19 @@ func run() error {
tmpDir: Live.Conf.GetString("tmp"),
}

forceNs := Live.Conf.GetInt64("force-namespace")
switch creds.GetUint64("namespace") {
case x.GalaxyNamespace:
ns := Live.Conf.GetInt64("force-namespace")
if ns < 0 {
if forceNs < 0 {
opt.preserveNs = true
opt.namespaceToLoad = math.MaxUint64
} else {
opt.namespaceToLoad = uint64(ns)
opt.namespaceToLoad = uint64(forceNs)
}
default:
if Live.Conf.IsSet("force-namespace") && uint64(forceNs) != creds.GetUint64("namespace") {
return errors.Errorf("cannot load into namespace %#x", forceNs)
}
opt.namespaceToLoad = creds.GetUint64("namespace")
}

Expand Down
43 changes: 30 additions & 13 deletions systest/multi-tenancy/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,7 @@ func TestLiveLoadMulti(t *testing.T) {
schema: `
name: string @index(term) .
`,
creds: &testutil.LoginParams{UserID: "groot", Passwd: "password",
Namespace: x.GalaxyNamespace},
creds: galaxyCreds,
forceNs: int64(ns),
}))

Expand All @@ -301,26 +300,46 @@ func TestLiveLoadMulti(t *testing.T) {

// Try loading into a multiple namespaces.
err = liveLoadData(t, &liveOpts{
rdfs: fmt.Sprintf(`_:c <name> "ns eon" <%#x> .`, ns),
schema: `[0x123456] name: string @index(term) .`,
creds: &testutil.LoginParams{UserID: "groot", Passwd: "password",
Namespace: x.GalaxyNamespace},
rdfs: fmt.Sprintf(`_:c <name> "ns eon" <%#x> .`, ns),
schema: `[0x123456] name: string @index(term) .`,
creds: galaxyCreds,
forceNs: -1,
})
require.Error(t, err)
require.Contains(t, err.Error(), "Namespace 0x123456 doesn't exist for pred")

err = liveLoadData(t, &liveOpts{
rdfs: fmt.Sprintf(`_:c <name> "ns eon" <0x123456> .`),
schema: `name: string @index(term) .`,
creds: &testutil.LoginParams{UserID: "groot", Passwd: "password",
Namespace: x.GalaxyNamespace},
rdfs: fmt.Sprintf(`_:c <name> "ns eon" <0x123456> .`),
schema: `name: string @index(term) .`,
creds: galaxyCreds,
forceNs: -1,
})
require.Error(t, err)
require.Contains(t, err.Error(), "Cannot load nquad")

// Load data by non-galaxy user.
err = liveLoadData(t, &liveOpts{
rdfs: `_:c <name> "ns hola" .`,
schema: `
name: string @index(term) .
`,
creds: &testutil.LoginParams{UserID: "groot", Passwd: "password", Namespace: ns},
forceNs: -1,
})
require.Error(t, err)
require.Contains(t, err.Error(), "cannot load into namespace")

err = liveLoadData(t, &liveOpts{
rdfs: `_:c <name> "ns hola" .`,
schema: `
name: string @index(term) .
`,
creds: &testutil.LoginParams{UserID: "groot", Passwd: "password", Namespace: ns},
forceNs: 10,
})
require.Error(t, err)
require.Contains(t, err.Error(), "cannot load into namespace")

require.NoError(t, liveLoadData(t, &liveOpts{
rdfs: fmt.Sprintf(`
_:a <name> "ns free" .
Expand All @@ -330,10 +349,8 @@ func TestLiveLoadMulti(t *testing.T) {
schema: `
name: string @index(term) .
`,
creds: &testutil.LoginParams{UserID: "groot", Passwd: "password", Namespace: ns},
forceNs: -1, // this will be ignored.
creds: &testutil.LoginParams{UserID: "groot", Passwd: "password", Namespace: ns},
}))

resp = testutil.QueryData(t, dc1, query3)
testutil.CompareJSON(t,
`{"me": [{"name":"ns alice"}, {"name": "ns bob"},{"name":"ns chew"},
Expand Down
4 changes: 3 additions & 1 deletion testutil/bulk.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ func LiveLoad(opts LiveOpts) error {
"--schema", opts.SchemaFile,
"--alpha", opts.Alpha,
"--zero", opts.Zero,
"--force-namespace", strconv.FormatInt(opts.ForceNs, 10),
}
if opts.ForceNs != 0 {
args = append(args, "--force-namespace", strconv.FormatInt(opts.ForceNs, 10))
}
if opts.Ludicrous {
args = append(args, "--ludicrous")
Expand Down

0 comments on commit 5629ca1

Please sign in to comment.