libzypp  17.32.4
provide_p.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \----------------------------------------------------------------------/
9 *
10 * This file contains private API, this might break at any time between releases.
11 * You have been warned!
12 *
13 */
14 #ifndef ZYPP_MEDIA_PRIVATE_PROVIDE_P_H_INCLUDED
15 #define ZYPP_MEDIA_PRIVATE_PROVIDE_P_H_INCLUDED
16 
17 #include "providefwd_p.h"
18 #include "providequeue_p.h"
19 #include "attachedmediainfo_p.h"
20 
21 #include <zypp-media/auth/CredentialManager>
22 #include <zypp-media/ng/Provide>
23 #include <zypp-media/ng/ProvideItem>
24 #include <zypp-media/ng/ProvideSpec>
25 #include <zypp-core/zyppng/base/private/base_p.h>
26 #include <zypp-core/zyppng/base/Timer>
27 #include <zypp-core/ManagedFile.h>
28 
29 namespace zyppng {
30 
31  namespace constants {
32  constexpr std::string_view DEFAULT_PROVIDE_WORKER_PATH = ZYPP_WORKER_PATH;
33  constexpr std::string_view ATTACHED_MEDIA_SUFFIX = "-media";
34  constexpr auto DEFAULT_ACTIVE_CONN_PER_HOST = 5; //< how many simultanious connections to the same host are allowed
35  constexpr auto DEFAULT_ACTIVE_CONN = 10; //< how many simultanious connections are allowed
36  constexpr auto DEFAULT_MAX_DYNAMIC_WORKERS = 20;
37  constexpr auto DEFAULT_CPU_WORKERS = 4;
38  }
39 
40  class ProvideQueue;
41  class RpcMessageStream;
42  using RpcMessageStreamPtr = std::shared_ptr<RpcMessageStream>;
43 
44 
45  class ProvidePrivate : public BasePrivate
46  {
48  public:
49  ProvidePrivate( zypp::Pathname &&workDir, Provide &pub );
50 
52  {
59  };
60 
61  void schedule( ScheduleReason reason );
62 
63  bool queueRequest ( ProvideRequestRef req );
64  bool dequeueRequest( ProvideRequestRef req, std::exception_ptr error );
65  void queueItem ( ProvideItemRef item );
66  void dequeueItem ( ProvideItem *item );
67 
68  std::string nextMediaId () const;
69  AttachedMediaInfo_Ptr addMedium ( AttachedMediaInfo_Ptr &&medium );
70 
71  std::string effectiveScheme ( const std::string &scheme ) const;
72 
73  void onPulseTimeout ( Timer & );
74  void onQueueIdle ();
75  void onItemStateChanged ( ProvideItem &item );
76  expected<ProvideQueue::Config> schemeConfig(const std::string &scheme);
77 
78  std::optional<zypp::ManagedFile> addToFileCache ( const zypp::Pathname &downloadedFile );
79  bool isInCache ( const zypp::Pathname &downloadedFile ) const;
80 
81  bool isRunning() const;
82 
83  const zypp::Pathname &workerPath() const;
84  const std::string queueName( ProvideQueue &q ) const;
85 
86  std::vector<AttachedMediaInfo_Ptr> &attachedMediaInfos();
87 
88  std::list<ProvideItemRef> &items();
89 
91 
92  ProvideStatusRef log () {
93  return _log;
94  }
95 
96  uint32_t nextRequestId();
97 
98  Signal< Provide::MediaChangeAction ( const std::string &, const std::string &, const int32_t, const std::vector<std::string> &, const std::optional<std::string> &) > _sigMediaChange;
99  Signal< std::optional<zypp::media::AuthData> ( const zypp::Url &reqUrl, const std::string &triedUsername, const std::map<std::string, std::string> &extraValues ) > _sigAuthRequired;
100 
101  protected:
102  void doSchedule (Timer &);
103 
104  //@TODO should we make those configurable?
105  std::unordered_map< std::string, std::string > _workerAlias {
106  {"ftp" ,"http"},
107  {"tftp" ,"http"},
108  {"https","http"},
109  {"cifs" ,"smb" },
110  {"nfs4" ,"nfs" },
111  {"cd" ,"disc"},
112  {"dvd" ,"disc"},
113  {"file" ,"dir" },
114  {"hd" ,"disk"}
115  };
116 
117  bool _isRunning = false;
118  bool _isScheduling = false;
119  Timer::Ptr _pulseTimer = Timer::create();
120  Timer::Ptr _scheduleTrigger = Timer::create(); //< instead of constantly calling schedule we set a trigger event so it runs as soon as event loop is on again
122 
123  std::list< ProvideItemRef > _items; //< The list of running provide Items, each of them can spawn multiple requests
124  uint32_t _nextRequestId = 0; //< The next request ID , we use controller wide unique IDs instead of worker locals IDs , its easier to track
125 
126  struct QueueItem {
127  std::string _schemeName;
128  std::deque<ProvideRequestRef> _requests;
129  };
130  std::deque<QueueItem> _queues; //< List of request queues for the workers, grouped by scheme. We use a deque and not a map because of possible changes to the list of queues during scheduling
131 
132 
133  std::vector< AttachedMediaInfo_Ptr > _attachedMediaInfos; //< List of currently attached medias
134 
135  std::unordered_map< std::string, ProvideQueueRef > _workerQueues;
136  std::unordered_map< std::string, ProvideQueue::Config > _schemeConfigs;
137 
138  struct FileCacheItem {
140  std::optional<std::chrono::steady_clock::time_point> _deathTimer; // timepoint where this item was seen first without a refcount
141  };
142  std::unordered_map< std::string, FileCacheItem > _fileCache;
143 
146 
147  ProvideStatusRef _log;
148  Signal<void()> _sigIdle;
149  };
150 }
151 
152 #endif
constexpr auto DEFAULT_MAX_DYNAMIC_WORKERS
Definition: provide_p.h:36
std::optional< zypp::ManagedFile > addToFileCache(const zypp::Pathname &downloadedFile)
Definition: provide.cc:729
bool queueRequest(ProvideRequestRef req)
Definition: provide.cc:791
ProvideStatusRef _log
Definition: provide_p.h:147
Signal< Provide::MediaChangeAction(const std::string &, const std::string &, const int32_t, const std::vector< std::string > &, const std::optional< std::string > &) > _sigMediaChange
Definition: provide_p.h:98
std::list< ProvideItemRef > _items
Definition: provide_p.h:123
Signal< std::optional< zypp::media::AuthData > const zypp::Url &reqUrl, const std::string &triedUsername, const std::map< std::string, std::string > &extraValues) > _sigAuthRequired
Definition: provide_p.h:99
Signal< void()> _sigIdle
Definition: provide_p.h:148
const zypp::Pathname & workerPath() const
Definition: provide.cc:829
std::string nextMediaId() const
Definition: provide.cc:773
std::unordered_map< std::string, ProvideQueue::Config > _schemeConfigs
Definition: provide_p.h:136
std::optional< std::chrono::steady_clock::time_point > _deathTimer
Definition: provide_p.h:140
uint32_t nextRequestId()
Definition: provide.cc:916
zypp::Pathname _workerPath
Definition: provide_p.h:144
Timer::Ptr _scheduleTrigger
Definition: provide_p.h:120
std::vector< AttachedMediaInfo_Ptr > & attachedMediaInfos()
Definition: provide.cc:709
bool isInCache(const zypp::Pathname &downloadedFile) const
Definition: provide.cc:749
expected< ProvideQueue::Config > schemeConfig(const std::string &scheme)
Definition: provide.cc:714
ProvidePrivate(zypp::Pathname &&workDir, Provide &pub)
Definition: provide.cc:21
std::deque< QueueItem > _queues
Definition: provide_p.h:130
void onItemStateChanged(ProvideItem &item)
Definition: provide.cc:898
std::list< ProvideItemRef > & items()
Definition: provide.cc:699
void onPulseTimeout(Timer &)
Definition: provide.cc:857
bool isRunning() const
Definition: provide.cc:843
ProvideStatusRef log()
Definition: provide_p.h:92
std::vector< AttachedMediaInfo_Ptr > _attachedMediaInfos
Definition: provide_p.h:133
void schedule(ScheduleReason reason)
Definition: provide.cc:38
zypp::Pathname _workDir
Definition: provide_p.h:121
constexpr auto DEFAULT_CPU_WORKERS
Definition: provide_p.h:37
constexpr auto DEFAULT_ACTIVE_CONN_PER_HOST
Definition: provide_p.h:34
const std::string queueName(ProvideQueue &q) const
Definition: provide.cc:834
void dequeueItem(ProvideItem *item)
Definition: provide.cc:761
zypp::media::CredManagerOptions _credManagerOptions
Definition: provide_p.h:145
std::shared_ptr< RpcMessageStream > RpcMessageStreamPtr
Definition: provide_p.h:42
constexpr std::string_view DEFAULT_PROVIDE_WORKER_PATH
Definition: provide_p.h:32
std::unordered_map< std::string, FileCacheItem > _fileCache
Definition: provide_p.h:142
bool dequeueRequest(ProvideRequestRef req, std::exception_ptr error)
Definition: provide.cc:807
constexpr auto DEFAULT_ACTIVE_CONN
Definition: provide_p.h:35
void queueItem(ProvideItemRef item)
Definition: provide.cc:755
zypp::media::CredManagerOptions & credManagerOptions()
Definition: provide.cc:704
std::unordered_map< std::string, std::string > _workerAlias
Definition: provide_p.h:105
Timer::Ptr _pulseTimer
Definition: provide_p.h:119
AttachedMediaInfo_Ptr addMedium(AttachedMediaInfo_Ptr &&medium)
Definition: provide.cc:779
void doSchedule(Timer &)
Definition: provide.cc:70
std::unordered_map< std::string, ProvideQueueRef > _workerQueues
Definition: provide_p.h:135
std::string effectiveScheme(const std::string &scheme) const
Definition: provide.cc:848
std::deque< ProvideRequestRef > _requests
Definition: provide_p.h:128
constexpr std::string_view ATTACHED_MEDIA_SUFFIX
Definition: provide_p.h:33
Url manipulation class.
Definition: Url.h:91