static void run_command (void) { mongoc_client_t *client; mongoc_collection_t *collection; bson_error_t error; bson_t *command; bson_t reply; char *str; client = mongoc_client_new ("mongodb://*****:*****@192.168.0.112:27017/"); collection = mongoc_client_get_collection (client, "test", "test"); command = BCON_NEW ("collStats", BCON_UTF8 ("test")); if (mongoc_collection_command_simple (collection, command, NULL, &reply, &error)) { str = bson_as_json (&reply, NULL); printf ("%s\n", str); bson_free (str); } else { fprintf (stderr, "Failed to run command: %s\n", error.message); } bson_destroy (command); bson_destroy (&reply); mongoc_collection_destroy (collection); mongoc_client_destroy (client); }
static void ha_config_add_shard (ha_node_t *node, ha_replica_set_t *replica_set) { mongoc_collection_t *collection; mongoc_client_t *client; bson_string_t *shardstr; bson_error_t error; bson_bool_t r; bson_t reply; bson_t cmd = BSON_INITIALIZER; char *uristr; uristr = bson_strdup_printf ("mongodb://127.0.0.1:%hu/", node->port); client = mongoc_client_new (uristr); collection = mongoc_client_get_collection (client, "admin", "fake"); shardstr = bson_string_new (NULL); bson_string_append_printf (shardstr, "%s/127.0.0.1:%hu", replica_set->name, replica_set->nodes->port); bson_append_utf8 (&cmd, "addShard", -1, shardstr->str, shardstr->len); bson_string_free (shardstr, TRUE); again: sleep (1); r = mongoc_collection_command_simple (collection, &cmd, NULL, &reply, &error); if (!r) { fprintf (stderr, "%s\n", error.message); goto again; } #if 1 { char *str; str = bson_as_json (&reply, NULL); printf ("%s\n", str); bson_free (str); } #endif bson_destroy (&reply); bson_destroy (&cmd); mongoc_collection_destroy (collection); mongoc_client_destroy (client); bson_free (uristr); }
SEXP R_mongo_collection_command_simple(SEXP ptr_col, SEXP command){ mongoc_collection_t *col = r2col(ptr_col); bson_t *cmd = r2bson(command); bson_t reply; bson_error_t err; if(!mongoc_collection_command_simple(col, cmd, NULL, &reply, &err)) stop(err.message); SEXP out = PROTECT(bson2list(&reply)); bson_destroy (&reply); UNPROTECT(1); return out; }
void configure_sharding() { mongoc_client_t *conn; mongoc_collection_t *collection; mongoc_collection_t *config; bson_error_t error; bson_t *command; bson_t reply; char *str; const char *process; mongoc_cursor_t *cursor; const bson_t *doc; bson_t *update = NULL; bson_t *query = NULL; conn = mongoc_client_new(DEFAULT_URI); collection = mongoc_client_get_collection(conn, "admin", "test"); //Work out if we arer talking to a mongod or a mongos command = BCON_NEW("serverStatus", BCON_INT32(1)); if (mongoc_collection_command_simple(collection, command, NULL, &reply, &error)) { process = get_bson_string(&reply, "process"); printf("Loader is talking to %s\n", process); } else { fprintf(stderr, "Failed to run command: %s\n", error.message); exit(1); } bson_destroy(command); bson_destroy(&reply); mongoc_collection_destroy(collection); //Could be mongos or mongos.exe if (strncmp(process, "mongos", 6) != 0) { mongoc_client_destroy(conn); return; } collection = mongoc_client_get_collection(conn, "config", "shards"); query = bson_new(); cursor = mongoc_collection_find(collection, MONGOC_QUERY_NONE, 0, 0, 0, query, NULL, NULL); int shardcount = 0; while (mongoc_cursor_next(cursor, &doc)) { shardnames[shardcount] = strdup(get_bson_string(doc, "_id")); shardcount++; } nshards = shardcount; bson_destroy(query); mongoc_cursor_destroy(cursor); //Shard our collection if it isnt already collection = mongoc_client_get_collection(conn, "admin", "test"); //Work out if we arer talking to a mongod or a mongos command = BCON_NEW("collMod", BCON_UTF8(NAMESPACE),"noPadding",BCON_BOOL(getenv("PACKTIGHT")?true:false)); if (mongoc_collection_command_simple(collection, command, NULL, &reply, &error)) { } else { fprintf(stderr, "Failed to disable padding on db (is this WiredTiger?): %s\n", error.message); } bson_destroy(command); bson_destroy(&reply); //Work out if we arer talking to a mongod or a mongos command = BCON_NEW("enableSharding", BCON_UTF8(DATA_DB)); if (mongoc_collection_command_simple(collection, command, NULL, &reply, &error)) { } else { fprintf(stderr, "Failed to enable sharding on db: %s\n", error.message); } bson_destroy(command); bson_destroy(&reply); //Work out if we arer talking to a mongod or a mongos command = BCON_NEW("shardCollection", BCON_UTF8(NAMESPACE),"key","{","_id",BCON_INT32(1),"}"); if (mongoc_collection_command_simple(collection, command, NULL, &reply, &error)) { } else { fprintf(stderr, "Failed to shard colleciton: %s\n", error.message); } bson_destroy(command); bson_destroy(&reply); mongoc_collection_destroy(collection); collection = mongoc_client_get_collection(conn, "config", "collections"); query = BCON_NEW ("_id", BCON_UTF8 (NAMESPACE)); update = BCON_NEW ("$set", "{","noBalance", BCON_BOOL(false),"}"); if (!mongoc_collection_update (collection, MONGOC_UPDATE_NONE, query, update, NULL, &error)) { printf ("Failed to turn off autobalancing for collection%s\n", error.message); } bson_destroy(query); bson_destroy(update); mongoc_client_destroy(conn); }
int carve_chunk( mongoc_client_t *conn, mongoc_collection_t *collection) { bson_error_t error; bson_t reply; unsigned long long seqno = -1; char *str; mongoc_collection_t *admin; bson_t *command; //Get a sequence value using find and modify on a record if (mongoc_collection_find_and_modify(collection, BCON_NEW("_id", BCON_UTF8("sequence")), NULL, BCON_NEW ("$inc", "{", "count", BCON_INT32 (1), "}"), NULL, false, true, true, &reply, &error)) { seqno = get_bson_int(&reply, "value.count"); printf("Got Sequence number %llu\n", seqno); } else { fprintf(stderr, "Cannot get sequence %s\n", error.message); } //Shard our collection if it isnt already admin = mongoc_client_get_collection(conn, "admin", "test"); //Work out if we arer talking to a mongod or a mongos int done = 0; while (!done) { done = 1; command = BCON_NEW("split", BCON_UTF8(NAMESPACE),"middle","{","_id",BCON_INT64(seqno<<32),"}"); if (mongoc_collection_command_simple(admin, command, NULL, &reply, &error)) { fprintf(stderr, "Split chunk: OK\n"); } else { // fprintf(stderr, "Failed to split chunk (trying again): %s\n", // error.message); if (strcmp(error.message, "split failed") == 0) { sleep(1); done = 0; } } bson_destroy(command); bson_destroy(&reply); } done = 0; while (!done) { done = 1; command = BCON_NEW("moveChunk", BCON_UTF8(NAMESPACE),"find","{","_id",BCON_INT64(seqno<<32),"}","to",BCON_UTF8(shardnames[seqno%nshards])); if (mongoc_collection_command_simple(admin, command, NULL, &reply, &error)) { fprintf(stderr, "Move chunk to %llu: OK\n", seqno % nshards); } else { // fprintf(stderr, "Failed to move chunk to %llu(trying again): %s\n", // seqno % nshards, error.message); if (strcmp(error.message, "move failed") == 0) { sleep(1); done = 0; } } bson_destroy(command); bson_destroy(&reply); } mongoc_collection_destroy(admin); return seqno; }