Skip to content

Commit 6b9ac52

Browse files
authored
logic for dynamically calculating sleep time (#608)
1 parent 40bdb62 commit 6b9ac52

File tree

1 file changed

+64
-42
lines changed

1 file changed

+64
-42
lines changed

snapshotEngine/snapshot-maker.sh

Lines changed: 64 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,6 @@ getSnapshotNames() {
2828
kubectl get volumesnapshots -o jsonpath="{.items[?(.status.readyToUse==$readyToUse)].metadata.name}" --namespace "$NAMESPACE" "$@"
2929
}
3030

31-
# SLEEP_TIME=0m
32-
33-
# if [ "${HISTORY_MODE}" = "archive" ]; then
34-
# SLEEP_TIME="${ARCHIVE_SLEEP_DELAY}"
35-
# if [ "${ARCHIVE_SLEEP_DELAY}" != "0m" ]; then
36-
# printf "%s artifactDelay.archive is set to %s sleeping...\n" "$(date "+%Y-%m-%d %H:%M:%S")" "${ARCHIVE_SLEEP_DELAY}"
37-
# fi
38-
# elif [ "${HISTORY_MODE}" = "rolling" ]; then
39-
# SLEEP_TIME="${ROLLING_SLEEP_DELAY}"
40-
# if [ "${ROLLING_SLEEP_DELAY}" != "0m" ]; then
41-
# printf "%s artifactDelay.rolling is set to %s sleeping...\n" "$(date "+%Y-%m-%d %H:%M:%S")" "${ROLLING_SLEEP_DELAY}"
42-
# fi
43-
# fi
44-
45-
# if [ "${SLEEP_TIME}" = "0m" ]; then
46-
# printf "%s artifactDelay.HISTORY_MODE was not set! No delay...\n" "$(date "+%Y-%m-%d %H:%M:%S")"
47-
# fi
48-
49-
# sleep "${SLEEP_TIME}"
50-
5131
cd /
5232

5333
ZIP_AND_UPLOAD_JOB_NAME=zip-and-upload-"${HISTORY_MODE}"
@@ -88,17 +68,79 @@ if [ "$(kubectl get pvc "${HISTORY_MODE}"-snap-volume)" ]; then
8868
sleep 5
8969
fi
9070

71+
# Check latest artifact and sleep if its too new
72+
# This was done because nodes sometimes OOM and jobs are restarted
73+
# Resulting in more artifacts created than should be with
74+
# a given sleep time. IE 3 days sleep should result in 2 artifacts in a 7 day lifecycle policy
75+
# but if the job restarts during its sleeping time it would result in more, multiple per day even.
76+
77+
SLEEP_TIME=0m
78+
79+
if [ "${HISTORY_MODE}" = "archive" ]; then
80+
SLEEP_TIME="${ARCHIVE_SLEEP_DELAY}"
81+
if [ "${ARCHIVE_SLEEP_DELAY}" != "0m" ]; then
82+
printf "%s artifactDelay.archive is set to %s.\n" "$(date "+%Y-%m-%d %H:%M:%S")" "${ARCHIVE_SLEEP_DELAY}"
83+
fi
84+
elif [ "${HISTORY_MODE}" = "rolling" ]; then
85+
SLEEP_TIME="${ROLLING_SLEEP_DELAY}"
86+
if [ "${ROLLING_SLEEP_DELAY}" != "0m" ]; then
87+
printf "%s artifactDelay.rolling is set to %s.\n" "$(date "+%Y-%m-%d %H:%M:%S")" "${ROLLING_SLEEP_DELAY}"
88+
fi
89+
fi
90+
91+
if [ "${SLEEP_TIME}" = "0m" ]; then
92+
printf "%s artifactDelay.HISTORY_MODE was not set! No delay...\n" "$(date "+%Y-%m-%d %H:%M:%S")"
93+
else
94+
# Latest timestamp of this network's artifact of this history mode
95+
LATEST_ARTIFACT_TIMESTAMP=$(curl https://${NAMESPACE}.nyc3.digitaloceanspaces.com/base.json | \
96+
jq --arg HISTORY_MODE "${HISTORY_MODE}" \
97+
'[.[] | select(.history_mode==$HISTORY_MODE)] | sort_by(.block_timestamp) | last | .block_timestamp' | tr -d '"')
98+
99+
# If base.json doesnt exists continue
100+
if [[ -n ${LATEST_ARTIFACT_TIMESTAMP} ]]; then
101+
printf "%s Latest artifact timestamp is %s.\n" "$(date "+%Y-%m-%d %H:%M:%S" "$@")" "${LATEST_ARTIFACT_TIMESTAMP}"
102+
printf "%s Sleep time is %s.\n" "$(date "+%Y-%m-%d %H:%M:%S" "$@")" "${SLEEP_TIME}"
103+
104+
## Now minus artifact timestamp
105+
ARTIFACT_EPOCH_TIME=$(date -d "${LATEST_ARTIFACT_TIMESTAMP}" -D "%Y-%m-%dT%H:%M:%SZ" +%s)
106+
107+
# Check if sleep time is in hours or days and convert to minutes
108+
if [[ -n $(echo $SLEEP_TIME | grep d) ]]; then
109+
# Converting hours to minutes
110+
SLEEP_TIME_MINUTES=$(( ${SLEEP_TIME%?} * 24 * 60 ))
111+
else
112+
# if its hours already just pop off the h
113+
SLEEP_TIME_MINUTES=$(( "${SLEEP_TIME%?}" * 60 ))
114+
fi
115+
116+
# Age of artifact in minutes
117+
ARTIFACT_AGE=$(( ($(date +%s) - ARTIFACT_EPOCH_TIME) / 60 ))
118+
119+
printf "%s Latest artifact is %s minutes old.\n" "$(date "+%Y-%m-%d %H:%M:%S" "$@")" "${ARTIFACT_AGE}"
120+
printf "%s Our set sleep time in minutes is %s.\n" "$(date "+%Y-%m-%d %H:%M:%S" "$@")" "${SLEEP_TIME_MINUTES}"
121+
122+
# If the age is less than our sleep minutes we need to continue to sleep
123+
if [[ ${ARTIFACT_AGE} -lt ${SLEEP_TIME_MINUTES} ]]; then
124+
TIME_LEFT=$(( SLEEP_TIME_MINUTES - ARTIFACT_AGE ))
125+
printf "%s We need to sleep for %s minutes.\n" "$(date "+%Y-%m-%d %H:%M:%S" "$@")" "${TIME_LEFT}"
126+
sleep "${TIME_LEFT}"m
127+
else
128+
printf "%s Newest artifact is older than sleep time starting job!.\n" "$(date "+%Y-%m-%d %H:%M:%S" "$@")"
129+
fi
130+
fi
131+
fi
132+
91133
# Take volume snapshot
92134
current_date=$(date "+%Y-%m-%d-%H-%M-%S" "$@")
93135
export SNAPSHOT_NAME="$current_date-$HISTORY_MODE-node-snapshot"
94136
# Update volume snapshot name
95137
yq e -i '.metadata.name=strenv(SNAPSHOT_NAME)' createVolumeSnapshot.yaml
96138

97-
printf "%s Creating snapshot ${SNAPSHOT_NAME} in ${NAMESPACE}.\n" "$(timestamp)"
139+
printf "%s Creating snapshot ${SNAPSHOT_NAME} in ${NAMESPACE}.\n" "$(date "+%Y-%m-%d %H:%M:%S" "$@")"
98140

99141
# Create snapshot
100142
if ! kubectl apply -f createVolumeSnapshot.yaml; then
101-
printf "%s ERROR creating volumeSnapshot ${SNAPSHOT_NAME} in ${NAMESPACE} .\n" "$(timestamp)"
143+
printf "%s ERROR creating volumeSnapshot ${SNAPSHOT_NAME} in ${NAMESPACE} .\n" "$(date "+%Y-%m-%d %H:%M:%S" "$@")"
102144
exit 1
103145
fi
104146

@@ -330,26 +372,6 @@ if ! [ "$(kubectl get jobs "zip-and-upload-${HISTORY_MODE}" --namespace "${NAMES
330372
# Delete all volumesnapshots so they arent setting around accruing charges
331373
kubectl delete vs -l history_mode=$HISTORY_MODE
332374

333-
SLEEP_TIME=0m
334-
335-
if [ "${HISTORY_MODE}" = "archive" ]; then
336-
SLEEP_TIME="${ARCHIVE_SLEEP_DELAY}"
337-
if [ "${ARCHIVE_SLEEP_DELAY}" != "0m" ]; then
338-
printf "%s artifactDelay.archive is set to %s sleeping...\n" "$(date "+%Y-%m-%d %H:%M:%S")" "${ARCHIVE_SLEEP_DELAY}"
339-
fi
340-
elif [ "${HISTORY_MODE}" = "rolling" ]; then
341-
SLEEP_TIME="${ROLLING_SLEEP_DELAY}"
342-
if [ "${ROLLING_SLEEP_DELAY}" != "0m" ]; then
343-
printf "%s artifactDelay.rolling is set to %s sleeping...\n" "$(date "+%Y-%m-%d %H:%M:%S")" "${ROLLING_SLEEP_DELAY}"
344-
fi
345-
fi
346-
347-
if [ "${SLEEP_TIME}" = "0m" ]; then
348-
printf "%s artifactDelay.HISTORY_MODE was not set! No delay...\n" "$(date "+%Y-%m-%d %H:%M:%S")"
349-
fi
350-
351-
sleep "${SLEEP_TIME}"
352-
353375
fi
354376

355377
printf "%s Deleting temporary snapshot volume.\n" "$(date "+%Y-%m-%d %H:%M:%S" "$@")"

0 commit comments

Comments
 (0)