def test_scram_sha1(self):
        client = MongoClient(host, port)
        self.assertTrue(
            client.pymongo_test.authenticate('user',
                                             'pass',
                                             mechanism='SCRAM-SHA-1'))
        client.pymongo_test.command('dbstats')

        client = MongoClient('mongodb://*****:*****@%s:%d/pymongo_test'
                             '?authMechanism=SCRAM-SHA-1' % (host, port))
        client.pymongo_test.command('dbstats')

        if self.set_name:
            client = MongoReplicaSetClient(
                'mongodb://*****:*****@%s:%d/pymongo_test?authMechanism=SCRAM-SHA-1'
                   '&replicaSet=%s' % (host, port, self.set_name))
            client = MongoReplicaSetClient(uri)
            client.pymongo_test.command('dbstats')
            client.read_preference = ReadPreference.SECONDARY
            client.pymongo_test.command('dbstats')
    def test_sasl_plain(self):

        client = MongoClient(SASL_HOST, SASL_PORT)
        self.assertTrue(
            client.ldap.authenticate(SASL_USER, SASL_PASS, SASL_DB, 'PLAIN'))
        client.ldap.test.find_one()

        uri = ('mongodb://%s:%s@%s:%d/?authMechanism=PLAIN;'
               'authSource=%s' % (quote_plus(SASL_USER), quote_plus(SASL_PASS),
                                  SASL_HOST, SASL_PORT, SASL_DB))
        client = MongoClient(uri)
        client.ldap.test.find_one()

        set_name = client.admin.command('ismaster').get('setName')
        if set_name:
            client = MongoReplicaSetClient(SASL_HOST,
                                           port=SASL_PORT,
                                           replicaSet=set_name)
            self.assertTrue(
                client.ldap.authenticate(SASL_USER, SASL_PASS, SASL_DB,
                                         'PLAIN'))
            client.ldap.test.find_one()

            uri = ('mongodb://%s:%s@%s:%d/?authMechanism=PLAIN;'
                   'authSource=%s;replicaSet=%s' %
                   (quote_plus(SASL_USER), quote_plus(SASL_PASS), SASL_HOST,
                    SASL_PORT, SASL_DB, str(set_name)))
            client = MongoReplicaSetClient(uri)
            client.ldap.test.find_one()
Exemple #3
0
    def test_gssapi_simple(self):

        client = MongoClient(GSSAPI_HOST, GSSAPI_PORT)
        self.assertTrue(client.test.authenticate(PRINCIPAL,
                                                 mechanism='GSSAPI'))
        # Just test that we can run a simple command.
        self.assertTrue(client.database_names())

        uri = ('mongodb://%s@%s:%d/?authMechanism='
               'GSSAPI' % (quote_plus(PRINCIPAL), GSSAPI_HOST, GSSAPI_PORT))
        client = MongoClient(uri)
        self.assertTrue(client.database_names())

        set_name = client.admin.command('ismaster').get('setName')
        if set_name:
            client = MongoReplicaSetClient(GSSAPI_HOST,
                                           port=GSSAPI_PORT,
                                           replicaSet=set_name)
            self.assertTrue(
                client.test.authenticate(PRINCIPAL, mechanism='GSSAPI'))
            self.assertTrue(client.database_names())
            uri = ('mongodb://%s@%s:%d/?authMechanism=GSSAPI;replicaSet'
                   '=%s' % (quote_plus(PRINCIPAL), GSSAPI_HOST, GSSAPI_PORT,
                            str(set_name)))
            client = MongoReplicaSetClient(uri)
            self.assertTrue(client.database_names())
    def test_init_disconnected_with_auth(self):
        c = self._get_client()

        auth_context.client.admin.add_user("admin", "pass")
        c.admin.authenticate("admin", "pass")
        try:
            c.pymongo_test.add_user("user",
                                    "pass",
                                    roles=['readWrite', 'userAdmin'])

            # Auth with lazy connection.
            host = one(self.hosts)
            uri = "mongodb://*****:*****@%s:%d/pymongo_test?replicaSet=%s" % (
                host[0], host[1], self.name)

            authenticated_client = MongoReplicaSetClient(uri, _connect=False)
            authenticated_client.pymongo_test.test.find_one()

            # Wrong password.
            bad_uri = (
                "mongodb://*****:*****@%s:%d/pymongo_test?replicaSet=%s" %
                (host[0], host[1], self.name))

            bad_client = MongoReplicaSetClient(bad_uri, _connect=False)
            self.assertRaises(OperationFailure,
                              bad_client.pymongo_test.test.find_one)

        finally:
            # Clean up.
            remove_all_users(c.pymongo_test)
            c.admin.remove_user('admin')
