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