fixed lookup for expired leases

initializing database if in inconsistent state
This commit is contained in:
Martin Willi
2008-05-13 07:24:53 +00:00
parent ef31d1161d
commit 5373f2a642
+9 -3
View File
@@ -141,13 +141,14 @@ static host_t* create_lease(private_sql_attribute_t *this,
*/
this->mutex->lock(this->mutex);
/* find an address which has outdated leases only */
/* find an address which has outdated leases only. The HAVING clause filters
* out leases which are active (released = NULL) or not expired */
e = this->db->query(this->db,
"SELECT pool, address, released, timeout FROM leases "
"JOIN pools ON leases.pool = pools.id "
"WHERE name = ? "
"GROUP BY address HAVING released IS NOT NULL "
"AND MAX(released) < (? + timeout) LIMIT 1",
"GROUP BY address HAVING COUNT(released) = COUNT(*) "
"AND MAX(released) < (? - timeout) LIMIT 1",
DB_TEXT, name, DB_UINT, time(NULL),
DB_UINT, DB_BLOB, DB_UINT, DB_UINT);
@@ -276,6 +277,11 @@ sql_attribute_t *sql_attribute_create(database_t *db)
this->db = db;
this->mutex = mutex_create(MUTEX_DEFAULT);
/* close any "online" leases in the case we crashed */
this->db->execute(this->db, NULL,
"UPDATE leases SET released = ? WHERE released IS NULL",
DB_UINT, time(NULL));
return &this->public;
}