Exemple #5
0
 def _establish_connection(cls, *args, **kwargs):
     for i in range(MAX_ATTEMPTS - 1):
         try:
             if 'replicaSet' in kwargs:
                 return MongoReplicaSetClient(*args, **kwargs)
             else:
                 return MongoClient(*args, **kwargs)
         except ConnectionFailure as e:
             logging.error(
                 'Fail to connect to %s, %s [%s]', args, kwargs, e)
             sleep(CONNECT_SLEEP_TIME)
     if 'replicaSet' in kwargs:
         return MongoReplicaSetClient(*args, **kwargs)
     else:
         return MongoClient(*args, **kwargs)
Exemple #6
0
    def test_alive(self):
        primary = ha_tools.get_primary()
        secondary = ha_tools.get_random_secondary()
        primary_cx = MongoClient(primary, use_greenlets=use_greenlets)
        secondary_cx = MongoClient(secondary, use_greenlets=use_greenlets)
        rsc = MongoReplicaSetClient(self.seed,
                                    replicaSet=self.name,
                                    use_greenlets=use_greenlets)

        try:
            self.assertTrue(primary_cx.alive())
            self.assertTrue(secondary_cx.alive())
            self.assertTrue(rsc.alive())

            ha_tools.kill_primary()
            time.sleep(0.5)

            self.assertFalse(primary_cx.alive())
            self.assertTrue(secondary_cx.alive())
            self.assertFalse(rsc.alive())

            ha_tools.kill_members([secondary], 2)
            time.sleep(0.5)

            self.assertFalse(primary_cx.alive())
            self.assertFalse(secondary_cx.alive())
            self.assertFalse(rsc.alive())
        finally:
            rsc.close()
Exemple #7
0
    def test_read_with_failover(self):
        c = MongoReplicaSetClient(self.seed,
                                  replicaSet=self.name,
                                  use_greenlets=use_greenlets)
        self.assertTrue(bool(len(c.secondaries)))

        def iter_cursor(cursor):
            for _ in cursor:
                pass
            return True

        db = c.pymongo_test
        w = len(c.secondaries) + 1
        db.test.remove({}, w=w)
        # Force replication
        db.test.insert([{'foo': i} for i in xrange(10)], w=w)
        self.assertEqual(10, db.test.count())

        db.read_preference = SECONDARY_PREFERRED
        cursor = db.test.find().batch_size(5)
        cursor.next()
        self.assertEqual(5, cursor._Cursor__retrieved)
        self.assertTrue(cursor._Cursor__connection_id in c.secondaries)
        ha_tools.kill_primary()
        # Primary failure shouldn't interrupt the cursor
        self.assertTrue(iter_cursor(cursor))
        self.assertEqual(10, cursor._Cursor__retrieved)
Exemple #8
0
    def test_writes_with_failover(self):
        c = MongoReplicaSetClient(self.seed,
                                  replicaSet=self.name,
                                  use_greenlets=use_greenlets)
        primary = c.primary
        db = c.pymongo_test
        w = len(c.secondaries) + 1
        db.test.remove({}, w=w)
        db.test.insert({'foo': 'bar'}, w=w)
        self.assertEqual('bar', db.test.find_one()['foo'])

        def try_write():
            for _ in xrange(30):
                try:
                    db.test.insert({'bar': 'baz'})
                    return True
                except AutoReconnect:
                    sleep(1)
            return False

        killed = ha_tools.kill_primary(9)
        self.assertTrue(bool(len(killed)))
        self.assertTrue(try_write())
        self.assertTrue(primary != c.primary)
        self.assertEqual('baz', db.test.find_one({'bar': 'baz'})['bar'])
