X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Ffilter_zoom.cpp;h=194fb8bcb70496726ab89f57d39b89394209e6ec;hb=6bd242f99846e3663a0551c6d18d6dedd7a94a38;hp=9ae28bb3a05d0517d88197f8ead0b392913075ef;hpb=ade7f72c658f8232e6afbc79b0094f7c6edd0fd7;p=metaproxy-moved-to-github.git diff --git a/src/filter_zoom.cpp b/src/filter_zoom.cpp index 9ae28bb..194fb8b 100644 --- a/src/filter_zoom.cpp +++ b/src/filter_zoom.cpp @@ -24,6 +24,8 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include #include #include +#include +#include #include "torus.hpp" #include @@ -85,16 +87,17 @@ namespace metaproxy_1 { friend class Impl; friend class Frontend; std::string zurl; - WRBUF m_apdu_wrbuf; + mp::wrbuf m_apdu_wrbuf; ZOOM_connection m_connection; ZOOM_resultset m_resultset; std::string m_frontend_database; SearchablePtr sptr; xsltStylesheetPtr xsp; - std::string content_session_id; + std::string cproxy_host; bool enable_cproxy; bool enable_explain; xmlDoc *explain_doc; + std::string m_proxy; public: Backend(); ~Backend(); @@ -169,7 +172,12 @@ namespace metaproxy_1 { ODR odr, BackendPtr b, Odr_oid *preferredRecordSyntax, const char *element_set_name); - + bool retry(mp::Package &package, + mp::odr &odr, + BackendPtr b, + int &error, char **addinfo, + int &proxy_step, int &same_retries, + int &proxy_retries); void log_diagnostic(mp::Package &package, int error, const char *addinfo); public: @@ -186,6 +194,10 @@ namespace metaproxy_1 { const char *path); private: void configure_local_records(const xmlNode * ptr, bool test_only); + bool check_proxy(const char *proxy); + + + FrontendPtr get_frontend(mp::Package &package); void release_frontend(mp::Package &package); SearchablePtr parse_torus_record(const xmlNode *ptr); @@ -201,6 +213,7 @@ namespace metaproxy_1 { std::string file_path; std::string content_proxy_server; std::string content_tmp_file; + std::string content_config_file; bool apdu_log; CCL_bibset bibset; std::string element_transform; @@ -210,6 +223,7 @@ namespace metaproxy_1 { xsltStylesheetPtr record_xsp; std::map s_map; std::string zoom_timeout; + int proxy_timeout; }; } } @@ -263,7 +277,6 @@ void yf::Zoom::process(mp::Package &package) const yf::Zoom::Backend::Backend() { - m_apdu_wrbuf = wrbuf_alloc(); m_connection = ZOOM_connection_create(0); ZOOM_connection_save_apdu_wrbuf(m_connection, m_apdu_wrbuf); m_resultset = 0; @@ -281,7 +294,6 @@ yf::Zoom::Backend::~Backend() xmlFreeDoc(explain_doc); ZOOM_connection_destroy(m_connection); ZOOM_resultset_destroy(m_resultset); - wrbuf_destroy(m_apdu_wrbuf); } @@ -302,6 +314,21 @@ void yf::Zoom::Backend::get_zoom_error(int *error, char **addinfo, *error = yaz_diag_srw_to_bib1(error0); else if (!strcmp(dset, "Bib-1")) *error = error0; + else if (!strcmp(dset, "ZOOM")) + { + *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR; + if (error0 == ZOOM_ERROR_INIT) + *error = YAZ_BIB1_INIT_AC_AUTHENTICATION_SYSTEM_ERROR; + else if (error0 == ZOOM_ERROR_DECODE) + { + if (zoom_addinfo) + { + if (strstr(zoom_addinfo, "Authentication") || + strstr(zoom_addinfo, "authentication")) + *error = YAZ_BIB1_INIT_AC_AUTHENTICATION_SYSTEM_ERROR; + } + } + } else *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR; @@ -322,7 +349,7 @@ void yf::Zoom::Backend::connect(std::string zurl, int *error, char **addinfo, ODR odr) { - ZOOM_connection_connect(m_connection, zurl.c_str(), 0); + ZOOM_connection_connect(m_connection, zurl.length() ? zurl.c_str() : 0, 0); get_zoom_error(error, addinfo, odr); } @@ -432,7 +459,7 @@ void yf::Zoom::Impl::release_frontend(mp::Package &package) yf::Zoom::Impl::Impl() : apdu_log(false), element_transform("pz2") , element_raw("raw"), - zoom_timeout("40") + zoom_timeout("40"), proxy_timeout(1) { bibset = ccl_qual_mk(); @@ -624,7 +651,6 @@ void yf::Zoom::Impl::configure(const xmlNode *ptr, bool test_only, std::string explain_xslt_fname; std::string record_xslt_fname; - content_tmp_file = "/tmp/cf.XXXXXX.p"; if (path && *path) { file_path = path; @@ -697,9 +723,19 @@ void yf::Zoom::Impl::configure(const xmlNode *ptr, bool test_only, for (attr = ptr->properties; attr; attr = attr->next) { if (!strcmp((const char *) attr->name, "server")) + { + yaz_log(YLOG_WARN, + "contentProxy's server attribute is deprecated"); + yaz_log(YLOG_LOG, + "Specify config_file instead. For example:"); + yaz_log(YLOG_LOG, + " content_file=\"/etc/cf-proxy/cproxy.cfg\""); content_proxy_server = mp::xml::get_text(attr->children); + } else if (!strcmp((const char *) attr->name, "tmp_file")) content_tmp_file = mp::xml::get_text(attr->children); + else if (!strcmp((const char *) attr->name, "config_file")) + content_config_file = mp::xml::get_text(attr->children); else throw mp::filter::FilterException( "Bad attribute " + std::string((const char *) @@ -726,6 +762,8 @@ void yf::Zoom::Impl::configure(const xmlNode *ptr, bool test_only, { if (!strcmp((const char *) attr->name, "timeout")) zoom_timeout = mp::xml::get_text(attr->children); + else if (!strcmp((const char *) attr->name, "proxy_timeout")) + proxy_timeout = mp::xml::get_int(attr->children, 1); else throw mp::filter::FilterException( "Bad attribute " + std::string((const char *) @@ -822,16 +860,98 @@ bool yf::Zoom::Frontend::create_content_session(mp::Package &package, { if (b->sptr->contentConnector.length()) { - char *fname = (char *) xmalloc(m_p->content_tmp_file.length() + 8); - strcpy(fname, m_p->content_tmp_file.c_str()); + std::string proxyhostname; + std::string tmp_file; + bool legacy_format = false; + + if (m_p->content_proxy_server.length()) + { + proxyhostname = m_p->content_proxy_server; + legacy_format = true; + } + + if (m_p->content_tmp_file.length()) + tmp_file = m_p->content_tmp_file; + + if (m_p->content_config_file.length()) + { + FILE *inf = fopen(m_p->content_config_file.c_str(), "r"); + if (inf) + { + char buf[1024]; + while (fgets(buf, sizeof(buf)-1, inf)) + { + char *cp; + cp = strchr(buf, '#'); + if (cp) + *cp = '\0'; + cp = strchr(buf, '\n'); + if (cp) + *cp = '\0'; + cp = strchr(buf, ':'); + if (cp) + { + char *cp1 = cp; + while (cp1 != buf && cp1[-1] == ' ') + cp1--; + *cp1 = '\0'; + cp++; + while (*cp == ' ') + cp++; + if (!strcmp(buf, "proxyhostname")) + proxyhostname = cp; + if (!strcmp(buf, "sessiondir") && *cp) + { + if (cp[strlen(cp)-1] == '/') + cp[strlen(cp)-1] = '\0'; + tmp_file = std::string(cp) + std::string("/cf.XXXXXX.p"); + } + } + } + fclose(inf); + } + else + { + package.log("zoom", YLOG_WARN|YLOG_ERRNO, + "unable to open content config %s", + m_p->content_config_file.c_str()); + *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR; + *addinfo = (char *) odr_malloc(odr, 60 + tmp_file.length()); + sprintf(*addinfo, "unable to open content config %s", + m_p->content_config_file.c_str()); + return false; + } + } + + if (proxyhostname.length() == 0) + { + package.log("zoom", YLOG_WARN, "no proxyhostname"); + return true; + } + if (tmp_file.length() == 0) + { + package.log("zoom", YLOG_WARN, "no tmp_file"); + return true; + } + + char *fname = xstrdup(tmp_file.c_str()); char *xx = strstr(fname, "XXXXXX"); if (!xx) { - xx = fname + strlen(fname); - strcat(fname, "XXXXXX"); + package.log("zoom", YLOG_WARN, "bad tmp_file %s", tmp_file.c_str()); + *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR; + *addinfo = (char *) odr_malloc(odr, 60 + tmp_file.length()); + sprintf(*addinfo, "bad format of content tmp_file: %s", + tmp_file.c_str()); + xfree(fname); + return false; } char tmp_char = xx[6]; sprintf(xx, "%06d", ((unsigned) rand()) % 1000000); + if (legacy_format) + b->cproxy_host = std::string(xx) + "." + proxyhostname; + else + b->cproxy_host = proxyhostname + "/" + xx; xx[6] = tmp_char; FILE *file = fopen(fname, "w"); @@ -844,8 +964,7 @@ bool yf::Zoom::Frontend::create_content_session(mp::Package &package, xfree(fname); return false; } - b->content_session_id.assign(xx, 6); - WRBUF w = wrbuf_alloc(); + mp::wrbuf w; wrbuf_puts(w, "#content_proxy\n"); wrbuf_printf(w, "connector: %s\n", b->sptr->contentConnector.c_str()); if (authentication.length()) @@ -855,11 +974,10 @@ bool yf::Zoom::Frontend::create_content_session(mp::Package &package, if (realm.length()) wrbuf_printf(w, "realm: %s\n", realm.c_str()); - fwrite(wrbuf_buf(w), 1, wrbuf_len(w), file); + fwrite(w.buf(), 1, w.len(), file); fclose(file); package.log("zoom", YLOG_LOG, "content file: %s", fname); xfree(fname); - wrbuf_destroy(w); } return true; } @@ -869,10 +987,16 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases( std::string &database, int *error, char **addinfo, mp::odr &odr, int *proxy_step) { + bool connection_reuse = false; + std::string proxy; + std::list::const_iterator map_it; if (m_backend && !m_backend->enable_explain && m_backend->m_frontend_database == database) - return m_backend; + { + connection_reuse = true; + proxy = m_backend->m_proxy; + } std::string input_args; std::string torus_db; @@ -887,7 +1011,6 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases( std::string authentication; std::string content_authentication; - std::string proxy; std::string content_proxy; std::string realm = m_p->default_realm; @@ -936,15 +1059,31 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases( char **dstr; int dnum = 0; nmem_strsplit(((ODR) odr)->mem, ",", value, &dstr, &dnum); - if (*proxy_step >= dnum) - *proxy_step = 0; + if (connection_reuse) + { + // find the step after our current proxy + int i; + for (i = 0; i < dnum; i++) + if (!strcmp(proxy.c_str(), dstr[i])) + break; + if (i >= dnum - 1) + *proxy_step = 0; + else + *proxy_step = i + 1; + } else { - proxy = dstr[*proxy_step]; - - (*proxy_step)++; - if (*proxy_step == dnum) + // step is known.. Guess our proxy from it + if (*proxy_step >= dnum) *proxy_step = 0; + else + { + proxy = dstr[*proxy_step]; + + (*proxy_step)++; + if (*proxy_step == dnum) + *proxy_step = 0; + } } } else if (!strcmp(name, "cproxysession")) @@ -969,7 +1108,16 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases( *addinfo = msg; return notfound; } + } + if (proxy.length()) + package.log("zoom", YLOG_LOG, "proxy: %s", proxy.c_str()); + + if (connection_reuse) + { + m_backend->connect("", error, addinfo, odr); + return m_backend; } + if (param_user) { authentication = std::string(param_user); @@ -1002,7 +1150,7 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases( if (!doc) { *error = YAZ_BIB1_DATABASE_DOES_NOT_EXIST; - *addinfo = odr_strdup(odr, database.c_str()); + *addinfo = odr_strdup(odr, torus_db.c_str()); BackendPtr b; return b; } @@ -1035,7 +1183,7 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases( if (!sptr) { *error = YAZ_BIB1_DATABASE_DOES_NOT_EXIST; - *addinfo = odr_strdup(odr, database.c_str()); + *addinfo = odr_strdup(odr, torus_db.c_str()); BackendPtr b; return b; } @@ -1112,6 +1260,7 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases( BackendPtr b(new Backend); + b->m_proxy = proxy; b->sptr = sptr; b->xsp = xsp; b->m_frontend_database = database; @@ -1191,9 +1340,6 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases( if (proxy.length()) b->set_option("proxy", proxy); } - if (proxy.length()) - package.log("zoom", YLOG_LOG, "proxy: %s", proxy.c_str()); - std::string url; if (sptr->sru.length()) { @@ -1407,28 +1553,21 @@ Z_Records *yf::Zoom::Frontend::get_records(Package &package, { // only return records if no error and at least one record const char *xsl_parms[3]; - char cproxy_host[1024]; - - if (b->enable_cproxy && b->content_session_id.length()) + mp::wrbuf cproxy_host; + + if (b->enable_cproxy && b->cproxy_host.length()) { - sprintf(cproxy_host, "%s.%s/", - b->content_session_id.c_str(), - m_p->content_proxy_server.c_str()); - - char *q_cproxy_host = (char *) - odr_malloc(odr, strlen(cproxy_host) + 3); - strcpy(q_cproxy_host, "\""); - strcat(q_cproxy_host, cproxy_host); - strcat(q_cproxy_host, "\""); + wrbuf_puts(cproxy_host, "\""); + wrbuf_puts(cproxy_host, b->cproxy_host.c_str()); + wrbuf_puts(cproxy_host, "/\""); xsl_parms[0] = "cproxyhost"; - xsl_parms[1] = q_cproxy_host; + xsl_parms[1] = wrbuf_cstr(cproxy_host); xsl_parms[2] = 0; } else { xsl_parms[0] = 0; - *cproxy_host = '\0'; } char *odr_database = odr_strdup(odr, @@ -1545,7 +1684,6 @@ Z_Records *yf::Zoom::Frontend::get_records(Package &package, xsltSaveResultToString(&xmlrec_buf, &rec_len, rec_res, xsp); rec_buf = (const char *) xmlrec_buf; - package.log("zoom", YLOG_LOG, "xslt successful"); package.log_write(rec_buf, rec_len); xmlFreeDoc(rec_res); @@ -1733,6 +1871,93 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::explain_search(mp::Package &package, } } +static bool wait_conn(COMSTACK cs, int secs) +{ + struct yaz_poll_fd pfd; + + yaz_poll_add(pfd.input_mask, yaz_poll_except); + if (cs->io_pending && CS_WANT_WRITE) + yaz_poll_add(pfd.input_mask, yaz_poll_write); + else if (cs->io_pending & CS_WANT_READ) + yaz_poll_add(pfd.input_mask, yaz_poll_read); + + pfd.fd = cs_fileno(cs); + pfd.client_data = 0; + + int ret = yaz_poll(&pfd, 1, secs, 0); + return ret > 0; +} + +bool yf::Zoom::Impl::check_proxy(const char *proxy) +{ + COMSTACK conn = 0; + const char *uri = "http://localhost/"; + void *add; + mp::odr odr; + bool outcome = false; + conn = cs_create_host_proxy(uri, 0, &add, proxy); + + if (!conn) + return false; + + Z_GDU *gdu = z_get_HTTP_Request_uri(odr, uri, 0, 1); + gdu->u.HTTP_Request->method = odr_strdup(odr, "GET"); + + if (z_GDU(odr, &gdu, 0, 0)) + { + int len; + char *buf = odr_getbuf(odr, &len, 0); + + int ret = cs_connect(conn, add); + if (ret > 0 || (ret == 0 && wait_conn(conn, 1))) + { + while (1) + { + ret = cs_put(conn, buf, len); + if (ret != 1) + break; + if (!wait_conn(conn, proxy_timeout)) + break; + } + } + } + cs_close(conn); + return outcome; +} + +bool yf::Zoom::Frontend::retry(mp::Package &package, + mp::odr &odr, + BackendPtr b, + int &error, char **addinfo, + int &proxy_step, int &same_retries, + int &proxy_retries) +{ + if (b && b->m_proxy.length() && !m_p->check_proxy(b->m_proxy.c_str())) + { + log_diagnostic(package, error, *addinfo); + package.log("zoom", YLOG_LOG, "proxy %s fails", b->m_proxy.c_str()); + m_backend.reset(); + if (proxy_step) // there is a failover + { + proxy_retries++; + package.log("zoom", YLOG_WARN, "search failed: trying next proxy"); + return true; + } + error = YAZ_BIB1_INIT_AC_AUTHENTICATION_SYSTEM_ERROR; + *addinfo = odr_strdup(odr, "proxy failure"); + } + else if (same_retries == 0 && proxy_retries == 0) + { + log_diagnostic(package, error, *addinfo); + same_retries++; + package.log("zoom", YLOG_WARN, "search failed: retry"); + m_backend.reset(); + proxy_step = 0; + return true; + } + return false; +} + void yf::Zoom::Frontend::handle_search(mp::Package &package) { Z_GDU *gdu = package.request().get(); @@ -1749,6 +1974,8 @@ void yf::Zoom::Frontend::handle_search(mp::Package &package) return; } int proxy_step = 0; + int same_retries = 0; + int proxy_retries = 0; next_proxy: @@ -1758,11 +1985,11 @@ next_proxy: BackendPtr b = get_backend_from_databases(package, db, &error, &addinfo, odr, &proxy_step); - if (error && proxy_step) + if (error) { - package.log("zoom", YLOG_WARN, - "create backend failed: trying next proxy"); - goto next_proxy; + if (retry(package, odr, b, error, &addinfo, proxy_step, + same_retries, proxy_retries)) + goto next_proxy; } if (error) { @@ -1788,20 +2015,18 @@ next_proxy: Odr_int hits = 0; Z_Query *query = sr->query; - WRBUF ccl_wrbuf = 0; - WRBUF pqf_wrbuf = 0; + mp::wrbuf ccl_wrbuf; + mp::wrbuf pqf_wrbuf; std::string sortkeys; if (query->which == Z_Query_type_1 || query->which == Z_Query_type_101) { // RPN - pqf_wrbuf = wrbuf_alloc(); yaz_rpnquery_to_wrbuf(pqf_wrbuf, query->u.type_1); } else if (query->which == Z_Query_type_2) { // CCL - ccl_wrbuf = wrbuf_alloc(); wrbuf_write(ccl_wrbuf, (const char *) query->u.type_2->buf, query->u.type_2->len); } @@ -1854,7 +2079,7 @@ next_proxy: return; } - WRBUF sru_sortkeys_wrbuf = wrbuf_alloc(); + mp::wrbuf sru_sortkeys_wrbuf; if (cql_sortby_to_sortkeys(cn, wrbuf_vp_puts, sru_sortkeys_wrbuf)) { error = YAZ_BIB1_ILLEGAL_SORT_RELATION; @@ -1863,16 +2088,12 @@ next_proxy: log_diagnostic(package, error, addinfo); apdu_res = odr.create_searchResponse(apdu_req, error, addinfo); package.response() = apdu_res; - wrbuf_destroy(sru_sortkeys_wrbuf); cql_parser_destroy(cp); return; } - WRBUF sort_spec_wrbuf = wrbuf_alloc(); + mp::wrbuf sort_spec_wrbuf; yaz_srw_sortkeys_to_sort_spec(wrbuf_cstr(sru_sortkeys_wrbuf), sort_spec_wrbuf); - wrbuf_destroy(sru_sortkeys_wrbuf); - - ccl_wrbuf = wrbuf_alloc(); wrbuf_puts(ccl_wrbuf, ccl_buf); yaz_tok_cfg_t tc = yaz_tok_cfg_create(); @@ -1907,8 +2128,6 @@ next_proxy: } } yaz_tok_parse_destroy(tp); - wrbuf_destroy(sort_spec_wrbuf); - cql_parser_destroy(cp); } else @@ -1921,19 +2140,18 @@ next_proxy: return; } - if (ccl_wrbuf) + if (ccl_wrbuf.len()) { // CCL to PQF - assert(pqf_wrbuf == 0); + assert(pqf_wrbuf.len() == 0); int cerror, cpos; struct ccl_rpn_node *cn; package.log("zoom", YLOG_LOG, "CCL: %s", wrbuf_cstr(ccl_wrbuf)); cn = ccl_find_str(b->sptr->ccl_bibset, wrbuf_cstr(ccl_wrbuf), &cerror, &cpos); - wrbuf_destroy(ccl_wrbuf); if (!cn) { - char *addinfo = odr_strdup(odr, ccl_err_msg(cerror)); + char *addinfo = odr_strdup_null(odr, ccl_err_msg(cerror)); error = YAZ_BIB1_MALFORMED_QUERY; switch (cerror) @@ -1956,13 +2174,12 @@ next_proxy: package.response() = apdu_res; return; } - pqf_wrbuf = wrbuf_alloc(); ccl_pquery(pqf_wrbuf, cn); package.log("zoom", YLOG_LOG, "RPN: %s", wrbuf_cstr(pqf_wrbuf)); ccl_rpn_delete(cn); } - assert(pqf_wrbuf); + assert(pqf_wrbuf.len()); ZOOM_query q = ZOOM_query_create(); ZOOM_query_sortby2(q, b->sptr->sortStrategy.c_str(), sortkeys.c_str()); @@ -1972,7 +2189,7 @@ next_proxy: int status = 0; Z_RPNQuery *zquery; zquery = p_query_rpn(odr, wrbuf_cstr(pqf_wrbuf)); - WRBUF wrb = wrbuf_alloc(); + mp::wrbuf wrb; if (!strcmp(b->get_option("sru"), "solr")) { @@ -1998,8 +2215,6 @@ next_proxy: } ZOOM_query_destroy(q); - wrbuf_destroy(wrb); - wrbuf_destroy(pqf_wrbuf); if (status) { error = YAZ_BIB1_MALFORMED_QUERY; @@ -2016,15 +2231,13 @@ next_proxy: package.log("zoom", YLOG_LOG, "search PQF: %s", wrbuf_cstr(pqf_wrbuf)); b->search(q, &hits, &error, &addinfo, odr); ZOOM_query_destroy(q); - wrbuf_destroy(pqf_wrbuf); } - if (error && proxy_step) + if (error) { - // reset below prevent reuse in get_backend_from_databases - m_backend.reset(); - package.log("zoom", YLOG_WARN, "search failed: trying next proxy"); - goto next_proxy; + if (retry(package, odr, b, error, &addinfo, proxy_step, + same_retries, proxy_retries)) + goto next_proxy; } const char *element_set_name = 0;