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

fix(live): fix usage of force-namespace parameter in live loader #7526

Merged
merged 1 commit into from
Mar 8, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
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