[asterisk-commits] tilghman: branch tilghman/realtime_failover r140694 - in /team/tilghman/realt...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Sep 2 17:59:24 CDT 2008


Author: tilghman
Date: Tue Sep  2 17:59:24 2008
New Revision: 140694

URL: http://svn.digium.com/view/asterisk?view=rev&rev=140694
Log:
Completed coding new feature

Modified:
    team/tilghman/realtime_failover/configs/extconfig.conf.sample
    team/tilghman/realtime_failover/main/config.c
    team/tilghman/realtime_failover/res/res_odbc.c

Modified: team/tilghman/realtime_failover/configs/extconfig.conf.sample
URL: http://svn.digium.com/view/asterisk/team/tilghman/realtime_failover/configs/extconfig.conf.sample?view=diff&rev=140694&r1=140693&r2=140694
==============================================================================
--- team/tilghman/realtime_failover/configs/extconfig.conf.sample (original)
+++ team/tilghman/realtime_failover/configs/extconfig.conf.sample Tue Sep  2 17:59:24 2008
@@ -9,7 +9,7 @@
 ;
 ; Static configuration files: 
 ;
-; file.conf => driver,database[,table]
+; file.conf => driver,database[,table[,priority]]
 ;
 ; maps a particular configuration file to the given
 ; database driver, database and table (or uses the
@@ -40,14 +40,26 @@
 ; database and table (or uses the name of
 ; the family if the table is not specified
 ;
-;example => odbc,asterisk,alttable
+;example => odbc,asterisk,alttable,1
+;example => mysql,asterisk,alttable,2
 ;example2 => ldap,"dc=oxymium,dc=net",example2
+;
+; Additionally, priorities are now supported for use as failover methods
+; for retrieving realtime data.  If one connection fails to retrieve any
+; information, the next sequential priority will be tried next.  This
+; especially works well with ODBC connections, since res_odbc now caches
+; when connection failures occur and prevents immediately retrying those
+; connections until after a specified timeout.  Note:  priorities must
+; start at 1 and be sequential (i.e. if you have only priorities 1, 2,
+; and 4, then 4 will be ignored, because there is no 3).
 ;
 ; "odbc" is shown in the examples below, but is not the only valid realtime
 ; engine.  There is:
 ;    odbc ... res_config_odbc
 ;    sqlite ... res_config_sqlite
 ;    pgsql ... res_config_pgsql
+;    curl ... res_config_curl
+;    ldap ... res_config_ldap
 ;
 ;iaxusers => odbc,asterisk
 ;iaxpeers => odbc,asterisk

Modified: team/tilghman/realtime_failover/main/config.c
URL: http://svn.digium.com/view/asterisk/team/tilghman/realtime_failover/main/config.c?view=diff&rev=140694&r1=140693&r2=140694
==============================================================================
--- team/tilghman/realtime_failover/main/config.c (original)
+++ team/tilghman/realtime_failover/main/config.c Tue Sep  2 17:59:24 2008
@@ -175,6 +175,7 @@
 
 static struct ast_config_map {
 	struct ast_config_map *next;
+	int priority;
 	char *name;
 	char *driver;
 	char *database;
@@ -1820,7 +1821,7 @@
 	ast_mutex_unlock(&config_lock);
 }
 
-static int append_mapping(const char *name, const char *driver, const char *database, const char *table)
+static int append_mapping(const char *name, const char *driver, const char *database, const char *table, int priority)
 {
 	struct ast_config_map *map;
 	int length;
@@ -1845,6 +1846,7 @@
 		map->table = map->database + strlen(map->database) + 1;
 		strcpy(map->table, table);
 	}
+	map->priority = priority;
 	map->next = config_maps;
 
 	ast_verb(2, "Binding %s to %s/%s/%s\n", map->name, map->driver, map->database, map->table ? map->table : map->name);
@@ -1857,8 +1859,9 @@
 {
 	struct ast_config *config, *configtmp;
 	struct ast_variable *v;
-	char *driver, *table, *database, *stringp, *tmp;
+	char *driver, *table, *database, *textpri, *stringp, *tmp;
 	struct ast_flags flags = { 0 };
+	int pri;
 
 	clear_config_maps();
 
@@ -1890,6 +1893,10 @@
 		}
 
 		table = strsep(&stringp, ",");
+		textpri = strsep(&stringp, ",");
+		if (!textpri || !(pri = atoi(textpri))) {
+			pri = 1;
+		}
 
 		if (!strcmp(v->name, extconfig_conf)) {
 			ast_log(LOG_WARNING, "Cannot bind '%s'!\n", extconfig_conf);
@@ -1910,14 +1917,14 @@
 			continue;
 		if (!strcasecmp(v->name, "sipfriends")) {
 			ast_log(LOG_WARNING, "The 'sipfriends' table is obsolete, update your config to use sipusers and sippeers, though they can point to the same table.\n");
-			append_mapping("sipusers", driver, database, table ? table : "sipfriends");
-			append_mapping("sippeers", driver, database, table ? table : "sipfriends");
+			append_mapping("sipusers", driver, database, table ? table : "sipfriends", pri);
+			append_mapping("sippeers", driver, database, table ? table : "sipfriends", pri);
 		} else if (!strcasecmp(v->name, "iaxfriends")) {
 			ast_log(LOG_WARNING, "The 'iaxfriends' table is obsolete, update your config to use iaxusers and iaxpeers, though they can point to the same table.\n");
-			append_mapping("iaxusers", driver, database, table ? table : "iaxfriends");
-			append_mapping("iaxpeers", driver, database, table ? table : "iaxfriends");
+			append_mapping("iaxusers", driver, database, table ? table : "iaxfriends", pri);
+			append_mapping("iaxpeers", driver, database, table ? table : "iaxfriends", pri);
 		} else 
-			append_mapping(v->name, driver, database, table);
+			append_mapping(v->name, driver, database, table, pri);
 	}
 		
 	ast_config_destroy(config);
@@ -1966,7 +1973,7 @@
 }
 
 /*! \brief Find realtime engine for realtime family */
-static struct ast_config_engine *find_engine(const char *family, char *database, int dbsiz, char *table, int tabsiz) 
+static struct ast_config_engine *find_engine(const char *family, int priority, char *database, int dbsiz, char *table, int tabsiz) 
 {
 	struct ast_config_engine *eng, *ret = NULL;
 	struct ast_config_map *map;
@@ -1974,7 +1981,7 @@
 	ast_mutex_lock(&config_lock);
 
 	for (map = config_maps; map; map = map->next) {
-		if (!strcasecmp(family, map->name)) {
+		if (!strcasecmp(family, map->name) && (priority == map->priority)) {
 			if (database)
 				ast_copy_string(database, map->database, dbsiz);
 			if (table)
@@ -2011,6 +2018,8 @@
 	char table[256];
 	struct ast_config_engine *loader = &text_file_engine;
 	struct ast_config *result; 
+	struct ast_config_engine *eng;
+	int final = 0, i;
 
 	/* The config file itself bumps include_level by 1 */
 	if (cfg->max_include_level > 0 && cfg->include_level == cfg->max_include_level + 1) {
@@ -2020,27 +2029,29 @@
 
 	cfg->include_level++;
 
-	if (strcmp(filename, extconfig_conf) && strcmp(filename, "asterisk.conf") && config_engine_list) {
-		struct ast_config_engine *eng;
-
-		eng = find_engine(filename, db, sizeof(db), table, sizeof(table));
-
-
-		if (eng && eng->load_func) {
-			loader = eng;
+	for (i = 1; !final; i++) {
+		if (strcmp(filename, extconfig_conf) && strcmp(filename, "asterisk.conf") && config_engine_list) {
+
+			eng = find_engine(filename, i, db, sizeof(db), table, sizeof(table));
+
+			if (eng && eng->load_func) {
+				loader = eng;
+			} else {
+				return ast_config_internal_load("global", cfg, flags, suggested_include_file, who_asked);
+			}
 		} else {
-			eng = find_engine("global", db, sizeof(db), table, sizeof(table));
-			if (eng && eng->load_func)
-				loader = eng;
-		}
-	}
-
-	result = loader->load_func(db, table, filename, cfg, flags, suggested_include_file, who_asked);
-
-	if (result && result != CONFIG_STATUS_FILEUNCHANGED)
-		result->include_level--;
-	else
-		cfg->include_level--;
+			final = 1;
+		}
+
+		result = loader->load_func(db, table, filename, cfg, flags, suggested_include_file, who_asked);
+
+		if (result && result != CONFIG_STATUS_FILEUNCHANGED) {
+			final = 1;
+			result->include_level--;
+		} else {
+			cfg->include_level--;
+		}
+	}
 
 	return result;
 }
@@ -2067,10 +2078,17 @@
 	char db[256]="";
 	char table[256]="";
 	struct ast_variable *res=NULL;
-
-	eng = find_engine(family, db, sizeof(db), table, sizeof(table));
-	if (eng && eng->realtime_func) 
-		res = eng->realtime_func(db, table, ap);
+	int i;
+
+	for (i = 1; ; i++) {
+		if ((eng = find_engine(family, i, db, sizeof(db), table, sizeof(table)))) {
+			if (eng->realtime_func && (res = eng->realtime_func(db, table, ap))) {
+				return res;
+			}
+		} else {
+			return NULL;
+		}
+	}
 
 	return res;
 }
@@ -2121,7 +2139,7 @@
 {
 	struct ast_config_engine *eng;
 
-	eng = find_engine(family, NULL, 0, NULL, 0);
+	eng = find_engine(family, 1, NULL, 0, NULL, 0);
 	if (eng)
 		return 1;
 	return 0;
@@ -2139,12 +2157,18 @@
 	char db[256] = "";
 	char table[256] = "";
 	va_list ap;
-	int res = -1;
+	int res = -1, i;
 
 	va_start(ap, family);
-	eng = find_engine(family, db, sizeof(db), table, sizeof(table));
-	if (eng && eng->require_func) {
-		res = eng->require_func(db, table, ap);
+	for (i = 1; ; i++) {
+		if ((eng = find_engine(family, i, db, sizeof(db), table, sizeof(table)))) {
+			/* If the require succeeds, it returns 0. */
+			if (eng->require_func && !(res = eng->require_func(db, table, ap))) {
+				break;
+			}
+		} else {
+			break;
+		}
 	}
 	va_end(ap);
 
@@ -2156,11 +2180,17 @@
 	struct ast_config_engine *eng;
 	char db[256] = "";
 	char table[256] = "";
-	int res = -1;
-
-	eng = find_engine(family, db, sizeof(db), table, sizeof(table));
-	if (eng && eng->unload_func) {
-		res = eng->unload_func(db, table);
+	int res = -1, i;
+
+	for (i = 1; ; i++) {
+		if ((eng = find_engine(family, i, db, sizeof(db), table, sizeof(table)))) {
+			if (eng->unload_func) {
+				/* Do this for ALL engines */
+				res = eng->unload_func(db, table);
+			}
+		} else {
+			break;
+		}
 	}
 	return res;
 }
@@ -2172,11 +2202,18 @@
 	char table[256]="";
 	struct ast_config *res=NULL;
 	va_list ap;
+	int i;
 
 	va_start(ap, family);
-	eng = find_engine(family, db, sizeof(db), table, sizeof(table));
-	if (eng && eng->realtime_multi_func) 
-		res = eng->realtime_multi_func(db, table, ap);
+	for (i = 1; ; i++) {
+		if ((eng = find_engine(family, i, db, sizeof(db), table, sizeof(table)))) {
+			if (eng->realtime_multi_func && (res = eng->realtime_multi_func(db, table, ap))) {
+				break;
+			}
+		} else {
+			break;
+		}
+	}
 	va_end(ap);
 
 	return res;
@@ -2185,15 +2222,22 @@
 int ast_update_realtime(const char *family, const char *keyfield, const char *lookup, ...)
 {
 	struct ast_config_engine *eng;
-	int res = -1;
+	int res = -1, i;
 	char db[256]="";
 	char table[256]="";
 	va_list ap;
 
 	va_start(ap, lookup);
-	eng = find_engine(family, db, sizeof(db), table, sizeof(table));
-	if (eng && eng->update_func) 
-		res = eng->update_func(db, table, keyfield, lookup, ap);
+	for (i = 1; ; i++) {
+		if ((eng = find_engine(family, i, db, sizeof(db), table, sizeof(table)))) {
+			/* If the update succeeds, it returns 0. */
+			if (eng->update_func && !(res = eng->update_func(db, table, keyfield, lookup, ap))) {
+				break;
+			}
+		} else {
+			break;
+		}
+	}
 	va_end(ap);
 
 	return res;
@@ -2202,15 +2246,22 @@
 int ast_store_realtime(const char *family, ...)
 {
 	struct ast_config_engine *eng;
-	int res = -1;
+	int res = -1, i;
 	char db[256]="";
 	char table[256]="";
 	va_list ap;
 
 	va_start(ap, family);
-	eng = find_engine(family, db, sizeof(db), table, sizeof(table));
-	if (eng && eng->store_func) 
-		res = eng->store_func(db, table, ap);
+	for (i = 1; ; i++) {
+		if ((eng = find_engine(family, i, db, sizeof(db), table, sizeof(table)))) {
+			/* If the store succeeds, it returns 0. */
+			if (eng->store_func && !(res = eng->store_func(db, table, ap))) {
+				break;
+			}
+		} else {
+			break;
+		}
+	}
 	va_end(ap);
 
 	return res;
@@ -2219,15 +2270,21 @@
 int ast_destroy_realtime(const char *family, const char *keyfield, const char *lookup, ...)
 {
 	struct ast_config_engine *eng;
-	int res = -1;
+	int res = -1, i;
 	char db[256]="";
 	char table[256]="";
 	va_list ap;
 
 	va_start(ap, lookup);
-	eng = find_engine(family, db, sizeof(db), table, sizeof(table));
-	if (eng && eng->destroy_func) 
-		res = eng->destroy_func(db, table, keyfield, lookup, ap);
+	for (i = 1; ; i++) {
+		if ((eng = find_engine(family, i, db, sizeof(db), table, sizeof(table)))) {
+			if (eng->destroy_func && !(res = eng->destroy_func(db, table, keyfield, lookup, ap))) {
+				break;
+			}
+		} else {
+			break;
+		}
+	}
 	va_end(ap);
 
 	return res;

Modified: team/tilghman/realtime_failover/res/res_odbc.c
URL: http://svn.digium.com/view/asterisk/team/tilghman/realtime_failover/res/res_odbc.c?view=diff&rev=140694&r1=140693&r2=140694
==============================================================================
--- team/tilghman/realtime_failover/res/res_odbc.c (original)
+++ team/tilghman/realtime_failover/res/res_odbc.c Tue Sep  2 17:59:24 2008
@@ -416,7 +416,7 @@
 	char *cat;
 	const char *dsn, *username, *password, *sanitysql;
 	int enabled, pooling, limit, bse, conntimeout;
-	struct timeval ncache;
+	struct timeval ncache = { 0, 0 };
 	unsigned int idlecheck;
 	int preconnect = 0, res = 0;
 	struct ast_flags config_flags = { 0 };
@@ -829,7 +829,7 @@
 
 	if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
 		ast_log(LOG_WARNING, "res_odbc: Error AllocHDB %d\n", res);
-		obj->last_negative_connect = ast_tvnow();
+		obj->parent->last_negative_connect = ast_tvnow();
 		ast_mutex_unlock(&obj->lock);
 		return ODBC_FAIL;
 	}
@@ -854,7 +854,7 @@
 
 	if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
 		SQLGetDiagRec(SQL_HANDLE_DBC, obj->con, 1, state, &err, msg, 100, &mlen);
-		obj->last_negative_connect = ast_tvnow();
+		obj->parent->last_negative_connect = ast_tvnow();
 		ast_mutex_unlock(&obj->lock);
 		ast_log(LOG_WARNING, "res_odbc: Error SQLConnect=%d errno=%d %s\n", res, (int)err, msg);
 		return ODBC_FAIL;




More information about the asterisk-commits mailing list