Skip to content

Instantly share code, notes, and snippets.

@dgnorton
Created March 6, 2020 12:40
Show Gist options
  • Save dgnorton/3ab595df9a19e1d685caef58c890924c to your computer and use it in GitHub Desktop.
Save dgnorton/3ab595df9a19e1d685caef58c890924c to your computer and use it in GitHub Desktop.
#!/bin/bash
# addDays takes a Unix timestamp (in seconds) and returns a new timestamp
# with numDays added to it.
function addDays() {
local ts=$1
local days=$2
echo $((ts + days * 86400))
}
# Database, retention policy, and initial timestamp used for the tests.
#db="issue15050"
db="db"
rp="autogen"
t0=$(addDays 0 7)
#t0=$(date +%s)
# setupTest drops the test db, recreates it, and recreates the overlapping
# shard state.
function setupTest() {
# Drop and recreate the database.
echo "Dropping and recreating database: ${db}"
influx -database ${db} -execute "drop database ${db}; create database ${db};"
# Uncommenting this ALTER statement will confirm that tests fail if a
# point is written outside the retention policy duration.
#influx -database ${db} -execute "alter retention policy ${rp} on ${db} duration 4w"
# Write a point 7 days after the epoch.
echo "Writing a single point 7 days after the epoch"
curl -f -XPOST "http://localhost:8086/write?db=${db}&rp=${rp}&precision=s" --data-binary "m v=1 ${t0}"
if [ $? -ne 0 ]; then
echo "FAIL -----------------------------------------------------------"
exit $?
fi
# Change shard duration.
echo "Changing shard duration from 1w to 3w"
influx -database ${db} -execute "alter retention policy ${rp} on ${db} shard duration 3w"
# Write second point into next shard to create overlapping shards.
echo "Writing point after end_time of first shard and before 3w from now."
echo "This will created a second shard that overlaps the first."
t1=$(addDays $t0 7)
curl -f -XPOST "http://localhost:8086/write?db=${db}&rp=${rp}&precision=s" --data-binary "m v=2 ${t1}"
if [ $? -ne 0 ]; then
echo "FAIL -----------------------------------------------------------"
exit $?
fi
echo ""
# Show shards.
echo "Shards:"
influx -database ${db} -execute "show shards" | grep ${db}
echo ""
}
# test1 writes 2 points in 1 request. Both points fall within the time range
# of both shards.
function test1() {
echo "Start test1 *****************************************************"
setupTest
local t1=$((t0 + 3600))
local t2=$((t1 + 3600))
curl -f -XPOST "http://localhost:8086/write?db=${db}&rp=${rp}&precision=s" --data-binary "m v=3 ${t1}
m v=4 ${t2}"
if [ $? -ne 0 ]; then
echo "FAIL -----------------------------------------------------------"
exit $?
fi
echo "Results:"
influx -database ${db} -precision rfc3339 -execute "select * from m"
echo "PASS +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo "End test1 ******************************************************"
}
# test2 writes 2 points in 1 request. Both points fall in the second shard.
function test2() {
echo "Start test2 *****************************************************"
setupTest
local t1=$(addDays $t0 7)
t1=$((t1 + 3600))
local t2=$((t1 + 3600))
curl -f -XPOST "http://localhost:8086/write?db=${db}&rp=${rp}&precision=s" --data-binary "m v=3 ${t1}
m v=4 ${t2}"
if [ $? -ne 0 ]; then
echo "FAIL -----------------------------------------------------------"
exit $?
fi
echo "Results:"
influx -database ${db} -precision rfc3339 -execute "select * from m"
echo "PASS +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo "End test2 ******************************************************"
}
# test3 writes 2 points in 1 request. One point could potentially fall in either
# and the other point can only fall in the second shard.
function test3() {
echo "Start test3 *****************************************************"
setupTest
local t1=$((t0 + 3600))
local t2=$(addDays $t0 7)
t2=$((t2 + 3600))
curl -f -XPOST "http://localhost:8086/write?db=${db}&rp=${rp}&precision=s" --data-binary "m v=3 ${t1}
m v=4 ${t2}"
if [ $? -ne 0 ]; then
echo "FAIL -----------------------------------------------------------"
exit $?
fi
echo "Results:"
influx -database ${db} -precision rfc3339 -execute "select * from m"
echo "PASS +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo "End test3 ******************************************************"
}
# Run the tests.
test1
echo ""
test2
echo ""
test3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment