MongoDB
 sql >> डेटाबेस >  >> NoSQL >> MongoDB

MongoDb क्षेत्रीय प्रतिकृति सेट - प्रत्येक क्षेत्र में प्राथमिक नोड?

धन्यवाद @avanti, @MarkusWMalhberg - टिप्पणियों का जवाब देने के तरीके पर विचार करने से मुझे सही दिशा में धक्का लगा। इसे एक साथ खींचने में थोड़ा सा समय लगा, इसलिए मैं कॉन्फ़िगरेशन को समझाते हुए थोड़ा वर्बोज़ बनूंगा।

अवलोकन

उपयोगकर्ता अनुभव पर ध्यान केंद्रित करते हुए, हम एक Mongo डेटाबेस कॉन्फ़िगरेशन बनाना चाहते हैं जो उपयोगकर्ता के सबसे करीब पढ़ने और लिखने की अनुमति देता है।

धारणाएं

  • उपयोगकर्ता लगभग हमेशा अपने क्षेत्र में दस्तावेज़ पढ़ते और लिखते हैं और अगर दूसरे क्षेत्र के डेटा को कम पढ़ने की गति धीमी है तो कोई आपत्ति नहीं है।
  • प्रत्येक दस्तावेज़ में एक कुंजी होती है जो उनके क्षेत्र को इंगित करती है (सादगी/स्पष्टता के लिए)

अधिकांश शार्डिंग दस्तावेज़ HA/DR पर केंद्रित हैं। उपयोगकर्ता अनुभव और क्षेत्रीय अनुपालन के साथ लोड वितरण के बजाय स्थानीयता पर ध्यान केंद्रित किया जाता है।

यह उदाहरण HA/DR को पूरी तरह से अनदेखा कर देगा, वरीयताओं को पढ़ेगा, और चिंताओं को लिखेगा लेकिन POC के परिपक्व होने पर इन पर ध्यान देने की आवश्यकता है। लक्ष्य को पूरा करने में स्पष्टता के पक्ष में उदाहरण इन पर ध्यान नहीं देता:स्थानीय पढ़ता/लिखता है।

संदर्भ

ट्रिक

हम जानते हैं

  • हम एक एप्लिकेशन डेटाबेस चाहते हैं ताकि सभी डेटा उपलब्ध हो
  • हम चाहते हैं कि उपयोगकर्ता स्थानीय रूप से पढ़ें/लिखें, इसलिए हमें प्रत्येक उपयोगकर्ता समूह के पास एक डेटाबेस की आवश्यकता है; हमें एक प्रतिकृति सेट की आवश्यकता है
  • लेखन केवल प्राथमिक प्रतिकृति सेट नोड्स के लिए किया जा सकता है, इसलिए प्रत्येक उपयोगकर्ता समूह के बगल में प्राथमिक नोड प्राप्त करने के लिए, हमें कई प्रतिकृति की आवश्यकता होती है; एक विभाजित क्लस्टर

मानक रेप्लिकासेट और साझाकरण ज्ञान के भीतर, इस कॉन्फ़िगरेशन की 2 कुंजियाँ हैं:

  • प्राथमिकता सुनिश्चित करने के लिए क्षेत्रीय स्थानीय रेप्लिकासेट नोड को प्राथमिकता दें।
  • लोकेशन अवेयर शार्ड की टैगिंग का उपयोग करके सुनिश्चित करें कि डेटा स्थानीय शार्क को लिखा गया है

शार्ड कुंजियाँ कुछ भी हो सकती हैं:हम केवल उपयोगकर्ताओं के प्रभावी लोड साझाकरण के विपरीत स्थानीय रूप से पढ़ने/लिखने में सक्षम होने से चिंतित हैं।

प्रत्येक संग्रह को शार्प करना होगा, या राइट्स शार्प जीरो पर जाएगा।

वांछित कॉन्फ़िगरेशन

कॉन्फ़िगरेशन

