summaryrefslogtreecommitdiffstats
path: root/ctdb/tests/INTEGRATION/database/readonly.001.basic.sh
blob: aeb974028e65b997ce413effe94800169f566bda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env bash

# Test support for read-only records

# Read-only records can be activated at runtime using a ctdb command.
# If read-only records are not activated, then any attempt to fetch a
# read-only copy should be automatically upgraded to a read-write
# fetch_locked().

# If read-only delegations are present, then any attempt to acquire a
# read-write fetch_lock will trigger revocation of all delegations
# before the fetch_locked().

# 1. Create a test database and some records
# 2. Try to fetch read-only records, this should not result in any delegations
# 3. Activate read-only support
# 4. Try to fetch read-only records, this should result in delegations
# 5. Do a fetchlock  and the delegations should be revoked
# 6. Try to fetch read-only records, this should result in delegations

. "${TEST_SCRIPTS_DIR}/integration.bash"

set -e

ctdb_test_init

######################################################################

# Confirm that no nodes have databases with read-only delegations
check_no_readonly ()
{
    try_command_on_node all $CTDB cattdb $testdb
    local ro_flags="RO_HAVE_READONLY|RO_HAVE_DELEGATIONS"
    local numreadonly=$(grep -c -E "$ro_flags" "$outfile") || true
    if [ $numreadonly -eq 0 ] ; then
	echo "GOOD: no read-only delegations"
    else
	echo "BAD: there are read-only delegations"
	cat "$outfile"
	exit 1
    fi
}

# Check that the test record has the correct read-only flags on the
# given nodes.  The first node is the dmaster, which should know there
# are delegations but should not be flagged as having a read-only
# copy.  Subsequent nodes should have a read-only copy but not know
# about any (other) delegations.
check_readonly ()
{
    local dmaster="$1" ; shift
    local others="$*"

    local count

    try_command_on_node $dmaster $CTDB cattdb $testdb
    count=$(grep -c -E "RO_HAVE_DELEGATIONS" "$outfile") || true
    if [ $count -eq 1 ] ; then
	echo "GOOD: dmaster ${dmaster} has read-only delegations"
    else
	echo "BAD: dmaster ${dmaster} has no read-only delegations"
	cat "$outfile"
	exit 1
    fi
    count=$(grep -c -E "RO_HAVE_READONLY" "$outfile") || true
    if [ $count -ne 0 ] ; then
	echo "BAD: dmaster ${dmaster} has a read-only copy"
	cat "$outfile"
	exit 1
    fi

    local o
    for o in $others ; do
	try_command_on_node $o $CTDB cattdb $testdb
	count=$(grep -c -E "RO_HAVE_READONLY" "$outfile") || true
	if [ $count -eq 1 ] ; then
	    echo "GOOD: node ${o} has a read-only copy"
	else
	    echo "BAD: node ${o} has no read-only copy"
	    cat "$outfile"
	    exit 1
	fi
	count=$(grep -c -E "RO_HAVE_DELEGATIONS" "$outfile") || true
	if [ $count -ne 0 ] ; then
	    echo "BAD: other node ${o} has read-only delegations"
	    cat "$outfile"
	    exit 1
	fi
    done
}

######################################################################

echo "Get list of nodes..."
ctdb_onnode 0 "-X listnodes"
all_nodes=$(awk -F'|' '{print $2}' "$outfile")

######################################################################

testdb="test.tdb"
echo "Create test database \"${testdb}\""
try_command_on_node 0 $CTDB attach $testdb

echo "Create some records..."
try_command_on_node all $CTDB_TEST_WRAPPER $VALGRIND update_record \
	-D ${testdb} -k testkey

######################################################################

echo "Try some readonly fetches, these should all be upgraded to full fetchlocks..."
try_command_on_node all $CTDB_TEST_WRAPPER $VALGRIND fetch_readonly \
	-D ${testdb} -k testkey

check_no_readonly

######################################################################

echo "Activate read-only record support for \"$testdb\"..."
try_command_on_node all $CTDB setdbreadonly $testdb

# Database should be tagged as READONLY
try_command_on_node 0 $CTDB getdbmap
db_details=$(awk -v db="$testdb" '$2 == foo="name:" db { print }' "$outfile")
if grep -q "READONLY" <<<"$db_details" ; then
    echo "GOOD: read-only record support is enabled"
else
    echo "BAD: could not activate read-only support"
    echo "$db_details"
    exit 1
fi

######################################################################

echo "Create 1 read-only delegation ..."
# dmaster=1
try_command_on_node 1 $CTDB_TEST_WRAPPER $VALGRIND update_record \
	-D ${testdb} -k testkey

# Fetch read-only to node 0
try_command_on_node 0 $CTDB_TEST_WRAPPER $VALGRIND fetch_readonly \
	-D ${testdb} -k testkey

check_readonly 1 0

######################################################################

echo "Verify that a fetchlock revokes read-only delegations..."
# Node 1 becomes dmaster
try_command_on_node 1 $CTDB_TEST_WRAPPER $VALGRIND update_record \
	-D ${testdb} -k testkey

check_no_readonly

######################################################################

echo "Create more read-only delegations..."
dmaster=1
try_command_on_node $dmaster $CTDB_TEST_WRAPPER $VALGRIND update_record \
	-D ${testdb} -k testkey

others=""
for n in $all_nodes ; do
    if [ "$n" != "$dmaster" ] ; then
	# Fetch read-only copy to this node
	try_command_on_node $n $CTDB_TEST_WRAPPER $VALGRIND fetch_readonly \
		-D ${testdb} -k testkey
	others="${others} ${n}"
    fi
done

check_readonly $dmaster $others

######################################################################

echo "Verify that a recovery will revoke the delegations..."
try_command_on_node 0 $CTDB recover

check_no_readonly