OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
resource_manager.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #include <boost/uuid/uuid_io.hpp>
6 #include <cmn/agent_cmn.h>
7 #include <cmn/agent.h>
8 #include <init/agent_param.h>
9 #include <init/agent_init.h>
15 
17  agent_(agent),
18  restore_work_queue_(agent->task_scheduler()->
19  GetTaskId(kAgentResourceRestoreTask), 0,
20  boost::bind(&ResourceManager::WorkQueueRestoreProcess,
21  this, _1)) {
22 
23  for (uint8_t type = uint8_t(Resource::INVALID + 1);
24  type < uint8_t(Resource::MAX);
25  type++) {
27  this));
28  }
29 
31  //Backup resource if configured
33  backup_mgr_.reset(new ResourceBackupManager(this));
34  } else {
36  }
37 }
38 
40 }
41 
43  if (backup_mgr_.get())
44  backup_mgr_->Init();
45 }
46 // Read the Data from Resore Queue and process it.
48  ResourceRestoreReqPtr restore_data) {
49  RestoreResource(restore_data.get()->key(), restore_data.get()->data());
50  return true;
51 }
52 
53 
55  ResourceRestoreReqPtr restore_data(new ResourceRestoreReq(key, data));
56  restore_work_queue_.Enqueue(restore_data);
57 }
58 
59 // Check for ResouceBackupEndkey if it is set mark it resource manager ready.
60 // Mark the resource key diry until we process the audit for validity of key.
62  if (dynamic_cast<ResourceBackupEndKey *>(key.get())) {
64  return;
65  }
66 
67  ResourceTable *resource_table = key->resource_table();
68  assert(resource_table->FindKey(key) == NULL);
69  //If its not used then candidate for deletion so mark dirty
70  key->set_dirty();
71  resource_table->RestoreKey(key, data);
72 }
73 
74 // Reserve the index for certain resource types.
76  IndexResourceTable *table = dynamic_cast<IndexResourceTable *>
77  (resource_table(type));
78  table->ReserveIndex(index);
79 }
80 
82  IndexResourceTable *table = dynamic_cast<IndexResourceTable *>
83  (resource_table(type));
84  table->ReleaseIndex(index);
85 }
86 
87 // Allocates the the index by resource table.
88 // if back up is enabled it will store the data to file.
90  assert(agent_->ResourceManagerReady());
91  ResourceManager::DataPtr data = key.get()->resource_table()->Allocate(key);
92  if (backup_mgr_.get()) {
93  backup_mgr_->BackupResource(key, data, ResourceBackupReq::ADD);
94  }
95  return data;
96 }
97 
98 // Releases the resource table index and key.
99 // Delete the data from file.
101  ResourceTable *resource_table = key.get()->resource_table();
102  ResourceManager::DataPtr data = resource_table->FindKeyPtr(key);
103  if (data.get() == NULL) {
104  return;
105  }
106 
107  if (backup_mgr_.get()) {
108  backup_mgr_->BackupResource(key, data, ResourceBackupReq::DEL);
109  }
110  resource_table->ReleaseKey(key, data);
111 }
112 
113 // Releases the reserved index.
115  IndexResourceTable *table = dynamic_cast<IndexResourceTable *>
116  (resource_table(type));
117  table->Release(index);
118 }
119 
121  return resource_table_[type].get();
122 }
123 
124 // Audit the records per resource table.
126  for (uint8_t type = uint8_t(Resource::INVALID + 1);
127  type < uint8_t(Resource::MAX);
128  type++) {
129  resource_table_[type].get()->FlushStale();
130  }
131  backup_mgr_->AuditDone();
132  return true;
133 }
bool WorkQueueRestoreProcess(ResourceRestoreReqPtr restore_data)
ResourceTable * resource_table(uint8_t type)
void EnqueueRestore(KeyPtr key, DataPtr data)
AgentInit * agent_init() const
Definition: agent.h:1123
boost::scoped_ptr< ResourceTable > resource_table_[Resource::MAX]
virtual void ReleaseKey(KeyPtr key, DataPtr data)=0
#define kAgentResourceRestoreTask
Definition: agent.h:339
void Release(KeyPtr key)
bool restart_backup_enable() const
Definition: agent_param.h:476
boost::shared_ptr< ResourceKey > KeyPtr
boost::shared_ptr< ResourceData > DataPtr
void ReleaseIndex(uint32_t index)
ResourceManager(Agent *agent)
void ReleaseIndex(Resource::Type, uint32_t index)
static ResourceTable * Create(Type type, ResourceManager *rm)
Definition: resource_cmn.cc:16
void ReserveIndex(Resource::Type, uint32_t index)
boost::scoped_ptr< ResourceBackupManager > backup_mgr_
ResourceData * FindKey(KeyPtr key)
WorkQueue< ResourceRestoreReqPtr > restore_work_queue_
uint8_t type
Definition: load_balance.h:109
Definition: agent.h:358
boost::shared_ptr< ResourceRestoreReq > ResourceRestoreReqPtr
AgentParam * params() const
Definition: agent.h:1218
ResourceManager::DataPtr Allocate(KeyPtr key)
DataPtr FindKeyPtr(KeyPtr key)
virtual void RestoreKey(KeyPtr key, DataPtr data)=0
void RestoreResource(KeyPtr key, DataPtr data)
bool ResourceManagerReady() const
Definition: agent.h:1233
void SetResourceManagerReady()
Definition: agent_init.cc:165
bool Enqueue(QueueEntryT entry)
Definition: queue_task.h:248
void set_resource_manager(ResourceManager *resource_manager)
Definition: agent.cc:1025
void ReserveIndex(uint32_t index)
void Release(uint32_t index)