#!/usr/bin/env bash

echo ">>> Clean up processes and files from previous runs"
echo ">>> killAll mongod mongos"
killall mongod mongos

echo ">>> Remove db files and logs"
rm -rf data
rm -rf log

# Create the common log directory
mkdir log

echo ">>> Start replica set for shard US-East"
mkdir -p data/shard-US-East/rsMemberEast data/shard-US-East/rsMemberWest
mongod --replSet shard-US-East --logpath "log/shard-US-East-rsMemberEast.log" --dbpath data/shard-US-East/rsMemberEast --port 37017 --fork --shardsvr --smallfiles
mongod --replSet shard-US-East --logpath "log/shard-US-East-rsMemberWest.log" --dbpath data/shard-US-East/rsMemberWest --port 37018 --fork --shardsvr --smallfiles

echo ">>> Sleep 15s to allow US-East replica set to start"
sleep 15

# The US-East replica set member is assigned priority 2 so that it becomes primary
echo ">>> Configure replica set for shard US-East"
mongo --port 37017 << 'EOF'
config = { _id: "shard-US-East", members:[
         { _id : 0, host : "localhost:37017", priority: 2 },
         { _id : 1, host : "localhost:37018" }]};
rs.initiate(config)
EOF

echo ">>> Start replica set for shard-US-West"
mkdir -p data/shard-US-West/rsMemberEast data/shard-US-West/rsMemberWest
mongod --replSet shard-US-West --logpath "log/shard-US-West-rsMemberEast.log" --dbpath data/shard-US-West/rsMemberEast --port 47017 --fork --shardsvr --smallfiles
mongod --replSet shard-US-West --logpath "log/shard-US-West-rsMemberWest.log" --dbpath data/shard-US-West/rsMemberWest --port 47018 --fork --shardsvr --smallfiles

echo ">>> Sleep 15s to allow US-West replica set to start"
sleep 15

# The US-West replica set member is assigned priority 2 so that it becomes primary
echo ">>> Configure replica set for shard-US-West"
mongo --port 47017 << 'EOF'
config = { _id: "shard-US-West", members:[
         { _id : 0, host : "localhost:47017" },
         { _id : 1, host : "localhost:47018", priority: 2 }]};
rs.initiate(config)
EOF

# Shard config servers: should be 3 and all must be up to deploy a shard cluster
# These are the mongos backing store for routing information
echo ">>> Start config servers"
mkdir -p data/config/config-us-east data/config/config-us-west data/config/config-redundant
mongod --logpath "log/cfg-us-east.log"   --dbpath data/config/config-us-east   --port 57040 --fork --configsvr --smallfiles
mongod --logpath "log/cfg-us-west.log"   --dbpath data/config/config-us-west   --port 57041 --fork --configsvr --smallfiles
mongod --logpath "log/cfg-redundant.log" --dbpath data/config/config-redundant --port 57042 --fork --configsvr --smallfiles

echo ">>> Sleep 5 to allow config servers to start and stabilize"
sleep 5

# All mongos's must point at the same config server, a coordinator dispatches writes to each
echo ">>> Start mongos"
mongos --logpath "log/mongos-us-east.log" --configdb localhost:57040,localhost:57041,localhost:57042 --port 27017 --fork
mongos --logpath "log/mongos-us-west.log" --configdb localhost:57040,localhost:57041,localhost:57042 --port 27018 --fork

echo ">>> Wait 60 seconds for the replica sets to stabilize"
sleep 60

# Enable sharding on the 'sales' database and 'sales.users' collection
# Every collection in 'sales' must be sharded or the writes will go to shard 0
# Add a shard tag so we can associate shard keys with the tag (region)
# Shard tag range main and max cannot be the same so we use a region id for US-East = 1
# and US-West = 2. sh.addTagRange() is inclusive of minKey and exclusive of maxKey.
# We only need to configure one mongos - config will be propogated to all mongos through
# the config server
echo ">>> Add shards to mongos"
mongo --port 27017 <<'EOF'
db.adminCommand( { addshard : "shard-US-East/"+"localhost:37017" } );
db.adminCommand( { addshard : "shard-US-West/"+"localhost:47017" } );

