libzypp  17.31.31
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-proto/core/envelope.pb.h>
26 #include <zypp-proto/media/provider.pb.h>
27 #include <zypp-core/zyppng/base/private/base_p.h>
28 #include <zypp-core/zyppng/base/Timer>
29 #include <zypp-core/ManagedFile.h>
30 
31 #include <queue>
32 #include <variant>
33 
34 namespace zyppng {
35 
36  namespace constants {
37  constexpr std::string_view DEFAULT_PROVIDE_WORKER_PATH = ZYPP_WORKER_PATH;
38  constexpr std::string_view ATTACHED_MEDIA_SUFFIX = "-media";
39  constexpr auto DEFAULT_ACTIVE_CONN_PER_HOST = 5; //< how many simultanious connections to the same host are allowed
40  constexpr auto DEFAULT_ACTIVE_CONN = 10; //< how many simultanious connections are allowed
41  constexpr auto DEFAULT_MAX_DYNAMIC_WORKERS = 20;
42  constexpr auto DEFAULT_CPU_WORKERS = 4;
43  }
44 
45  class ProvideQueue;
46  class RpcMessageStream;
47  using RpcMessageStreamPtr = std::shared_ptr<RpcMessageStream>;
48 
49 
50  class ProvidePrivate : public BasePrivate
51  {
53  public:
54  ProvidePrivate( zypp::Pathname &&workDir, Provide &pub );
55 
57  {
64  };
65 
66  void schedule( ScheduleReason reason );
67 
68  bool queueRequest ( ProvideRequestRef req );
69  bool dequeueRequest( ProvideRequestRef req, std::exception_ptr error );
70  void queueItem ( ProvideItemRef item );
71  void dequeueItem ( ProvideItem *item );
72 
73  std::string nextMediaId () const;
74  AttachedMediaInfo &addMedium ( zypp::proto::Capabilities::WorkerType workerType, const zypp::Url &baseUrl, ProvideMediaSpec &spec );
75  AttachedMediaInfo &addMedium ( zypp::proto::Capabilities::WorkerType workerType, ProvideQueueWeakRef backingQueue, const std::string &id, const zypp::Url &baseUrl, ProvideMediaSpec &spec );
76 
77  std::string effectiveScheme ( const std::string &scheme ) const;
78 
79  void onPulseTimeout ( Timer & );
80  void onQueueIdle ();
81  void onItemStateChanged ( ProvideItem &item );
82  expected<ProvideQueue::Config> schemeConfig(const std::string &scheme);
83 
84  std::optional<zypp::ManagedFile> addToFileCache ( const zypp::Pathname &downloadedFile );
85  bool isInCache ( const zypp::Pathname &downloadedFile ) const;
86 
87  bool isRunning() const;
88 
89  const zypp::Pathname &workerPath() const;
90  const std::string queueName( ProvideQueue &q ) const;
91 
92  std::vector<AttachedMediaInfo> &attachedMediaInfos();
93 
94  std::list<ProvideItemRef> &items();
95 
97 
98  ProvideStatusRef log () {
99  return _log;
100  }
101 
102  uint32_t nextRequestId();
103 
104  Signal< Provide::MediaChangeAction ( const std::string &, const std::string &, const int32_t, const std::vector<std::string> &, const std::optional<std::string> &) > _sigMediaChange;
105  Signal< std::optional<zypp::media::AuthData> ( const zypp::Url &reqUrl, const std::string &triedUsername, const std::map<std::string, std::string> &extraValues ) > _sigAuthRequired;
106 
107  protected:
108  void doSchedule (Timer &);
109 
110  //@TODO should we make those configurable?
111  std::unordered_map< std::string, std::string > _workerAlias {
112  {"ftp" ,"http"},
113  {"tftp" ,"http"},
114  {"https","http"},
115  {"cifs" ,"smb" },
116  {"nfs4" ,"nfs" },
117  {"cd" ,"disc"},
118  {"dvd" ,"disc"},
119  {"file" ,"dir" },
120  {"hd" ,"disk"}
121  };
122 
123  bool _isRunning = false;
124  bool _isScheduling = false;
125  Timer::Ptr _pulseTimer = Timer::create();
126  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
128 
129  std::list< ProvideItemRef > _items; //< The list of running provide Items, each of them can spawn multiple requests
130  uint32_t _nextRequestId = 0; //< The next request ID , we use controller wide unique IDs instead of worker locals IDs , its easier to track
131 
132  struct QueueItem {
133  std::string _schemeName;
134  std::deque<ProvideRequestRef> _requests;
135  };
136  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
137 
138 
139  std::vector< AttachedMediaInfo > _attachedMediaInfos; //< List of currently attached medias
140 
141  std::unordered_map< std::string, ProvideQueueRef > _workerQueues;
142  std::unordered_map< std::string, ProvideQueue::Config > _schemeConfigs;
143 
144  struct FileCacheItem {
146  std::optional<std::chrono::steady_clock::time_point> _deathTimer; // timepoint where this item was seen first without a refcount
147  };
148  std::unordered_map< std::string, FileCacheItem > _fileCache;
149 
152 
153  ProvideStatusRef _log;
154  Signal<void()> _sigIdle;
155  };
156 }
157 
158 #endif
constexpr auto DEFAULT_MAX_DYNAMIC_WORKERS
Definition: provide_p.h:41
std::optional< zypp::ManagedFile > addToFileCache(const zypp::Pathname &downloadedFile)
Definition: provide.cc:727
bool queueRequest(ProvideRequestRef req)
Definition: provide.cc:792
ProvideStatusRef _log
Definition: provide_p.h:153
std::vector< AttachedMediaInfo > _attachedMediaInfos
Definition: provide_p.h:139
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:104
std::list< ProvideItemRef > _items
Definition: provide_p.h:129
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:105
Signal< void()> _sigIdle
Definition: provide_p.h:154
const zypp::Pathname & workerPath() const
Definition: provide.cc:830
std::string nextMediaId() const
Definition: provide.cc:771
std::unordered_map< std::string, ProvideQueue::Config > _schemeConfigs
Definition: provide_p.h:142
std::optional< std::chrono::steady_clock::time_point > _deathTimer
Definition: provide_p.h:146
uint32_t nextRequestId()
Definition: provide.cc:917
zypp::Pathname _workerPath
Definition: provide_p.h:150
Timer::Ptr _scheduleTrigger
Definition: provide_p.h:126
bool isInCache(const zypp::Pathname &downloadedFile) const
Definition: provide.cc:747
expected< ProvideQueue::Config > schemeConfig(const std::string &scheme)
Definition: provide.cc:712
ProvidePrivate(zypp::Pathname &&workDir, Provide &pub)
Definition: provide.cc:21
std::vector< AttachedMediaInfo > & attachedMediaInfos()
Definition: provide.cc:707
std::deque< QueueItem > _queues
Definition: provide_p.h:136
void onItemStateChanged(ProvideItem &item)
Definition: provide.cc:899
std::list< ProvideItemRef > & items()
Definition: provide.cc:697
void onPulseTimeout(Timer &)
Definition: provide.cc:858
bool isRunning() const
Definition: provide.cc:844
ProvideStatusRef log()
Definition: provide_p.h:98
void schedule(ScheduleReason reason)
Definition: provide.cc:38
zypp::Pathname _workDir
Definition: provide_p.h:127
constexpr auto DEFAULT_CPU_WORKERS
Definition: provide_p.h:42
constexpr auto DEFAULT_ACTIVE_CONN_PER_HOST
Definition: provide_p.h:39
const std::string queueName(ProvideQueue &q) const
Definition: provide.cc:835
void dequeueItem(ProvideItem *item)
Definition: provide.cc:759
zypp::media::CredManagerOptions _credManagerOptions
Definition: provide_p.h:151
std::shared_ptr< RpcMessageStream > RpcMessageStreamPtr
Definition: provide_p.h:47
constexpr std::string_view DEFAULT_PROVIDE_WORKER_PATH
Definition: provide_p.h:37
std::unordered_map< std::string, FileCacheItem > _fileCache
Definition: provide_p.h:148
bool dequeueRequest(ProvideRequestRef req, std::exception_ptr error)
Definition: provide.cc:808
constexpr auto DEFAULT_ACTIVE_CONN
Definition: provide_p.h:40
void queueItem(ProvideItemRef item)
Definition: provide.cc:753
zypp::media::CredManagerOptions & credManagerOptions()
Definition: provide.cc:702
std::unordered_map< std::string, std::string > _workerAlias
Definition: provide_p.h:111
AttachedMediaInfo & addMedium(zypp::proto::Capabilities::WorkerType workerType, const zypp::Url &baseUrl, ProvideMediaSpec &spec)
Timer::Ptr _pulseTimer
Definition: provide_p.h:125
void doSchedule(Timer &)
Definition: provide.cc:70
std::unordered_map< std::string, ProvideQueueRef > _workerQueues
Definition: provide_p.h:141
std::string effectiveScheme(const std::string &scheme) const
Definition: provide.cc:849
std::deque< ProvideRequestRef > _requests
Definition: provide_p.h:134
constexpr std::string_view ATTACHED_MEDIA_SUFFIX
Definition: provide_p.h:38
Url manipulation class.
Definition: Url.h:91