Exemple #9
0
    def test_secondary_failure(self):
        c = MongoReplicaSetClient(self.seed,
                                  replicaSet=self.name,
                                  use_greenlets=use_greenlets)
        self.assertTrue(bool(len(c.secondaries)))
        primary = c.primary
        secondaries = c.secondaries

        def readers_changed():
            for _ in xrange(20):
                if c.secondaries != secondaries:
                    return True

                sleep(1)
            return False

        killed = ha_tools.kill_secondary()
        sleep(2 * MONITOR_INTERVAL)
        self.assertTrue(bool(len(killed)))
        self.assertEqual(primary, c.primary)
        self.assertTrue(readers_changed())
        secondaries = c.secondaries

        ha_tools.restart_members([killed])
        self.assertEqual(primary, c.primary)
        self.assertTrue(readers_changed())
    def test_gssapi_threaded(self):

        # Use auto_start_request=True to make sure each thread
        # uses a different socket.
        client = MongoClient(GSSAPI_HOST, auto_start_request=True)
        self.assertTrue(client.test.authenticate(PRINCIPAL,
                                                 mechanism='GSSAPI'))

        threads = []
        for _ in xrange(4):
            threads.append(AutoAuthenticateThread(client.foo))
        for thread in threads:
            thread.start()
        for thread in threads:
            thread.join()
            self.assertTrue(thread.success)

        set_name = client.admin.command('ismaster').get('setName')
        if set_name:
            preference = ReadPreference.SECONDARY
            client = MongoReplicaSetClient(GSSAPI_HOST,
                                           replicaSet=set_name,
                                           read_preference=preference)
            self.assertTrue(
                client.test.authenticate(PRINCIPAL, mechanism='GSSAPI'))
            self.assertTrue(client.foo.command('dbstats'))

            threads = []
            for _ in xrange(4):
                threads.append(AutoAuthenticateThread(client.foo))
            for thread in threads:
                thread.start()
            for thread in threads:
                thread.join()
                self.assertTrue(thread.success)
Exemple #11
0
    def test_cert_ssl_implicitly_set(self):
        # Expects the server to be running with the server.pem, ca.pem
        # and crl.pem provided in mongodb and the server tests eg:
        #
        #   --sslPEMKeyFile=jstests/libs/server.pem
        #   --sslCAFile=jstests/libs/ca.pem
        #   --sslCRLFile=jstests/libs/crl.pem
        #
        # Also requires an /etc/hosts entry where "server" is resolvable
        if not CERT_SSL:
            raise SkipTest("No mongod available over SSL with certs")

        client = MongoClient(host, port, ssl_certfile=CLIENT_PEM)
        response = client.admin.command('ismaster')
        if 'setName' in response:
            client = MongoReplicaSetClient(pair,
                                           replicaSet=response['setName'],
                                           w=len(response['hosts']),
                                           ssl_certfile=CLIENT_PEM)

        db = client.pymongo_ssl_test
        db.test.drop()
        self.assertTrue(db.test.insert({'ssl': True}))
        self.assertTrue(db.test.find_one()['ssl'])
        client.drop_database('pymongo_ssl_test')
Exemple #12
0
def mongoDBHelper(dbName, readOnly=False):
    global connection
    if len(app_config.MONGODB_CONFIG) == 0:
        connection = None
        return
    try:
        connection
    except NameError:
        if app_config.MONGODB_CONFIG['RS'] != "":
            connection = MongoReplicaSetClient(
                app_config.MONGODB_CONFIG['URL'],
                replicaSet=app_config.MONGODB_CONFIG['RS'])
        else:
            connection = MongoClient(app_config.MONGODB_CONFIG['URL'])

    db = connection[dbName]
    if 'AUTH' in app_config.MONGODB_CONFIG:
        if 'true' == app_config.MONGODB_CONFIG['AUTH']:
            db.authenticate(app_config.MONGODB_CONFIG['USER'],
                            app_config.MONGODB_CONFIG['PASSWORD'])
        else:
            pass
    # if readOnly:
    #     db.read_preference = ReadPreference.SECONDARY_PREFERRED
    # else:
    #     db.read_preference = ReadPreference.PRIMARY_PREFERRED
    return db