db.adminCommand({enableSharding: "sales"})
db.adminCommand({shardCollection: "sales.users", key: {region:1}});

sh.addShardTag("shard-US-East", "US-East")
sh.addShardTag("shard-US-West", "US-West")
sh.addTagRange("sales.users", { region: 1 }, { region: 2 }, "US-East")
sh.addTagRange("sales.users", { region: 2 }, { region: 3 }, "US-West")
EOF

परीक्षण

सत्यापित करें कि हमारा कॉन्फ़िगरेशन sh.status() . के साथ सही है . नोट शार्क को सही ढंग से असाइन किया गया है और टैग, और क्षेत्रीय शार्ड कुंजियों को सही ढंग से असाइन किया गया है।

[[email protected] RegionalSharding 14:38:50]$ mongo --port 27017 sales
...
rakshasa(mongos-3.0.5)[mongos] sales> sh.status()
  sharding version: {
    "_id": 1,
    "minCompatibleVersion": 5,
    "currentVersion": 6,
    "clusterId": ObjectId("55fdddc5746e30dc3651cda4")
  }
  shards:
    {  "_id": "shard-US-East",  "host": "shard-US-East/localhost:37017,localhost:37018",  "tags": [   "US-East" ] }
    {  "_id": "shard-US-West",  "host": "shard-US-West/localhost:47017,localhost:47018",  "tags": [   "US-West" ] }
  balancer:
    Currently enabled:  yes
    Currently running:  no
    Failed balancer rounds in last 5 attempts:  0
    Migration Results for the last 24 hours: 
        1 : Success
  databases:
    {  "_id": "admin",  "partitioned": false,  "primary": "config" }
    {  "_id": "test",  "partitioned": false,  "primary": "shard-US-East" }
    {  "_id": "sales",  "partitioned": true,  "primary": "shard-US-East" }
    sales.users
      shard key: { "region": 1 }
      chunks:
        shard-US-East: 2
        shard-US-West: 1
        { "region": { "$minKey" : 1 } } -> { "region": 1 } on: shard-US-East Timestamp(2, 1) 
        { "region": 1 } -> { "region": 2 } on: shard-US-East Timestamp(1, 3) 
        { "region": 2 } -> { "region": { "$maxKey" : 1 } } on: shard-US-West Timestamp(2, 0) 
        tag: US-East  {
  "region": 1
} -> {
  "region": 2
}
        tag: US-West  {
  "region": 2
} -> {
  "region": 3
}

सत्यापित करें कि राइट शार्प और प्राइमरी पर लिखे गए हैं। प्रत्येक क्षेत्र में एक रिकॉर्ड बनाएं

db.users.insert({region:1, name:"us east user"})
db.users.insert({region:2, name:"us west user"})

आप प्रत्येक प्रतिकृति सेट के प्रत्येक सदस्य पर लॉग ऑन कर सकते हैं और पूर्व उपयोगकर्ता को केवल यूएस-ईस्ट शार्क पर और पश्चिमी उपयोगकर्ता को केवल यूएस-वेस्ट शार्क पर देख सकते हैं।



  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. कुल $लुकअप तत्वों के मूल सरणी क्रम को वापस नहीं करता है

  2. मोंगोडब-नोडज ड्राइवर में दायर किए गए सभी का योग प्रदर्शित करना

  3. नेवला/मोंगोडब क्वेरी एकाधिक प्रकार

  4. Mongodb में, मैं कैसे जांच सकता हूं कि सभी दस्तावेज़ एक मूल्य के लिए अद्वितीय हैं या नहीं?

  5. मोंगोडीबी $ मिलीसेकंड