ATLAS Offline Software
Loading...
Searching...
No Matches
ReplicaSorter Class Reference

#include <ReplicaSorter.h>

Inheritance diagram for ReplicaSorter:
Collaboration diagram for ReplicaSorter:

Public Member Functions

 ReplicaSorter ()
void sort (std::vector< const coral::IDatabaseServiceDescription * > &replicaSet)
 ReplicaSorter ()
void sort (std::vector< const coral::IDatabaseServiceDescription * > &replicaSet)

Private Types

typedef std::pair< std::string, int > ServerPair
typedef std::vector< ServerPairServerMap
typedef std::pair< std::string, int > ServerPair
typedef std::vector< ServerPairServerMap

Private Member Functions

bool readConfig ()
bool readConfig ()
FILE * findFile (const std::string &filename, const std::string &pathvar)

Static Private Member Functions

static FILE * findFile (const std::string &filename, const std::string &pathvar)

Private Attributes

std::string m_hostname
ServerMap m_servermap
bool m_frontiergen

Detailed Description

Member Typedef Documentation

◆ ServerMap [1/2]

typedef std::vector< ServerPair > ReplicaSorter::ServerMap
private

◆ ServerMap [2/2]

typedef std::vector< ServerPair > ReplicaSorter::ServerMap
private

Definition at line 22 of file LumiBlock/LumiCalc/LumiCalc/ReplicaSorter.h.

◆ ServerPair [1/2]

typedef std::pair<std::string,int> ReplicaSorter::ServerPair
private

◆ ServerPair [2/2]

typedef std::pair<std::string,int> ReplicaSorter::ServerPair
private

Definition at line 21 of file LumiBlock/LumiCalc/LumiCalc/ReplicaSorter.h.

Constructor & Destructor Documentation

◆ ReplicaSorter() [1/2]

ReplicaSorter::ReplicaSorter ( )

◆ ReplicaSorter() [2/2]

ReplicaSorter::ReplicaSorter ( )

Member Function Documentation

◆ findFile() [1/2]

FILE * ReplicaSorter::findFile ( const std::string & filename,
const std::string & pathvar )
staticprivate

Definition at line 170 of file Database/CoolConvUtilities/src/ReplicaSorter.cxx.

171 {
172 // behave like pathresolver
173 std::string::size_type iofs1,iofs2,len;
174 FILE* fptr=nullptr;
175 iofs1=0;
176 len=pathvar.size();
177 std::string name;
178 while (!fptr && iofs1<len) {
179 iofs2=pathvar.find(':',iofs1);
180 if (iofs2==std::string::npos) iofs2=len;
181 name=pathvar.substr(iofs1,iofs2-iofs1);
182 name+='/';
183 name+=filename;
184 fptr=fopen(name.c_str(),"r");
185 iofs1=iofs2+1;
186 }
187 return fptr;
188}
std::vector< TFile * > fptr
Definition hcg.cxx:51

◆ findFile() [2/2]

FILE * ReplicaSorter::findFile ( const std::string & filename,
const std::string & pathvar )
private

◆ readConfig() [1/2]

bool ReplicaSorter::readConfig ( )
private

Definition at line 57 of file Database/CoolConvUtilities/src/ReplicaSorter.cxx.