Exemple #13
0
def InsertFunc(addr, insertnum):
    contentsrc = "cs.txt"
    f = open(contentsrc, "r")
    mm = mmap.mmap(f.fileno(), 0, prot=mmap.PROT_READ)
    fsize = os.path.getsize(contentsrc)
    addrnew = addr + ":29002"
    client = MongoReplicaSetClient(addrnew, replicaSet="rs0")
    db = client.topicdb
    collection = db.topiccol
    count = 0
    while count < insertnum:
        post = GenerateTopicPost(mm, fsize)
        try:
            collection.insert(post)
        except InvalidStringData:
            continue
        except pymongo.errors.AutoReconnect:
            print "insert auto reconnect", time.ctime(time.time())
            time.sleep(5)
            print "sleep over", time.ctime(time.time())
            continue
            print "continue:", count
        except Exception, e:
            print e
        count += 1
Exemple #14
0
def QueryFunc(addr, insertnum):
    print "query process"
    contentsrc = "objids_0113.txt"
    f = open(contentsrc, "r")
    print "h0"
    mm = mmap.mmap(f.fileno(), 0, prot=mmap.PROT_READ)
    print "hi"
    fsize = os.path.getsize(contentsrc)
    numtopicids = fsize / 24
    addrnew = addr + ":29002"
    print "h1"
    client = MongoReplicaSetClient(addrnew, replicaSet="rs0")
    db = client.topicdb
    collection = db.topiccol
    count = 0
    totinsertnum = insertnum * 3
    print "h2"
    while count < totinsertnum:
        topicidstart = random.randint(0, numtopicids - 1) * 24
        topicidsend = topicidstart + 24
        rand_topicid = mm[topicidstart:topicidsend]
        try:
            #collection.find_one({"_id":ObjectId(rand_topicid)}, read_preference=ReadPreference.SECONDARY_PREFERRED)
            collection.find_one(
                {"_id": ObjectId(rand_topicid)},
                read_preference=ReadPreference.SECONDARY_PREFERRED)
            #print "q id:", rand_topicid
            #collection.find_one({"_id":ObjectId(rand_topicid)})
        except pymongo.errors.AutoReconnect:
            print "auto reconnect"
            time.sleep(2)
            continue
        count += 1
    mm.close()
    def test_rs_client_attrs(self):
        pymongo_rs_client_only = set(['pool_class']).union(pymongo_client_only)

        sync_cx_rs = MongoReplicaSetClient(env.rs_uri)
        self.assertEqual(
            attrs(sync_cx_rs) - pymongo_rs_client_only,
            attrs(self.rsc) - motor_client_only)
    def get_mongo(self):
        """Return the mongo connection from the environment."""
        if self.mongo_db:
            return self.mongo_db

        if len(self.uri_params['nodelist']) > 1 and MongoReplicaSetClient:
            nodes = ','.join(
                ['%s:%d' % n for n in self.uri_params['nodelist']])
            client = MongoReplicaSetClient(nodes, **self.uri_params['options'])
        else:
            client = MongoClient(self.uri_params['nodelist'][0][0],
                                 self.uri_params['nodelist'][0][1],
                                 **self.uri_params['options'])

        db = client[self.uri_params['database']]
        if self.uri_params['username']:
            if not db.authenticate(self.uri_params['username'],
                                   self.uri_params['password']):
                raise PluginError('Cannot authenticate to MongoDB for '
                                  'user: %s' % self.uri_params['username'])

        if self.post_create:
            db = self.post_create(db)

        self.mongo_db = db

        return self.mongo_db
Exemple #17
0
    def test_recovering_member_triggers_refresh(self):
        # To test that find_one() and count() trigger immediate refreshes,
        # we'll create a separate client for each
        self.c_find_one, self.c_count = [
            MongoReplicaSetClient(self.seed,
                                  replicaSet=self.name,
                                  use_greenlets=use_greenlets,
                                  read_preference=SECONDARY) for _ in xrange(2)
        ]

        # We've started the primary and one secondary
        primary = ha_tools.get_primary()
        secondary = ha_tools.get_secondaries()[0]

        # Pre-condition: just make sure they all connected OK
        for c in self.c_find_one, self.c_count:
            self.assertEqual(one(c.secondaries), _partition_node(secondary))

        ha_tools.set_maintenance(secondary, True)

        # Trigger a refresh in various ways
        self.assertRaises(AutoReconnect, self.c_find_one.test.test.find_one)
        self.assertRaises(AutoReconnect, self.c_count.test.test.count)

        # Wait for the immediate refresh to complete - we're not waiting for
        # the periodic refresh, which has been disabled
        sleep(1)

        for c in self.c_find_one, self.c_count:
            self.assertFalse(c.secondaries)
            self.assertEqual(_partition_node(primary), c.primary)
