Ignore:
Timestamp:
Nov 21, 2008, 4:16:38 AM (16 years ago)
Author:
quentin
Message:

Switched from caching ORM to direct database queries; now uncached queries are almost as fast as the cached queries used to be, and we don't have any cache coherency problems

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/packages/invirt-remote-server/files/usr/sbin/invirt-remconffs

    r1726 r1728  
    66from syslog import *
    77from time import time
     8import sqlalchemy as sa
    89
    910from invirt import database
     
    2829        """
    2930        super(RemConfFS, self).__init__(*args, **kw)
    30         self.lasttime = 0
    3131        self.fuse_args.add("allow_other", True)
    3232       
     
    4343        return m
    4444   
    45     def recache(self):
    46         if time() - self.lasttime > 5:
    47             self.lasttime = time()
    48             database.clear_cache()
    49             self.machines = dict((machine.name, machine) for machine in database.session.query(database.Machine).all())
    50    
    5145    def getroot(self, **kw):
    5246        return ['acl', 'conf']
     
    5549        """Build the ACL file for a machine
    5650        """
    57         self.recache()
    58         machine = self.machines[machine]
    59         users = [acl.user for acl in machine.acl]
    60         return "\n".join(map(self.userToPrinc, users)
     51        s = sa.sql.select([database.machine_access_table.c.user], # Field to select from
     52                          sa.sql.and_( # where clause
     53                database.machine_table.c.machine_id==database.machine_access_table.c.machine_id, # join field
     54                database.machine_table.c.name == machine), # filter field
     55                          from_obj=[database.machine_access_table, database.machine_table]) # from tables
     56        users = [self.userToPrinc(acl[0]) for acl in
     57                 database.session.execute(s)]
     58        return "\n".join(users
    6159                 + ['include /etc/remctl/acl/web',
    6260                    ''])
     
    7169   
    7270    def getmachines(self, **kw):
    73         """Get the list of VMs in the database, clearing the cache if it's
    74         older than 15 seconds"""
    75         self.recache()
    76         return self.machines.keys()
     71        """Get the list of VMs in the database. Does not cache to prevent race conditions."""
     72        return list(row[0] for row in database.session.execute(sa.sql.select([database.Machine.c.name])))
    7773   
    7874    def userToPrinc(self, user):
Note: See TracChangeset for help on using the changeset viewer.