OpenSDN source code
resource_manager.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #include <boost/uuid/uuid_io.hpp>
7 #include <cmn/agent_cmn.h>
8 #include <cmn/agent.h>
9 #include <init/agent_param.h>
10 #include <init/agent_init.h>
16 
18  agent_(agent),
19  restore_work_queue_(agent->task_scheduler()->
20  GetTaskId(kAgentResourceRestoreTask), 0,
21  boost::bind(&ResourceManager::WorkQueueRestoreProcess,
22  this, _1)) {
23 
24  for (uint8_t type = uint8_t(Resource::INVALID + 1);
25  type < uint8_t(Resource::MAX);
26  type++) {
28  this));
29  }
30 
32  //Backup resource if configured
34  backup_mgr_.reset(new ResourceBackupManager(this));
35  } else {
37  }
38 }
39 
41 }
42 
44  if (backup_mgr_.get())
45  backup_mgr_->Init();
46 }
47 // Read the Data from Resore Queue and process it.
49  ResourceRestoreReqPtr restore_data) {
50  RestoreResource(restore_data.get()->key(), restore_data.get()->data());
51  return true;
52 }
53 
54 
56  ResourceRestoreReqPtr restore_data(new ResourceRestoreReq(key, data));
57  restore_work_queue_.Enqueue(restore_data);
58 }
59 
60 // Check for ResouceBackupEndkey if it is set mark it resource manager ready.
61 // Mark the resource key diry until we process the audit for validity of key.
63  if (dynamic_cast<ResourceBackupEndKey *>(key.get())) {
65  return;
66  }
67 
68  ResourceTable *resource_table = key->resource_table();
69  assert(resource_table->FindKey(key) == NULL);
70  //If its not used then candidate for deletion so mark dirty
71  key->set_dirty();
72  resource_table->RestoreKey(key, data);
73 }
74 
75 // Reserve the index for certain resource types.
77  IndexResourceTable *table = dynamic_cast<IndexResourceTable *>
79  table->ReserveIndex(index);
80 }
81 
83  IndexResourceTable *table = dynamic_cast<IndexResourceTable *>
85  table->ReleaseIndex(index);
86 }
87 
88 // Allocates the the index by resource table.
89 // if back up is enabled it will store the data to file.
91  assert(agent_->ResourceManagerReady());
92  ResourceManager::DataPtr data = key.get()->resource_table()->Allocate(key);
93  if (backup_mgr_.get()) {
94  backup_mgr_->BackupResource(key, data, ResourceBackupReq::ADD);
95  }
96  return data;
97 }
98 
99 // Releases the resource table index and key.
100 // Delete the data from file.
102  ResourceTable *resource_table = key.get()->resource_table();
104  if (data.get() == NULL) {
105  return;
106  }
107 
108  if (backup_mgr_.get()) {
109  backup_mgr_->BackupResource(key, data, ResourceBackupReq::DEL);
110  }
111  resource_table->ReleaseKey(key, data);
112 }
113 
114 // Releases the reserved index.
116  IndexResourceTable *table = dynamic_cast<IndexResourceTable *>
117  (resource_table(type));
118  table->Release(index);
119 }
120 
122  return resource_table_[type].get();
123 }
124 
125 // Audit the records per resource table.
127  for (uint8_t type = uint8_t(Resource::INVALID + 1);
128  type < uint8_t(Resource::MAX);
129  type++) {
130  resource_table_[type].get()->FlushStale();
131  }
132  backup_mgr_->AuditDone();
133  return true;
134 }
#define kAgentResourceRestoreTask
Definition: agent.h:342
void SetResourceManagerReady()
Definition: agent_init.cc:166
bool restart_backup_enable() const
Definition: agent_param.h:475
Definition: agent.h:361
bool ResourceManagerReady() const
Definition: agent.h:1242
void set_resource_manager(ResourceManager *resource_manager)
Definition: agent.cc:1029
AgentParam * params() const
Definition: agent.h:1227
AgentInit * agent_init() const
Definition: agent.h:1126
void Release(uint32_t index)
void ReserveIndex(uint32_t index)
void ReleaseIndex(uint32_t index)
void EnqueueRestore(KeyPtr key, DataPtr data)
ResourceTable * resource_table(uint8_t type)
boost::shared_ptr< ResourceRestoreReq > ResourceRestoreReqPtr
void ReleaseIndex(Resource::Type, uint32_t index)
boost::scoped_ptr< ResourceBackupManager > backup_mgr_
void ReserveIndex(Resource::Type, uint32_t index)
void Release(KeyPtr key)
boost::shared_ptr< ResourceKey > KeyPtr
bool WorkQueueRestoreProcess(ResourceRestoreReqPtr restore_data)
WorkQueue< ResourceRestoreReqPtr > restore_work_queue_
boost::scoped_ptr< ResourceTable > resource_table_[Resource::MAX]
void RestoreResource(KeyPtr key, DataPtr data)
ResourceManager(Agent *agent)
boost::shared_ptr< ResourceData > DataPtr
ResourceManager::DataPtr Allocate(KeyPtr key)
DataPtr FindKeyPtr(KeyPtr key)
virtual void RestoreKey(KeyPtr key, DataPtr data)=0
ResourceData * FindKey(KeyPtr key)
virtual void ReleaseKey(KeyPtr key, DataPtr data)=0
static ResourceTable * Create(Type type, ResourceManager *rm)
Definition: resource_cmn.cc:17
bool Enqueue(QueueEntryT entry)
Definition: queue_task.h:249
uint8_t type
Definition: load_balance.h:2