def getClient(conf):
    '''
        Return  a connexion to  database specified  in  conf file
        take care with autentication
    '''
    mongodb_host = conf.get("mongodb_host", "127.0.0.1")
    mongodb_port = conf.get("mongodb_port", "27017")
    mongodb_URL = "mongodb://"+str(mongodb_host)+":"+str(mongodb_port)
    #mongodb replication
    is_mongo_replicat = conf.get("is_mongo_replicat", 0)
    mongodb_set = "'"+conf.get("mongodb_set","")+"'"
    mongodb_replicaSet =conf.get("mongodb_replicaSet",None)
    mongodb_read_preference = conf.get("mongodb_read_preference",None)
    cluster = conf.get("cluster", "ceph")
    if is_mongo_replicat ==  1:
        client = MongoReplicaSetClient(eval(mongodb_set), replicaSet=mongodb_replicaSet, read_preference=eval(mongodb_read_preference))
    else:
        #if not replicated
        client = MongoClient(mongodb_URL)
    # mongo db  authentication
    is_mongo_authenticate = conf.get("is_mongo_authenticate", 0)
    mongodb_user = conf.get("mongodb_user", "ceph")
    mongodb_passwd = conf.get("mongodb_passwd", "empty")
    if is_mongo_authenticate == 1:
        client[cluster].authenticate(mongodb_user,mongodb_passwd)
    return client
Exemple #19
0
    def test_stepdown_triggers_refresh(self):
        c_find_one = MongoReplicaSetClient(self.seed,
                                           replicaSet=self.name,
                                           use_greenlets=use_greenlets)

        # We've started the primary and one secondary
        primary = ha_tools.get_primary()
        secondary = ha_tools.get_secondaries()[0]
        self.assertEqual(one(c_find_one.secondaries),
                         _partition_node(secondary))

        ha_tools.stepdown_primary()

        # Make sure the stepdown completes
        sleep(1)

        # Trigger a refresh
        self.assertRaises(AutoReconnect, c_find_one.test.test.find_one)

        # Wait for the immediate refresh to complete - we're not waiting for
        # the periodic refresh, which has been disabled
        sleep(1)

        # We've detected the stepdown
        self.assertTrue(not c_find_one.primary
                        or primary != _partition_node(c_find_one.primary))
Exemple #20
0
    def test_cert_ssl_validation_hostname_fail(self):
        # Expects the server to be running with the server.pem, ca.pem
        # and crl.pem provided in mongodb and the server tests eg:
        #
        #   --sslPEMKeyFile=jstests/libs/server.pem
        #   --sslCAFile=jstests/libs/ca.pem
        #   --sslCRLFile=jstests/libs/crl.pem
        if not CERT_SSL:
            raise SkipTest("No mongod available over SSL with certs")

        client = MongoClient(host, port, ssl=True, ssl_certfile=CLIENT_PEM)
        response = client.admin.command('ismaster')

        try:
            MongoClient(pair,
                        ssl=True,
                        ssl_certfile=CLIENT_PEM,
                        ssl_cert_reqs=ssl.CERT_REQUIRED,
                        ssl_ca_certs=CA_PEM)
            self.fail("Invalid hostname should have failed")
        except CertificateError:
            pass

        if 'setName' in response:
            try:
                MongoReplicaSetClient(pair,
                                      replicaSet=response['setName'],
                                      w=len(response['hosts']),
                                      ssl=True,
                                      ssl_certfile=CLIENT_PEM,
                                      ssl_cert_reqs=ssl.CERT_REQUIRED,
                                      ssl_ca_certs=CA_PEM)
                self.fail("Invalid hostname should have failed")
            except CertificateError:
                pass
