branching: Add retries to git push/review commands

Review.opendev.org seem unreliable when hit
with a lot of transactions in a short time.
This means that an attempt to create a release branch,
e.g. r/stx.6.0 failes repeatedly, and requires a lot
of manual repair.

By adding retires and delays, we greatly improve
our odds of success.

Closes-bug: 1954536
Signed-off-by: Scott Little <scott.little@windriver.com>
Change-Id: Ia03ed58b67c02e6984a7e0ee01c6666027df6cbe
This commit is contained in:
Scott Little 2021-12-10 16:06:00 -05:00
parent d015858239
commit 06537894b2
8 changed files with 166 additions and 64 deletions

View File

@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
# #
# Copyright (c) 2020 Wind River Systems, Inc. # Copyright (c) 2020-2021 Wind River Systems, Inc.
# #
# SPDX-License-Identifier: Apache-2.0 # SPDX-License-Identifier: Apache-2.0
# #
@ -215,18 +215,21 @@ update_gitreview () {
fi fi
if [ $need_commit -eq 1 ]; then if [ $need_commit -eq 1 ]; then
echo_stderr "determinging git review method in ${DIR}"
review_method=$(git_repo_review_method) review_method=$(git_repo_review_method)
if [ "${review_method}" == "gerrit" ] ; then if [ "${review_method}" == "gerrit" ] ; then
timeout 15 git review -s echo_stderr "running git review -s in ${DIR}"
with_retries -d 45 -t 15 -k 5 5 git review -s >&2
if [ $? != 0 ] ; then if [ $? != 0 ] ; then
if [ ${new_host} -eq 0 ]; then if [ ${new_host} -eq 0 ]; then
echo_stderr "ERROR: failed to setup git review in ${DIR}" echo_stderr "ERROR: 3: failed to setup git review in ${DIR}"
exit 1 exit 1
fi fi
need_rm=1 need_rm=1
message="Delete .gitreview for ${branch}" message="Delete .gitreview for ${branch}"
fi fi
echo_stderr "finished git review -s in ${DIR}"
else else
need_rm=1 need_rm=1
message="Delete .gitreview for ${branch}" message="Delete .gitreview for ${branch}"
@ -338,9 +341,9 @@ for subgit in $SUBGITS; do
review_method=$(git_repo_review_method) review_method=$(git_repo_review_method)
if [ -f .gitreview ] && [ "${review_method}" == "gerrit" ] ; then if [ -f .gitreview ] && [ "${review_method}" == "gerrit" ] ; then
git review -s > /dev/null with_retries -d 45 -t 15 -k 5 5 git review -s >&2
if [ $? != 0 ] ; then if [ $? != 0 ] ; then
echo_stderr "ERROR: failed to setup git review in ${subgit}" echo_stderr "ERROR: 1: failed to setup git review in ${subgit}"
exit 1 exit 1
fi fi
fi fi
@ -409,9 +412,9 @@ if [ $MANIFEST -eq 1 ]; then
review_method=$(git_review_method) review_method=$(git_review_method)
if [ -f .gitreview ] && [ "${review_method}" == "gerrit" ] ; then if [ -f .gitreview ] && [ "${review_method}" == "gerrit" ] ; then
git review -s > /dev/null with_retries -d 45 -t 15 -k 5 5 git review -s >&2
if [ $? != 0 ] ; then if [ $? != 0 ] ; then
echo_stderr "ERROR: failed to setup git review in ${new_manifest_dir}" echo_stderr "ERROR: 2: failed to setup git review in ${new_manifest_dir}"
exit 1 exit 1
fi fi
fi fi

View File

@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
# #
# Copyright (c) 2020 Wind River Systems, Inc. # Copyright (c) 2020-2021 Wind River Systems, Inc.
# #
# SPDX-License-Identifier: Apache-2.0 # SPDX-License-Identifier: Apache-2.0
# #
@ -190,7 +190,7 @@ for subgit in $SUBGITS; do
review_method=$(git_repo_review_method) review_method=$(git_repo_review_method)
if [ -f .gitreview ] && [ "${review_method}" == "gerrit" ] ; then if [ -f .gitreview ] && [ "${review_method}" == "gerrit" ] ; then
git review -s > /dev/null with_retries -d 45 -t 15 -k 5 5 git review -s >&2
if [ $? != 0 ] ; then if [ $? != 0 ] ; then
echo_stderr "ERROR: failed to setup git review in ${subgit}" echo_stderr "ERROR: failed to setup git review in ${subgit}"
exit 1 exit 1
@ -221,7 +221,7 @@ if [ $MANIFEST -eq 1 ]; then
review_method=$(git_review_method) review_method=$(git_review_method)
if [ -f .gitreview ] && [ "${review_method}" == "gerrit" ] ; then if [ -f .gitreview ] && [ "${review_method}" == "gerrit" ] ; then
git review -s > /dev/null with_retries -d 45 -t 15 -k 5 5 git review -s >&2
if [ $? != 0 ] ; then if [ $? != 0 ] ; then
echo_stderr "ERROR: failed to setup git review in ${new_manifest_dir}" echo_stderr "ERROR: failed to setup git review in ${new_manifest_dir}"
exit 1 exit 1

View File

@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
# #
# Copyright (c) 2020 Wind River Systems, Inc. # Copyright (c) 2020-2021 Wind River Systems, Inc.
# #
# SPDX-License-Identifier: Apache-2.0 # SPDX-License-Identifier: Apache-2.0
# #
@ -224,26 +224,25 @@ for subgit in $SUBGITS; do
port=$(url_port "${url}") port=$(url_port "${url}")
path=$(url_path "${url}") path=$(url_path "${url}")
if [ "${host}" == "review.opendev.org" ] || git_match_safe_gerrit_host "${host}" ; then if [ "${host}" == "review.opendev.org" ] || git_match_safe_gerrit_host "${host}" ; then
echo "git push ${review_remote} ${tag} && \\" echo "git push ${review_remote} ${tag}" && \
echo "ssh -p ${port} ${host} gerrit create-branch ${path} ${branch} ${tag} && \\" with_retries -d 45 -t 15 -k 5 5 git push $DRY_RUN ${review_remote} ${tag} && \
echo "git config --local --replace-all branch.${branch}.merge refs/heads/${branch} && \\" echo "ssh -p ${port} ${host} gerrit create-branch ${path} ${branch} ${tag}" && \
echo "git review --topic=${branch}"
git push $DRY_RUN ${review_remote} ${tag} && \
$DRY_RUN_CMD ssh -p ${port} ${host} gerrit create-branch ${path} ${branch} ${tag} && \ $DRY_RUN_CMD ssh -p ${port} ${host} gerrit create-branch ${path} ${branch} ${tag} && \
echo "git config --local --replace-all branch.${branch}.merge refs/heads/${branch}" && \
$DRY_RUN_CMD git config --local --replace-all "branch.${branch}.merge" refs/heads/${branch} && \ $DRY_RUN_CMD git config --local --replace-all "branch.${branch}.merge" refs/heads/${branch} && \
$DRY_RUN_CMD git review --topic="${branch}" echo "git review --topic=${branch/\//.}" && \
$DRY_RUN_CMD with_retries -d 45 -t 15 -k 5 5 git review --topic="${branch/\//.}"
else else
echo "git push ${review_remote} ${branch}:${branch} $DRY_RUN" echo "git push ${review_remote} ${branch}:${branch} $DRY_RUN" && \
git push ${review_remote} ${branch}:${branch} $DRY_RUN with_retries -d 45 -t 15 -k 5 5 git push ${review_remote} ${branch}:${branch} $DRY_RUN && \
echo "git push ${review_remote} ${tag}:${tag} $DRY_RUN" echo "git push ${review_remote} ${tag}:${tag} $DRY_RUN" && \
git push ${review_remote} ${tag}:${tag} $DRY_RUN with_retries -d 45 -t 15 -k 5 5 git push ${review_remote} ${tag}:${tag} $DRY_RUN
fi fi
else else
echo "git push ${remote} ${branch}:${branch} $DRY_RUN" echo "git push ${remote} ${branch}:${branch} $DRY_RUN" && \
git push ${remote} ${branch}:${branch} $DRY_RUN with_retries -d 45 -t 15 -k 5 5 git push ${remote} ${branch}:${branch} $DRY_RUN && \
echo "git push ${remote} ${tag}:${tag} $DRY_RUN" echo "git push ${remote} ${tag}:${tag} $DRY_RUN" && \
git push ${remote} ${tag}:${tag} $DRY_RUN with_retries -d 45 -t 15 -k 5 5 git push ${remote} ${tag}:${tag} $DRY_RUN
fi fi
if [ $? != 0 ] ; then if [ $? != 0 ] ; then
@ -314,22 +313,21 @@ if [ $MANIFEST -eq 1 ]; then
port=$(url_port "${url}") port=$(url_port "${url}")
path=$(url_path "${url}") path=$(url_path "${url}")
if [ "${host}" == "review.opendev.org" ] || git_match_safe_gerrit_host "${host}" ; then if [ "${host}" == "review.opendev.org" ] || git_match_safe_gerrit_host "${host}" ; then
echo "git push ${review_remote} ${tag} && \\" echo "git push ${review_remote} ${tag}" && \
echo "ssh -p ${port} ${host} gerrit create-branch ${path} ${branch} ${tag} && \\" with_retries -d 45 -t 15 -k 5 5 git push ${review_remote} ${tag} $DRY_RUN && \
echo "git config --local --replace-all branch.${branch}.merge refs/heads/${branch} && \\" echo "ssh -p ${port} ${host} gerrit create-branch ${path} ${branch} ${tag}" && \
echo "git review --yes --topic=${branch}"
git push ${review_remote} ${tag} $DRY_RUN && \
$DRY_RUN_CMD ssh -p ${port} ${host} gerrit create-branch ${path} ${branch} ${tag} && \ $DRY_RUN_CMD ssh -p ${port} ${host} gerrit create-branch ${path} ${branch} ${tag} && \
echo "git config --local --replace-all branch.${branch}.merge refs/heads/${branch}" && \
$DRY_RUN_CMD git config --local --replace-all "branch.${branch}.merge" refs/heads/${branch} && \ $DRY_RUN_CMD git config --local --replace-all "branch.${branch}.merge" refs/heads/${branch} && \
$DRY_RUN_CMD git review --yes --topic="${branch}" echo "git review --yes --topic=${branch/\//.}" && \
$DRY_RUN_CMD with_retries -d 45 -t 15 -k 5 5 git review --yes --topic="${branch/\//.}"
else else
echo git push --tags ${review_remote} ${branch} $DRY_RUN echo git push --tags ${review_remote} ${branch} $DRY_RUN && \
git push --tags ${review_remote} ${branch} $DRY_RUN with_retries -d 45 -t 15 -k 5 5 git push --tags ${review_remote} ${branch} $DRY_RUN
fi fi
else else
echo git push --tags --set-upstream ${review_remote} ${branch} $DRY_RUN echo git push --tags --set-upstream ${review_remote} ${branch} $DRY_RUN && \
git push --tags --set-upstream ${review_remote} ${branch} $DRY_RUN with_retries -d 45 -t 15 -k 5 5 git push --tags --set-upstream ${review_remote} ${branch} $DRY_RUN
fi fi
if [ $? != 0 ] ; then if [ $? != 0 ] ; then

View File

@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
# #
# Copyright (c) 2020 Wind River Systems, Inc. # Copyright (c) 2020-2021 Wind River Systems, Inc.
# #
# SPDX-License-Identifier: Apache-2.0 # SPDX-License-Identifier: Apache-2.0
# #
@ -182,10 +182,10 @@ for subgit in $SUBGITS; do
echo "Pushing tag $tag in ${subgit}" echo "Pushing tag $tag in ${subgit}"
if [ "${review_method}" == "gerrit" ] && [ $BYPASS_GERRIT -eq 0 ]; then if [ "${review_method}" == "gerrit" ] && [ $BYPASS_GERRIT -eq 0 ]; then
echo "git push ${review_remote} ${tag}" echo "git push ${review_remote} ${tag}"
git push ${review_remote} ${tag} ${DRY_RUN} with_retries -d 45 -t 15 -k 5 5 git push ${review_remote} ${tag} ${DRY_RUN}
else else
echo "git push ${remote} ${tag}" echo "git push ${remote} ${tag}"
git push ${remote} ${tag} ${DRY_RUN} with_retries -d 45 -t 15 -k 5 5 git push ${remote} ${tag} ${DRY_RUN}
fi fi
if [ $? != 0 ] ; then if [ $? != 0 ] ; then
@ -246,7 +246,7 @@ if [ $MANIFEST -eq 1 ]; then
echo "Pushing tag $tag in ${new_manifest_dir}" echo "Pushing tag $tag in ${new_manifest_dir}"
if [ "${review_method}" == "gerrit" ] && [ $BYPASS_GERRIT -eq 0 ]; then if [ "${review_method}" == "gerrit" ] && [ $BYPASS_GERRIT -eq 0 ]; then
git review with_retries -d 45 -t 15 -k 5 5 git review
if [ $? != 0 ] ; then if [ $? != 0 ] ; then
echo_stderr "ERROR: Failed to create git review from ${new_manifest_dir}" echo_stderr "ERROR: Failed to create git review from ${new_manifest_dir}"
exit 1 exit 1
@ -255,8 +255,10 @@ if [ $MANIFEST -eq 1 ]; then
echo " cd ${new_manifest_dir}" echo " cd ${new_manifest_dir}"
echo " git push ${review_remote} ${tag}" echo " git push ${review_remote} ${tag}"
else else
git push ${remote} ${local_branch}:${remote_branch} ${DRY_RUN} echo "git push ${remote} ${local_branch}:${remote_branch}" && \
git push ${remote} ${tag}:${tag} ${DRY_RUN} with_retries -d 45 -t 15 -k 5 5 git push ${remote} ${local_branch}:${remote_branch} ${DRY_RUN} && \
echo "git push ${remote} ${tag}:${tag}" && \
with_retries -d 45 -t 15 -k 5 5 git push ${remote} ${tag}:${tag} ${DRY_RUN}
fi fi
if [ $? != 0 ] ; then if [ $? != 0 ] ; then

View File

@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
# #
# Copyright (c) 2020 Wind River Systems, Inc. # Copyright (c) 2020-2021 Wind River Systems, Inc.
# #
# SPDX-License-Identifier: Apache-2.0 # SPDX-License-Identifier: Apache-2.0
# #
@ -182,8 +182,7 @@ git_repo_review_remote () {
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
# Perhaps we need to run git review -s' and try again # Perhaps we need to run git review -s' and try again
# echo a blank line into git in case it prompts for a username with_retries -d 45 -t 15 -k 5 5 git review -s >&2 || return 1
echo | git review -s > /dev/null || return 1
git config remote.gerrit.url > /dev/null || return 1 git config remote.gerrit.url > /dev/null || return 1
fi fi
echo "gerrit" echo "gerrit"
@ -205,8 +204,7 @@ git_repo_review_url () {
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
# Perhaps we need to run git review -s' and try again # Perhaps we need to run git review -s' and try again
# echo a blank line into git in case it prompts for a username with_retries -d 45 -t 15 -k 5 5 git review -s >&2 || return 1
echo | git review -s > /dev/null || return 1
git config remote.gerrit.url || return 1 git config remote.gerrit.url || return 1
fi fi
else else

View File

@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
# #
# Copyright (c) 2018 Wind River Systems, Inc. # Copyright (c) 2018-2021 Wind River Systems, Inc.
# #
# SPDX-License-Identifier: Apache-2.0 # SPDX-License-Identifier: Apache-2.0
# #
@ -11,10 +11,10 @@
# by repo manifests. # by repo manifests.
# #
echo_stderr () GIT_UTILS_DIR="$(dirname "$(readlink -f "${BASH_SOURCE[0]}" )" )"
{
echo "$@" >&2 source ${GIT_UTILS_DIR}/utils.sh
}
git_ctx_root_dir () { git_ctx_root_dir () {
dirname "${MY_REPO}" dirname "${MY_REPO}"
@ -562,7 +562,7 @@ git_review_url () {
git config remote.gerrit.url git config remote.gerrit.url
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
# Perhaps we need to run git review -s' and try again # Perhaps we need to run git review -s' and try again
git review -s > /dev/null || return 1 with_retries -d 45 -t 15 -k 5 5 git review -s >&2 || return 1
git config remote.gerrit.url git config remote.gerrit.url
fi fi
else else
@ -577,7 +577,7 @@ git_review_remote () {
git config remote.gerrit.url > /dev/null git config remote.gerrit.url > /dev/null
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
# Perhaps we need to run git review -s' and try again # Perhaps we need to run git review -s' and try again
git review -s > /dev/null || return 1 with_retries -d 45 -t 15 -k 5 5 git review -s >&2 || return 1
git config remote.gerrit.url > /dev/null || return 1 git config remote.gerrit.url > /dev/null || return 1
fi fi
echo "gerrit" echo "gerrit"

View File

@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
# #
# Copyright (c) 2020 Wind River Systems, Inc. # Copyright (c) 2020-2021 Wind River Systems, Inc.
# #
# SPDX-License-Identifier: Apache-2.0 # SPDX-License-Identifier: Apache-2.0
# #
@ -10,16 +10,10 @@
# A collection of utilities relating to 'repo' # A collection of utilities relating to 'repo'
# #
REPO_UTILS_DIR="$(dirname "$(readlink -f "${BASH_SOURCE[0]}" )" )"
# source ${REPO_UTILS_DIR}/utils.sh
# Echo to stderr
# echo_stderr [any text you want]
#
echo_stderr ()
{
echo "$@" >&2
}
# #
# Get the root dir of a repo managed repository # Get the root dir of a repo managed repository

107
build-tools/utils.sh Normal file
View File

@ -0,0 +1,107 @@
#!/bin/bash
#
# Copyright (c) 2019-2021 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
# Image and wheel build utility functions
#
#
# Echo to stderr
# echo_stderr [any text you want]
#
echo_stderr ()
{
echo "$@" >&2
}
#
# Function to call a command, with support for retries
#
# with_retries [<options>] <retries> <cmd> [<cmd_args>...]
#
# Options:
# -d <secs> | --delay <secs>
# Wait given number of seconds between retries
# -t <secs> | --timeout <secs>
# Each iteration of the command runs under a timeout
# -k <secs> | --kill-timeout <secs>
# Each iteration of the command is killed violently
# if it doesn't exit voluntarily within the set time
# after the initial timeout signal.
#
function with_retries {
local delay=5
local max_time=0
local kill_time=0
local to_cmd=""
while [ $1 != "" ]; do
case "$1" in
-d | --delay)
delay=$2
shift 2
;;
-t | --timeout)
max_time=$2
shift 2
;;
-k | --kill-timeout)
kill_time=$2
shift 2
;;
*)
break
;;
esac
done
local max_attempts=$1
local cmd=$2
shift 2
if [ ${max_time} -gt 0 ]; then
to_cmd="timeout "
if [ ${kill_time} -gt 0 ]; then
to_cmd+="--kill-after=${kill_time} "
fi
to_cmd+="${max_time} "
fi
# Pop the first two arguments off the list,
# so we can pass additional args to the command safely
local -i attempt=0
local rc=0
while :; do
let attempt++
echo_stderr "Running: ${cmd} $@"
${to_cmd} ${cmd} "$@"
rc=$?
if [ $rc -eq 0 ]; then
return 0
fi
if [ $rc -eq 124 ]; then
echo_stderr "Command (${cmd}) timed out, attempt ${attempt} of ${max_attempts}."
elif [ $rc -eq 137 ]; then
echo_stderr "Command (${cmd}) timed out and killed, attempt ${attempt} of ${max_attempts}."
else
echo_stderr "Command (${cmd}) failed, attempt ${attempt} of ${max_attempts}."
fi
if [ ${attempt} -lt ${max_attempts} ]; then
echo_stderr "Waiting ${delay} seconds before retrying..."
sleep ${delay}
continue
else
echo_stderr "Max command attempts reached. Aborting..."
return 1
fi
done
}