/* This file is part of Metaproxy.
- Copyright (C) 2005-2008 Index Data
+ Copyright (C) 2005-2011 Index Data
Metaproxy is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
#include "config.hpp"
-#include "filter.hpp"
-#include "package.hpp"
+#include <metaproxy/filter.hpp>
+#include <metaproxy/package.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/format.hpp>
-#include "util.hpp"
+#include <metaproxy/util.hpp>
#include "filter_session_shared.hpp"
#include <yaz/log.h>
const yazpp_1::Yaz_Z_Query &query);
bool search(
Package &frontend_package,
+ Package &search_package,
const Z_APDU *apdu_req,
const BackendInstancePtr bp,
- bool &fatal_error);
+ Z_Records **z_records);
};
// backend connection instance
class SessionShared::BackendInstance {
private:
void init(Package &package, const Z_GDU *gdu,
FrontendPtr frontend);
+ void start();
boost::mutex m_mutex;
boost::condition m_cond_session_ready;
std::map<mp::Session, FrontendPtr> m_clients;
int m_resultset_ttl;
int m_resultset_max;
int m_session_ttl;
+ bool m_optimize_search;
};
}
}
m_named_result_sets = false;
Z_GDU *gdu = init_package.response().get();
- if (!init_package.session().is_closed()
- && gdu && gdu->which == Z_GDU_Z3950
- && gdu->u.z3950->which == Z_APDU_initResponse)
+ if (init_package.session().is_closed())
+ {
+ /* already closed. We don't know why */
+ return null;
+ }
+ else if (gdu && gdu->which == Z_GDU_Z3950
+ && gdu->u.z3950->which == Z_APDU_initResponse
+ && *gdu->u.z3950->u.initResponse->result)
{
+ /* successful init response */
Z_InitResponse *res = gdu->u.z3950->u.initResponse;
- if (!*res->result)
- return null;
m_init_response = gdu->u.z3950;
if (ODR_MASK_GET(res->options, Z_Options_namedResultSets))
{
}
else
{
- // did not receive an init response or closed
+ /* not init or init rejected */
+ init_package.copy_filter(frontend_package);
+ init_package.session().close();
+ init_package.move();
return null;
}
bp->m_in_use = true;
timestamp();
}
+static int get_diagnostic(Z_DefaultDiagFormat *r)
+{
+ return *r->condition;
+}
+
bool yf::SessionShared::BackendSet::search(
mp::Package &frontend_package,
+ mp::Package &search_package,
const Z_APDU *frontend_apdu,
const BackendInstancePtr bp,
- bool & fatal_error)
+ Z_Records **z_records)
{
- Package search_package(bp->m_session, frontend_package.origin());
-
- search_package.copy_filter(frontend_package);
-
mp::odr odr;
Z_APDU *apdu_req = zget_APDU(odr, Z_APDU_searchRequest);
Z_SearchRequest *req = apdu_req->u.searchRequest;
search_package.request() = apdu_req;
search_package.move();
- fatal_error = false; // assume backend session is good
- Z_Records *z_records_diag = 0;
Z_GDU *gdu = search_package.response().get();
if (!search_package.session().is_closed()
&& gdu && gdu->which == Z_GDU_Z3950
&& gdu->u.z3950->which == Z_APDU_searchResponse)
{
Z_SearchResponse *b_resp = gdu->u.z3950->u.searchResponse;
- if (b_resp->records)
- {
- if (b_resp->records->which == Z_Records_NSD
- || b_resp->records->which == Z_Records_multipleNSD)
- z_records_diag = b_resp->records;
- }
- if (z_records_diag)
- {
- // there could be diagnostics that are so bad.. that
- // we simply mark the error as fatal.. For now we assume
- // we can resume
- if (frontend_apdu->which == Z_APDU_searchRequest)
- {
- Z_APDU *f_apdu = odr.create_searchResponse(frontend_apdu,
- 0, 0);
- Z_SearchResponse *f_resp = f_apdu->u.searchResponse;
- *f_resp->searchStatus = *b_resp->searchStatus;
- f_resp->records = z_records_diag;
- frontend_package.response() = f_apdu;
- return false;
- }
- if (frontend_apdu->which == Z_APDU_presentRequest)
- {
- Z_APDU *f_apdu = odr.create_presentResponse(frontend_apdu,
- 0, 0);
- Z_PresentResponse *f_resp = f_apdu->u.presentResponse;
- f_resp->records = z_records_diag;
- frontend_package.response() = f_apdu;
- return false;
- }
- }
+ *z_records = b_resp->records;
m_result_set_size = *b_resp->resultCount;
return true;
}
f_apdu = odr.create_close(
frontend_apdu, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, 0);
frontend_package.response() = f_apdu;
- fatal_error = true; // weired response.. bad backend
return false;
}
BackendClassPtr bc = m_backend_class;
{
boost::mutex::scoped_lock lock(bc->m_mutex_backend_class);
-
- // look at each backend and see if we have a similar search
- BackendInstanceList::const_iterator it = bc->m_backend_list.begin();
- for (; it != bc->m_backend_list.end(); it++)
+ if (m_p->m_optimize_search)
{
- if (!(*it)->m_in_use)
+ // look at each backend and see if we have a similar search
+ BackendInstanceList::const_iterator it = bc->m_backend_list.begin();
+ for (; it != bc->m_backend_list.end(); it++)
{
- BackendSetList::const_iterator set_it = (*it)->m_sets.begin();
- for (; set_it != (*it)->m_sets.end(); set_it++)
+ if (!(*it)->m_in_use)
{
- if ((*set_it)->m_databases == databases
- && query.match(&(*set_it)->m_query))
+ BackendSetList::const_iterator set_it = (*it)->m_sets.begin();
+ for (; set_it != (*it)->m_sets.end(); set_it++)
{
- found_set = *set_it;
- found_backend = *it;
- bc->use_backend(found_backend);
- found_set->timestamp();
- // found matching set. No need to search again
- return;
+ if ((*set_it)->m_databases == databases
+ && query.match(&(*set_it)->m_query))
+ {
+ found_set = *set_it;
+ found_backend = *it;
+ bc->use_backend(found_backend);
+ found_set->timestamp();
+ // found matching set. No need to search again
+ return;
+ }
}
}
}
// we must search ...
BackendSetPtr new_set(new BackendSet(result_set_id,
databases, query));
- bool fatal_error = false;
- if (!new_set->search(package, apdu_req, found_backend, fatal_error))
+ Z_Records *z_records = 0;
+
+ Package search_package(found_backend->m_session, package.origin());
+ search_package.copy_filter(package);
+
+ if (!new_set->search(package, search_package,
+ apdu_req, found_backend, &z_records))
{
- if (fatal_error)
+ bc->remove_backend(found_backend);
+ return; // search error
+ }
+
+ if (z_records)
+ {
+ int condition = 0;
+ if (z_records->which == Z_Records_NSD)
+ {
+ condition =
+ get_diagnostic(z_records->u.nonSurrogateDiagnostic);
+ }
+ else if (z_records->which == Z_Records_multipleNSD)
+ {
+ if (z_records->u.multipleNonSurDiagnostics->num_diagRecs >= 1
+ &&
+
+ z_records->u.multipleNonSurDiagnostics->diagRecs[0]->which ==
+ Z_DiagRec_defaultFormat)
+ {
+ condition = get_diagnostic(
+ z_records->u.multipleNonSurDiagnostics->diagRecs[0]->u.defaultFormat);
+
+ }
+ }
+ if (!session_restarted &&
+ condition == YAZ_BIB1_TEMPORARY_SYSTEM_ERROR)
+ {
bc->remove_backend(found_backend);
- else
+ session_restarted = true;
+ found_backend.reset();
+ goto restart;
+
+ }
+
+ if (condition)
+ {
+ mp::odr odr;
+ if (apdu_req->which == Z_APDU_searchRequest)
+ {
+ Z_APDU *f_apdu = odr.create_searchResponse(apdu_req,
+ 0, 0);
+ Z_SearchResponse *f_resp = f_apdu->u.searchResponse;
+ *f_resp->searchStatus = Z_SearchResponse_none;
+ f_resp->records = z_records;
+ package.response() = f_apdu;
+ }
+ if (apdu_req->which == Z_APDU_presentRequest)
+ {
+ Z_APDU *f_apdu = odr.create_presentResponse(apdu_req,
+ 0, 0);
+ Z_PresentResponse *f_resp = f_apdu->u.presentResponse;
+ f_resp->records = z_records;
+ package.response() = f_apdu;
+ }
bc->release_backend(found_backend);
- return; // search error
+ return; // search error
+ }
}
if (!session_restarted && new_set->m_result_set_size < 0)
{
Z_APDU *f_apdu_res = odr.create_presentResponse(apdu_req, 0, 0);
Z_PresentResponse *f_resp = f_apdu_res->u.presentResponse;
- yaz_log(YLOG_LOG, "Found %d+%d records in cache %p",
+ yaz_log(YLOG_LOG, "Found " ODR_INT_PRINTF "+" ODR_INT_PRINTF
+ " records in cache %p",
*req->resultSetStartPoint,
*req->numberOfRecordsRequested,
&found_set->m_record_cache);
if (b_resp->records && b_resp->records->which == Z_Records_DBOSD)
{
- yaz_log(YLOG_LOG, "Adding %d+%d records to cache %p",
+ yaz_log(YLOG_LOG, "Adding " ODR_INT_PRINTF "+" ODR_INT_PRINTF
+ " records to cache %p",
*req->resultSetStartPoint,
*f_resp->numberOfRecordsReturned,
&found_set->m_record_cache);
m_resultset_ttl = 30;
m_resultset_max = 10;
m_session_ttl = 90;
+ m_optimize_search = true;
+}
+
+void yf::SessionShared::Rep::start()
+{
yf::SessionShared::Worker w(this);
m_thrds.add_thread(new boost::thread(w));
}
yf::SessionShared::~SessionShared() {
}
+void yf::SessionShared::start() const
+{
+ m_p->start();
+}
yf::SessionShared::Frontend::Frontend(Rep *rep) : m_is_virtual(false), m_p(rep)
{
m_p->m_resultset_max =
mp::xml::get_int(attr->children, 10);
}
+ else if (!strcmp((const char *) attr->name, "optimizesearch"))
+ {
+ m_p->m_optimize_search =
+ mp::xml::get_bool(attr->children, true);
+ }
else
throw mp::filter::FilterException(
"Bad attribute " + std::string((const char *)