From 652bb777d5d37c2fb71f68c6eafc2af56a55e615 Mon Sep 17 00:00:00 2001 From: SAKURAI Masashi Date: Thu, 8 Sep 2011 01:33:38 +0900 Subject: [PATCH] Modified for new version c-driver v0.4 --- mongo-c-driver | 2 +- ngx_http_gridfs_module.c | 133 +++++++++++++++------------------------ 2 files changed, 53 insertions(+), 82 deletions(-) diff --git a/mongo-c-driver b/mongo-c-driver index 74cc0b8..1380839 160000 --- a/mongo-c-driver +++ b/mongo-c-driver @@ -1 +1 @@ -Subproject commit 74cc0b8005fbbc84ec4a0003ff80c68f9e9cc7de +Subproject commit 13808396bd1b128dfa03ea3ba4d3cdccb9d990c7 diff --git a/ngx_http_gridfs_module.c b/ngx_http_gridfs_module.c index 655d461..a95b61c 100644 --- a/ngx_http_gridfs_module.c +++ b/ngx_http_gridfs_module.c @@ -88,7 +88,7 @@ typedef struct { typedef struct { ngx_str_t name; - mongo_connection conn; + mongo conn; ngx_array_t *auths; /* ngx_http_mongo_auth_t */ } ngx_http_mongo_connection_t; @@ -261,11 +261,11 @@ static char* ngx_http_gridfs(ngx_conf_t* cf, ngx_command_t* command, void* void_ if (type.len == 0) { gridfs_loc_conf->type = NGX_CONF_UNSET_UINT; } else if (ngx_strcasecmp(type.data, (u_char *)"objectid") == 0) { - gridfs_loc_conf->type = bson_oid; + gridfs_loc_conf->type = BSON_OID; } else if (ngx_strcasecmp(type.data, (u_char *)"string") == 0) { - gridfs_loc_conf->type = bson_string; + gridfs_loc_conf->type = BSON_STRING; } else if (ngx_strcasecmp(type.data, (u_char *)"int") == 0) { - gridfs_loc_conf->type = bson_int; + gridfs_loc_conf->type = BSON_INT; } else { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "Unsupported Type: %s", (char *)value[i].data); @@ -294,7 +294,7 @@ static char* ngx_http_gridfs(ngx_conf_t* cf, ngx_command_t* command, void* void_ if (gridfs_loc_conf->field.data != NULL && ngx_strcmp(gridfs_loc_conf->field.data, "filename") == 0 - && gridfs_loc_conf->type != bson_string) { + && gridfs_loc_conf->type != BSON_STRING) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "Field: filename, must be of Type: string"); return NGX_CONF_ERROR; @@ -372,7 +372,7 @@ static char* ngx_http_gridfs_merge_loc_conf(ngx_conf_t* cf, void* void_parent, v ngx_conf_merge_str_value(child->db, parent->db, NULL); ngx_conf_merge_str_value(child->root_collection, parent->root_collection, "fs"); ngx_conf_merge_str_value(child->field, parent->field, "_id"); - ngx_conf_merge_uint_value(child->type, parent->type, bson_oid); + ngx_conf_merge_uint_value(child->type, parent->type, BSON_OID); ngx_conf_merge_str_value(child->user, parent->user, NULL); ngx_conf_merge_str_value(child->pass, parent->pass, NULL); ngx_conf_merge_str_value(child->mongo, parent->mongo, "127.0.0.1:27017"); @@ -465,7 +465,7 @@ static ngx_int_t ngx_http_mongo_authenticate(ngx_log_t *log, ngx_http_gridfs_loc static ngx_int_t ngx_http_mongo_add_connection(ngx_cycle_t* cycle, ngx_http_gridfs_loc_conf_t* gridfs_loc_conf) { ngx_http_mongo_connection_t* mongo_conn; - mongo_conn_return status; + int status; ngx_http_mongod_server_t *mongods; volatile ngx_uint_t i; u_char host[255]; @@ -491,7 +491,7 @@ static ngx_int_t ngx_http_mongo_add_connection(ngx_cycle_t* cycle, ngx_http_grid } else if ( gridfs_loc_conf->mongods->nelts >= 2 && gridfs_loc_conf->mongods->nelts < 9 ) { /* Initiate replica set connection. */ - mongo_replset_init_conn( &mongo_conn->conn, (const char *)gridfs_loc_conf->replset.data ); + mongo_replset_init( &mongo_conn->conn, (const char *)gridfs_loc_conf->replset.data ); /* Add replica set seeds. */ for( i=0; imongods->nelts; ++i ) { @@ -506,29 +506,29 @@ static ngx_int_t ngx_http_mongo_add_connection(ngx_cycle_t* cycle, ngx_http_grid } switch (status) { - case mongo_conn_success: + case MONGO_CONN_SUCCESS: break; - case mongo_conn_bad_arg: - ngx_log_error(NGX_LOG_ERR, cycle->log, 0, - "Mongo Exception: Bad Arguments"); - return NGX_ERROR; - case mongo_conn_no_socket: + case MONGO_CONN_NO_SOCKET: ngx_log_error(NGX_LOG_ERR, cycle->log, 0, "Mongo Exception: No Socket"); return NGX_ERROR; - case mongo_conn_fail: + case MONGO_CONN_FAIL: ngx_log_error(NGX_LOG_ERR, cycle->log, 0, "Mongo Exception: Connection Failure."); return NGX_ERROR; - case mongo_conn_not_master: + case MONGO_CONN_ADDR_FAIL: + ngx_log_error(NGX_LOG_ERR, cycle->log, 0, + "Mongo Exception: getaddrinfo Failure."); + return NGX_ERROR; + case MONGO_CONN_NOT_MASTER: ngx_log_error(NGX_LOG_ERR, cycle->log, 0, "Mongo Exception: Not Master"); return NGX_ERROR; - case mongo_conn_bad_set_name: + case MONGO_CONN_BAD_SET_NAME: ngx_log_error(NGX_LOG_ERR, cycle->log, 0, "Mongo Exception: Replica set name %s does not match.", gridfs_loc_conf->replset.data); return NGX_ERROR; - case mongo_conn_cannot_find_primary: + case MONGO_CONN_NO_PRIMARY: ngx_log_error(NGX_LOG_ERR, cycle->log, 0, "Mongo Exception: Cannot connect to primary node."); return NGX_ERROR; @@ -565,34 +565,34 @@ static ngx_int_t ngx_http_gridfs_init_worker(ngx_cycle_t* cycle) { } static ngx_int_t ngx_http_mongo_reconnect(ngx_log_t *log, ngx_http_mongo_connection_t *mongo_conn) { - volatile mongo_conn_return status = mongo_conn_fail; + volatile int status = MONGO_CONN_FAIL; - MONGO_TRY_GENERIC(&mongo_conn->conn) { - if(&mongo_conn->conn.connected) { mongo_disconnect(&mongo_conn->conn); } + if (&mongo_conn->conn.connected) { + mongo_disconnect(&mongo_conn->conn); ngx_msleep(MONGO_RECONNECT_WAITTIME); status = mongo_reconnect(&mongo_conn->conn); - } MONGO_CATCH_GENERIC(&mongo_conn->conn) { - status = mongo_conn_fail; + } else { + status = MONGO_CONN_FAIL; } switch (status) { - case mongo_conn_success: + case MONGO_CONN_SUCCESS: break; - case mongo_conn_bad_arg: - ngx_log_error(NGX_LOG_ERR, log, 0, - "Mongo Exception: Bad Arguments"); - return NGX_ERROR; - case mongo_conn_no_socket: + case MONGO_CONN_NO_SOCKET: ngx_log_error(NGX_LOG_ERR, log, 0, "Mongo Exception: No Socket"); return NGX_ERROR; - case mongo_conn_fail: + case MONGO_CONN_FAIL: ngx_log_error(NGX_LOG_ERR, log, 0, "Mongo Exception: Connection Failure %s:%i;", mongo_conn->conn.primary->host, mongo_conn->conn.primary->port); return NGX_ERROR; - case mongo_conn_not_master: + case MONGO_CONN_ADDR_FAIL: + ngx_log_error(NGX_LOG_ERR, log, 0, + "Mongo Exception: getaddrinfo Failure"); + return NGX_ERROR; + case MONGO_CONN_NOT_MASTER: ngx_log_error(NGX_LOG_ERR, log, 0, "Mongo Exception: Not Master"); return NGX_ERROR; @@ -611,16 +611,10 @@ static ngx_int_t ngx_http_mongo_reauth(ngx_log_t *log, ngx_http_mongo_connection auths = mongo_conn->auths->elts; for (i = 0; i < mongo_conn->auths->nelts; i++) { - MONGO_TRY_GENERIC(&mongo_conn->conn) { - success = mongo_cmd_authenticate( &mongo_conn->conn, - (const char*)auths[i].db.data, - (const char*)auths[i].user.data, - (const char*)auths[i].pass.data ); - } MONGO_CATCH_GENERIC(&mongo_conn->conn) { - ngx_log_error(NGX_LOG_ERR, log, 0, - "Mongo connection error, during reauth"); - return NGX_ERROR; - } + success = mongo_cmd_authenticate( &mongo_conn->conn, + (const char*)auths[i].db.data, + (const char*)auths[i].user.data, + (const char*)auths[i].pass.data ); if (!success) { ngx_log_error(NGX_LOG_ERR, log, 0, "Invalid mongo user/pass: %s/%s, during reauth", @@ -685,10 +679,8 @@ static ngx_int_t ngx_http_gridfs_handler(ngx_http_request_t* request) { ngx_uint_t numchunks; char* contenttype; volatile ngx_uint_t i; - volatile ngx_int_t found = 0; ngx_int_t rc = NGX_OK; bson query; - bson_buffer buf; bson_oid_t oid; mongo_cursor ** cursors; gridfs_offset chunk_len; @@ -697,6 +689,7 @@ static ngx_int_t ngx_http_gridfs_handler(ngx_http_request_t* request) { bson chunk; ngx_pool_cleanup_t* gridfs_cln; ngx_http_gridfs_cleanup_t* gridfs_clndata; + int status; volatile ngx_uint_t e = FALSE; volatile ngx_uint_t ecounter = 0; @@ -721,7 +714,6 @@ static ngx_int_t ngx_http_gridfs_handler(ngx_http_request_t* request) { return NGX_HTTP_SERVICE_UNAVAILABLE; } - // ---------- RETRIEVE KEY ---------- // location_name = core_conf->name; @@ -752,13 +744,11 @@ static ngx_int_t ngx_http_gridfs_handler(ngx_http_request_t* request) { // ---------- RETRIEVE GRIDFILE ---------- // do { - MONGO_TRY_GENERIC(&mongo_conn->conn){ - e = FALSE; - gridfs_init(&mongo_conn->conn, + e = FALSE; + if (gridfs_init(&mongo_conn->conn, (const char*)gridfs_conf->db.data, (const char*)gridfs_conf->root_collection.data, - &gfs); - } MONGO_CATCH_GENERIC(&mongo_conn->conn) { + &gfs) != MONGO_OK) { e = TRUE; ecounter++; if (ecounter > MONGO_MAX_RETRIES_PER_REQUEST || ngx_http_mongo_reconnect(request->connection->log, mongo_conn) == NGX_ERROR @@ -771,44 +761,27 @@ static ngx_int_t ngx_http_gridfs_handler(ngx_http_request_t* request) { } } while (e); - bson_buffer_init(&buf); + bson_init(&query); switch (gridfs_conf->type) { - case bson_oid: + case BSON_OID: bson_oid_from_string(&oid, value); - bson_append_oid(&buf, (char*)gridfs_conf->field.data, &oid); + bson_append_oid(&query, (char*)gridfs_conf->field.data, &oid); break; - case bson_int: - bson_append_int(&buf, (char*)gridfs_conf->field.data, ngx_atoi((u_char*)value, strlen(value))); + case BSON_INT: + bson_append_int(&query, (char*)gridfs_conf->field.data, ngx_atoi((u_char*)value, strlen(value))); break; - case bson_string: - bson_append_string(&buf, (char*)gridfs_conf->field.data, value); + case BSON_STRING: + bson_append_string(&query, (char*)gridfs_conf->field.data, value); break; } - bson_from_buffer(&query, &buf); + bson_finish(&query); - do { - MONGO_TRY_GENERIC(&mongo_conn->conn){ - e = FALSE; - found = gridfs_find_query(&gfs, &query, &gfile); - } MONGO_CATCH_GENERIC(&mongo_conn->conn) { - e = TRUE; ecounter++; - if (ecounter > MONGO_MAX_RETRIES_PER_REQUEST - || ngx_http_mongo_reconnect(request->connection->log, mongo_conn) == NGX_ERROR - || ngx_http_mongo_reauth(request->connection->log, mongo_conn) == NGX_ERROR) { - ngx_log_error(NGX_LOG_ERR, request->connection->log, 0, - "Mongo connection dropped, could not reconnect"); - if(&mongo_conn->conn.connected) { mongo_disconnect(&mongo_conn->conn); } - bson_destroy(&query); - free(value); - return NGX_HTTP_SERVICE_UNAVAILABLE; - } - } - } while (e); + status = gridfs_find_query(&gfs, &query, &gfile); bson_destroy(&query); free(value); - if(!found){ + if(status == MONGO_ERROR) { return NGX_HTTP_NOT_FOUND; } @@ -895,11 +868,9 @@ static ngx_int_t ngx_http_gridfs_handler(ngx_http_request_t* request) { /* Fetch the chunk from mongo */ do { - MONGO_TRY_GENERIC(&mongo_conn->conn){ - e = FALSE; - cursors[i] = gridfile_get_chunks(&gfile, i, 1); - mongo_cursor_next(cursors[i]); - } MONGO_CATCH_GENERIC(&mongo_conn->conn) { + e = FALSE; + cursors[i] = gridfile_get_chunks(&gfile, i, 1); + if (!(cursors[i] && mongo_cursor_next(cursors[i]) == MONGO_OK)) { e = TRUE; ecounter++; if (ecounter > MONGO_MAX_RETRIES_PER_REQUEST || ngx_http_mongo_reconnect(request->connection->log, mongo_conn) == NGX_ERROR