XRootD
XrdThrottleFileSystemConfig.cc
Go to the documentation of this file.
1 
2 #include <fcntl.h>
3 
4 #include "XrdVersion.hh"
5 
6 #include "XrdSys/XrdSysPlugin.hh"
7 #include "XrdSys/XrdSysLogger.hh"
8 #include "XrdOuc/XrdOuca2x.hh"
9 #include "XrdOuc/XrdOucEnv.hh"
10 #include "XrdOuc/XrdOucStream.hh"
11 
15 
16 using namespace XrdThrottle;
17 
18 #define OFS_NAME "libXrdOfs.so"
19 
20 /*
21  * Note nothing in this file is thread-safe.
22  */
23 
24 static XrdSfsFileSystem *
25 LoadFS(const std::string &fslib, XrdSysError &eDest, const std::string &config_file){
26  // Load the library
27  XrdSysPlugin ofsLib(&eDest, fslib.c_str(), "fslib", NULL);
28  XrdSfsFileSystem *fs;
29  if (fslib == OFS_NAME)
30  {
32  XrdSysLogger *lp,
33  const char *configfn,
34  XrdOucEnv *EnvInfo);
35 
36  if (!(fs = XrdSfsGetDefaultFileSystem(0, eDest.logger(), config_file.c_str(), 0)))
37  {
38  eDest.Emsg("Config", "Unable to load OFS filesystem.");
39  }
40  }
41  else
42  {
43  XrdSfsFileSystem *(*ep)(XrdSfsFileSystem *, XrdSysLogger *, const char *);
44  if (!(ep = (XrdSfsFileSystem *(*)(XrdSfsFileSystem *,XrdSysLogger *,const char *))
45  ofsLib.getPlugin("XrdSfsGetFileSystem")))
46  return NULL;
47  if (!(fs = (*ep)(0, eDest.logger(), config_file.c_str())))
48  {
49  eDest.Emsg("Config", "Unable to create file system object via", fslib.c_str());
50  return NULL;
51  }
52  }
53  ofsLib.Persist();
54 
55  return fs;
56 }
57 
58 namespace XrdThrottle {
61  XrdSysLogger *lp,
62  const char *configfn,
63  XrdOucEnv *envP)
64 {
65  FileSystem* fs = NULL;
66  if (envP && envP->GetInt("XrdOssThrottle") == 1) {
67  XrdSysError eDest(lp, "XrdOssThrottle");
68  eDest.Emsg("Config", "XrdOssThrottle is loaded; not stacking XrdThrottle on OFS. "
69  "This is a warning for backward compatibility; this configuration may generate an "
70  "error in the future.");
71  return native_fs;
72  }
73  FileSystem::Initialize(fs, native_fs, lp, configfn, envP);
74  return fs;
75 }
76 }
77 
78 // Export the symbol necessary for this to be dynamically loaded.
79 extern "C" {
82  XrdSysLogger *lp,
83  const char *configfn)
84 {
85  return XrdSfsGetFileSystem_Internal(native_fs, lp, configfn, nullptr);
86 }
87 
90  XrdSysLogger *lp,
91  const char *configfn,
92  XrdOucEnv *envP)
93 {
94  return XrdSfsGetFileSystem_Internal(native_fs, lp, configfn, envP);
95 }
96 }
97 
100 
101 FileSystem* FileSystem::m_instance = 0;
102 
103 FileSystem::FileSystem()
104  : m_eroute(0), m_trace(&m_eroute), m_sfs_ptr(0), m_initialized(false), m_throttle(&m_eroute, &m_trace)
105 {
106  myVersion = &XrdVERSIONINFOVAR(XrdSfsGetFileSystem);
107 }
108 
109 FileSystem::~FileSystem() {}
110 
111 void
112 FileSystem::Initialize(FileSystem *&fs,
113  XrdSfsFileSystem *native_fs,
114  XrdSysLogger *lp,
115  const char *configfn,
116  XrdOucEnv *envP)
117 {
118  fs = NULL;
119  if (m_instance == NULL && !(m_instance = new FileSystem()))
120  {
121  return;
122  }
123  fs = m_instance;
124  if (!fs->m_initialized)
125  {
126  fs->m_config_file = configfn;
127  fs->m_eroute.logger(lp);
128  fs->m_eroute.Say("Initializing a Throttled file system.");
129  if (fs->Configure(fs->m_eroute, native_fs, envP))
130  {
131  fs->m_eroute.Say("Initialization of throttled file system failed.");
132  fs = NULL;
133  return;
134  }
135  fs->m_throttle.Init();
136  fs->m_initialized = true;
137  }
138 }
139 
140 #define TS_Xeq(key, func) NoGo = (strcmp(key, var) == 0) ? func(Config) : 0
141 int
142 FileSystem::Configure(XrdSysError & log, XrdSfsFileSystem *native_fs, XrdOucEnv *envP)
143 {
145  if (Config.Configure(m_config_file))
146  {
147  log.Emsg("Config", "Unable to load configuration file", m_config_file.c_str());
148  return 1;
149  }
150 
151  m_throttle.FromConfig(Config);
152  m_trace.What = Config.GetTraceLevels();
153 
154  // Load the filesystem object.
155  m_sfs_ptr = native_fs ? native_fs : LoadFS(Config.GetFileSystemLibrary(), m_eroute, m_config_file);
156  if (!m_sfs_ptr) return 1;
157 
158  // Overwrite the environment variable saying that throttling is the fslib.
159  XrdOucEnv::Export("XRDOFSLIB", Config.GetFileSystemLibrary().c_str());
160 
161  if (envP)
162  {
163  auto gstream = reinterpret_cast<XrdXrootdGStream*>(envP->GetPtr("Throttle.gStream*"));
164  log.Say("Config", "Throttle g-stream has", gstream ? "" : " NOT", " been configured via xrootd.mongstream directive");
165  m_throttle.SetMonitor(gstream);
166  }
167 
168  // The Feature function is not a virtual but implemented by the base class to
169  // look at a protected member. Thus, to forward the call, we need to copy
170  // from the underlying filesystem
171  FeatureSet = m_sfs_ptr->Features();
172  return 0;
173 }
XrdSysError eDest(0, "HttpMon")
XrdSfsFileSystem * XrdSfsGetDefaultFileSystem(XrdSfsFileSystem *native_fs, XrdSysLogger *lp, const char *configfn, XrdOucEnv *EnvInfo)
Definition: XrdOfsFS.cc:49
static XrdSfsFileSystem * LoadFS(const std::string &fslib, XrdSysError &eDest, const std::string &config_file)
#define OFS_NAME
XrdSfsFileSystem * XrdSfsGetFileSystem2(XrdSfsFileSystem *native_fs, XrdSysLogger *lp, const char *configfn, XrdOucEnv *envP)
XrdSfsFileSystem * XrdSfsGetFileSystem(XrdSfsFileSystem *native_fs, XrdSysLogger *lp, const char *configfn)
XrdVERSIONINFO(XrdSfsGetFileSystem, FileSystem)
long GetInt(const char *varname)
Definition: XrdOucEnv.cc:253
static int Export(const char *Var, const char *Val)
Definition: XrdOucEnv.cc:188
void * GetPtr(const char *varname)
Definition: XrdOucEnv.cc:281
uint64_t FeatureSet
Adjust features at initialization.
int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0)
Definition: XrdSysError.cc:116
void Say(const char *text1, const char *text2=0, const char *txt3=0, const char *text4=0, const char *text5=0, const char *txt6=0)
Definition: XrdSysError.cc:162
XrdSysLogger * logger(XrdSysLogger *lp=0)
Definition: XrdSysError.hh:175
void * getPlugin(const char *pname, int optional=0)
void * Persist()
void FromConfig(XrdThrottle::Configuration &config)
void SetMonitor(XrdXrootdGStream *gstream)
XrdVersionInfo myVersion
XrdCmsConfig Config
XrdOucEnv * envP
Definition: XrdPss.cc:110
XrdSfsFileSystem * XrdSfsGetFileSystem_Internal(XrdSfsFileSystem *native_fs, XrdSysLogger *lp, const char *configfn, XrdOucEnv *envP)