Exemple #21
0
 def setUp(self):
     members = [{}, {}, {'arbiterOnly': True}]
     res = ha_tools.start_replica_set(members)
     self.c = MongoReplicaSetClient(res[0],
                                    replicaSet=res[1],
                                    use_greenlets=use_greenlets,
                                    auto_start_request=True)
Exemple #22
0
def startapp(app):
    # do not display server and version information
    # https://stackoverflow.com/a/55209796/2249798
    # https://stackoverflow.com/a/54947461/2249798
    cherrypy.__version__ = ""
    cherrypy.config.update({"response.headers.server": ""})
    cherrypy._cperror._HTTPErrorTemplate = \
        cherrypy._cperror._HTTPErrorTemplate.replace(
            "Powered by <a href=\"http://www.cherrypy.org\">"
            + "CherryPy %(version)s</a>\n",
            "%(version)s"
        )

    if config["mongodb"].getboolean('replicaset'):
        print("Connecting to MongoDB ReplicaSet: %s" %
              config["mongodb"]["url"])
        dbe = MongoReplicaSetClient(config["mongodb"]["url"],
                                    w="majority",
                                    maxPoolSize=16,
                                    socketTimeoutMS=60000,
                                    connectTimeoutMS=30000,
                                    waitQueueTimeoutMS=60000,
                                    waitQueueMultiple=64)
        atexit.register(dbe.close)
    else:
        print("Connecting to MongoDB: %s" % config["mongodb"]["url"])
        dbe = MongoClient(config["mongodb"]["url"],
                          maxPoolSize=16,
                          socketTimeoutMS=60000,
                          connectTimeoutMS=30000,
                          waitQueueTimeoutMS=60000,
                          waitQueueMultiple=64)
    dbm = dbe[config["mongodb"]["dbname"]]
    return cherrypy.Application(app(dbm), script_name=None, config=None)
def getClient(conf, dbname):
    '''
        conf : json  conf objet
        conf=load_conf(configfile)
        db = getClient(conf)['ceph']
        collection =db['cluster']
        cursor=collection.find_one()
        Return  a connexion to  database specified  in  conf file
        take care with authentication
    '''
    mongodb_host = conf.get("mongodb_host", "127.0.0.1")
    mongodb_port = conf.get("mongodb_port", "27017")
    mongodb_URL = "mongodb://" + mongodb_host + ":" + str(mongodb_port)
    #mongodb replication
    is_mongo_replicat = conf.get("is_mongo_replicat", 0)
    mongodb_set = "'" + conf.get("mongodb_set", "") + "'"
    mongodb_replicaSet = conf.get("mongodb_replicaSet", None)
    mongodb_read_preference = conf.get("mongodb_read_preference", None)

    if is_mongo_replicat == 1:
        client = MongoReplicaSetClient(
            eval(mongodb_set),
            replicaSet=mongodb_replicaSet,
            read_preference=eval(mongodb_read_preference))
    else:
        #if not replicated
        client = MongoClient(mongodb_URL)
    # mongo db  authentication
    is_mongo_authenticate = conf.get("is_mongo_authenticate", 0)
    mongodb_user = conf.get("mongodb_user", "ceph")
    mongodb_passwd = conf.get("mongodb_passwd", "empty")
    if is_mongo_authenticate == 1:
        client[dbname].authenticate(mongodb_user, mongodb_passwd)

    return client[dbname]
