API¶
Activates caching of metadata.
Parameters: timeout (int) – Number of seconds to cache metadata for. Caching is generally a good thing. However, during a migration there will be a pause equal to whatever the caching timeout. This is to avoid stale reads and writes when the source of truth for a shard changes location.
Connects to the controlling database. This contains information about the realms, shards and clusters.
Parameters: - uri (str) – The Mongo URI to connect to. This should typically detail several replica members to ensure connectivity.
- db_name (str) – The name of the database to connect to on the given replica set.
Migrates the data with the given shard key in the given collection to the new location. E.g.
>>> do_migration('some_collection', 1, 'cluster-1/some_db')
Would migrate everything from some_collection where the shard field is set to 1 to the database some_db on cluster-1.
Parameters: - collection_name (str) – The name of the collection to migrate
- shard_key – The key of the shard that is to be moved
- new_location (str) – Location that the shard should be moved to in the format “cluster/database”.
This method blocks until the migration is completed.
Ensures that a cluster with the given name exists. If it doesn’t exist, a new cluster definition will be created using name and uri. If it does exist then no changes will be made.
Parameters: - name (str) – The name of the cluster
- uri (str) – The URI to use for the cluster
Ensures that a realm of the given name exists and matches the expected settings.
Parameters: - name (str) – The name of the realm
- shard_field – The field in documents that should be used as the shard field. The only supported values that can go in this field are strings and integers.
- collection_name (str) – The name of the collection that this realm corresponds to. In general, the collection name should match the realm name.
- default_dest (str) – The default destination for any data that isn’t explicitly sharded to a specific location.
Returns: None
Returns a new object that proxies the given collection and makes it shard aware.
Marks a shard as being at rest in the given location. This is used for initiating shards in preparation for migration. Unless force is True this will raise an exception if a shard is already at rest in a specific location.
Parameters: - realm (str) – The name of the realm for the shard
- shard_key – The key of the shard
- location (str) – The location that the data is at (or should be in the case of a brand new shard)
- force (bool) – Force a shard to be placed at rest in a specific location even if it has already been placed somewhere.
Returns: None
Returns a string of the form cluster/database that says where a particular shard of data resides.
Parameters: - collection_name – The collection name for the shard
- shard_key – The shard key to look for
Wipes all metadata. Should only be used during testing. There is no undo.
Wipes caches as well.