From 13ad57cef92a336e57bf065b421a77cf7e977e6c Mon Sep 17 00:00:00 2001 From: Thierry Parmentelat Date: Thu, 23 Nov 2006 19:35:38 +0000 Subject: [PATCH] cached Keys the class_id field in cached classes is deprecated, it was redundant with primary_key, --- PLC/Cache.py | 47 ++++++++++++++++++++++++++--------------------- PLC/Keys.py | 6 ++++++ PLC/Nodes.py | 4 ++-- PLC/Slices.py | 1 - planetlab4.sql | 32 +++++++++++++++++++------------- 5 files changed, 53 insertions(+), 37 deletions(-) diff --git a/PLC/Cache.py b/PLC/Cache.py index 23408c6..fb0fce5 100644 --- a/PLC/Cache.py +++ b/PLC/Cache.py @@ -4,6 +4,7 @@ from PLC.Filter import Filter from PLC.Table import Row, Table verbose_flag=False; +#verbose_flag=True; def verbose (*args): if verbose_flag: print (args) @@ -19,7 +20,7 @@ def class_attributes (classname): return {'row_class':row_class, 'table_class':table_class, - 'class_id': row_class.__dict__['class_id'], + 'primary_key': row_class.__dict__['primary_key'], 'class_key': row_class.__dict__['class_key'], 'foreign_fields': row_class.__dict__['foreign_fields'], 'foreign_xrefs': row_class.__dict__['foreign_xrefs'], @@ -45,18 +46,16 @@ class Cache: def __init__ (self, api, classname, alien_objects): self.api = api attrs = class_attributes (classname) - self.class_id = attrs['class_id'] + self.primary_key = attrs['primary_key'] self.class_key = attrs['class_key'] # cannot use dict, it's acquired by xmlrpc and is untyped - d = {} - for x in alien_objects: - verbose ('indexing',x) - d[x[self.class_id]]=x - self.alien_objects_byid = d + self.alien_objects_byid = dict( [ (x[self.primary_key],x) for x in alien_objects ] ) + # retrieve local objects local_objects = attrs['table_class'] (api) self.local_objects_byname = local_objects.dict(self.class_key) + verbose ('Transcoder init :',classname, self.alien_objects_byid.keys(), self.local_objects_byname.keys()) @@ -71,7 +70,7 @@ class Cache: verbose ('got name',name,) local_object=self.local_objects_byname[name] verbose ('found local obj') - local_id=local_object[self.class_id] + local_id=local_object[self.primary_key] verbose ('and local_id',local_id) return local_id @@ -114,6 +113,7 @@ class Cache: # classname: the type of objects we are talking about; e.g. 'Slice' # peer_object_list list of objects at a given peer - e.g. peer.GetSlices() # alien_xref_objs_dict : a dict {'classname':alien_obj_list} e.g. {'Node':peer.GetNodes()} + # his must match the keys in xref_specs # lambda_ignore : the alien objects are ignored if this returns true def update_table (self, classname, @@ -127,18 +127,18 @@ class Cache: attrs = class_attributes (classname) row_class = attrs['row_class'] table_class = attrs['table_class'] - class_id = attrs['class_id'] + primary_key = attrs['primary_key'] class_key = attrs['class_key'] foreign_fields = attrs['foreign_fields'] foreign_xrefs = attrs['foreign_xrefs'] ## allocate transcoders and xreftables once, for each item in foreign_xrefs - accessories={} - for xref_classname,xref_spec in foreign_xrefs.iteritems(): - d={} - d['transcoder']=Cache.Transcoder (self.api,xref_classname,alien_xref_objs_dict[xref_classname]) - d['xref_table'] =Cache.XrefTable (self.api,xref_spec['table'],classname,xref_classname) - accessories[xref_classname]=d + # create a dict 'classname' -> {'transcoder' : ..., 'xref_table' : ...} + accessories = dict( + [ (xref_classname, + {'transcoder':Cache.Transcoder (self.api,xref_classname,alien_xref_objs_dict[xref_classname]), + 'xref_table':Cache.XrefTable (self.api,xref_spec['table'],classname,xref_classname)}) + for xref_classname,xref_spec in foreign_xrefs.iteritems()]) ### get current local table # get ALL local objects so as to cope with @@ -146,6 +146,8 @@ class Cache: # (*) or naming conflicts local_objects = table_class (self.api) ### index upon class_key for future searches + verbose ('local objects:',local_objects) + verbose ('class_key',class_key) local_objects_index = local_objects.dict(class_key) verbose ('update_table',classname,local_objects_index.keys()) @@ -225,7 +227,7 @@ class Cache: former_xrefs=local_object[xref_spec['field']] except: former_xrefs=[] - xref_table.update_item (local_object[class_id], + xref_table.update_item (local_object[primary_key], former_xrefs, local_values) elif isinstance (alien_value,int): @@ -269,19 +271,22 @@ class Cache: def refresh_peer (self): + peer_local_keys = self.peer_server.GetKeys(self.auth,{'peer_id':None}) peer_local_nodes = self.peer_server.GetNodes(self.auth,None,None,'local') + # xxx would ideally get our own nodes only, + # requires to know remote peer's peer_id for ourselves, mmhh peer_foreign_nodes = self.peer_server.GetNodes(self.auth,None,None,'foreign') peer_local_slices = self.peer_server.GetSlices(self.auth,{'peer_id':None}) - from PLC.Nodes import Nodes - verbose ('local nodes before refresh',len(Nodes(self.api))) - nb_new_nodes = self.refresh_nodes(peer_local_nodes) - verbose ('local nodes after refresh',len(Nodes(self.api))) - + nb_new_keys = self.update_table('Key', peer_local_keys) + + nb_new_nodes = self.update_table('Node', peer_local_nodes) + # rough and temporary nb_new_slices = self.refresh_slices(peer_local_slices,peer_local_nodes+peer_foreign_nodes) return {'plcname':self.api.config.PLC_NAME, + 'new_keys':nb_new_keys, 'new_nodes':nb_new_nodes, 'new_slices':nb_new_slices} diff --git a/PLC/Keys.py b/PLC/Keys.py index 841e6eb..9af1e6c 100644 --- a/PLC/Keys.py +++ b/PLC/Keys.py @@ -21,8 +21,14 @@ class Key(Row): 'key_id': Parameter(int, "Key identifier"), 'key_type': Parameter(str, "Key type"), 'key': Parameter(str, "Key value", max = 4096), + 'peer_id': Parameter(int, "Peer at which this node is managed", nullok = True), } + # for Cache + class_key= 'key' + foreign_fields = ['key_type'] + foreign_xrefs = {} + def validate_key_type(self, key_type): key_types = [row['key_type'] for row in KeyTypes(self.api)] if key_type not in key_types: diff --git a/PLC/Nodes.py b/PLC/Nodes.py index 520edb8..3c7dae6 100644 --- a/PLC/Nodes.py +++ b/PLC/Nodes.py @@ -4,7 +4,7 @@ # Mark Huang # Copyright (C) 2006 The Trustees of Princeton University # -# $Id: Nodes.py,v 1.21 2006/11/21 10:57:00 thierry Exp $ +# $Id: Nodes.py,v 1.22 2006/11/23 11:55:24 thierry Exp $ # from types import StringTypes @@ -62,7 +62,7 @@ class Node(Row): 'ports': Parameter([int], "List of PCU ports that this node is connected to"), } - class_id = 'node_id' + # for Cache class_key = 'hostname' foreign_fields = ['boot_state','model','version','date_created','last_updated'] foreign_xrefs = {} diff --git a/PLC/Slices.py b/PLC/Slices.py index a779121..d62922f 100644 --- a/PLC/Slices.py +++ b/PLC/Slices.py @@ -38,7 +38,6 @@ class Slice(Row): 'slice_attribute_ids': Parameter([int], "List of slice attributes", ro = True), } # for Cache - class_id = 'slice_id' class_key = 'name' foreign_fields = ['instantiation', 'url', 'description', 'max_nodes', 'created', 'expires'] diff --git a/planetlab4.sql b/planetlab4.sql index 908459d..82636c2 100644 --- a/planetlab4.sql +++ b/planetlab4.sql @@ -9,7 +9,7 @@ -- -- Copyright (C) 2006 The Trustees of Princeton University -- --- $Id: planetlab4.sql,v 1.41 2006/11/17 19:31:08 tmack Exp $ +-- $Id: planetlab4.sql,v 1.42 2006/11/21 10:57:00 thierry Exp $ -- -------------------------------------------------------------------------------- @@ -36,6 +36,22 @@ CREATE TABLE plc_db_version ( INSERT INTO plc_db_version (version) VALUES (4); +-------------------------------------------------------------------------------- +-- Peers +-------------------------------------------------------------------------------- + +-- Peers +CREATE TABLE peers ( + peer_id serial PRIMARY KEY, -- identifier + peername text NOT NULL, -- free text + peer_url text NOT NULL, -- the url of that peer's API + -- oops, looks like we have a dependency loop here + --person_id integer REFERENCES persons NOT NULL, -- the account we use for logging in + person_id integer NOT NULL, -- the account we use for logging in + + deleted boolean NOT NULL DEFAULT false +) WITH OIDS; + -------------------------------------------------------------------------------- -- Accounts -------------------------------------------------------------------------------- @@ -197,7 +213,8 @@ CREATE TABLE keys ( key_id serial PRIMARY KEY, -- Key identifier key_type text REFERENCES key_types NOT NULL, -- Key type key text NOT NULL, -- Key material - is_blacklisted boolean NOT NULL DEFAULT false -- Has been blacklisted + is_blacklisted boolean NOT NULL DEFAULT false, -- Has been blacklisted + peer_id integer REFERENCES peers -- From which peer ) WITH OIDS; -- Account authentication key(s) @@ -264,17 +281,6 @@ INSERT INTO boot_states (boot_state) VALUES ('rins'); INSERT INTO boot_states (boot_state) VALUES ('rcnf'); INSERT INTO boot_states (boot_state) VALUES ('new'); --- Peers -CREATE TABLE peers ( - peer_id serial PRIMARY KEY, -- identifier - peername text NOT NULL, -- free text - peer_url text NOT NULL, -- the url of that peer's API - person_id integer REFERENCES persons NOT NULL, -- the account we use for logging in - - deleted boolean NOT NULL DEFAULT false -) WITH OIDS; - - -- Nodes CREATE TABLE nodes ( -- Mandatory -- 2.43.0