Skip to content

Instantly share code, notes, and snippets.

@jcoglan
Created April 10, 2024 11:21
Show Gist options
  • Save jcoglan/0a5feb4af2a496ce10c9b80cf02ea28f to your computer and use it in GitHub Desktop.
Save jcoglan/0a5feb4af2a496ce10c9b80cf02ea28f to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
CDB_PORT=15984
DB=test
cdb () {
curl -gsu 'admin:admin' \
-H 'Content-Type: application/json' \
"http://127.0.0.1:${CDB_PORT}$1" "${@:2}"
}
start-container () {
local name="$1"
local version="$2"
docker run -d --name "$name" \
-e COUCHDB_USER=admin \
-e COUCHDB_PASSWORD=admin \
-p "${CDB_PORT}:5984" \
-v ./data:/opt/couchdb/data \
"couchdb:$version"
until cdb '/' ; do sleep 1 ; done
}
stop-container () {
local name="$1"
docker rm -f "$name"
}
show-files () {
wait-for-fsync
find data/.shards -name '*.view' | sort
hexdump -C data/.shards/00000000-1fffffff/$DB.*_design/mrview/*.view
}
wait-for-fsync () {
sleep 5
}
wait-for-ken () {
sleep 60
}
rm -rf data
# start CouchDB 2.3.1
start-container couchdb-2 2.3.1
# create _users to remove some log noise
cdb '/_users' -X PUT
# create a database and basic design document
cdb "/$DB" -X PUT
cdb "/$DB/_design/foo" -X PUT -d '{ "views": { "by-name": { "map": "function(doc) { emit(doc.name) }" } } }'
# query the view and then display the resulting index files; we get files named
# f34d2db313bbab4c286ae4a154bcec19.view and they contain the right signature:
#
# 00000000 01 00 00 00 2e be ad 2f 7d 2a 63 7b d6 4b b2 5c |......./}*c{.K.\|
# 00000010 95 63 10 e4 b0 83 68 02 6d 00 00 00 10 f3 4d 2d |.c....h.m.....M-|
# 00000020 b3 13 bb ab 4c 28 6a e4 a1 54 bc ec 19 64 00 03 |....L(j..T...d..|
# 00000030 6e 69 6c |nil|
# 00000033
#
cdb "/$DB/_design/foo/_view/by-name"
show-files
# stop CouchDB 2.3.1 and start 3.3.3
stop-container couchdb-2
start-container couchdb-3 3.3.3
# query the view again; after this the files have been renamed to
# 9011a47c3298f1463c596ade42ba7e35.view but still contain the old signature
#
# 00000000 01 00 00 00 2e be ad 2f 7d 2a 63 7b d6 4b b2 5c |......./}*c{.K.\|
# 00000010 95 63 10 e4 b0 83 68 02 6d 00 00 00 10 f3 4d 2d |.c....h.m.....M-|
# 00000020 b3 13 bb ab 4c 28 6a e4 a1 54 bc ec 19 64 00 03 |....L(j..T...d..|
# 00000030 6e 69 6c |nil|
# 00000033
#
cdb "/$DB/_design/foo/_view/by-name"
show-files
# wait for a cycle of ken background indexing and then create a doc
wait-for-ken
cdb "/$DB/trigger-bug" -X PUT -d '{}'
# check the logs for the 'wrong signature' event, which should appear
sleep 10
echo '==> checking logs...'
docker logs couchdb-3 2>&1 | grep 'wrong signature'
echo '==> done.'
# although the 'wrong signature' event resets the index state, the empty index
# shard still contains the old signature because it's not had any new data
# written to it
show-files
# querying the view forces the index to be regenerated and produces the right
# signature inside the file
#
# 00000000 01 00 00 00 2e 12 2b 5f 8a 33 72 c0 36 e1 e3 de |......+_.3r.6...|
# 00000010 73 1d 44 4f e2 83 68 02 6d 00 00 00 10 90 11 a4 |s.DO..h.m.......|
# 00000020 7c 32 98 f1 46 3c 59 6a de 42 ba 7e 35 64 00 03 ||2..F<Yj.B.~5d..|
# 00000030 6e 69 6c
# 00000033
#
cdb "/$DB/_design/foo/_view/by-name"
show-files
stop-container couchdb-3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment