zoom: properly handle proxy failover for re-used backends
[metaproxy-moved-to-github.git] / src / filter_zoom.cpp
index bf6f4ab..0c02b58 100644 (file)
@@ -24,6 +24,8 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #include <metaproxy/package.hpp>
 #include <metaproxy/util.hpp>
 #include <metaproxy/xmlutil.hpp>
+#include <yaz/comstack.h>
+#include <yaz/poll.h>
 #include "torus.hpp"
 
 #include <libxslt/xsltutils.h>
@@ -91,10 +93,11 @@ namespace metaproxy_1 {
             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();
@@ -186,6 +189,7 @@ 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 +205,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 +215,7 @@ namespace metaproxy_1 {
             xsltStylesheetPtr record_xsp;
             std::map<std::string,SearchablePtr> s_map;
             std::string zoom_timeout;
+            int proxy_timeout;
         };
     }
 }
@@ -300,6 +306,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;
         
@@ -430,7 +451,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();
 
@@ -622,7 +643,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;
@@ -695,9 +715,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 *)
@@ -724,6 +754,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 *)
@@ -820,16 +852,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");
@@ -842,7 +956,6 @@ bool yf::Zoom::Frontend::create_content_session(mp::Package &package,
             xfree(fname);
             return false;
         }
-        b->content_session_id.assign(xx, 6);
         mp::wrbuf w;
         wrbuf_puts(w, "#content_proxy\n");
         wrbuf_printf(w, "connector: %s\n", b->sptr->contentConnector.c_str());
@@ -866,12 +979,15 @@ 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<BackendPtr>::const_iterator map_it;
     if (m_backend && !m_backend->enable_explain && 
         m_backend->m_frontend_database == database)
     {
-        m_backend->connect("", error, addinfo, odr);
-        return m_backend;
+        connection_reuse = true;
+        proxy = m_backend->m_proxy;
     }
 
     std::string input_args;
@@ -887,7 +1003,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 +1051,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 +1100,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);
@@ -1112,6 +1252,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 +1332,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())
     {
@@ -1409,11 +1547,12 @@ Z_Records *yf::Zoom::Frontend::get_records(Package &package,
         const char *xsl_parms[3];
         mp::wrbuf cproxy_host;
         
-        if (b->enable_cproxy && b->content_session_id.length())
+        if (b->enable_cproxy && b->cproxy_host.length())
         {
-            wrbuf_printf(cproxy_host, "\"%s.%s/\"",
-                         b->content_session_id.c_str(),
-                         m_p->content_proxy_server.c_str());
+            wrbuf_puts(cproxy_host, "\"");
+            wrbuf_puts(cproxy_host, b->cproxy_host.c_str());
+            wrbuf_puts(cproxy_host, "/\"");
+
             xsl_parms[0] = "cproxyhost";
             xsl_parms[1] = wrbuf_cstr(cproxy_host);
             xsl_parms[2] = 0;
@@ -1724,6 +1863,60 @@ 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;
+}
+
 void yf::Zoom::Frontend::handle_search(mp::Package &package)
 {
     Z_GDU *gdu = package.request().get();
@@ -1740,6 +1933,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:
 
@@ -1749,11 +1944,31 @@ 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 (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");
+                goto next_proxy;
+            }
+            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;
+            goto next_proxy;
+        }
     }
     if (error)
     {
@@ -1997,12 +2212,31 @@ next_proxy:
         ZOOM_query_destroy(q);
     }
 
-    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 (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");
+                goto next_proxy;
+            }
+            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;
+            goto next_proxy;
+        }
     }
 
     const char *element_set_name = 0;