CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
XrdRequestManager.h
Go to the documentation of this file.
1 #ifndef Utilities_XrdAdaptor_XrdRequestManager_h
2 #define Utilities_XrdAdaptor_XrdRequestManager_h
3 
4 #include <mutex>
5 #include <vector>
6 #include <set>
7 #include <condition_variable>
8 #include <random>
9 #include <sys/stat.h>
10 
11 #include "tbb/concurrent_unordered_set.h"
12 
14 
15 #include "XrdCl/XrdClFileSystem.hh"
16 
17 #include "XrdRequest.h"
18 #include "XrdSource.h"
19 
20 namespace XrdCl {
21  class File;
22 }
23 
24 namespace XrdAdaptor {
25 
26  struct SourceHash {
27  using Key = std::shared_ptr<Source>;
28  size_t operator()(const Key &iKey) const { return std::hash<Key::element_type *>{}(iKey.get()); }
29  };
30 
32  public:
34  : Exception(code), m_code(xrootd_status.code) {}
35 
36  ~XrootdException() noexcept override{};
37 
38  uint16_t getCode() { return m_code; }
39 
40  private:
41  uint16_t m_code;
42  };
43 
45  public:
49 
50  RequestManager(const RequestManager &) = delete;
51  RequestManager &operator=(const RequestManager &) = delete;
52 
53  static const unsigned int XRD_DEFAULT_TIMEOUT = 3 * 60;
54 
55  virtual ~RequestManager() = default;
56 
60  std::future<IOSize> handle(void *into, IOSize size, IOOffset off) {
61  auto c_ptr = std::make_shared<XrdAdaptor::ClientRequest>(*this, into, size, off);
62  return handle(c_ptr);
63  }
64 
65  std::future<IOSize> handle(std::shared_ptr<std::vector<IOPosBuffer>> iolist);
66 
73  std::future<IOSize> handle(std::shared_ptr<XrdAdaptor::ClientRequest> c_ptr);
74 
78  void requestFailure(std::shared_ptr<XrdAdaptor::ClientRequest> c_ptr, XrdCl::Status &c_status);
79 
84  void getActiveSourceNames(std::vector<std::string> &sources) const;
85  void getPrettyActiveSourceNames(std::vector<std::string> &sources) const;
86 
91  void getDisabledSourceNames(std::vector<std::string> &sources) const;
92 
97  std::shared_ptr<XrdCl::File> getActiveFile() const;
98 
102  void addConnections(cms::Exception &) const;
103 
107  const std::string &getFilename() const { return m_name; }
108 
116  static std::shared_ptr<RequestManager> getInstance(const std::string &filename,
117  XrdCl::OpenFlags::Flags flags,
118  XrdCl::Access::Mode perms) {
119  std::shared_ptr<RequestManager> instance(new RequestManager(filename, flags, perms));
120  instance->initialize(instance);
121  return instance;
122  }
123 
124  private:
125  RequestManager(const std::string &filename, XrdCl::OpenFlags::Flags flags, XrdCl::Access::Mode perms);
126 
133  void initialize(std::weak_ptr<RequestManager> selfref);
134 
138  virtual void handleOpen(XrdCl::XRootDStatus &status, std::shared_ptr<Source>);
139 
143  void splitClientRequest(const std::vector<IOPosBuffer> &iolist,
144  std::vector<IOPosBuffer> &req1,
145  std::vector<IOPosBuffer> &req2,
146  std::vector<std::shared_ptr<Source>> const &activeSources) const;
147 
153  void broadcastRequest(const ClientRequest &, bool active);
154 
161  void checkSources(timespec &now,
162  IOSize requestSize,
163  std::vector<std::shared_ptr<Source>> &activeSources,
164  std::vector<std::shared_ptr<Source>> &inactiveSources); // TODO: inline
165  void checkSourcesImpl(timespec &now,
166  IOSize requestSize,
167  std::vector<std::shared_ptr<Source>> &activeSources,
168  std::vector<std::shared_ptr<Source>> &inactiveSources);
177  bool compareSources(const timespec &now,
178  unsigned a,
179  unsigned b,
180  std::vector<std::shared_ptr<Source>> &activeSources,
181  std::vector<std::shared_ptr<Source>> &inactiveSources) const;
182 
187  void reportSiteChange(std::vector<std::shared_ptr<Source>> const &iOld,
188  std::vector<std::shared_ptr<Source>> const &iNew,
189  std::string orig_site = std::string{}) const;
190 
194  inline void updateCurrentServer();
196 
200  std::shared_ptr<Source> pickSingleSource();
201 
207 
212  std::vector<std::shared_ptr<Source>> m_activeSources;
213  std::vector<std::shared_ptr<Source>> m_inactiveSources;
214 
215  tbb::concurrent_unordered_set<std::string> m_disabledSourceStrings;
216  tbb::concurrent_unordered_set<std::string> m_disabledExcludeStrings;
217  tbb::concurrent_unordered_set<std::shared_ptr<Source>, SourceHash> m_disabledSources;
218 
219  // StatisticsSenderService wants to know what our current server is;
220  // this holds last-successfully-opened server name
221  std::atomic<std::string *> m_serverToAdvertise;
222 
225  // If set to true, the next active source should be 1; 0 otherwise.
227  // The time when the next active source check should be performed.
230 
232  XrdCl::OpenFlags::Flags m_flags;
234  mutable std::recursive_mutex m_source_mutex;
235 
236  std::mt19937 m_generator;
237  std::uniform_real_distribution<float> m_distribution;
238 
239  std::atomic<unsigned> m_excluded_active_count;
240 
241  class OpenHandler : public XrdCl::ResponseHandler {
242  public:
243  OpenHandler(const OpenHandler &) = delete;
244  OpenHandler &operator=(const OpenHandler &) = delete;
245 
246  static std::shared_ptr<OpenHandler> getInstance(std::weak_ptr<RequestManager> manager) {
247  OpenHandler *instance_ptr = new OpenHandler(manager);
248  std::shared_ptr<OpenHandler> instance(instance_ptr);
249  instance_ptr->m_self_weak = instance;
250  return instance;
251  }
252 
253  ~OpenHandler() override;
254 
258  void HandleResponseWithHosts(XrdCl::XRootDStatus *status,
259  XrdCl::AnyObject *response,
260  XrdCl::HostList *hostList) override;
261 
271  std::shared_future<std::shared_ptr<Source>> open();
272 
277 
278  private:
279  OpenHandler(std::weak_ptr<RequestManager> manager);
280  std::shared_future<std::shared_ptr<Source>> m_shared_future;
281  std::promise<std::shared_ptr<Source>> m_promise;
282  // Set to true only when there is an outstanding open request; not
283  // protected by m_mutex, so the caller is required to know it is in a
284  // thread-safe context.
285  std::atomic<bool> m_outstanding_open{false};
286  // Can only be touched when m_mutex is held.
287  std::unique_ptr<XrdCl::File> m_file;
288  std::recursive_mutex m_mutex;
289  std::shared_ptr<OpenHandler> m_self;
290 
291  // Always maintain a weak self-reference; when the open is in-progress,
292  // this is upgraded to a strong reference to prevent this object from
293  // deletion as long as XrdCl has not performed the callback.
294  std::weak_ptr<OpenHandler> m_self_weak;
295  std::weak_ptr<RequestManager> m_manager;
296  };
297 
298  std::shared_ptr<OpenHandler> m_open_handler;
299  };
300 
301 } // namespace XrdAdaptor
302 
303 #endif
~XrootdException() noexceptoverride
int64_t IOOffset
Definition: IOTypes.h:20
virtual ~RequestManager()=default
std::shared_future< std::shared_ptr< Source > > open()
std::shared_ptr< XrdCl::File > getActiveFile() const
void getDisabledSourceNames(std::vector< std::string > &sources) const
const std::string & getFilename() const
static PFTauRenderPlugin instance
std::uniform_real_distribution< float > m_distribution
size_t operator()(const Key &iKey) const
XrootdException(XrdCl::Status &xrootd_status, edm::Exception::Code code)
list status
Definition: mps_update.py:107
std::string prepareOpaqueString() const
OpenHandler & operator=(const OpenHandler &)=delete
std::shared_ptr< OpenHandler > m_self
virtual void handleOpen(XrdCl::XRootDStatus &status, std::shared_ptr< Source >)
static std::shared_ptr< RequestManager > getInstance(const std::string &filename, XrdCl::OpenFlags::Flags flags, XrdCl::Access::Mode perms)
std::weak_ptr< OpenHandler > m_self_weak
static const unsigned int XRD_DEFAULT_TIMEOUT
OpenHandler(const OpenHandler &)=delete
void addConnections(cms::Exception &) const
void splitClientRequest(const std::vector< IOPosBuffer > &iolist, std::vector< IOPosBuffer > &req1, std::vector< IOPosBuffer > &req2, std::vector< std::shared_ptr< Source >> const &activeSources) const
std::shared_future< std::shared_ptr< Source > > m_shared_future
void queueUpdateCurrentServer(const std::string &)
SiPixelHitStatus Status
std::weak_ptr< RequestManager > m_manager
RequestManager & operator=(const RequestManager &)=delete
bool compareSources(const timespec &now, unsigned a, unsigned b, std::vector< std::shared_ptr< Source >> &activeSources, std::vector< std::shared_ptr< Source >> &inactiveSources) const
std::vector< std::shared_ptr< Source > > m_inactiveSources
tbb::concurrent_unordered_set< std::string > m_disabledSourceStrings
void checkSourcesImpl(timespec &now, IOSize requestSize, std::vector< std::shared_ptr< Source >> &activeSources, std::vector< std::shared_ptr< Source >> &inactiveSources)
void getPrettyActiveSourceNames(std::vector< std::string > &sources) const
tbb::concurrent_unordered_set< std::shared_ptr< Source >, SourceHash > m_disabledSources
std::atomic< std::string * > m_serverToAdvertise
std::shared_ptr< OpenHandler > m_open_handler
void requestFailure(std::shared_ptr< XrdAdaptor::ClientRequest > c_ptr, XrdCl::Status &c_status)
void HandleResponseWithHosts(XrdCl::XRootDStatus *status, XrdCl::AnyObject *response, XrdCl::HostList *hostList) override
void getActiveSourceNames(std::vector< std::string > &sources) const
size_t IOSize
Definition: IOTypes.h:15
XrdCl::OpenFlags::Flags m_flags
static std::shared_ptr< OpenHandler > getInstance(std::weak_ptr< RequestManager > manager)
void reportSiteChange(std::vector< std::shared_ptr< Source >> const &iOld, std::vector< std::shared_ptr< Source >> const &iNew, std::string orig_site=std::string{}) const
std::vector< std::shared_ptr< Source > > m_activeSources
XrdCl::Access::Mode m_perms
double b
Definition: hdecay.h:118
tbb::concurrent_unordered_set< std::string > m_disabledExcludeStrings
edm::storage::IOOffset IOOffset
std::atomic< unsigned > m_excluded_active_count
double a
Definition: hdecay.h:119
std::shared_ptr< Source > pickSingleSource()
tuple filename
Definition: lut2db_cfg.py:20
std::unique_ptr< XrdCl::File > m_file
edm::storage::IOSize IOSize
std::future< IOSize > handle(void *into, IOSize size, IOOffset off)
std::promise< std::shared_ptr< Source > > m_promise
RequestManager(const RequestManager &)=delete
tuple size
Write out results.
std::recursive_mutex m_source_mutex
void broadcastRequest(const ClientRequest &, bool active)
void initialize(std::weak_ptr< RequestManager > selfref)
void checkSources(timespec &now, IOSize requestSize, std::vector< std::shared_ptr< Source >> &activeSources, std::vector< std::shared_ptr< Source >> &inactiveSources)