github.com/dshekhar95/sub_dgraph@v0.0.0-20230424164411-6be28e40bbf1/dgraph/cmd/bulk/systest/test-bulk-schema.sh (about) 1 #!/bin/bash 2 # verify fix of https://github.com/dgraph-io/dgraph/issues/2616 3 4 readonly ME=${0##*/} 5 readonly SRCROOT=$(git rev-parse --show-toplevel) 6 readonly DOCKER_CONF=$SRCROOT/dgraph/cmd/bulk/systest/docker-compose.yml 7 8 declare -ri ZERO_PORT=5180 HTTP_PORT=8180 9 10 INFO() { echo "$ME: $@"; } 11 ERROR() { echo >&2 "$ME: $@"; } 12 FATAL() { ERROR "$@"; exit 1; } 13 14 function DockerCompose { 15 docker-compose -p dgraph "$@" 16 } 17 18 set -e 19 20 INFO "rebuilding dgraph" 21 22 cd $SRCROOT 23 make install >/dev/null 24 25 INFO "running bulk load schema test" 26 27 WORKDIR=$(mktemp --tmpdir -d $ME.tmp-XXXXXX) 28 INFO "using workdir $WORKDIR" 29 cd $WORKDIR 30 31 LOGFILE=$WORKDIR/output.log 32 33 trap ErrorExit EXIT 34 function ErrorExit 35 { 36 local ev=$? 37 if [[ $ev -ne 0 ]]; then 38 ERROR "*** unexpected error ***" 39 if [[ -e $LOGFILE ]]; then 40 tail -40 $LOGFILE 41 fi 42 fi 43 if [[ ! $DEBUG ]]; then 44 rm -rf $WORKDIR 45 fi 46 exit $ev 47 } 48 49 function StartZero 50 { 51 INFO "starting zero container" 52 DockerCompose -f $DOCKER_CONF up --force-recreate --remove-orphans -d zero1 53 TIMEOUT=10 54 while [[ $TIMEOUT > 0 ]]; do 55 if docker logs zero1 2>&1 | grep -q 'CID set'; then 56 return 57 else 58 TIMEOUT=$((TIMEOUT - 1)) 59 sleep 1 60 fi 61 done 62 FATAL "failed to start zero" 63 } 64 65 function StartAlpha 66 { 67 local p_dir=$1 68 69 INFO "starting alpha container" 70 DockerCompose -f $DOCKER_CONF up --force-recreate --remove-orphans --no-start alpha1 71 if [[ $p_dir ]]; then 72 docker cp $p_dir alpha1:/data/alpha1/ 73 fi 74 DockerCompose -f $DOCKER_CONF up -d --remove-orphans alpha1 75 76 TIMEOUT=10 77 while [[ $TIMEOUT > 0 ]]; do 78 if docker logs alpha1 2>&1 | grep -q 'Got Zero leader'; then 79 return 80 else 81 TIMEOUT=$((TIMEOUT - 1)) 82 sleep 1 83 fi 84 done 85 FATAL "failed to start alpha" 86 } 87 88 function ResetCluster 89 { 90 INFO "restarting cluster with only one zero and alpha" 91 DockerCompose -f $DOCKER_CONF down --remove-orphans 92 StartZero 93 StartAlpha 94 } 95 96 function UpdateDatabase 97 { 98 INFO "adding predicate with default type to schema" 99 curl localhost:$HTTP_PORT/alter -X POST -d$' 100 predicate_with_no_uid_count:string . 101 predicate_with_default_type:default . 102 predicate_with_index_no_uid_count:string @index(exact) . 103 ' &>/dev/null 104 105 # Wait for background indexing to finish. 106 # TODO: Use better way of waiting once it's available. 107 sleep 5 108 109 curl -H "Content-Type: application/rdf" localhost:$HTTP_PORT/mutate?commitNow=true -X POST -d $' 110 { 111 set { 112 _:company1 <predicate_with_default_type> "CompanyABC" . 113 } 114 } 115 ' &>/dev/null 116 } 117 118 function QuerySchema 119 { 120 INFO "running schema query" 121 local out_file="schema.out" 122 curl -sS -H "Content-Type: application/dql" localhost:$HTTP_PORT/query -XPOST -d'schema(pred:[genre,language,name,revenue,predicate_with_default_type,predicate_with_index_no_uid_count,predicate_with_no_uid_count]) {}' | python3 -c "import json,sys; d=json.load(sys.stdin); json.dump(d['data'],sys.stdout,sort_keys=True,indent=2)" > $out_file 123 echo >> $out_file 124 } 125 126 function DoExport 127 { 128 INFO "running export" 129 docker exec alpha1 curl -Ss -H "Content-Type: application/json" localhost:$HTTP_PORT/admin -XPOST -d '{ "query": "mutation { export(input: {format: \"rdf\"}) { response { code message } }}" }' &>/dev/null 130 sleep 2 131 docker cp alpha1:/data/alpha1/export . 132 sleep 1 133 } 134 135 function BulkLoadExportedData 136 { 137 INFO "bulk loading exported data" 138 # using a random HTTP port for pprof to avoid collisions with other processes 139 HTTPPORT=$(( ( RANDOM % 1000 ) + 8080 )) 140 dgraph bulk -z localhost:$ZERO_PORT --http "localhost:$HTTPPORT"\ 141 -s ../dir1/export/*/g01.schema.gz \ 142 -f ../dir1/export/*/g01.rdf.gz \ 143 >$LOGFILE 2>&1 </dev/null 144 mv $LOGFILE $LOGFILE.export 145 } 146 147 function BulkLoadFixtureData 148 { 149 INFO "bulk loading fixture data" 150 151 # schema test cases: 152 # 153 # 1. predicate with non-default type (name) 154 # 2. predicate with default type (genre) 155 # 3. predicate not used in rdf (language) 156 cat >fixture.schema <<EOF 157 name:string @index(term) . 158 genre:default . 159 language:string . 160 EOF 161 162 # rdf test cases: 163 # 164 # 4. predicate not in schema (revenue) 165 cat >fixture.rdf <<EOF 166 _:et <name> "E.T. the Extra-Terrestrial" . 167 _:et <genre> "Science Fiction" . 168 _:et <revenue> "792.9" . 169 EOF 170 171 dgraph bulk -z localhost:$ZERO_PORT -s fixture.schema -f fixture.rdf \ 172 >$LOGFILE 2>&1 </dev/null 173 mv $LOGFILE $LOGFILE.fixture 174 } 175 176 function TestBulkLoadMultiShard 177 { 178 INFO "bulk loading into multiple shards" 179 180 cat >fixture.schema <<EOF 181 name:string @index(term) . 182 genre:default . 183 language:string . 184 EOF 185 186 cat >fixture.rdf <<EOF 187 _:et <name> "E.T. the Extra-Terrestrial" . 188 _:et <genre> "Science Fiction" . 189 _:et <revenue> "792.9" . 190 EOF 191 192 dgraph bulk -z localhost:$ZERO_PORT -s fixture.schema -f fixture.rdf \ 193 --map_shards 2 --reduce_shards 2 \ 194 >$LOGFILE 2>&1 </dev/null 195 mv $LOGFILE $LOGFILE.multi 196 197 INFO "checking that each predicate appears in only one shard" 198 199 dgraph debug -p out/0/p 2>|/dev/null | grep '{s}' | cut -d' ' -f3 > all_dbs.out 200 dgraph debug -p out/1/p 2>|/dev/null | grep '{s}' | cut -d' ' -f3 >> all_dbs.out 201 diff <(LC_ALL=C sort all_dbs.out | uniq -c) - <<EOF 202 1 dgraph.acl.rule 203 1 dgraph.cors 204 1 dgraph.drop.op 205 1 dgraph.graphql.p_query 206 1 dgraph.graphql.schema 207 1 dgraph.graphql.schema_created_at 208 1 dgraph.graphql.schema_history 209 1 dgraph.graphql.xid 210 1 dgraph.password 211 1 dgraph.rule.permission 212 1 dgraph.rule.predicate 213 1 dgraph.type 214 1 dgraph.user.group 215 1 dgraph.xid 216 1 genre 217 1 language 218 1 name 219 1 revenue 220 EOF 221 } 222 223 function StopServers 224 { 225 INFO "stopping containers" 226 DockerCompose -f $DOCKER_CONF down --remove-orphans 227 } 228 229 function Cleanup 230 { 231 INFO "removing $WORKDIR" 232 rm -rf $WORKDIR 233 } 234 235 mkdir dir1 236 pushd dir1 >/dev/null 237 238 ResetCluster 239 UpdateDatabase 240 QuerySchema 241 DoExport 242 StopServers 243 popd >/dev/null 244 mkdir dir2 245 pushd dir2 >/dev/null 246 247 StartZero 248 BulkLoadExportedData 249 StartAlpha "./out/0/p" 250 sleep 5 251 QuerySchema 252 StopServers 253 254 popd >/dev/null 255 256 INFO "verifying schema is same before export and after bulk import" 257 diff -b dir1/schema.out dir2/schema.out || FATAL "schema incorrect" 258 INFO "schema is correct" 259 260 mkdir dir3 261 pushd dir3 >/dev/null 262 263 StartZero 264 BulkLoadFixtureData 265 StartAlpha "./out/0/p" 266 sleep 5 267 QuerySchema 268 StopServers 269 270 popd >/dev/null 271 272 # final schema should include *all* predicates regardless of whether they were 273 # introduced by the schema or rdf file, used or not used, or of default type 274 # or non-default type 275 INFO "verifying schema contains all predicates" 276 diff -b - dir3/schema.out <<EOF || FATAL "schema incorrect" 277 { 278 "schema": [ 279 { 280 "predicate": "genre", 281 "type": "default" 282 }, 283 { 284 "predicate": "language", 285 "type": "string" 286 }, 287 { 288 "index": true, 289 "predicate": "name", 290 "tokenizer": [ 291 "term" 292 ], 293 "type": "string" 294 }, 295 { 296 "predicate": "revenue", 297 "type": "default" 298 } 299 ] 300 } 301 EOF 302 303 StartZero 304 TestBulkLoadMultiShard 305 StopServers 306 307 INFO "schema is correct" 308 309 Cleanup 310 311 exit 0 312 313 # eof