From a47f78f49103ae96d18de3ccd9ce44e965a9a0d7 Mon Sep 17 00:00:00 2001 From: Tomek Mrugalski Date: Thu, 23 Jun 2016 14:27:04 +0200 Subject: [PATCH] renamed classes and files, fixed issues, added comments # Conflicts: # ChangeLog # src/lib/dhcpsrv/cfg_hosts.cc --- configure.ac | 6 +- src/bin/admin/admin-utils.sh | 21 +++-- src/bin/admin/kea-admin.in | 80 ++++++++-------- src/bin/admin/tests/cql_tests.sh | 38 -------- src/bin/admin/tests/cql_tests.sh.in | 92 +++++++++++++++++-- src/bin/admin/tests/dhcpdb_create_1.0.cql | 11 +-- src/lib/dhcpsrv/cql_connection.cc | 7 +- src/lib/dhcpsrv/cql_connection.h | 4 +- src/lib/dhcpsrv/dhcpsrv_messages.mes | 33 ++++--- src/lib/dhcpsrv/lease_mgr_factory.cc | 4 +- src/lib/dhcpsrv/mysql_lease_mgr.h | 2 +- src/lib/dhcpsrv/pgsql_lease_mgr.cc | 2 +- src/lib/dhcpsrv/pgsql_lease_mgr.h | 2 +- .../dhcpsrv/tests/cql_lease_mgr_unittest.cc | 16 ++-- src/lib/dhcpsrv/testutils/Makefile.am | 13 +++ src/lib/dhcpsrv/testutils/cql_schema.cc | 36 ++++++-- src/lib/dhcpsrv/testutils/cql_schema.h | 33 ++++++- src/lib/dhcpsrv/testutils/pgsql_schema.cc | 2 +- src/share/database/scripts/cql/Makefile.am | 3 +- .../scripts/mysql/dhcpdb_create.mysql | 2 +- 20 files changed, 248 insertions(+), 159 deletions(-) mode change 100644 => 100755 src/bin/admin/admin-utils.sh delete mode 100644 src/bin/admin/tests/cql_tests.sh diff --git a/configure.ac b/configure.ac index 8d0cf5b409..a5e390ee80 100755 --- a/configure.ac +++ b/configure.ac @@ -1012,11 +1012,9 @@ if test "$CQL_CONFIG" != "" ; then AC_MSG_ERROR([--with-cql should point to a cql_config program]) fi - CQL_CPPFLAGS=`$CQL_CONFIG --cppflags` CQL_INCLUDEDIR=`$CQL_CONFIG --includedir` - CQL_CPPFLAGS="$CQL_CPPFLAGS -I$CQL_INCLUDEDIR" - CQL_LIBS=`$CQL_CONFIG --libdir` - CQL_LIBS="-L$CQL_LIBS -lcassandra_static -luv" + CQL_CPPFLAGS="`$CQL_CONFIG --cppflags` -I$CQL_INCLUDEDIR" + CQL_LIBS="-L`$CQL_CONFIG --libdir` -lcassandra_static -luv" CQL_VERSION=`$CQL_CONFIG --version` AC_SUBST(CQL_CPPFLAGS) diff --git a/src/bin/admin/admin-utils.sh b/src/bin/admin/admin-utils.sh old mode 100644 new mode 100755 index 1596c88a5f..03ec7dc5ff --- a/src/bin/admin/admin-utils.sh +++ b/src/bin/admin/admin-utils.sh @@ -48,11 +48,11 @@ pgsql_execute() { QUERY=$1 shift if [ $# -gt 0 ]; then - echo $QUERY | psql --set ON_ERROR_STOP=1 -A -t -q $* + echo $QUERY | psql --set ON_ERROR_STOP=1 -A -t -h localhost -q $* retcode=$? else export PGPASSWORD=$db_password - echo $QUERY | psql --set ON_ERROR_STOP=1 -A -t -q -U $db_user -d $db_name + echo $QUERY | psql --set ON_ERROR_STOP=1 -A -t -h localhost -q -U $db_user -d $db_name retcode=$? fi return $retcode @@ -72,11 +72,11 @@ pgsql_execute_script() { file=$1 shift if [ $# -gt 0 ]; then - psql --set ON_ERROR_STOP=1 -A -t -q -f $file $* + psql --set ON_ERROR_STOP=1 -A -t -h localhost -q -f $file $* retcode=$? else export PGPASSWORD=$db_password - psql --set ON_ERROR_STOP=1 -A -t -q -U $db_user -d $db_name -f $file + psql --set ON_ERROR_STOP=1 -A -t -h localhost -q -U $db_user -d $db_name -h localhost -f $file retcode=$? fi return $retcode @@ -88,14 +88,19 @@ pgsql_version() { } cql_execute() { - QUERY=$1 + query=$1 shift if [ $# -gt 1 ]; then - cqlsh $* -e "${QUERY}" + cqlsh $* -e "$query" retcode=$? else - cqlsh -u $db_user -p $db_password -e "${QUERY}" -k $db_name - retcode="$?" + cqlsh -u $db_user -p $db_password -k $db_name -e "$query" + retcode=$? + fi + + if [ $retcode -ne 0 ]; then + printf "cqlsh returned with exit status $retcode\n" + exit $retcode fi return $retcode diff --git a/src/bin/admin/kea-admin.in b/src/bin/admin/kea-admin.in index 84ac9efb07..1cd45475b4 100644 --- a/src/bin/admin/kea-admin.in +++ b/src/bin/admin/kea-admin.in @@ -201,44 +201,31 @@ pgsql_init() { } cql_init() { - printf "Checking if there is a database initialized already. Please ignore errors.\n" - - # Let's try to count the number of tables. Anything above 0 means that there - # is some database in place. If there is anything, we abort. Note that - # cql may spit out connection or access errors to stderr, we ignore those. - # We should not hide them as they may give hints to user what is wrong with - # his setup. - # - RESULT=`echo "DESCRIBE keyspaces;" | cqlsh` - ERRCODE=$? - if [ $ERRCODE -ne 0 ] - then - log_error "cql_init table query failed, cqlsh status = $ERRCODE" - exit 1 + printf "Checking if there is a database initialized already... Please ignore errors.\n" + + result=`cql_execute "DESCRIBE KEYSPACES;"` + if [ "$result" != "" ]; then + result=`echo "$result" | sed -n "/$db_name/ p"` + if [ "$result" = "" ]; then + printf "Creating keyspace $db_name...\n" + cql_execute "CREATE KEYSPACE $db_name WITH replication = {'class' : 'SimpleStrategy','replication_factor' : 1};" + else + printf "Keyspace $db_name already exists.\n" + fi fi - COUNT=`echo $RESULT | tr " " "\n" | grep $db_name | wc -w` - if [ $COUNT -gt 0 ]; then - # Let't start with a new line. cqlsh could have printed something out. - printf "\n" - log_error "Expected no database $db_name, but there are $COUNT databases: \n$RESULT. Aborting." - exit 1 + result=`cql_execute "USE $db_name; DESCRIBE tables;"` + if [ "$result"="" ]; then + printf "Creating and initializing tables using script %s...\n" $scripts_dir/cql/dhcpdb_create.cql + cql_execute_script $scripts_dir/cql/dhcpdb_create.cql + else + printf "Tables are already created.\n" fi - printf "Initializing database using script %s\n" $scripts_dir/cql/dhcpdb_create.cql - cqlsh -u $db_user -p $db_password -e "CREATE KEYSPACE $db_name WITH replication = {'class' : 'SimpleStrategy','replication_factor' : 1};" - cqlsh -u $db_user -p $db_password -k $db_name -f $scripts_dir/cql/dhcpdb_create.cql - ERRCODE=$? - - printf "cqlsh returned status code $ERRCODE\n" - - if [ "$ERRCODE" -eq 0 ]; then - printf "Lease DB version reported after initialization: " - cql_version - printf "\n" - fi + version=`cql_version` + printf "Lease DB version reported after initialization: $version\n" - exit $ERRCODE + return 0 } ### Functions that implement database version checking commands @@ -333,22 +320,27 @@ cql_upgrade() { exit 1 fi - # Check if there are any files in it - num_files=$(find ${scripts_dir}/cql/upgrade*.sh -type f | wc -l) - if [ $num_files -eq 0 ]; then - log_error "No scripts in ${scripts_dir}/cql or the directory is not readable or does not have any upgrade* scripts." + # Check if directory is readable. + if [ ! -r ${scripts_dir}/cql ]; then + log_error "Directory is not readable: ${scripts_dir}/cql" exit 1 fi - for script in ${scripts_dir}/cql/upgrade*.sh - do - echo "Processing $script file..." - sh ${script} -u ${db_user} -p ${db_password} -k ${db_name} - done + # Check if there are upgrade scripts. + files=$(find ${scripts_dir}/cql/upgrade*.sh -type f) + if [ $? -eq 0 ]; then # Upgrade scripts are present. + for script in ${scripts_dir}/cql/upgrade*.sh + do + echo "Processing $script file..." + sh ${script} -u ${db_user} -p ${db_password} -k ${db_name} + done + else + echo "No upgrade script available." + fi version=`cql_version` printf "Lease DB version reported after upgrade: $version\n" - exit 0 + return 0 } # Utility function which tests if the given file exists and @@ -543,7 +535,7 @@ cql_dump() { # delete the tmp file on success rm $tmp_file echo lease$dump_type successfully dumped to $dump_file - exit 0 + return 0 } ### Script starts here ### diff --git a/src/bin/admin/tests/cql_tests.sh b/src/bin/admin/tests/cql_tests.sh deleted file mode 100644 index 5919d6005d..0000000000 --- a/src/bin/admin/tests/cql_tests.sh +++ /dev/null @@ -1,38 +0,0 @@ -#!/bin/sh - -# Copyright (C) 2014-2015 Internet Systems Consortium, Inc. ("ISC") -# -# This Source Code Form is subject to the terms of the Mozilla Public -# License, v. 2.0. If a copy of the MPL was not distributed with this -# file, You can obtain one at http://mozilla.org/MPL/2.0/. - -# Include common test library. -. /home/andrei/work/git/isc-kea-integration/kea/src/lib/testutils/dhcp_test_lib.sh - -cql_init_test() { - test_start "cql.init" - - # @todo: Implement this - - test_finish 0 -} - -cql_version_test() { - test_start "cql.version" - - # @todo: Implement this - - test_finish 0 -} - -cql_upgrade_test() { - test_start "cql.upgrade" - - # @todo: Implement this - - test_finish 0 -} - -cql_init_test -cql_version_test -cql_upgrade_test diff --git a/src/bin/admin/tests/cql_tests.sh.in b/src/bin/admin/tests/cql_tests.sh.in index c65121da3d..fc5d5b5634 100644 --- a/src/bin/admin/tests/cql_tests.sh.in +++ b/src/bin/admin/tests/cql_tests.sh.in @@ -9,26 +9,106 @@ # Include common test library. . @abs_top_builddir@/src/lib/testutils/dhcp_test_lib.sh +# Include admin utilities +. @abs_top_srcdir@/src/bin/admin/admin-utils.sh + +# Set path to the production schema scripts +db_scripts_dir=@abs_top_srcdir@/src/share/database/scripts + +db_user="keatest" +db_password="keatest" +db_name="keatest" + +# Set location of the kea-admin. +keaadmin=@abs_top_builddir@/src/bin/admin/kea-admin + cql_init_test() { test_start "cql.init" - - # @todo: Implement this + + # Wipe the database. + cql_execute_script $db_scripts_dir/cql/dhcpdb_drop.cql + assert_eq 0 $? "drop table query failed, exit code %d, expected %d" + + # Create the database + $keaadmin lease-init cql -u $db_user -p $db_password -n $db_name -d $db_scripts_dir + assert_eq 0 $? "kea-admin lease-init cql failed, expected exit code: %d, actual: %d" + + # Verify that all the expected tables exist + + # Check schema_version table + cql_execute "SELECT version, minor FROM schema_version;" + assert_eq 0 $? "schema_version table check failed, expected exit code: %d, actual: %d" + + # Check lease4 table + cql_execute "SELECT address, hwaddr, client_id, valid_lifetime, expire, subnet_id, fqdn_fwd, fqdn_rev, hostname, state FROM lease4;" + assert_eq 0 $? "lease4 table check failed, expected exit code: %d, actual: %d" + + # Check lease6 table + cql_execute "SELECT address, duid, valid_lifetime, expire, subnet_id, pref_lifetime, lease_type, iaid, prefix_len, fqdn_fwd, fqdn_rev, hostname,\ + state FROM lease6;" + assert_eq 0 $? "lease6 table check failed, expected exit code: %d, actual: %d" + + # Check lease6_types table + cql_execute "SELECT lease_type, name FROM lease6_types;" + assert_eq 0 $? "lease6_types table check failed, expected exit code: %d, actual: %d" + + # Check lease_state table + cql_execute "SELECT state, name FROM lease_state;" + assert_eq 0 $? "lease_state table check failed, expected exit code: %d, actual: %d" + + # Trying to create it again should fail. This verifies the db present + # check + echo "" + echo "Making sure keyspace creation fails the second time..." + $keaadmin lease-init cql -u $db_user -p $db_password -n $db_name -d $db_scripts_dir + assert_eq 2 $? "kea-admin failed to deny lease-init, expected exit code: %d, actual: %d" + + # Wipe the database. + cql_execute_script $db_scripts_dir/cql/dhcpdb_drop.cql + assert_eq 0 $? "drop table query failed, exit code %d, expected %d" test_finish 0 } cql_version_test() { test_start "cql.version" - - # @todo: Implement this + + # Wipe the database. + cql_execute_script $db_scripts_dir/cql/dhcpdb_drop.cql + assert_eq 0 $? "drop table query failed, exit code %d, expected %d" + + # Create the database + $keaadmin lease-init cql -u $db_user -p $db_password -n $db_name -d $db_scripts_dir + assert_eq 0 $? "kea-admin lease-init cql failed, expected exit code: %d, actual: %d" + + # Verfiy that kea-admin lease-version returns the correct version + version=$($keaadmin lease-version cql -u $db_user -p $db_password -n $db_name) + assert_str_eq "1.0" $version "Expected kea-admin to return %s, returned value was %s" + + # Wipe the database. + cql_execute_script $db_scripts_dir/cql/dhcpdb_drop.cql + assert_eq 0 $? "drop table query failed, exit code %d, expected %d" test_finish 0 } cql_upgrade_test() { test_start "cql.upgrade" - - # @todo: Implement this + + # Wipe the database. + cql_execute_script $db_scripts_dir/cql/dhcpdb_drop.cql + assert_eq 0 $? "drop table query failed, exit code %d, expected %d" + + # Initialize database to schema 1.0. + cql_execute_script @abs_top_srcdir@/src/bin/admin/tests/dhcpdb_create_1.0.cql + assert_eq 0 $? "cannot initialize the database, expected exit code: %d, actual: %d" + + $keaadmin lease-upgrade cql -u $db_user -p $db_password -n $db_name -d $db_scripts_dir + assert_eq 0 $? "lease-upgrade failed, expected exit code: %d, actual: %d" + + # Wipe the database. + cql_execute_script $db_scripts_dir/cql/dhcpdb_drop.cql + assert_eq 0 $? "drop table query failed, exit code %d, expected %d" test_finish 0 } diff --git a/src/bin/admin/tests/dhcpdb_create_1.0.cql b/src/bin/admin/tests/dhcpdb_create_1.0.cql index 8104859cfb..06c594f9bd 100644 --- a/src/bin/admin/tests/dhcpdb_create_1.0.cql +++ b/src/bin/admin/tests/dhcpdb_create_1.0.cql @@ -31,7 +31,7 @@ -- SOURCE dhcpdb_create.cql --- This script is also called from kea-admin, see kea-admin init cql +-- This script is also called from kea-admin, see kea-admin lease-init cql -- Over time, Kea database schema will evolve. Each version is marked with -- major.minor version. This file is organized sequentially, i.e. database @@ -103,11 +103,10 @@ CREATE TABLE lease6_types ( name varchar, -- Name of the lease type PRIMARY KEY (lease_type) ); ---START TRANSACTION; INSERT INTO lease6_types (lease_type, name) VALUES (0, 'IA_NA'); -- Non-temporary v6 addresses INSERT INTO lease6_types (lease_type, name) VALUES (1, 'IA_TA'); -- Temporary v6 addresses INSERT INTO lease6_types (lease_type, name) VALUES (2, 'IA_PD'); -- Prefix delegations ---COMMIT; + -- Kea keeps track of the hardware/MAC address source, i.e. how the address -- was obtained. Depending on the technique and your network topology, it may @@ -199,15 +198,9 @@ INSERT INTO lease_state (state, name) VALUES (2, 'expired-reclaimed'); -- (related to the names of the columns in the BIND 10 DNS database file), the -- first column is called "version" and not "major". --- NOTE: this MUST be kept in step with src/lib/dhcpsrv/tests/schema_copy.h, --- which defines the schema for the unit tests. If you are updating --- the version number, the schema has changed: please ensure that --- schema_copy.h has been updated as well. CREATE TABLE schema_version ( version int, minor int, PRIMARY KEY (version) ); ---START TRANSACTION; INSERT INTO schema_version (version, minor) VALUES (1, 0); ---COMMIT; diff --git a/src/lib/dhcpsrv/cql_connection.cc b/src/lib/dhcpsrv/cql_connection.cc index 13795105f8..9b43486ac4 100644 --- a/src/lib/dhcpsrv/cql_connection.cc +++ b/src/lib/dhcpsrv/cql_connection.cc @@ -119,7 +119,8 @@ CqlConnection::openDatabase() { session_ = cass_session_new(); - CassFuture* connect_future = cass_session_connect_keyspace(session_, cluster_, keyspace); + CassFuture* connect_future = cass_session_connect_keyspace(session_, + cluster_, keyspace); cass_future_wait(connect_future); std::string error; checkStatementError(error, connect_future, "could not connect to DB"); @@ -199,9 +200,9 @@ CqlConnection::rollback() { LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_CQL_ROLLBACK); } - void -CqlConnection::checkStatementError(std::string& error, CassFuture* future, uint32_t stindex, const char* what) const +CqlConnection::checkStatementError(std::string& error, CassFuture* future, + uint32_t stindex, const char* what) const { CassError rc; const char* errorMessage; diff --git a/src/lib/dhcpsrv/cql_connection.h b/src/lib/dhcpsrv/cql_connection.h index ad3caa3148..4fbb107210 100644 --- a/src/lib/dhcpsrv/cql_connection.h +++ b/src/lib/dhcpsrv/cql_connection.h @@ -79,7 +79,7 @@ public: /// /// @return Type of the backend. virtual std::string getType() const { - return (std::string("cassandra")); + return (std::string("cql")); } /// @brief Returns name of the database. @@ -133,7 +133,7 @@ public: CassCluster* cluster_; CassSession* session_; std::vector statements_; ///< Prepared statements - CqlTaggedStatement *tagged_statements_; + CqlTaggedStatement* tagged_statements_; }; }; // end of isc::dhcp namespace diff --git a/src/lib/dhcpsrv/dhcpsrv_messages.mes b/src/lib/dhcpsrv/dhcpsrv_messages.mes index 97cbc170ef..2ecf516671 100644 --- a/src/lib/dhcpsrv/dhcpsrv_messages.mes +++ b/src/lib/dhcpsrv/dhcpsrv_messages.mes @@ -326,9 +326,9 @@ IPv4 leases from the memory file database for a client with the specified hardware address. % DHCPSRV_MEMFILE_GET_IAID_DUID obtaining IPv6 leases for IAID %1 and DUID %2 and lease type %3 -A debug message issued when the server is attempting to obtain a set of -IPv6 lease from the memory file database for a client with the specified -IAID (Identity Association ID) and DUID (DHCP Unique Identifier). +A debug message issued when the server is attempting to obtain a set of IPv6 +leases from the memory file database for a client with the specified IAID +(Identity Association ID) and DUID (DHCP Unique Identifier). % DHCPSRV_MEMFILE_GET_IAID_SUBID_DUID obtaining IPv6 leases for IAID %1, Subnet ID %2, DUID %3 and lease type %4 A debug message issued when the server is attempting to obtain an IPv6 @@ -518,9 +518,9 @@ of IPv4 leases from the MySQL database for a client with the specified hardware address. % DHCPSRV_MYSQL_GET_IAID_DUID obtaining IPv6 leases for IAID %1, DUID %2, lease type %3 -A debug message issued when the server is attempting to obtain a set of -IPv6 lease from the MySQL database for a client with the specified IAID -(Identity Association ID) and DUID (DHCP Unique Identifier). +A debug message issued when the server is attempting to obtain a set of IPv6 +leases from the MySQL database for a client with the specified IAID (Identity +Association ID) and DUID (DHCP Unique Identifier). % DHCPSRV_MYSQL_GET_IAID_SUBID_DUID obtaining IPv6 leases for IAID %1, Subnet ID %2, DUID %3, lease type %4 A debug message issued when the server is attempting to obtain an IPv6 @@ -663,8 +663,8 @@ of IPv4 leases from the PostgreSQL database for a client with the specified hardware address. % DHCPSRV_PGSQL_GET_IAID_DUID obtaining IPv4 leases for IAID %1 and DUID %2, lease type %3 -A debug message issued when the server is attempting to obtain a set of -IPv6 lease from the PostgreSQL database for a client with the specified IAID +A debug message issued when the server is attempting to obtain a set of IPv6 +leases from the PostgreSQL database for a client with the specified IAID (Identity Association ID) and DUID (DHCP Unique Identifier). % DHCPSRV_PGSQL_GET_IAID_SUBID_DUID obtaining IPv4 leases for IAID %1, Subnet ID %2, DUID %3, and lease type %4 @@ -818,7 +818,7 @@ A debug message issued when the server is about to add an IPv6 lease with the specified address to the Cassandra backend database. % DHCPSRV_CQL_COMMIT committing to Cassandra database -The code has issued a commit call. +A commit call been issued on the server. For Cassandra, this is a no-op. % DHCPSRV_CQL_DB opening Cassandra lease database: %1 This informational message is logged when a DHCP server (either V4 or @@ -827,9 +827,8 @@ the connection including database name and username needed to access it (but not the password if any) are logged. % DHCPSRV_CQL_DELETE_ADDR deleting lease for address %1 -A debug message issued when the server is attempting to delete a lease -for the specified address from the Cassandra database for the specified -address. +A debug message issued when the server is attempting to delete a lease from the +Cassandra database for the specified address. % DHCPSRV_CQL_DELETE_EXPIRED_RECLAIMED4 deleting reclaimed IPv4 leases that expired more than %1 seconds ago A debug message issued when the server is removing reclaimed DHCPv4 @@ -877,9 +876,9 @@ IPv4 leases from the Cassandra database for a client with the specified hardware address. % DHCPSRV_CQL_GET_IAID_DUID obtaining IPv6 leases for IAID %1 and DUID %2 and lease type %3 -A debug message issued when the server is attempting to obtain a set of -IPv6 lease from the Cassandra database for a client with the specified -IAID (Identity Association ID) and DUID (DHCP Unique Identifier). +A debug message issued when the server is attempting to obtain a set of IPv6 +leases from the Cassandra database for a client with the specified IAID +(Identity Association ID) and DUID (DHCP Unique Identifier). % DHCPSRV_CQL_GET_IAID_SUBID_DUID obtaining IPv6 leases for IAID %1, Subnet ID %2, DUID %3 and lease type %4 A debug message issued when the server is attempting to obtain an IPv6 @@ -901,7 +900,8 @@ A debug message issued when the server is about to obtain schema version information from the Cassandra database. % DHCPSRV_CQL_ROLLBACK rolling back Cassandra database -The code has issued a rollback call. +The code has issued a rollback call. For Cassandra, this is +a no-op. % DHCPSRV_CQL_UPDATE_ADDR4 updating IPv4 lease for address %1 A debug message issued when the server is attempting to update IPv4 @@ -910,4 +910,3 @@ lease from the Cassandra database for the specified address. % DHCPSRV_CQL_UPDATE_ADDR6 updating IPv6 lease for address %1 A debug message issued when the server is attempting to update IPv6 lease from the Cassandra database for the specified address. - diff --git a/src/lib/dhcpsrv/lease_mgr_factory.cc b/src/lib/dhcpsrv/lease_mgr_factory.cc index 0d18d6fa5d..5f2f33a4f3 100755 --- a/src/lib/dhcpsrv/lease_mgr_factory.cc +++ b/src/lib/dhcpsrv/lease_mgr_factory.cc @@ -74,7 +74,7 @@ LeaseMgrFactory::create(const std::string& dbaccess) { #ifdef HAVE_CQL if (parameters[type] == string("cql")) { LOG_INFO(dhcpsrv_logger, DHCPSRV_CQL_DB).arg(redacted); - getLeaseMgrPtr().reset(new CQLLeaseMgr(parameters)); + getLeaseMgrPtr().reset(new CqlLeaseMgr(parameters)); return; } #endif @@ -87,7 +87,7 @@ LeaseMgrFactory::create(const std::string& dbaccess) { // Get here on no match LOG_ERROR(dhcpsrv_logger, DHCPSRV_UNKNOWN_DB).arg(parameters[type]); isc_throw(InvalidType, "Database access parameter 'type' does " - "not specify a supported database backend"); + "not specify a supported database backend:" << parameters[type]); } void diff --git a/src/lib/dhcpsrv/mysql_lease_mgr.h b/src/lib/dhcpsrv/mysql_lease_mgr.h index e328711b6b..a5824becbb 100755 --- a/src/lib/dhcpsrv/mysql_lease_mgr.h +++ b/src/lib/dhcpsrv/mysql_lease_mgr.h @@ -376,7 +376,7 @@ public: /// Commits all pending database operations. On databases that don't /// support transactions, this is a no-op. /// - /// @throw DbOperationError Iif the commit failed. + /// @throw DbOperationError If the commit failed. virtual void commit(); /// @brief Rollback Transactions diff --git a/src/lib/dhcpsrv/pgsql_lease_mgr.cc b/src/lib/dhcpsrv/pgsql_lease_mgr.cc index 72d5a129b8..19be2d0a93 100644 --- a/src/lib/dhcpsrv/pgsql_lease_mgr.cc +++ b/src/lib/dhcpsrv/pgsql_lease_mgr.cc @@ -159,7 +159,7 @@ PgSqlTaggedStatement tagged_statements[] = { // INSERT_LEASE4 { 10, { OID_INT8, OID_BYTEA, OID_BYTEA, OID_INT8, OID_TIMESTAMP, OID_INT8, - OID_BOOL, OID_BOOL, OID_VARCHAR, OID_INT8, OID_INT8 }, + OID_BOOL, OID_BOOL, OID_VARCHAR, OID_INT8 }, "insert_lease4", "INSERT INTO lease4(address, hwaddr, client_id, " "valid_lifetime, expire, subnet_id, fqdn_fwd, fqdn_rev, hostname, " diff --git a/src/lib/dhcpsrv/pgsql_lease_mgr.h b/src/lib/dhcpsrv/pgsql_lease_mgr.h index 396cc45855..e871b90c0f 100644 --- a/src/lib/dhcpsrv/pgsql_lease_mgr.h +++ b/src/lib/dhcpsrv/pgsql_lease_mgr.h @@ -351,7 +351,7 @@ public: /// /// Commits all pending database operations. /// - /// @throw DbOperationError Iif the commit failed. + /// @throw DbOperationError If the commit failed. virtual void commit(); /// @brief Rollback Transactions diff --git a/src/lib/dhcpsrv/tests/cql_lease_mgr_unittest.cc b/src/lib/dhcpsrv/tests/cql_lease_mgr_unittest.cc index 7b62bd23d0..616b64f518 100644 --- a/src/lib/dhcpsrv/tests/cql_lease_mgr_unittest.cc +++ b/src/lib/dhcpsrv/tests/cql_lease_mgr_unittest.cc @@ -45,8 +45,8 @@ public: CqlLeaseMgrTest() { // Ensure schema is the correct one. - destroyCqlSchema(); - createCqlSchema(); + destroyCqlSchema(false, true); + createCqlSchema(false, true); // Connect to the database try { @@ -69,7 +69,7 @@ public: virtual ~CqlLeaseMgrTest() { lmptr_->rollback(); LeaseMgrFactory::destroy(); - destroyCqlSchema(); + destroyCqlSchema(false, true); } /// @brief Reopen the database @@ -97,8 +97,8 @@ public: TEST(CQLOpenTest, OpenDatabase) { // Schema needs to be created for the test to work. - destroyCqlSchema(); - createCqlSchema(); + destroyCqlSchema(false, true); + createCqlSchema(false, true); // Check that lease manager open the database opens correctly and tidy up. // If it fails, print the error message. @@ -147,15 +147,15 @@ TEST(CQLOpenTest, OpenDatabase) { NoDatabaseName); // Tidy up after the test - destroyCqlSchema(); + destroyCqlSchema(false, true); } /// @brief Check the getType() method /// /// getType() returns a string giving the type of the backend, which should -/// always be "cassandra". +/// always be "cql". TEST_F(CqlLeaseMgrTest, getType) { - EXPECT_EQ(std::string("cassandra"), lmptr_->getType()); + EXPECT_EQ(std::string("cql"), lmptr_->getType()); } /// @brief Check conversion functions diff --git a/src/lib/dhcpsrv/testutils/Makefile.am b/src/lib/dhcpsrv/testutils/Makefile.am index 7ff34a859e..7f62aa3775 100644 --- a/src/lib/dhcpsrv/testutils/Makefile.am +++ b/src/lib/dhcpsrv/testutils/Makefile.am @@ -20,6 +20,10 @@ libdhcpsrvtest_la_SOURCES += schema.cc schema.h else if HAVE_PGSQL libdhcpsrvtest_la_SOURCES += schema.cc schema.h +else +if HAVE_CQL +libdhcpsrvtest_la_SOURCES += schema.cc schema.h +endif endif endif @@ -29,6 +33,9 @@ endif if HAVE_PGSQL libdhcpsrvtest_la_SOURCES += pgsql_schema.cc pgsql_schema.h endif +if HAVE_CQL +libdhcpsrvtest_la_SOURCES += cql_schema.cc cql_schema.h +endif libdhcpsrvtest_la_CXXFLAGS = $(AM_CXXFLAGS) libdhcpsrvtest_la_CPPFLAGS = $(AM_CPPFLAGS) $(GTEST_INCLUDES) @@ -38,6 +45,9 @@ endif if HAVE_PGSQL libdhcpsrvtest_la_CPPFLAGS += $(PGSQL_CPPFLAGS) endif +if HAVE_CQL +libdhcpsrvtest_la_CPPFLAGS += $(CQL_CPPFLAGS) +endif libdhcpsrvtest_la_LDFLAGS = $(AM_LDFLAGS) if HAVE_MYSQL @@ -46,6 +56,9 @@ endif if HAVE_PGSQL libdhcpsrvtest_la_LDFLAGS += $(PGSQL_LIBS) endif +if HAVE_CQL +libdhcpsrvtest_la_LDFLAGS += $(CQL_LIBS) +endif libdhcpsrvtest_la_LIBADD = $(top_builddir)/src/lib/cc/libkea-cc.la libdhcpsrvtest_la_LIBADD += $(top_builddir)/src/lib/log/libkea-log.la diff --git a/src/lib/dhcpsrv/testutils/cql_schema.cc b/src/lib/dhcpsrv/testutils/cql_schema.cc index c0f602b563..20110da2a3 100644 --- a/src/lib/dhcpsrv/testutils/cql_schema.cc +++ b/src/lib/dhcpsrv/testutils/cql_schema.cc @@ -21,7 +21,7 @@ namespace isc { namespace dhcp { namespace test { -const char* CQL_VALID_TYPE = "type=cassandra"; +const char* CQL_VALID_TYPE = "type=cql"; string validCqlConnectionString() { @@ -29,25 +29,45 @@ validCqlConnectionString() { VALID_USER, VALID_PASSWORD)); } -void destroyCqlSchema(bool show_err) { - runCqlScript(DATABASE_SCRIPTS_DIR, "cql/dhcpdb_drop.cql", show_err); +bool softWipeEnabled() { + const char* const env = getenv("KEA_TEST_CASSANDRA_WIPE"); + if (env && (string(env) == string("soft"))) { + return (true); + } + + return (false); } -void createCqlSchema(bool show_err) { - runCqlScript(DATABASE_SCRIPTS_DIR, "cql/dhcpdb_create.cql", - show_err); +void destroyCqlSchema(bool , bool show_err) { +// if (force_wipe || !softWipeEnabled()) { + // Do full wipe + runCqlScript(DATABASE_SCRIPTS_DIR, "cql/dhcpdb_drop.cql", show_err); +// } else { + + // do soft wipe (just remove the data, not the structures) +// runCqlScript(DATABASE_SCRIPTS_DIR, "cql/soft_wipe.cql", show_err); +// } +} + +void createCqlSchema(bool force_wipe, bool show_err) { + if (force_wipe || !softWipeEnabled()) { + runCqlScript(DATABASE_SCRIPTS_DIR, "cql/dhcpdb_create.cql", + show_err); + } } void runCqlScript(const std::string& path, const std::string& script_name, bool show_err) { std::ostringstream cmd; - cmd << "cqlsh -u keatest -p keatest -k keatest -f"; + cmd << "cqlsh -u keatest -p keatest -k keatest"; if (!show_err) { cmd << " 2>/dev/null "; } + cmd << " -f"; + if (!path.empty()) { - cmd << " < " << path << "/"; + cmd << path << "/"; } cmd << script_name; diff --git a/src/lib/dhcpsrv/testutils/cql_schema.h b/src/lib/dhcpsrv/testutils/cql_schema.h index cacb32cad3..7724a50f18 100644 --- a/src/lib/dhcpsrv/testutils/cql_schema.h +++ b/src/lib/dhcpsrv/testutils/cql_schema.h @@ -32,8 +32,10 @@ std::string validCqlConnectionString(); /// will fail. The output of stderr is suppressed unless the parameter, /// show_err is true. /// +/// @param force_wipe forces wipe of the database, even if KEA_TEST_CASSANDRA_WIPE +/// is set. /// @param show_err flag which governs whether or not stderr is suppressed. -void destroyCqlSchema(bool show_err = false); +void destroyCqlSchema(bool force_wipe, bool show_err = false); /// @brief Create the CQL Schema /// @@ -45,10 +47,12 @@ void destroyCqlSchema(bool show_err = false); /// will fail. The output of stderr is suppressed unless the parameter, /// show_err is true. /// +/// @param force_wipe forces wipe of the database, even if KEA_TEST_CASSANDRA_WIPE +/// is set. /// @param show_err flag which governs whether or not stderr is suppressed. -void createCqlSchema(bool show_err = false); +void createCqlSchema(bool force_wipe, bool show_err = false); -/// @brief Run a CQL SQL script against the CQL unit test database +/// @brief Run a CQL script against the CQL unit test database /// /// Submits the given SQL script to CQL via cqlsh CLI. The output of /// stderr is suppressed unless the parameter, show_err is true. The is done @@ -61,6 +65,29 @@ void createCqlSchema(bool show_err = false); void runCqlScript(const std::string& path, const std::string& script_name, bool show_err); +/// @brief Returns status if the soft-wipe is enabled or not. +/// +/// In some deployments (In case of Tomek's dev system) Cassandra tests take +/// a very long time to execute. This was traced back to slow table/indexes +/// creation/deletion. With full wipe and recreation of all structures, it +/// took over 60 seconds for each test to execute. To avoid this problem, a +/// feature called soft-wipe has been implemented. If enabled, it does not +/// remove the structures, just the data from essential tables. To enable +/// it set KEA_TEST_CASSANDRA_WIPE environment variable to 'soft'. Make sure +/// that the database schema is set up properly before running in soft-wipe +/// mode. +/// +/// For example to use soft-wipe mode, you can: +/// +/// $ cqlsh -u keatest -p keatest -k keatest +/// -f src/share/database/scripts/cql/dhcpdb_create.cql +/// $ export KEA_TEST_CASSANDRA_WIPE=soft +/// $ cd src/lib/dhcpsrv +/// $ make -j9 +/// $ tests/libdhcpsrv_unittests --gtest_filter=CqlLeaseMgrTest.* +/// +/// @return true if soft-wipe is enabled, false otherwise +bool softWipeEnabled(); }; }; }; diff --git a/src/lib/dhcpsrv/testutils/pgsql_schema.cc b/src/lib/dhcpsrv/testutils/pgsql_schema.cc index beb80deef0..319fafd5ba 100644 --- a/src/lib/dhcpsrv/testutils/pgsql_schema.cc +++ b/src/lib/dhcpsrv/testutils/pgsql_schema.cc @@ -49,7 +49,7 @@ void runPgSQLScript(const std::string& path, const std::string& script_name, } cmd << script_name - << " | psql --set ON_ERROR_STOP=1 -A -t -q -U keatest -d keatest"; + << " | psql --set ON_ERROR_STOP=1 -A -t -h localhost -q -U keatest -d keatest"; if (!show_err) { cmd << " 2>/dev/null "; diff --git a/src/share/database/scripts/cql/Makefile.am b/src/share/database/scripts/cql/Makefile.am index 400bcfcc6d..ee335b0a83 100644 --- a/src/share/database/scripts/cql/Makefile.am +++ b/src/share/database/scripts/cql/Makefile.am @@ -1,7 +1,6 @@ SUBDIRS = . sqlscriptsdir = ${datarootdir}/${PACKAGE_NAME}/scripts/cql -sqlscripts_DATA = dhcpdb_create.cql -sqlscripts_DATA += dhcpdb_drop.cql +sqlscripts_DATA = dhcpdb_create.cql dhcpdb_drop.cql EXTRA_DIST = ${sqlscripts_DATA} diff --git a/src/share/database/scripts/mysql/dhcpdb_create.mysql b/src/share/database/scripts/mysql/dhcpdb_create.mysql index 94524c2571..6cc866ed08 100755 --- a/src/share/database/scripts/mysql/dhcpdb_create.mysql +++ b/src/share/database/scripts/mysql/dhcpdb_create.mysql @@ -19,7 +19,7 @@ # # source dhcpdb_create.mysql # -# This script is also called from kea-admin, see kea-admin init mysql +# This script is also called from kea-admin, see kea-admin lease-init mysql # # Over time, Kea database schema will evolve. Each version is marked with # major.minor version. This file is organized sequentially, i.e. database -- 2.47.2