Back to index

nordugrid-arc-nox  1.1.0~rc6
TargetGenerator.cpp
Go to the documentation of this file.
00001 // -*- indent-tabs-mode: nil -*-
00002 
00003 #ifdef HAVE_CONFIG_H
00004 #include <config.h>
00005 #endif
00006 
00007 #include <algorithm>
00008 #include <iostream>
00009 
00010 #include <arc/ArcConfig.h>
00011 #include <arc/IString.h>
00012 #include <arc/Logger.h>
00013 #include <arc/StringConv.h>
00014 #include <arc/XMLNode.h>
00015 #include <arc/client/TargetGenerator.h>
00016 #include <arc/client/ClientInterface.h>
00017 #include <arc/UserConfig.h>
00018 
00019 namespace Arc {
00020 
00021   Logger TargetGenerator::logger(Logger::getRootLogger(), "TargetGenerator");
00022 
00023   TargetGenerator::TargetGenerator(const UserConfig& usercfg)
00024     : threadCounter(0), usercfg(usercfg) {
00025 
00026     /* When loading a specific middleware plugin fails, subsequent loads
00027      * should fail aswell. Therefore it should be unecessary to load the
00028      * same plugin multiple times, if it failed the first time.
00029      */
00030     std::map<std::string, bool> pluginLoaded;
00031     for (URLListMap::const_iterator it = usercfg.GetSelectedServices(COMPUTING).begin();
00032          it != usercfg.GetSelectedServices(COMPUTING).end(); it++)
00033       for (std::list<URL>::const_iterator it2 = it->second.begin();
00034            it2 != it->second.end(); it2++) {
00035         if (pluginLoaded[it->first] = (loader.load(it->first, usercfg, *it2, COMPUTING) != NULL))
00036           break;
00037       }
00038 
00039     for (URLListMap::const_iterator it = usercfg.GetSelectedServices(INDEX).begin();
00040          it != usercfg.GetSelectedServices(INDEX).end(); it++) {
00041       if (pluginLoaded.find(it->first) != pluginLoaded.end() && !pluginLoaded[it->first]) // Do not try to load if it failed above.
00042         continue;
00043       for (std::list<URL>::const_iterator it2 = it->second.begin();
00044            it2 != it->second.end(); it2++) {
00045         if (loader.load(it->first, usercfg, *it2, INDEX) == NULL)
00046           break;
00047       }
00048     }
00049   }
00050 
00051   TargetGenerator::~TargetGenerator() {
00052 
00053     if (foundJobs.size() > 0)
00054       for (std::list<XMLNode*>::iterator it = foundJobs.begin();
00055            it != foundJobs.end(); it++)
00056         delete *it;
00057   }
00058 
00059   void TargetGenerator::GetTargets(int targetType, int detailLevel) {
00060 
00061     logger.msg(VERBOSE, "Running resource (target) discovery");
00062 
00063     for (std::list<TargetRetriever*>::const_iterator it =
00064            loader.GetTargetRetrievers().begin();
00065          it != loader.GetTargetRetrievers().end(); it++)
00066       (*it)->GetTargets(*this, targetType, detailLevel);
00067 
00068     {
00069       Glib::Mutex::Lock threadLock(threadMutex);
00070       while (threadCounter > 0)
00071         threadCond.wait(threadMutex);
00072     }
00073 
00074     logger.msg(INFO, "Found %ld targets", foundTargets.size());
00075 
00076     for (std::list<ExecutionTarget>::iterator iter = foundTargets.begin();
00077          iter != foundTargets.end(); iter++) {
00078       logger.msg(VERBOSE, "Cluster: %s", iter->DomainName);
00079       logger.msg(VERBOSE, "Health State: %s", iter->HealthState);
00080     }
00081 
00082   }
00083 
00084   const std::list<ExecutionTarget>& TargetGenerator::FoundTargets() const {
00085     return foundTargets;
00086   }
00087 
00088   std::list<ExecutionTarget>& TargetGenerator::ModifyFoundTargets() {
00089     return foundTargets;
00090   }
00091 
00092   const std::list<XMLNode*>& TargetGenerator::FoundJobs() const {
00093     return foundJobs;
00094   }
00095 
00096   bool TargetGenerator::AddService(const URL& url) {
00097 
00098     for (URLListMap::const_iterator it = usercfg.GetRejectedServices(COMPUTING).begin();
00099          it != usercfg.GetRejectedServices(COMPUTING).end(); it++)
00100       if (std::find(it->second.begin(), it->second.end(), url) !=
00101           it->second.end()) {
00102         logger.msg(INFO, "Rejecting service: %s", url.str());
00103         return false;
00104       }
00105 
00106     bool added = false;
00107     Glib::Mutex::Lock serviceLock(serviceMutex);
00108     if (std::find(foundServices.begin(), foundServices.end(), url) ==
00109         foundServices.end()) {
00110       foundServices.push_back(url);
00111       added = true;
00112       Glib::Mutex::Lock threadLock(threadMutex);
00113       threadCounter++;
00114     }
00115     return added;
00116   }
00117 
00118   bool TargetGenerator::AddIndexServer(const URL& url) {
00119 
00120     for (URLListMap::const_iterator it = usercfg.GetRejectedServices(INDEX).begin();
00121          it != usercfg.GetRejectedServices(INDEX).end(); it++)
00122       if (std::find(it->second.begin(), it->second.end(), url) !=
00123           it->second.end()) {
00124         logger.msg(INFO, "Rejecting service: %s", url.str());
00125         return false;
00126       }
00127 
00128     bool added = false;
00129     Glib::Mutex::Lock indexServerLock(indexServerMutex);
00130     if (std::find(foundIndexServers.begin(), foundIndexServers.end(), url) ==
00131         foundIndexServers.end()) {
00132       foundIndexServers.push_back(url);
00133       added = true;
00134       Glib::Mutex::Lock threadLock(threadMutex);
00135       threadCounter++;
00136     }
00137     return added;
00138   }
00139 
00140   void TargetGenerator::AddTarget(const ExecutionTarget& target) {
00141     Glib::Mutex::Lock targetLock(targetMutex);
00142     foundTargets.push_back(target);
00143   }
00144 
00145 
00146   void TargetGenerator::AddJob(const XMLNode& job) {
00147     Glib::Mutex::Lock jobLock(jobMutex);
00148     NS ns;
00149     XMLNode *j = new XMLNode(ns, "");
00150     j->Replace(job);
00151     foundJobs.push_back(j);
00152   }
00153 
00154   void TargetGenerator::RetrieverDone() {
00155     Glib::Mutex::Lock threadLock(threadMutex);
00156     threadCounter--;
00157     if (threadCounter == 0)
00158       threadCond.signal();
00159   }
00160 
00161   void TargetGenerator::PrintTargetInfo(bool longlist) const {
00162     for (std::list<ExecutionTarget>::const_iterator cli = foundTargets.begin();
00163          cli != foundTargets.end(); cli++)
00164       cli->Print(longlist);
00165   }
00166 
00167 } // namespace Arc