57 {
58 // determine the hostname from ATLAS_CONDDB, HOSTNAME or hostname -fqdn
59 m_hostname="";
60 const char* chost=getenv("ATLAS_CONDDB");
61 if (chost) m_hostname=chost;
62 if (m_hostname.empty()) {
63 const char* chost=getenv("HOSTNAME");
64 if (chost) m_hostname=chost;
65 // check if the returned host has a .
66 if (m_hostname.find('.')==std::string::npos) {
67 m_hostname="unknown";
68 system("hostname --fqdn > hostnamelookup.tmp");
69 std::ifstream infile;
70 infile.open("hostnamelookup.tmp");
71 if (infile) {
73 } else {
74 m_hostname="unknown";
75 }
76 }
77 }
78 std::cout << "Using machine hostname " << m_hostname <<
79 " for DB replica resolution" << std::endl;
80 // check if FRONTIER_SERVER is set, if so, allow generic replicas
81 const char* cfrontier=getenv("FRONTIER_SERVER");
82 if (cfrontier && strcmp(cfrontier,"")!=0) {
83 std::cout << "Frontier server at " << cfrontier << " will be considered"
84 << std::endl;
85 m_frontiergen=true;
86 }
87
88 // try to locate configuration file using pathresolver
89 FILE* p_inp=nullptr;
90 const char* datapath=getenv("DATAPATH");
91 if (datapath!=nullptr) p_inp=findFile("dbreplica.config",datapath);
92 if (p_inp==nullptr) {
93 std::cout << "Cannot open/locate configuration file dbreplica.config"
94 << std::endl;
95 return false;
96 }
97 // buffer for reading line
98 unsigned int bufsize=999;
99 char* p_buf=new char[bufsize];
100 while (!feof(p_inp)) {
101 char* p_line=fgets(p_buf,bufsize,p_inp);
102 if (p_line!=nullptr && p_line[0]!='#') {
103 std::string buf=std::string(p_line);
104 std::string::size_type iofs1=0;
105 // analyse based on spaces as seperator
106 bool sequal=false;
107 std::vector<std::string> domains;
108 std::vector<std::string> servers;
109 while (iofs1<buf.size()) {
110 std::string::size_type iofs2=buf.find(' ',iofs1);
111 // allow for trailing linefeed
112 if (iofs2==std::string::npos) iofs2=buf.size()-1;
113 std::string token=buf.substr(iofs1,iofs2-iofs1);
114 // skip empty or space tokens
115 if (!token.empty() && token!=" ") {
116 if (token=="=") {
117 sequal=true;
118 } else if (!sequal) {
119 // token is a domain name
120 domains.push_back(std::move(token));
121 } else {
122 // token is a server name
123 // only add Frontier ATLF server if FRONTIER_CLIENT set
124 if (token!="ATLF" || m_frontiergen) servers.push_back(std::move(token));
125 }
126 }
127 iofs1=iofs2+1;
128 }
129 // check the list of domains against the hostname to see if this
130 // set of servers is appropriate
131 bool useit=false;
132 unsigned int bestlen=0;
133 for (std::vector<std::string>::const_iterator itr=domains.begin();
134 itr!=domains.end();++itr) {
135 std::string::size_type len=(itr->size());
136 std::string::size_type hlen=m_hostname.size();
137 if (hlen>=len && *itr==m_hostname.substr(hlen-len,len)) {
138 if (len>bestlen) {
139 useit=true;
140 bestlen=len;
141 }
142 }
143 // for 'default' domain name, add the servers as a last resort
144 // if nothing has been found so far
145 if ("default"==*itr && m_servermap.empty()) {
146 std::cout <<
147 "No specific match for domain found - use default fallback"
148 << std::endl;
149 useit=true;
150 bestlen=0;
151 }
152 }
153 if (useit) {
154 // assign these servers, priority based on position in list
155 // and length of match of domain name
156 for (unsigned int i=0;i<servers.size();++i) {
157 int priority=i-100*bestlen;
158 m_servermap.push_back(ServerPair(servers[i],priority));
159 }
160 }
161 }
162 }
163 fclose(p_inp);
164 delete [] p_buf;
165 std::cout << "Total of " << m_servermap.size() <<
166 " servers found for host " << m_hostname << std::endl;
167 return true;
168}
static FILE * findFile(const std::string &filename, const std::string &pathvar)
std::pair< std::string, int > ServerPair
std::string getenv(const std::string &variableName)
get an environment variable
str infile
Definition run.py:13

◆ readConfig() [2/2]

bool ReplicaSorter::readConfig ( )
private

◆ sort() [1/2]

void ReplicaSorter::sort ( std::vector< const coral::IDatabaseServiceDescription * > & replicaSet)

Definition at line 21 of file Database/CoolConvUtilities/src/ReplicaSorter.cxx.

22 {
23 // loop through all the offered replicas
24 std::map<int,const coral::IDatabaseServiceDescription*> primap;
25 for (std::vector<const coral::IDatabaseServiceDescription*>::const_iterator
26 itr=replicaSet.begin();itr!=replicaSet.end();++itr) {
27 const std::string conn=(**itr).connectionString();
28 // do not use SQLite files
29 if (conn.find("sqlite_file")==std::string::npos) {
30 // extract the server name (assuming URLs "techno://server/schema")
31 std::string::size_type ipos1=conn.find("://");
32 std::string::size_type ipos2=conn.find('/',ipos1+3);
33 if (ipos1!=std::string::npos && ipos2!=std::string::npos) {
34 const std::string_view server=std::string_view(conn).substr(ipos1+3,ipos2-ipos1-3);
35 // check if this server is on list of replicas to use for domain
36 // if so, add it with its associated priority
37 for (ServerMap::const_iterator sitr=m_servermap.begin();
38 sitr!=m_servermap.end();++sitr) {
39 if (sitr->first==server)
40 primap[sitr->second]=*itr;
41 }
42 }
43 }
44 }
45 // now create sorted list
46 replicaSet.clear();
47 for (std::map<int,const coral::IDatabaseServiceDescription*>::const_iterator
48 itr=primap.begin();itr!=primap.end();++itr) {
49 replicaSet.push_back(itr->second);
50 std::cout << "Allowed replica to try (priority " << itr->first
51 << ") : " << (itr->second)->connectionString() << std::endl;
52 }
53 if (replicaSet.empty())
54 std::cout << "No matching replicas found" << std::endl;
55}

◆ sort() [2/2]

void ReplicaSorter::sort ( std::vector< const coral::IDatabaseServiceDescription * > & replicaSet)

Member Data Documentation

◆ m_frontiergen

bool ReplicaSorter::m_frontiergen
private

◆ m_hostname

std::string ReplicaSorter::m_hostname
private

◆ m_servermap

ServerMap ReplicaSorter::m_servermap
private

The documentation for this class was generated from the following files: