Lab: Shard a Collection need hints

I have used following commands for Shard a Collection lab:

show dbs
sh.enableSharding(“m103”)

use m103
db.products.count()
db.products.find().limit(1).pretty()

db.products.createIndex({ “sku”: “hashed” })
sh.shardCollection(“m103.products”, { “sku”: “hashed” })

sh.status()

However, got an error:
2 total, 1 passed, 0 skipped:
[PASS] “The dataset is imported to m103.products”
[FAIL] “m103.products uses the correct shard key”

Did you shard the 'm103.products' collection using the correct shard key?

HINT: There are 2 document fields which are correct shard keys!
2 Likes

I do not think that you want a hashed shard key. The sku is not a monotonically increasing value.

1 Like

Hi @Baris_98176,

I hope you found @steevej-1495’s response helpful.

Let us know if you are still having any issues.

~ Shubham

What are the two correct fields, I feel like I’ve tried every possible permutation, sku and name (high card and low freq), or am I doing something foolish in the index creatio:

mongoimport --port 26000 -u “m103-admin” -p “m103-pass” --authenticationDatabase “admin” --db=“m103” /dataset/products.json
mongo --port 26000 -u “m103-admin” -p “m103-pass” --authenticationDatabase “admin”
use m103
sh.enableSharding(“m103”)

db.products.createIndex( { “sku” : 1 } )
db.products.createIndex( { “name” : 1 } )
sh.shardCollection(“m103.products”, { “sku” : 1, “name”: 1 })

2 Likes

You do not have an index { sku : 1 , name : 1 } so you cannot use it to shard the collection.

Thanks - that solved it.
Previously I had the indexes but separately, and not as a compound index.

i am not sure what is wrong here
sh.enableSharding(“m103”)
db.products.createIndex({"_id":hashed})
sh.shardCollection(“m103.products”,{"_id":hashed})

still getting same error
Did you shard the ‘m103.products’ collection using the correct shard key?
HINT: There are 2 document fields which are correct shard keys!

Hi @Soumyashree_M.S,

According to the problem statement, you have to create the index on the good shard key, i.e., name and sku (in this case)

So, it will be db.products.createIndex({ "name": 1 })
followed by sharding using sh.shardCollection("m103.products",{name: 1})

Similarly, you have to do it with the second shard key i.e., sku.

Hope it helps!!

In case of any further questions, please feel free to reach out.

:slightly_smiling_face:
Thanks
Kushagra

1 Like

Did you issue the command use m103 before creating the index?

If not, then the index has not been created in the appropriate database.

1 Like

I tried both still getting the error and also i tried to implement the solution provided in the course that also does work for me.
here is my code snippet.
mongod --port 26000
mongoimport /dataset/products.json --port 26000 -u m103-admin -p m103-pass --authenticationDatabase admin -d m103 --collection products
mongos --configdb csrs/localhost:27004 --logpath “/var/mongodb/logs/mongos.log” --fork
use m103
sh.enableSharding(“m103”)
db.products.createIndex({“sku”:1})
sh.shardCollection(“m103.products”,{sku: 1})
db.products.createIndex({“name”:1})
sh.shardCollection(“m103.products”,{name: 1})

image

A few things are wrong.

You are not supposed to start mongod. The lab states that:

Two shards have already been added to your cluster, shard1 and shard2

I am really surprised that you did not get an error when you ran this command since mongos is already listening at that port.

You are not supposed to start mongos as it is already running with the lab supplied configuration file.

use mongos as the target for mongoimport - you can find the configuration details for this process in mongos.conf

I would be interested to see a screenshot of the terminal where you entered your commands.

1 Like

Hi Steeve,

Here it it:



user@M103# mongod --port 26000
2021-06-23T15:10:44.088+0000 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2021-06-23T15:10:44.095+0000 I CONTROL  [initandlisten] MongoDB starting : pid=1190 port=26000 dbpath=/data/db 64-bit host=AeYWKTKMVgXw153oUM
2021-06-23T15:10:44.095+0000 I CONTROL  [initandlisten] db version v4.0.5
2021-06-23T15:10:44.095+0000 I CONTROL  [initandlisten] git version: 3739429dd92b92d1b0ab120911a23d50bf03c412
2021-06-23T15:10:44.095+0000 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.1.1g  21 Apr 2020
2021-06-23T15:10:44.096+0000 I CONTROL  [initandlisten] allocator: system
2021-06-23T15:10:44.096+0000 I CONTROL  [initandlisten] modules: none
2021-06-23T15:10:44.096+0000 I CONTROL  [initandlisten] build environment:
2021-06-23T15:10:44.096+0000 I CONTROL  [initandlisten]     distarch: x86_64
2021-06-23T15:10:44.096+0000 I CONTROL  [initandlisten]     target_arch: x86_64
2021-06-23T15:10:44.096+0000 I CONTROL  [initandlisten] options: { net: { port: 26000 } }
2021-06-23T15:10:44.096+0000 E STORAGE  [initandlisten] Failed to set up listener: SocketException: Address in use
2021-06-23T15:10:44.096+0000 I CONTROL  [initandlisten] now exiting
2021-06-23T15:10:44.096+0000 I CONTROL  [initandlisten] shutting down with code:48
user@M103# mongoimport /dataset/products.json --port 26000 -u m103-admin -p m103-pass --authenticationDatabase admin -d m103 --collection products
2021-06-23T15:11:02.945+0000    connected to: mongodb://localhost:26000/
2021-06-23T15:11:05.273+0000    9966 document(s) imported successfully. 0 document(s) failed to import.
user@M103# mongos --configdb csrs/localhost:27004 --logpath "/var/mongodb/logs/mongos.log" --fork
2021-06-23T15:11:14.315+0000 W SHARDING [main] Running a sharded cluster with fewer than 3 config servers should only be done for testing purposes and is not recommended for production.
about to fork child process, waiting until server is ready for connections.
forked process: 1235
use m103
sh.enableSharding("m103")
db.products.createIndex({"sku":1}) 
sh.shardCollection("m103.products",{sku: 1})
db.products.createIndex({"name":1})
sh.shardCollection("m103.products",{name: 1})
sh.status()


Result:



2 total, 1 passed, 0 skipped:
[PASS] "The dataset is imported to m103.products"
[FAIL] "m103.products uses the correct shard key"

Did you shard the 'm103.products' collection using the correct shard key?

HINT: There are 2 document fields which are correct shard keys!

Your first command did not succeed.It failed/exited with socket error “address in use”
As
steevej
Steeve Juneau mentioned you have another mongod/mongos already running on the same port 26000

Your second command to start mongos was not supplied port so it would have started on default port 27017
You can check your mongos logs
Can you show the screenshot from the point you connected to your DB and ran those commands starting with use m103

Most likely you connected to wrong instance
That’s why your validation is failing

1 Like

The output you shown:

use m103
sh.enableSharding("m103")
db.products.createIndex({"sku":1}) 
sh.shardCollection("m103.products",{sku: 1})
db.products.createIndex({"name":1})
sh.shardCollection("m103.products",{name: 1})
sh.status()

and its lack of prompt indicates that you were not connected to mongos when you entered the commands.

2 Likes

Ok got it! Thank you so much !!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.