Inquirer.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 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 /* DEBUG: section 16 Cache Manager API */
10 
11 #include "squid.h"
12 #include "base/TextException.h"
13 #include "comm.h"
14 #include "comm/Connection.h"
15 #include "comm/Write.h"
16 #include "CommCalls.h"
17 #include "errorpage.h"
18 #include "HttpReply.h"
19 #include "HttpRequest.h"
20 #include "ipc/UdsOp.h"
21 #include "mgr/ActionWriter.h"
22 #include "mgr/Command.h"
23 #include "mgr/Inquirer.h"
24 #include "mgr/IntParam.h"
25 #include "mgr/Request.h"
26 #include "mgr/Response.h"
27 #include "SquidTime.h"
28 #include <memory>
29 #include <algorithm>
30 
31 CBDATA_NAMESPACED_CLASS_INIT(Mgr, Inquirer);
32 
34  const Request &aCause, const Ipc::StrandCoords &coords):
35  Ipc::Inquirer(aCause.clone(), applyQueryParams(coords, aCause.params.queryParams), anAction->atomic() ? 10 : 100),
36  aggrAction(anAction)
37 {
38  conn = aCause.conn;
39  Ipc::ImportFdIntoComm(conn, SOCK_STREAM, IPPROTO_TCP, Ipc::fdnHttpSocket);
40 
41  debugs(16, 5, HERE << conn << " action: " << aggrAction);
42 
43  closer = asyncCall(16, 5, "Mgr::Inquirer::noteCommClosed",
46 }
47 
49 void
51 {
52  if (Comm::IsConnOpen(conn)) {
53  removeCloseHandler();
54  conn->close();
55  }
56 }
57 
58 void
60 {
61  if (closer != NULL) {
62  comm_remove_close_handler(conn->fd, closer);
63  closer = NULL;
64  }
65 }
66 
67 void
69 {
70  debugs(16, 5, HERE);
73  Must(aggrAction != NULL);
74 
75  std::unique_ptr<MemBuf> replyBuf;
76  if (strands.empty()) {
77  const char *url = aggrAction->command().params.httpUri.termedBuf();
79  HttpRequest *req = HttpRequest::FromUrl(url, mx);
81  std::unique_ptr<HttpReply> reply(err.BuildHttpReply());
82  replyBuf.reset(reply->pack());
83  } else {
84  std::unique_ptr<HttpReply> reply(new HttpReply);
85  reply->setHeaders(Http::scOkay, NULL, "text/plain", -1, squid_curtime, squid_curtime);
86  reply->header.putStr(Http::HdrType::CONNECTION, "close"); // until we chunk response
87  replyBuf.reset(reply->pack());
88  }
89  writer = asyncCall(16, 5, "Mgr::Inquirer::noteWroteHeader",
91  Comm::Write(conn, replyBuf.get(), writer);
92 }
93 
95 void
97 {
98  debugs(16, 5, HERE);
99  writer = NULL;
100  Must(params.flag == Comm::OK);
101  Must(params.conn.getRaw() == conn.getRaw());
102  Must(params.size != 0);
103  // start inquiries at the initial pos
104  inquire();
105 }
106 
108 void
110 {
111  debugs(16, 5, HERE);
112  Must(!Comm::IsConnOpen(conn) && params.conn.getRaw() == conn.getRaw());
113  conn = NULL;
114  mustStop("commClosed");
115 }
116 
117 bool
119 {
120  Mgr::Response& response = static_cast<Response&>(*aResponse);
121  if (response.hasAction())
122  aggrAction->add(response.getAction());
123  return true;
124 }
125 
126 void
128 {
129  if (!strands.empty() && aggrAction->aggregatable()) {
130  removeCloseHandler();
131  AsyncJob::Start(new ActionWriter(aggrAction, conn));
132  conn = NULL; // should not close because we passed it to ActionWriter
133  }
134 }
135 
136 bool
138 {
139  return !writer && Ipc::Inquirer::doneAll();
140 }
141 
144 {
146 
147  QueryParam::Pointer processesParam = aParams.get("processes");
148  QueryParam::Pointer workersParam = aParams.get("workers");
149 
150  if (processesParam == NULL || workersParam == NULL) {
151  if (processesParam != NULL) {
152  IntParam* param = dynamic_cast<IntParam*>(processesParam.getRaw());
153  if (param != NULL && param->type == QueryParam::ptInt) {
154  const std::vector<int>& processes = param->value();
155  for (Ipc::StrandCoords::const_iterator iter = aStrands.begin();
156  iter != aStrands.end(); ++iter) {
157  if (std::find(processes.begin(), processes.end(), iter->kidId) != processes.end())
158  sc.push_back(*iter);
159  }
160  }
161  } else if (workersParam != NULL) {
162  IntParam* param = dynamic_cast<IntParam*>(workersParam.getRaw());
163  if (param != NULL && param->type == QueryParam::ptInt) {
164  const std::vector<int>& workers = param->value();
165  for (int i = 0; i < (int)aStrands.size(); ++i) {
166  if (std::find(workers.begin(), workers.end(), i + 1) != workers.end())
167  sc.push_back(aStrands[i]);
168  }
169  }
170  } else {
171  sc = aStrands;
172  }
173  }
174 
175  debugs(16, 4, HERE << "strands kid IDs = ");
176  for (Ipc::StrandCoords::const_iterator iter = sc.begin(); iter != sc.end(); ++iter) {
177  debugs(16, 4, HERE << iter->kidId);
178  }
179 
180  return sc;
181 }
182 
CBDATA_NAMESPACED_CLASS_INIT(Ipc, Inquirer)
cache manager request
Definition: Request.h:22
virtual void start()
called by AsyncStart; do not call directly
Definition: Inquirer.cc:68
static int sc[16]
Definition: smbdes.c:121
Comm::ConnectionPointer conn
HTTP client socket descriptor.
Definition: Inquirer.h:53
int i
Definition: membanger.c:49
MemBuf * pack() const
Definition: HttpReply.cc:101
virtual bool doneAll() const
whether positive goal has been reached
Definition: Inquirer.cc:137
Definition: Flag.h:16
int conn
the current server connection FD
Definition: Transport.cc:26
time_t squid_curtime
Definition: stub_time.cc:17
AsyncCall::Pointer closer
comm_close handler
Definition: Inquirer.h:56
Ipc::StrandCoords applyQueryParams(const Ipc::StrandCoords &aStrands, const QueryParams &aParams)
Definition: Inquirer.cc:143
AsyncCall * asyncCall(int aDebugSection, int aDebugLevel, const char *aName, const Dialer &aDialer)
Definition: AsyncCall.h:156
static Pointer Start(AsyncJob *job)
starts a freshly created job (i.e., makes the job asynchronous)
Definition: AsyncJob.cc:23
Comm::ConnectionPointer conn
Definition: CommCalls.h:85
bool IsConnOpen(const Comm::ConnectionPointer &conn)
Definition: Connection.cc:24
const Comm::ConnectionPointer & ImportFdIntoComm(const Comm::ConnectionPointer &conn, int socktype, int protocol, FdNoteId noteId)
import socket fd from another strand into our Comm state
Definition: UdsOp.cc:194
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:123
void setHeaders(Http::StatusCode status, const char *reason, const char *ctype, int64_t clen, time_t lmt, time_t expires)
Definition: HttpReply.cc:151
void noteCommClosed(const CommCloseCbParams &params)
called when the HTTP client or some external force closed our socket
Definition: Inquirer.cc:109
Inquirer(Action::Pointer anAction, const Request &aCause, const Ipc::StrandCoords &coords)
Definition: Inquirer.cc:33
std::vector< StrandCoord > StrandCoords
a collection of strand coordinates; the order, if any, is owner-dependent
Definition: StrandCoords.h:19
const Action & getAction() const
returns action object
Definition: Response.cc:72
static HttpRequest * FromUrl(const char *url, const MasterXaction::Pointer &, const HttpRequestMethod &method=Http::METHOD_GET)
Definition: HttpRequest.cc:523
std::ostream & HERE(std::ostream &s)
Definition: Debug.h:147
Action::Pointer aggrAction
Definition: Inquirer.h:51
#define Must(cond)
Definition: TextException.h:89
HttpHeader header
Definition: Message.h:74
bool SIGHDLR int STUB void int
Definition: stub_tools.cc:68
Comm::ConnectionPointer conn
HTTP client connection descriptor.
Definition: Request.h:37
void putStr(Http::HdrType id, const char *str)
Definition: HttpHeader.cc:1063
Comm::Flag flag
comm layer result status.
Definition: CommCalls.h:87
void removeCloseHandler()
Definition: Inquirer.cc:59
const std::vector< int > & value() const
Definition: IntParam.cc:47
AsyncCall::Pointer comm_add_close_handler(int fd, CLCB *handler, void *data)
Definition: comm.cc:961
virtual void sendResponse()
send response to client
Definition: Inquirer.cc:127
HttpReply * BuildHttpReply(void)
Definition: errorpage.cc:1104
virtual void start()
called by AsyncStart; do not call directly
Definition: Inquirer.cc:56
void noteWroteHeader(const CommIoCbParams &params)
called when we wrote the response header
Definition: Inquirer.cc:96
virtual bool doneAll() const
whether positive goal has been reached
Definition: Inquirer.cc:115
void comm_remove_close_handler(int fd, CLCB *handler, void *data)
Definition: comm.cc:988
bool hasAction() const
whether response contain action object
Definition: Response.cc:66
QueryParam::Pointer get(const String &name) const
returns query parameter by name
Definition: QueryParams.cc:19
virtual void cleanup()
closes our copy of the client HTTP connection socket
Definition: Inquirer.cc:50
C * getRaw() const
Definition: RefCount.h:74
#define NULL
Definition: types.h:166
void Write(const Comm::ConnectionPointer &conn, const char *buf, int size, AsyncCall::Pointer &callback, FREE *free_func)
Definition: Write.cc:35
virtual bool aggregate(Ipc::Response::Pointer aResponse)
perform aggregating of responses and returns true if need to continue
Definition: Inquirer.cc:118

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors