Exported schema isn't consistent


Report a Dgraph Bug

What version of Dgraph are you using?

Master on https://github.com/dgraph-io/dgraph/commit/3e3a53f532d453d9b9479faca51220bda599a0d2

Have you tried reproducing the issue with the latest release?

Yes

Steps to reproduce the issue (command/config used to run Dgraph).

Run the TestExportSchemaMinio test multiple times.

 ~/Projects/…/…/…/dgraph-io/dgraph   master ⍟5  which dra                                                                                                                               
dra: aliased to docker rm -v -f $(docker ps -aq)
 ~/Projects/…/…/…/dgraph-io/dgraph   master ⍟5  which m  
m: aliased to make install
 ~/Projects/…/…/…/dgraph-io/dgraph   master ⍟5  which gtf
gtf: aliased to go test -v -failfast
 ~/Projects/…/…/…/dgraph-io/dgraph   master ⍟5  while dra || m && docker-compose -f systest/export/docker-compose.yml up -d && sleep 5 && gtf -run Minio ./systest/export; do : ; done 
5889bc44dfb7
a046b47e06a7
a4f3d5e0a0d7
99b95a709623
d441289a7032
Creating minio1 ... done
Creating alpha1 ... done
Creating zero1  ... done
Creating alpha2 ... done
Creating alpha3 ... done
[Decoder]: Using assembly version of decoder
=== RUN   TestExportSchemaToMinio
--- PASS: TestExportSchemaToMinio (0.31s)
PASS
ok  	github.com/dgraph-io/dgraph/systest/export	0.319s
d238d8d837e2
1f795168bf29
2e7e071c8f62
d7767938af67
83e9dde7a2f7
Creating minio1 ... done
Creating zero1  ... done
Creating alpha1 ... done
Creating alpha2 ... done
Creating alpha3 ... done
[Decoder]: Using assembly version of decoder
=== RUN   TestExportSchemaToMinio
    export_test.go:76: 
        	Error Trace:	export_test.go:76
        	Error:      	Not equal: 
        	            	expected: "<movie>:string . \n<dgraph.type>:[string] @index(exact) . \n<dgraph.graphql.xid>:string @index(exact) @upsert . \n<dgraph.graphql.schema>:string . \ntype Node {\n\tmovie\n}\ntype dgraph.graphql {\n\tdgraph.graphql.schema\n\tdgraph.graphql.xid\n}\n"
        	            	actual  : "<movie>:string . \ntype Node {\n\tmovie\n}\ntype dgraph.graphql {\n\tdgraph.graphql.schema\n\tdgraph.graphql.xid\n}\n"
        	            	
        	            	Diff:
        	            	--- Expected
        	            	+++ Actual
        	            	@@ -1,5 +1,2 @@
        	            	 <movie>:string . 
        	            	-<dgraph.type>:[string] @index(exact) . 
        	            	-<dgraph.graphql.xid>:string @index(exact) @upsert . 
        	            	-<dgraph.graphql.schema>:string . 
        	            	 type Node {
        	Test:       	TestExportSchemaToMinio
--- FAIL: TestExportSchemaToMinio (0.30s)
FAIL
FAIL	github.com/dgraph-io/dgraph/systest/export	0.316s
FAIL

Expected behaviour and actual result.

The TestExportSchemaToMinio test fails intermittently. It is not the issue with the test. The schema generated by export is not consistent (I verified this by doing a admin/export and checking the generated schema file). Sometimes it does not add the dgraph.graphql.xid predicate to the schema

This is not an issue with export itself since the test started three Alpha nodes which were forming different groups. We only export the data and schema belonging to the group being served by the Alpha. Here the data was being sharded so the test was flaky. I have fixed it by passing --replicas as 3 to Zero.

One group contains the complete schema while other groups know only about the predicates they serve.

$ docker exec -it alpha1 bash
root@60814001a3ab:/data/alpha1/export# zcat dgraph.r18.u0727.1126/g03.schema.gz 
<movie>:string . 
type Node {
	movie
}
type dgraph.graphql {
	dgraph.graphql.schema
	dgraph.graphql.xid
}
 
$ docker exec -it alpha2 bash
root@f011a32ec16e:/data/alpha2# zcat export/dgraph.r18.u0727.1127/g01.schema.gz 
<dgraph.type>:[string] @index(exact) . 
<dgraph.graphql.xid>:string @index(exact) @upsert . 
<dgraph.graphql.schema>:string . 
type Node {
	movie
}
type dgraph.graphql {
	dgraph.graphql.schema
	dgraph.graphql.xid
}

$ docker exec -it alpha3 bash
root@f494310d122a:/data/alpha3# zcat export/dgraph.r18.u0727.1127/g02.schema.gz 
type Node {
	movie
}
type dgraph.graphql {
	dgraph.graphql.schema
	dgraph.graphql.xid
}

Trying to import the exported schema from alpha1 or alpha3 results in the following error because the predicates for the types are missing

{"errors":[{"message":"Schema does not contain a matching predicate for field movie in type Node","extensions":{"code":"Error"}}]}

We should be checking for existence of predicates while applying schema across all the groups.

I’ll look at the code and check if we do this right now. If not, I’ll send a PR for this.