Exemple #24
0
    def __init__(self, mongo_host=None, mongo_db_name=None, mongo_replica_set=None,
                 default_rating=3, max_rating=5,
                 log_level=logging.DEBUG):

        self.info_used = set() # Info used in addition to item_id. Only for in-memory testing, otherwise there is utils collection in the MongoDB
        self.default_rating = default_rating  # Rating inserted by default
        self.max_rating = max_rating
        self._items_cooccurrence = pd.DataFrame  # cooccurrence of items
        self._categories_cooccurrence = {} # cooccurrence of categories
        self.cooccurrence_updated = 0.0  # Time of update
        self.item_ratings = defaultdict(dict)  # matrix of ratings for a item (inmemory testing)
        self.user_ratings = defaultdict(dict)  # matrix of ratings for a user (inmemory testing)
        self.items = defaultdict(dict)  # matrix of item's information {item_id: {"Author": "AA. VV."....}
        self.item_id_key = 'id'
        # categories --same as above, but separated as they are not always available
        self.tot_categories_user_ratings = defaultdict(lambda: defaultdict(lambda: defaultdict(int)))  # sum of all ratings  (inmemory testing)
        self.tot_categories_item_ratings = defaultdict(lambda: defaultdict(lambda: defaultdict(int)))  # ditto
        self.n_categories_user_ratings = defaultdict(lambda: defaultdict(lambda: defaultdict(int)))  # number of ratings  (inmemory testing)
        self.n_categories_item_ratings = defaultdict(lambda: defaultdict(lambda: defaultdict(int)))  # ditto
        self.items_by_popularity = []
        self.items_by_popularity_updated = 0.0  # Time of update
        # Loggin stuff
        self.logger = logging.getLogger("csrc")
        self.logger.setLevel(log_level)
        ch = logging.StreamHandler()
        ch.setLevel(logging.DEBUG)
        self.logger.addHandler(ch)
        self.logger.debug("============ Creating a Recommender Instance ================")
        self.db = None

        if mongo_host is not None:
            self.logger.debug("============ Host: %s", str(mongo_host))
            if mongo_replica_set is not None:
                self.logger.debug("============ Replica: %s", str(mongo_replica_set))

            assert (mongo_db_name is not None)
            if mongo_replica_set is not None:
                from pymongo import MongoReplicaSetClient
                self.mongo_host = mongo_host
                self.mongo_replica_set = mongo_replica_set
                self.mongo_db_name = mongo_db_name
                self.mongo_client = MongoReplicaSetClient(self.mongo_host,
                                                          replicaSet=self.mongo_replica_set)
                self.db = self.mongo_client[self.mongo_db_name]
                # reading --for producing recommendations-- could be even out of sync.
                # this can be added if most replicas are in-memory
                # self.db.read_preference = ReadPreference.SECONDARY_PREFERRED
            else:
                from pymongo import MongoClient
                self.mongo_host = mongo_host
                self.mongo_replica_set = mongo_replica_set
                self.mongo_db_name = mongo_db_name
                self.mongo_client = MongoClient(self.mongo_host)
                self.db = self.mongo_client[self.mongo_db_name]
            # If these tables do not exist, it might create problems
            if not self.db['user_ratings'].find_one():
                self.db['user_ratings'].insert({})
            if not self.db['item_ratings'].find_one():
                self.db['item_ratings'].insert({})
    def test_lazy_auth_raises_operation_failure(self):
        lazy_client = MongoReplicaSetClient(
            "mongodb://*****:*****@%s/pymongo_test" % pair,
            replicaSet=self.name,
            _connect=False)

        assertRaisesExactly(OperationFailure,
                            lazy_client.test.collection.find_one)
    def test_init_disconnected_with_auth_failure(self):
        c = MongoReplicaSetClient(
            "mongodb://*****:*****@somedomainthatdoesntexist",
            replicaSet="rs",
            connectTimeoutMS=1,
            _connect=False)

        self.assertRaises(ConnectionFailure, c.pymongo_test.test.find_one)
    def test_uri_options(self):
        # Test default to admin
        client = MongoClient('mongodb://*****:*****@%s:%d' % (host, port))
        self.assertTrue(client.admin.command('dbstats'))

        if self.set_name:
            uri = ('mongodb://*****:*****@%s:%d/?replicaSet=%s' % (host, port, self.set_name))
            client = MongoReplicaSetClient(uri)
            self.assertTrue(client.admin.command('dbstats'))
            client.read_preference = ReadPreference.SECONDARY
            self.assertTrue(client.admin.command('dbstats'))

        # Test explicit database
        uri = 'mongodb://*****:*****@%s:%d/pymongo_test' % (host, port)
        client = MongoClient(uri)
        self.assertRaises(OperationFailure, client.admin.command, 'dbstats')
        self.assertTrue(client.pymongo_test.command('dbstats'))

        if self.set_name:
            uri = ('mongodb://*****:*****@%s:%d'
                   '/pymongo_test?replicaSet=%s' % (host, port, self.set_name))
            client = MongoReplicaSetClient(uri)
            self.assertRaises(OperationFailure, client.admin.command,
                              'dbstats')
            self.assertTrue(client.pymongo_test.command('dbstats'))
            client.read_preference = ReadPreference.SECONDARY
            self.assertTrue(client.pymongo_test.command('dbstats'))

        # Test authSource
        uri = ('mongodb://*****:*****@%s:%d'
               '/pymongo_test2?authSource=pymongo_test' % (host, port))
        client = MongoClient(uri)
        self.assertRaises(OperationFailure, client.pymongo_test2.command,
                          'dbstats')
        self.assertTrue(client.pymongo_test.command('dbstats'))

        if self.set_name:
            uri = ('mongodb://*****:*****@%s:%d/pymongo_test2?replicaSet='
                   '%s;authSource=pymongo_test' % (host, port, self.set_name))
            client = MongoReplicaSetClient(uri)
            self.assertRaises(OperationFailure, client.pymongo_test2.command,
                              'dbstats')
            self.assertTrue(client.pymongo_test.command('dbstats'))
            client.read_preference = ReadPreference.SECONDARY
            self.assertTrue(client.pymongo_test.command('dbstats'))
