From da186c5b5656cd210b2a8a8d9c75d2af0c8c3f66 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Thu, 6 Oct 2011 12:01:36 +0200 Subject: [PATCH] Refactor: host entries no longer part of database Simplifies code and allows pz:url to be modified within a session for some target. --- src/client.c | 22 ++++------------------ src/client.h | 3 +-- src/connection.c | 37 ++++++++++++++++++++++++++++--------- src/database.c | 18 +----------------- src/database.h | 4 ++-- src/session.c | 4 +--- src/session.h | 7 +------ 7 files changed, 38 insertions(+), 57 deletions(-) diff --git a/src/client.c b/src/client.c index f2e1742..2cd3540 100644 --- a/src/client.c +++ b/src/client.c @@ -119,7 +119,6 @@ struct client { ZOOM_resultset resultset; YAZ_MUTEX mutex; int ref_count; - /* copy of database->url */ char *url; }; @@ -752,7 +751,7 @@ void client_start_search(struct client *cl) connection_continue(co); } -struct client *client_create(void) +struct client *client_create(const char *url) { struct client *cl = xmalloc(sizeof(*cl)); cl->maxrecs = 100; @@ -772,7 +771,8 @@ struct client *client_create(void) pazpar2_mutex_create(&cl->mutex, "client"); cl->preferred = 0; cl->ref_count = 1; - cl->url = 0; + assert(url); + cl->url = xstrdup(url); client_use(1); return cl; @@ -1126,7 +1126,6 @@ int client_is_active_preferred(struct client *cl) return 0; } - Odr_int client_get_hits(struct client *cl) { return cl->hits; @@ -1150,24 +1149,11 @@ int client_get_diagnostic(struct client *cl) void client_set_database(struct client *cl, struct session_database *db) { cl->database = db; - /* Copy the URL for safe logging even after session is gone */ - if (db) { - cl->url = xstrdup(db->database->url); - } -} - -struct host *client_get_host(struct client *cl) -{ - return client_get_database(cl)->database->host; } const char *client_get_url(struct client *cl) { - if (cl->url) - return cl->url; - else - /* This must not happen anymore, as the url is present until destruction of client */ - return "NOURL"; + return cl->url; } void client_set_maxrecs(struct client *cl, int v) diff --git a/src/client.h b/src/client.h index fcb4c9c..f037a04 100644 --- a/src/client.h +++ b/src/client.h @@ -67,7 +67,7 @@ int client_is_our_response(struct client *cl); void client_continue(struct client *cl); -struct client *client_create(void); +struct client *client_create(const char *url); int client_destroy(struct client *c); void client_set_connection(struct client *cl, struct connection *con); @@ -89,7 +89,6 @@ int client_get_num_records(struct client *cl); int client_get_diagnostic(struct client *cl); void client_set_diagnostic(struct client *cl, int diagnostic); void client_set_database(struct client *cl, struct session_database *db); -struct host *client_get_host(struct client *cl); const char *client_get_url(struct client *cl); void client_set_maxrecs(struct client *cl, int v); int client_get_maxrecs(struct client *cl); diff --git a/src/connection.c b/src/connection.c index 219380c..2df1d25 100644 --- a/src/connection.c +++ b/src/connection.c @@ -166,12 +166,12 @@ static void connection_destroy(struct connection *co) // Creates a new connection for client, associated with the host of // client's database static struct connection *connection_create(struct client *cl, + struct host *host, int operation_timeout, int session_timeout, iochan_man_t iochan_man) { struct connection *co; - struct host *host = client_get_host(cl); co = xmalloc(sizeof(*co)); co->host = host; @@ -434,20 +434,37 @@ int client_prep_connection(struct client *cl, const struct timeval *abstime) { struct connection *co; - struct host *host = client_get_host(cl); struct session_database *sdb = client_get_database(cl); const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY); + const char *url = session_setting_oneval(sdb, PZ_URL); + struct host *host = 0; if (zproxy && zproxy[0] == '\0') zproxy = 0; - if (!host) - return 0; + if (!url || !*url) + url = sdb->database->url; - co = client_get_connection(cl); + host = find_host(client_get_session(cl)->service->server->database_hosts, + url); + + yaz_log(YLOG_DEBUG, "client_prep_connection: target=%s url=%s", + client_get_url(cl), url); - yaz_log(YLOG_DEBUG, "Client prep %s", client_get_url(cl)); + co = client_get_connection(cl); + if (co) + { + assert(co->host); + if (co->host == host) + ; /* reusing connection. It's ours! */ + else + { + client_incref(cl); + connection_release(co); + co = 0; + } + } if (!co) { int max_connections = 0; @@ -456,7 +473,7 @@ int client_prep_connection(struct client *cl, PZ_MAX_CONNECTIONS); if (v && *v) max_connections = atoi(v); - + v = session_setting_oneval(client_get_database(cl), PZ_REUSE_CONNECTIONS); if (v && *v) @@ -470,7 +487,8 @@ int client_prep_connection(struct client *cl, int num_connections = 0; for (co = host->connections; co; co = co->next) num_connections++; - if (reuse_connections) { + if (reuse_connections) + { for (co = host->connections; co; co = co->next) { if (connection_is_idle(co) && @@ -526,9 +544,10 @@ int client_prep_connection(struct client *cl, else { yaz_mutex_leave(host->mutex); - co = connection_create(cl, operation_timeout, session_timeout, + co = connection_create(cl, host, operation_timeout, session_timeout, iochan_man); } + assert(co->host); } if (co && co->link) diff --git a/src/database.c b/src/database.c index ef3ae25..2a871a4 100644 --- a/src/database.c +++ b/src/database.c @@ -80,8 +80,7 @@ static struct host *create_host(const char *hostport) return host; } -static struct host *find_host(database_hosts_t hosts, - const char *hostport) +struct host *find_host(database_hosts_t hosts, const char *hostport) { struct host *p; yaz_mutex_enter(hosts->mutex); @@ -101,19 +100,6 @@ static struct host *find_host(database_hosts_t hosts, return p; } -int resolve_database(struct conf_service *service, struct database *db, - const char *hostport) -{ - if (db->host == 0) - { - struct host *host; - if (!(host = find_host(service->server->database_hosts, hostport))) - return -1; - db->host = host; - } - return 0; -} - struct database *new_database(const char *id, NMEM nmem) { struct database *db; @@ -123,8 +109,6 @@ struct database *new_database(const char *id, NMEM nmem) memset(db, 0, sizeof(*db)); db->url = nmem_strdup(nmem, id); - db->errors = 0; - db->host = 0; db->num_settings = PZ_MAX_EOF; db->settings = nmem_malloc(nmem, sizeof(struct settings*) * diff --git a/src/database.h b/src/database.h index 3d9bfbf..febe104 100644 --- a/src/database.h +++ b/src/database.h @@ -30,11 +30,11 @@ int session_grep_databases(struct session *se, const char *filter, int predef_grep_databases(void *context, struct conf_service *service, void (*fun)(void *context, struct database *db)); int match_zurl(const char *zurl, const char *pattern); -int resolve_database(struct conf_service *service, struct database *db, - const char *hostport); struct database *new_database(const char *id, NMEM nmem); database_hosts_t database_hosts_create(void); void database_hosts_destroy(database_hosts_t *); +struct host *find_host(database_hosts_t hosts, const char *hostport); + #endif diff --git a/src/session.c b/src/session.c index 3567ab6..751117e 100644 --- a/src/session.c +++ b/src/session.c @@ -524,15 +524,13 @@ void session_alert_watch(struct session *s, int what) static void select_targets_callback(struct session *se, struct session_database *db) { - struct client *cl = client_create(); + struct client *cl = client_create(db->database->url); struct client_list *l; const char *url = session_setting_oneval(db, PZ_URL); if (!url || !*url) url = db->database->url; - resolve_database(se->service, db->database, url); - client_set_database(cl, db); client_set_session(cl, se); diff --git a/src/session.h b/src/session.h index 7647e46..e6f7ca3 100644 --- a/src/session.h +++ b/src/session.h @@ -52,12 +52,9 @@ enum pazpar2_error_code { PAZPAR2_LAST_ERROR }; -struct host; -// Represents a (virtual) database on a host +// Represents a database struct database { - struct host *host; char *url; - int errors; int num_settings; struct setting **settings; struct database *next; @@ -172,8 +169,6 @@ int session_is_preferred_clients_ready(struct session *s); void session_apply_setting(struct session *se, char *dbname, char *setting, char *value); const char *session_setting_oneval(struct session_database *db, int offset); -int host_getaddrinfo(struct host *host, iochan_man_t iochan_man); - int ingest_record(struct client *cl, const char *rec, int record_no, NMEM nmem); void session_alert_watch(struct session *s, int what); void add_facet(struct session *s, const char *type, const char *value, int count); -- 1.7.10.4