3 # rbd_mirror_helpers.sh - shared rbd-mirror daemon helper functions
5 # The scripts starts two ("local" and "remote") clusters using mstart.sh script,
6 # creates a temporary directory, used for cluster configs, daemon logs, admin
7 # socket, temporary files, and launches rbd-mirror daemon.
9 # There are several env variables useful when troubleshooting a test failure:
11 # RBD_MIRROR_NOCLEANUP - if not empty, don't run the cleanup (stop processes,
12 # destroy the clusters and remove the temp directory)
13 # on exit, so it is possible to check the test state
15 # RBD_MIRROR_TEMDIR - use this path when creating the temporary directory
16 # (should not exist) instead of running mktemp(1).
17 # RBD_MIRROR_ARGS - use this to pass additional arguments to started
19 # RBD_MIRROR_VARGS - use this to pass additional arguments to vstart.sh
20 # when starting clusters.
22 # The cleanup can be done as a separate step, running the script with
23 # `cleanup ${RBD_MIRROR_TEMDIR}' arguments.
25 # Note, as other workunits tests, rbd_mirror.sh expects to find ceph binaries
28 # Thus a typical troubleshooting session:
30 # From Ceph src dir (CEPH_SRC_PATH), start the test in NOCLEANUP mode and with
31 # TEMPDIR pointing to a known location:
34 # PATH=$CEPH_SRC_PATH:$PATH
35 # RBD_MIRROR_NOCLEANUP=1 RBD_MIRROR_TEMDIR=/tmp/tmp.rbd_mirror \
36 # ../qa/workunits/rbd/rbd_mirror.sh
38 # After the test failure cd to TEMPDIR and check the current state:
40 # cd /tmp/tmp.rbd_mirror
42 # less rbd-mirror.cluster1_daemon.$pid.log
43 # ceph --cluster cluster1 -s
44 # ceph --cluster cluster1 -s
45 # rbd --cluster cluster2 -p mirror ls
46 # rbd --cluster cluster2 -p mirror journal status --image test
47 # ceph --admin-daemon rbd-mirror.cluster1_daemon.cluster1.$pid.asok help
50 # Also you can execute commands (functions) from the script:
53 # export RBD_MIRROR_TEMDIR=/tmp/tmp.rbd_mirror
54 # ../qa/workunits/rbd/rbd_mirror.sh status
55 # ../qa/workunits/rbd/rbd_mirror.sh stop_mirror cluster1
56 # ../qa/workunits/rbd/rbd_mirror.sh start_mirror cluster2
57 # ../qa/workunits/rbd/rbd_mirror.sh flush cluster2
60 # Eventually, run the cleanup:
63 # RBD_MIRROR_TEMDIR=/tmp/tmp.rbd_mirror \
64 # ../qa/workunits/rbd/rbd_mirror.sh cleanup
70 PARENT_POOL
=mirror_parent
73 export CEPH_ARGS
="--id ${USER_ID}"
75 CEPH_ROOT
=$
(readlink
-f $
(dirname $0)/..
/..
/..
/src
)
78 if [ -e CMakeCache.txt
]; then
83 # needed for ceph CLI under cmake
84 export LD_LIBRARY_PATH
=${CEPH_ROOT}/lib
:${LD_LIBRARY_PATH}
85 export PYTHONPATH
=${PYTHONPATH}:${CEPH_SRC}/pybind
86 for x
in ${CEPH_ROOT}/lib
/cython_modules
/lib
* ; do
87 export PYTHONPATH
="${PYTHONPATH}:${x}"
91 # These vars facilitate running this script in an environment with
92 # ceph installed from packages, like teuthology. These are not defined
95 # RBD_MIRROR_USE_EXISTING_CLUSTER - if set, do not start and stop ceph clusters
96 # RBD_MIRROR_USE_RBD_MIRROR - if set, use an existing instance of rbd-mirror
97 # running as ceph client $CEPH_ID. If empty,
98 # this script will start and stop rbd-mirror
104 # Parse a value in format cluster[:instance] and set cluster and instance vars.
105 set_cluster_instance
()
108 local cluster_var_name
=$2
109 local instance_var_name
=$3
114 if [ "${instance}" = "${val}" ]; then
115 # instance was not specified, use default
119 eval ${cluster_var_name}=${cluster}
120 eval ${instance_var_name}=${instance}
125 local local_cluster
=$1
129 set_cluster_instance
"${local_cluster}" local_cluster instance
131 if [ -n "${RBD_MIRROR_USE_RBD_MIRROR}" ]; then
132 echo $
(ceph-conf
--cluster $local_cluster --name "client.${CEPH_ID}" 'admin socket')
134 echo "${TEMPDIR}/rbd-mirror.${local_cluster}_daemon.${instance}.${cluster}.asok"
143 set_cluster_instance
"${cluster}" cluster instance
145 if [ -n "${RBD_MIRROR_USE_RBD_MIRROR}" ]; then
146 echo $
(ceph-conf
--cluster $cluster --name "client.${CEPH_ID}" 'pid file')
148 echo "${TEMPDIR}/rbd-mirror.${cluster}_daemon.${instance}.pid"
154 echo $
(date '+%F %T') $@ |
tee -a "${TEMPDIR}/rbd-mirror.test.log" >&2
159 local expected
="$1" ; shift
160 local out
=${TEMPDIR}/expect_failure.out
162 if "$@" > ${out} 2>&1 ; then
167 if [ -z "${expected}" ]; then
171 if ! grep -q "${expected}" ${out} ; then
182 trap cleanup INT TERM EXIT
184 if [ -n "${RBD_MIRROR_TEMDIR}" ]; then
185 test -d "${RBD_MIRROR_TEMDIR}" ||
186 mkdir
"${RBD_MIRROR_TEMDIR}"
187 TEMPDIR
="${RBD_MIRROR_TEMDIR}"
193 if [ -z "${RBD_MIRROR_USE_EXISTING_CLUSTER}" ]; then
195 CEPH_ARGS
='' ${CEPH_SRC}/mstart.sh ${CLUSTER1} -n ${RBD_MIRROR_VARGS}
196 CEPH_ARGS
='' ${CEPH_SRC}/mstart.sh ${CLUSTER2} -n ${RBD_MIRROR_VARGS}
198 CEPH_ARGS
='' ceph
--conf run
/${CLUSTER1}/ceph.conf \
199 auth get-or-create client.
${USER_ID} mon
'profile rbd' osd
'profile rbd' >> \
200 run
/${CLUSTER1}/keyring
201 CEPH_ARGS
='' ceph
--conf run
/${CLUSTER2}/ceph.conf \
202 auth get-or-create client.
${USER_ID} mon
'profile rbd' osd
'profile rbd' >> \
203 run
/${CLUSTER2}/keyring
205 rm -f ${TEMPDIR}/${CLUSTER1}.conf
206 ln -s $
(readlink
-f run
/${CLUSTER1}/ceph.conf
) \
207 ${TEMPDIR}/${CLUSTER1}.conf
208 rm -f ${TEMPDIR}/${CLUSTER2}.conf
209 ln -s $
(readlink
-f run
/${CLUSTER2}/ceph.conf
) \
210 ${TEMPDIR}/${CLUSTER2}.conf
215 CEPH_ARGS
='' ceph
--cluster ${CLUSTER1} osd pool create
${POOL} 64 64
216 CEPH_ARGS
='' ceph
--cluster ${CLUSTER1} osd pool create
${PARENT_POOL} 64 64
217 CEPH_ARGS
='' ceph
--cluster ${CLUSTER2} osd pool create
${PARENT_POOL} 64 64
218 CEPH_ARGS
='' ceph
--cluster ${CLUSTER2} osd pool create
${POOL} 64 64
220 rbd
--cluster ${CLUSTER1} mirror pool
enable ${POOL} pool
221 rbd
--cluster ${CLUSTER2} mirror pool
enable ${POOL} pool
222 rbd
--cluster ${CLUSTER1} mirror pool
enable ${PARENT_POOL} image
223 rbd
--cluster ${CLUSTER2} mirror pool
enable ${PARENT_POOL} image
225 rbd
--cluster ${CLUSTER1} mirror pool peer add ${POOL} ${CLUSTER2}
226 rbd
--cluster ${CLUSTER2} mirror pool peer add ${POOL} ${CLUSTER1}
227 rbd
--cluster ${CLUSTER1} mirror pool peer add ${PARENT_POOL} ${CLUSTER2}
228 rbd
--cluster ${CLUSTER2} mirror pool peer add ${PARENT_POOL} ${CLUSTER1}
233 test -n "${RBD_MIRROR_NOCLEANUP}" && return
234 local cluster instance
238 for cluster
in "${CLUSTER1}" "${CLUSTER2}"; do
239 for instance
in `seq 0 9`; do
240 stop_mirror
"${cluster}:${instance}"
244 if [ -z "${RBD_MIRROR_USE_EXISTING_CLUSTER}" ]; then
246 CEPH_ARGS
='' ${CEPH_SRC}/mstop.sh
${CLUSTER1}
247 CEPH_ARGS
='' ${CEPH_SRC}/mstop.sh
${CLUSTER2}
249 CEPH_ARGS
='' ceph
--cluster ${CLUSTER1} osd pool rm ${POOL} ${POOL} --yes-i-really-really-mean-it
250 CEPH_ARGS
='' ceph
--cluster ${CLUSTER2} osd pool rm ${POOL} ${POOL} --yes-i-really-really-mean-it
251 CEPH_ARGS
='' ceph
--cluster ${CLUSTER1} osd pool rm ${PARENT_POOL} ${PARENT_POOL} --yes-i-really-really-mean-it
252 CEPH_ARGS
='' ceph
--cluster ${CLUSTER2} osd pool rm ${PARENT_POOL} ${PARENT_POOL} --yes-i-really-really-mean-it
254 test "${RBD_MIRROR_TEMDIR}" = "${TEMPDIR}" ||
263 set_cluster_instance
"${cluster}" cluster instance
265 test -n "${RBD_MIRROR_USE_RBD_MIRROR}" && return
268 --cluster ${cluster} \
270 --pid-file=$
(daemon_pid_file
"${cluster}:${instance}") \
271 --log-file=${TEMPDIR}/rbd-mirror.${cluster}_daemon.${instance}.log \
272 --admin-socket=${TEMPDIR}/rbd-mirror.${cluster}_daemon.${instance}.\
$cluster.asok \
273 --rbd-mirror-delete-retry-interval=5 \
274 --rbd-mirror-image-state-check-interval=5 \
275 --rbd-mirror-journal-poll-age=1 \
276 --rbd-mirror-pool-replayers-refresh-interval=5 \
277 --debug-rbd=30 --debug-journaler=30 \
278 --debug-rbd_mirror=30 \
288 test -n "${RBD_MIRROR_USE_RBD_MIRROR}" && return
291 pid
=$
(cat $
(daemon_pid_file
"${cluster}") 2>/dev
/null
) ||
:
295 for s
in 1 2 4 8 16 32; do
297 ps auxww |
awk -v pid
=${pid} '$2 == pid {print; exit 1}' && break
299 ps auxww |
awk -v pid
=${pid} '$2 == pid {print; exit 1}'
301 rm -f $
(daemon_asok_file
"${cluster}" "${CLUSTER1}")
302 rm -f $
(daemon_asok_file
"${cluster}" "${CLUSTER2}")
303 rm -f $
(daemon_pid_file
"${cluster}")
308 local cluster
=$1 ; shift
311 set_cluster_instance
"${cluster}" cluster instance
313 local asok_file
=$
(daemon_asok_file
"${cluster}:${instance}" "${cluster}")
314 test -S "${asok_file}"
316 ceph
--admin-daemon ${asok_file} $@
321 local cluster daemon image_pool image
323 for cluster
in ${CLUSTER1} ${CLUSTER2}
325 echo "${cluster} status"
326 ceph
--cluster ${cluster} -s
329 for image_pool
in ${POOL} ${PARENT_POOL}
331 echo "${cluster} ${image_pool} images"
332 rbd
--cluster ${cluster} -p ${image_pool} ls
335 echo "${cluster} ${image_pool} mirror pool status"
336 rbd
--cluster ${cluster} -p ${image_pool} mirror pool status
--verbose
339 for image
in `rbd --cluster ${cluster} -p ${image_pool} ls 2>/dev/null`
341 echo "image ${image} info"
342 rbd
--cluster ${cluster} -p ${image_pool} info ${image}
344 echo "image ${image} journal status"
345 rbd
--cluster ${cluster} -p ${image_pool} journal status --image ${image}
353 for cluster
in "${CLUSTER1}" "${CLUSTER2}"
355 local pid_file
=$
(daemon_pid_file
${cluster} )
356 if [ ! -e ${pid_file} ]
358 echo "${cluster} rbd-mirror not running or unknown" \
359 "(${pid_file} not exist)"
364 pid
=$
(cat ${pid_file} 2>/dev
/null
) ||
:
367 echo "${cluster} rbd-mirror not running or unknown" \
368 "(can't find pid using ${pid_file})"
373 echo "${daemon} rbd-mirror process in ps output:"
375 awk -v pid
=${pid} 'NR == 1 {print} $2 == pid {print; exit 1}'
378 echo "${cluster} rbd-mirror not running" \
379 "(can't find pid $pid in ps output)"
385 local asok_file
=$
(daemon_asok_file
${cluster} ${cluster})
386 if [ ! -S "${asok_file}" ]
388 echo "${cluster} rbd-mirror asok is unknown (${asok_file} not exits)"
393 echo "${cluster} rbd-mirror status"
394 ceph
--admin-daemon ${asok_file} rbd mirror status
406 local cmd
="rbd mirror flush"
410 cmd
="${cmd} ${pool}/${image}"
413 admin_daemon
"${cluster}" ${cmd}
416 test_image_replay_state
()
422 local current_state
=stopped
424 admin_daemon
"${cluster}" help |
425 fgrep
"\"rbd mirror status ${pool}/${image}\"" &&
426 admin_daemon
"${cluster}" rbd mirror status ${pool}/${image} |
427 grep -i 'state.*Replaying' &&
428 current_state=started
430 test "${test_state}" = "${current_state}"
433 wait_for_image_replay_state()
441 # TODO: add a way to force rbd-mirror to update replayers
442 for s in 1 2 4 8 8 8 8 8 8 8 8 16 16; do
444 test_image_replay_state "${cluster}" "${pool}" "${image}" "${state}" && return 0
449 wait_for_image_replay_started()
455 wait_for_image_replay_state "${cluster}" "${pool}" "${image}" started
458 wait_for_image_replay_stopped()
464 wait_for_image_replay_state "${cluster}" "${pool}" "${image}" stopped
474 # Parse line like below, looking for the first position
475 # [id=, commit_position=[positions=[[object_number=1, tag_tid=3, entry_tid=9], [object_number=0, tag_tid=3, entry_tid=8], [object_number=3, tag_tid=3, entry_tid=7], [object_number=2, tag_tid=3, entry_tid=6]]]]
477 local status_log=${TEMPDIR}/${CLUSTER2}-${pool}-${image}.status
478 rbd --cluster ${cluster} -p ${pool} journal status --image ${image} |
479 tee ${status_log} >&2
480 sed -nEe 's/^.*\[id='"${id_regexp}"',.*positions=\[\[([^]]*)\],.*state=connected.*$/\1/p' \
484 get_master_position()
490 get_position "${cluster}" "${pool}" "${image}" ''
493 get_mirror_position()
499 get_position "${cluster}" "${pool}" "${image}" '..*'
502 wait_for_replay_complete()
504 local local_cluster=$1
508 local s master_pos mirror_pos last_mirror_pos
509 local master_tag master_entry mirror_tag mirror_entry
512 for s in 0.2 0.4 0.8 1.6 2 2 4 4 8 8 16 16 32 32; do
514 flush "${local_cluster}" "${pool}" "${image}"
515 master_pos=$(get_master_position "${cluster}" "${pool}" "${image}")
516 mirror_pos=$(get_mirror_position "${cluster}" "${pool}" "${image}")
517 test -n "${master_pos}" -a "${master_pos}" = "${mirror_pos}" && return 0
518 test "${mirror_pos}" != "${last_mirror_pos}" && break
521 test "${mirror_pos}" = "${last_mirror_pos}" && return 1
522 last_mirror_pos="${mirror_pos}"
524 # handle the case where the mirror is ahead of the master
525 master_tag=$(echo "${master_pos}" | grep -Eo "tag_tid
=[0-9]*" | cut -d'=' -f 2)
526 mirror_tag=$(echo "${mirror_pos}" | grep -Eo "tag_tid
=[0-9]*" | cut -d'=' -f 2)
527 master_entry=$(echo "${master_pos}" | grep -Eo "entry_tid
=[0-9]*" | cut -d'=' -f 2)
528 mirror_entry=$(echo "${mirror_pos}" | grep -Eo "entry_tid
=[0-9]*" | cut -d'=' -f 2)
529 test "${master_tag}" = "${mirror_tag}" -a ${master_entry} -le ${mirror_entry} && return 0
534 test_status_in_pool_dir
()
539 local state_pattern
=$4
540 local description_pattern
=$5
542 local status_log
=${TEMPDIR}/${cluster}-${image}.mirror_status
543 rbd
--cluster ${cluster} -p ${pool} mirror image status ${image} |
544 tee ${status_log} >&2
545 grep "state: .*${state_pattern}" ${status_log} ||
return 1
546 grep "description: .*${description_pattern}" ${status_log} ||
return 1
549 wait_for_status_in_pool_dir
()
554 local state_pattern
=$4
555 local description_pattern
=$5
557 for s
in 1 2 4 8 8 8 8 8 8 8 8 16 16; do
559 test_status_in_pool_dir
${cluster} ${pool} ${image} ${state_pattern} ${description_pattern} && return 0
566 local cluster
=$1 ; shift
567 local pool
=$1 ; shift
568 local image
=$1 ; shift
576 rbd
--cluster ${cluster} -p ${pool} create --size ${size} \
577 --image-feature layering
,exclusive-lock
,journaling $@
${image}
588 rbd
--cluster ${cluster} -p ${pool} image-meta set ${image} $key $val
597 rbd
--cluster=${cluster} -p ${pool} snap purge ${image}
598 rbd
--cluster=${cluster} -p ${pool} rm ${image}
607 for s
in 1 2 4 8 16 32; do
608 remove_image
${cluster} ${pool} ${image} && return 0
618 local parent_image
=$3
623 rbd
--cluster ${cluster} clone ${parent_pool}/${parent_image}@${parent_snap} \
624 ${clone_pool}/${clone_image} --image-feature layering
,exclusive-lock
,journaling
633 rbd
--cluster ${cluster} -p ${pool} journal client disconnect \
644 rbd
--cluster ${cluster} -p ${pool} snap create ${image}@${snap}
654 rbd
--cluster ${cluster} -p ${pool} snap rm ${image}@${snap}
665 rbd
--cluster ${cluster} -p ${pool} snap rename ${image}@${snap} ${image}@${new_snap}
674 rbd
--cluster ${cluster} -p ${pool} snap purge ${image}
684 rbd
--cluster ${cluster} -p ${pool} snap protect ${image}@${snap}
694 rbd
--cluster ${cluster} -p ${pool} snap unprotect ${image}@${snap}
697 wait_for_snap_present
()
705 for s
in 1 2 4 8 8 8 8 8 8 8 8 16 16 16 16 32 32 32 32; do
707 rbd
--cluster ${cluster} -p ${pool} info ${image}@${snap_name} ||
continue
721 test -n "${size}" || size
=4096
723 rbd
--cluster ${cluster} -p ${pool} bench ${image} --io-type write \
724 --io-size ${size} --io-threads 1 --io-total $
((size
* count
)) \
733 local duration
=$
(awk 'BEGIN {srand(); print int(10 * rand()) + 5}')
735 timeout
${duration}s ceph_test_rbd_mirror_random_write \
736 --cluster ${cluster} ${pool} ${image} \
737 --debug-rbd=20 --debug-journaler=20 \
738 2> ${TEMPDIR}/rbd-mirror-random-write.log || true
746 local rmt_export
=${TEMPDIR}/${CLUSTER2}-${pool}-${image}.
export
747 local loc_export
=${TEMPDIR}/${CLUSTER1}-${pool}-${image}.
export
749 rm -f ${rmt_export} ${loc_export}
750 rbd
--cluster ${CLUSTER2} -p ${pool} export ${image} ${rmt_export}
751 rbd
--cluster ${CLUSTER1} -p ${pool} export ${image} ${loc_export}
752 cmp ${rmt_export} ${loc_export}
753 rm -f ${rmt_export} ${loc_export}
762 rbd
--cluster=${cluster} mirror image demote ${pool}/${image}
772 rbd
--cluster=${cluster} mirror image promote ${pool}/${image} ${force}
775 set_pool_mirror_mode
()
781 rbd
--cluster=${cluster} -p ${pool} mirror pool enable ${mode}
790 rbd
--cluster=${cluster} mirror image disable ${pool}/${image}
799 rbd
--cluster=${cluster} mirror image enable ${pool}/${image}
809 local current_state
=deleted
810 local current_image_id
812 current_image_id
=$
(get_image_id
${cluster} ${pool} ${image})
813 test -n "${current_image_id}" &&
814 test -z "${image_id}" -o "${image_id}" = "${current_image_id}" &&
815 current_state
=present
817 test "${test_state}" = "${current_state}"
820 wait_for_image_present
()
829 test -n "${image_id}" ||
830 image_id
=$
(get_image_id
${cluster} ${pool} ${image})
832 # TODO: add a way to force rbd-mirror to update replayers
833 for s
in 0.1 1 2 4 8 8 8 8 8 8 8 8 16 16 32 32; do
836 "${cluster}" "${pool}" "${image}" "${state}" "${image_id}" &&
848 rbd
--cluster=${cluster} -p ${pool} info ${image} |
849 sed -ne 's/^.*block_name_prefix: rbd_data\.//p'
852 request_resync_image
()
857 local image_id_var_name
=$1
859 eval "${image_id_var_name}='$(get_image_id ${cluster} ${pool} ${image})'"
860 eval 'test -n "$'${image_id_var_name}'"'
862 rbd
--cluster=${cluster} -p ${pool} mirror image resync ${image}
871 if [ -z "${RBD_MIRROR_TEMDIR}" ]
873 echo "RBD_MIRROR_TEMDIR is not set" >&2
877 TEMPDIR
="${RBD_MIRROR_TEMDIR}"