Exemple #28
0
def get_mongo_db(host_or_uri, db_name, replica_set=None):
    if replica_set:
        c = MongoReplicaSetClient(host_or_uri,
                                  replicaSet=replica_set,
                                  read_preference=ReadPreference.NEAREST)
    else:
        c = MongoClient(host_or_uri, read_preference=ReadPreference.NEAREST)
    return c[db_name]
Exemple #29
0
def makeDBConnection():
    global _mongo_C

    logging.info("attempting to establish mongo db connection on port %s ..." %
                 _mongo_port)
    logging.info("using pymongo version %s" % pymongo.version)
    try:
        if pymongo.version_tuple[0] >= 3 or _mongo_kwargs.get(
                "replicaset", None) is None:
            _mongo_C = MongoClient(port=_mongo_port, **_mongo_kwargs)
        else:
            _mongo_C = MongoReplicaSetClient(port=_mongo_port, **_mongo_kwargs)
        mongo_info = _mongo_C.server_info()
        logging.info("mongodb version: %s" % mongo_info["version"])
        logging.info("_mongo_C = %s", (_mongo_C, ))
        #the reads are not necessarily from host/address
        #those depend on the cursor, and can be checked with cursor.conn_id or cursor.address
        if pymongo.version_tuple[0] >= 3:
            logging.info("_mongo_C.address = %s" % (_mongo_C.address, ))
        else:
            logging.info("_mongo_C.host = %s" % (_mongo_C.host, ))

        logging.info("_mongo_C.nodes = %s" % (_mongo_C.nodes, ))
        logging.info("_mongo_C.read_preference = %s" %
                     (_mongo_C.read_preference, ))

        try:
            _mongo_C["admin"].authenticate(_mongo_user, _mongo_pass)
            if _mongo_user == "webserver":
                logging.info(
                    "authentication: partial read-write access enabled")
        except pymongo.errors.PyMongoError as err:
            logging.error("authentication: FAILED -- aborting")
            raise err
        #read something from the db
        #and check from where was it read
        if pymongo.version_tuple[0] >= 3:
            cursor = _mongo_C.knowledge.knowls.find({}, {
                '_id': True
            }).limit(-1)
            list(cursor)
            logging.info("MongoClient connection is reading from: %s" %
                         (cursor.address, ))
        elif _mongo_kwargs.get("replicaset", None) is not None:
            cursor = _mongo_C.knowledge.knowls.find({}, {
                '_id': True
            }).limit(-1)
            list(cursor)
            logging.info(
                "MongoReplicaSetClient connection is reading from: %s" %
                (cursor.conn_id, ))
        else:
            logging.info("MongoClient connection is reading from: %s" %
                         (_mongo_C.host, ))
    except Exception as err:
        logging.info("connection attempt failed: %s", err)
        _mongo_C = None
        return
Exemple #30
0
def __init_connection():
    url = settings.mongo.MONGODB_URL

    if settings.mongo.MONGODB_REPLICA_SET:
        client = MongoReplicaSetClient(url, replicaSet=settings.mongo.MONGODB_REPLICA_SET)
    else:
        client = MongoClient(url)

    return client[settings.mongo.MONGODB_DATABASE]