PeerPoolMgr.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2022 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 #include "squid.h"
10 #include "AccessLogEntry.h"
11 #include "base/AsyncJobCalls.h"
12 #include "base/RunnersRegistry.h"
13 #include "CachePeer.h"
14 #include "comm/Connection.h"
15 #include "comm/ConnOpener.h"
16 #include "debug/Stream.h"
17 #include "fd.h"
18 #include "FwdState.h"
19 #include "globals.h"
20 #include "HttpRequest.h"
21 #include "MasterXaction.h"
22 #include "neighbors.h"
23 #include "pconn.h"
24 #include "PeerPoolMgr.h"
26 #include "SquidConfig.h"
27 
29 
31 class MyAnswerDialer: public UnaryMemFunT<PeerPoolMgr, Security::EncryptorAnswer, Security::EncryptorAnswer&>,
33 {
34 public:
35  MyAnswerDialer(const JobPointer &aJob, Method aMethod):
36  UnaryMemFunT<PeerPoolMgr, Security::EncryptorAnswer, Security::EncryptorAnswer&>(aJob, aMethod, Security::EncryptorAnswer()) {}
37 
38  /* Security::PeerConnector::CbDialer API */
39  virtual Security::EncryptorAnswer &answer() { return arg1; }
40 };
41 
43  peer(cbdataReference(aPeer)),
44  request(),
45  transportWait(),
46  encryptionWait(),
47  addrUsed(0)
48 {
49 }
50 
52 {
54 }
55 
56 void
58 {
60 
61  const auto mx = MasterXaction::MakePortless<XactionInitiator::initPeerPool>();
62  // ErrorState, getOutgoingAddress(), and other APIs may require a request.
63  // We fake one. TODO: Optionally send this request to peers?
66 
67  checkpoint("peer initialized");
68 }
69 
70 void
72 {
74 }
75 
76 bool
78 {
80 }
81 
82 bool
84 {
85  return !(validPeer() && peer->standby.limit) && AsyncJob::doneAll();
86 }
87 
88 void
90 {
91  transportWait.finish();
92 
93  if (!validPeer()) {
94  debugs(48, 3, "peer gone");
95  if (params.conn != NULL)
96  params.conn->close();
97  return;
98  }
99 
100  if (params.flag != Comm::OK) {
102  checkpoint("conn opening failure"); // may retry
103  return;
104  }
105 
106  Must(params.conn != NULL);
107 
108  // Handle TLS peers.
110  // XXX: Exceptions orphan params.conn
111  AsyncCall::Pointer callback = asyncCall(48, 4, "PeerPoolMgr::handleSecuredPeer",
113 
114  const auto peerTimeout = peer->connectTimeout();
115  const int timeUsed = squid_curtime - params.conn->startTime();
116  // Use positive timeout when less than one second is left for conn.
117  const int timeLeft = positiveTimeout(peerTimeout - timeUsed);
118  const auto connector = new Security::BlindPeerConnector(request, params.conn, callback, nullptr, timeLeft);
119  encryptionWait.start(connector, callback);
120  return;
121  }
122 
123  pushNewConnection(params.conn);
124 }
125 
126 void
128 {
129  Must(validPeer());
131  peer->standby.pool->push(conn, NULL /* domain */);
132  // push() will trigger a checkpoint()
133 }
134 
135 void
137 {
138  encryptionWait.finish();
139 
140  if (!validPeer()) {
141  debugs(48, 3, "peer gone");
142  if (answer.conn != NULL)
143  answer.conn->close();
144  return;
145  }
146 
147  assert(!answer.tunneled);
148  if (answer.error.get()) {
149  assert(!answer.conn);
150  // PeerConnector calls peerConnectFailed() for us;
151  checkpoint("conn securing failure"); // may retry
152  return;
153  }
154 
155  assert(answer.conn);
156 
157  // The socket could get closed while our callback was queued. Sync
158  // Connection. XXX: Connection::fd may already be stale/invalid here.
159  if (answer.conn->isOpen() && fd_table[answer.conn->fd].closing()) {
160  answer.conn->noteClosure();
161  checkpoint("external connection closure"); // may retry
162  return;
163  }
164 
165  pushNewConnection(answer.conn);
166 }
167 
168 void
170 {
171  // KISS: Do nothing else when we are already doing something.
173  debugs(48, 7, "busy: " << transportWait << '|' << encryptionWait << '|' << shutting_down);
174  return; // there will be another checkpoint when we are done opening/securing
175  }
176 
177  // Do not talk to a peer until it is ready.
178  if (!neighborUp(peer)) // provides debugging
179  return; // there will be another checkpoint when peer is up
180 
181  // Do not violate peer limits.
182  if (!peerCanOpenMore(peer)) { // provides debugging
183  peer->standby.waitingForClose = true; // may already be true
184  return; // there will be another checkpoint when a peer conn closes
185  }
186 
187  // Do not violate global restrictions.
188  if (fdUsageHigh()) {
189  debugs(48, 7, "overwhelmed");
190  peer->standby.waitingForClose = true; // may already be true
191  // There will be another checkpoint when a peer conn closes OR when
192  // a future pop() fails due to an empty pool. See PconnPool::pop().
193  return;
194  }
195 
196  peer->standby.waitingForClose = false;
197 
199  Must(peer->n_addresses); // guaranteed by neighborUp() above
200  // cycle through all available IP addresses
201  conn->remote = peer->addresses[addrUsed++ % peer->n_addresses];
202  conn->remote.port(peer->http_port);
203  conn->peerType = STANDBY_POOL; // should be reset by peerSelect()
204  conn->setPeer(peer);
207 
208  const auto ctimeout = peer->connectTimeout();
210  AsyncCall::Pointer callback = JobCallback(48, 5, Dialer, this, PeerPoolMgr::handleOpenedConnection);
211  const auto cs = new Comm::ConnOpener(conn, callback, ctimeout);
212  transportWait.start(cs, callback);
213 }
214 
215 void
217 {
218  debugs(48, 8, howMany);
219  peer->standby.pool->closeN(howMany);
220 }
221 
222 void
223 PeerPoolMgr::checkpoint(const char *reason)
224 {
225  if (!validPeer()) {
226  debugs(48, 3, reason << " and peer gone");
227  return; // nothing to do after our owner dies; the job will quit
228  }
229 
230  const int count = peer->standby.pool->count();
231  const int limit = peer->standby.limit;
232  debugs(48, 7, reason << " with " << count << " ? " << limit);
233 
234  if (count < limit)
236  else if (count > limit)
237  closeOldConnections(count - limit);
238 }
239 
240 void
241 PeerPoolMgr::Checkpoint(const Pointer &mgr, const char *reason)
242 {
243  CallJobHere1(48, 5, mgr, PeerPoolMgr, checkpoint, reason);
244 }
245 
248 {
249 public:
250  /* RegisteredRunner API */
251  virtual void useConfig() { syncConfig(); }
252  virtual void syncConfig();
253 };
254 
256 
257 void
259 {
260  for (CachePeer *p = Config.peers; p; p = p->next) {
261  // On reconfigure, Squid deletes the old config (and old peers in it),
262  // so should always be dealing with a brand new configuration.
263  assert(!p->standby.mgr);
264  assert(!p->standby.pool);
265  if (p->standby.limit) {
266  p->standby.mgr = new PeerPoolMgr(p);
267  p->standby.pool = new PconnPool(p->name, p->standby.mgr);
268  AsyncJob::Start(p->standby.mgr.get());
269  }
270  }
271 }
272 
bool waitingForClose
a conn must close before we open a standby conn
Definition: CachePeer.h:184
void handleSecuredPeer(Security::EncryptorAnswer &answer)
Security::PeerConnector callback.
Definition: PeerPoolMgr.cc:136
Maintains an fixed-size "standby" PconnPool for a single CachePeer.
Definition: PeerPoolMgr.h:23
void closeN(int n)
closes any n connections, regardless of their destination
Definition: pconn.cc:507
Ip::Address addresses[10]
Definition: CachePeer.h:150
Cbc * get() const
a temporary valid raw Cbc pointer or NULL
Definition: CbcPointer.h:162
bool tunneled
whether we spliced the connections instead of negotiating encryption
JobWait< Security::BlindPeerConnector > encryptionWait
waits for the established transport connection to be secured/encrypted
Definition: PeerPoolMgr.h:69
AnyP::Uri url
the request URI
Definition: HttpRequest.h:115
void peerConnectFailed(CachePeer *p)
Definition: neighbors.cc:1298
virtual void swanSong()
Definition: AsyncJob.h:59
CbcPointer< ErrorState > error
problem details (nil on success)
int fdUsageHigh(void)
Definition: fd.cc:271
PconnPool * pool
idle connection pool for this peer
Definition: CachePeer.h:181
void handleOpenedConnection(const CommConnectCbParams &params)
Comm::ConnOpener calls this when done opening a connection for us.
Definition: PeerPoolMgr.cc:89
@ STANDBY_POOL
Definition: hier_code.h:37
struct CachePeer::@35 standby
optional "cache_peer standby=limit" feature
JobWait< Comm::ConnOpener > transportWait
waits for a transport connection to the peer to be established/opened
Definition: PeerPoolMgr.h:66
Gives Security::PeerConnector access to Answer in the PeerPoolMgr callback dialer.
Definition: PeerPoolMgr.cc:33
C * getRaw() const
Definition: RefCount.h:80
time_t startTime() const
Definition: Connection.h:118
int cbdataReferenceValid(const void *p)
Definition: cbdata.cc:398
bool IsConnOpen(const Comm::ConnectionPointer &conn)
Definition: Connection.cc:27
@ OK
Definition: Flag.h:16
virtual void syncConfig()
Definition: PeerPoolMgr.cc:258
int limit
the limit itself
Definition: CachePeer.h:183
time_t connectTimeout() const
Definition: CachePeer.cc:51
#define cbdataReference(var)
Definition: cbdata.h:341
virtual void swanSong()
Definition: PeerPoolMgr.cc:71
CachePeer * peer
the owner of the pool we manage
Definition: PeerPoolMgr.h:62
@ METHOD_OPTIONS
Definition: MethodType.h:31
void getOutgoingAddress(HttpRequest *request, const Comm::ConnectionPointer &conn)
Definition: FwdState.cc:1524
unsigned int addrUsed
counter for cycling through peer addresses
Definition: PeerPoolMgr.h:71
MyAnswerDialer(const JobPointer &aJob, Method aMethod)
Definition: PeerPoolMgr.cc:35
Comm::ConnectionPointer conn
peer connection (secured on success)
bool validPeer() const
whether the peer is still out there and in a valid state we can safely use
Definition: PeerPoolMgr.cc:77
#define NULL
Definition: types.h:166
virtual bool doneAll() const
whether positive goal has been reached
Definition: AsyncJob.cc:97
launches PeerPoolMgrs for peers configured with standby.limit
Definition: PeerPoolMgr.cc:248
int n_addresses
Definition: CachePeer.h:151
void count(int uses)
void push(const Comm::ConnectionPointer &serverConn, const char *domain)
Definition: pconn.cc:412
Callback dialer API to allow PeerConnector to set the answer.
Definition: PeerConnector.h:57
Comm::ConnectionPointer conn
Definition: CommCalls.h:85
int conn
the current server connection FD
Definition: Transport.cc:26
#define assert(EX)
Definition: assert.h:19
SSL Connection
Definition: Session.h:45
Comm::Flag flag
comm layer result status.
Definition: CommCalls.h:87
#define cbdataReferenceDone(var)
Definition: cbdata.h:350
#define JobCallback(dbgSection, dbgLevel, Dialer, job, method)
Convenience macro to create a Dialer-based job callback.
Definition: AsyncJobCalls.h:69
#define CBDATA_CLASS_INIT(type)
Definition: cbdata.h:318
bool peerCanOpenMore(const CachePeer *p)
Whether we can open new connections to the peer (e.g., despite max-conn)
Definition: neighbors.cc:223
time_t squid_curtime
Definition: stub_libtime.cc:20
virtual Security::EncryptorAnswer & answer()
gives PeerConnector access to the in-dialer answer
Definition: PeerPoolMgr.cc:39
A simple PeerConnector for SSL/TLS cache_peers. No SslBump capabilities.
#define fd_table
Definition: fde.h:189
void start(const JobPointer &aJob, const AsyncCall::Pointer &aCallback)
starts waiting for the given job to call the given callback
Definition: JobWait.h:69
time_t positiveTimeout(const time_t timeout)
Definition: neighbors.cc:1195
void openNewConnection()
starts the process of opening a new standby connection (if possible)
Definition: PeerPoolMgr.cc:169
@ PROTO_HTTP
Definition: ProtocolType.h:25
char * host
Definition: CachePeer.h:40
RunnerRegistrationEntry(PeerPoolMgrsRr)
PeerPoolMgr(CachePeer *aPeer)
Definition: PeerPoolMgr.cc:42
void checkpoint(const char *reason)
Definition: PeerPoolMgr.cc:223
virtual void start()
called by AsyncStart; do not call directly
Definition: AsyncJob.cc:44
#define Must(condition)
Definition: TextException.h:71
int neighborUp(const CachePeer *p)
Definition: neighbors.cc:1163
AsyncCall * asyncCall(int aDebugSection, int aDebugLevel, const char *aName, const Dialer &aDialer)
Definition: AsyncCall.h:154
CachePeer * peers
Definition: SquidConfig.h:252
virtual void start()
called by AsyncStart; do not call directly
Definition: PeerPoolMgr.cc:57
int shutting_down
virtual ~PeerPoolMgr()
Definition: PeerPoolMgr.cc:51
bool encryptTransport
whether transport encryption (TLS/SSL) is to be used on connections to the peer
Definition: PeerOptions.h:145
virtual void useConfig()
Definition: PeerPoolMgr.cc:251
bool isOpen() const
Definition: Connection.h:99
CachePeer * next
Definition: CachePeer.h:153
void pushNewConnection(const Comm::ConnectionPointer &conn)
the final step in connection opening (and, optionally, securing) sequence
Definition: PeerPoolMgr.cc:127
static void Checkpoint(const Pointer &mgr, const char *reason)
Definition: PeerPoolMgr.cc:241
Network/connection security abstraction layer.
Definition: Connection.h:34
Security::PeerOptions secure
security settings for peer connection
Definition: CachePeer.h:190
void host(const char *src)
Definition: Uri.cc:99
struct _request * request(char *urlin)
Definition: tcp-banger2.c:291
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:196
unsigned short http_port
Definition: CachePeer.h:77
virtual bool doneAll() const
whether positive goal has been reached
Definition: PeerPoolMgr.cc:83
void closeOldConnections(const int howMany)
closes 'howMany' standby connections
Definition: PeerPoolMgr.cc:216
class SquidConfig Config
Definition: SquidConfig.cc:12
void GetMarkingsToServer(HttpRequest *request, Comm::Connection &conn)
Definition: FwdState.cc:1603
RefCount< HttpRequest > request
fake HTTP request for conn opening code
Definition: PeerPoolMgr.h:63
static void Start(const Pointer &job)
Definition: AsyncJob.cc:24
#define CallJobHere1(debugSection, debugLevel, job, Class, method, arg1)
Definition: AsyncJobCalls.h:63

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors