[openstack-dev] [Nova] Question about fixed_ip_bulk_create() db.api function.

Jay Pipes jaypipes at gmail.com
Tue Jul 2 10:37:28 UTC 2013


On 07/02/2013 05:32 AM, Victor Sergeyev wrote:
> Hello All.
>
> I have a question about the patch `Add unique constraint to FixedIp`
> (see https://review.openstack.org/#/c/29364/), which is a part of
> blueprint `Complete db unique key enforcement on all tables dbs`
> (https://blueprints.launchpad.net/nova/+spec/db-enforce-unique-keys).
>
> There is a discussion about fixed_ip_bulk_create() behavior in this
> patch review. At the moment this function receives a list of fixed_ips
> and tries to insert these IPs to database one-by-one. If
> DBDuplicateEntry exception raised, the function raises an exception with
> the first duplicate IP in exception message. The same is true for
> floating_ip_bulk_create() function.
>
> Currently we can get only one duplicate IP error message per request.
> So, if we add 1000 IP addresses, including 100 duplicates, we have to
> call this function 100 times to find out which IPs aren't unique.
>
> A few ways of modifying of this function have been proposed:
> 1) we can try to add all IPs to database using a bulk insert. If one of
> those IPs violates the unique constraint, the exception is raised
> containing the first duplicate IP address, the transaction is rolled
> back and no IPs are saved to DB.
> This keeps current behavior, but is significantly faster when a large
> number of rows are inserted (Oslo integrity error handling code should
> be modified a bit to retrieve the duplicated value from an exception
> message).
>
> 2) we can add all unique IPs to database and write duplicate IPs to log.
> This is much slower, because we have to create a separate transaction
> for each IP (as the first integrity error would cancel the current
> transaction until ROLLBACK was emitted).
> On the other hand, this allows us to save valid IPs to database and
> provide the caller with a list of duplicate IPs.
>
> 3) we can try to add all IPs to database. All duplicate IPs are
> collected. If there is at least one duplicate, no IPs are saved to
> database, but the caller receives a list with all duplicate IPs.
> (A separate transaction is needed for each IP. The rollback must be done
> manually by issuing of DELETE FROM matching duplicate IPs).

Or option #4:

Do a single query before starting any transaction that finds the 
intersection of inserted floating IPs with existing floating IPs. Let's 
call that set A.

if len(A) > 0:
    raise SomeException("Message with list of existing floating IPs")
else:
    try:
      session.begin()
      execute single query to insert records.
      session.commit()
    except:
      session.rollback()

That way you get a fail-fast scenario and the only time you have any 
issues (i.e. a rollback) is the rare occasion when you have a matching 
floating IP inserted into the database in between your original query to 
get the intersection and when you issue the call to insert the records.

Best,
-jay




More information about the OpenStack-dev mailing list