alibaba / MongoShake

MongoShake is a universal data replication platform based on MongoDB's oplog. Redundant replication and active-active replication are two most important functions. 基于mongodb oplog的集群复制工具,可以满足迁移和同步的需求,进一步实现灾备和多活功能。
GNU General Public License v3.0
1.72k stars 442 forks source link

MongoDB集群同步到MongoDB集群,目标端MongoS 查不到数据,数据写入了目标端集群的分片 #815

Open syd2488951516 opened 1 year ago

syd2488951516 commented 1 year ago

使用mongoshake同步后,mongos 查不到部分库下的集合。同步日志无报错

源端数据: syd 、syn、syx 均未分片

mongos> show dbs;
admin   0.000GB
config  0.002GB
syd     0.000GB
syn     0.000GB
syx     0.000GB
ycsb    0.124GB
mongos> use syd
switched to db syd
mongos> show tables;
mycol
mongos> db.mycol.find()
{ "_id" : ObjectId("6445f5de318749d599382eb8"), "name" : "mycol" }
mongos> use syn
switched to db syn
mongos> show tables;
mycol
mycol2
mongos> db.mycol.find()
{ "_id" : ObjectId("64536a7d08c5bb55a7bfc103"), "name" : "mycol" }
mongos> db.mycol2.find()
{ "_id" : ObjectId("64536a8108c5bb55a7bfc104"), "name" : "mycol2" }
mongos> use syx
switched to db syx
mongos> show tables;
mycol
mycol2
mongos> db.mycol.find()
{ "_id" : ObjectId("643520a999c92a249fc12783"), "name" : "mycol" }
mongos> db.mycol2.find()
{ "_id" : ObjectId("643520af99c92a249fc12784"), "name" : "mycol2" }

源端执行 sh.status():
 active mongoses:
        "3.6.8" : 2
  autosplit:
        Currently enabled: yes
  balancer:
        Currently enabled:  no
        Currently running:  no
        Failed balancer rounds in last 5 attempts:  0
        Migration Results for the last 24 hours: 
                No recent migrations
  databases:
        {  "_id" : "config",  "primary" : "config",  "partitioned" : true }
                config.system.sessions
                        shard key: { "_id" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                08c45835-ceda-41a6-b36e-5f68654a36ab    1
                        { "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : 08c45835-ceda-41a6-b36e-5f68654a36ab Timestamp(1, 0) 
        {  "_id" : "syd",  "primary" : "08c45835-ceda-41a6-b36e-5f68654a36ab",  "partitioned" : false }
        {  "_id" : "syn",  "primary" : "08c45835-ceda-41a6-b36e-5f68654a36ab",  "partitioned" : false }
        {  "_id" : "syx",  "primary" : "08c45835-ceda-41a6-b36e-5f68654a36ab",  "partitioned" : false }
        {  "_id" : "ycsb",  "primary" : "08c45835-ceda-41a6-b36e-5f68654a36ab",  "partitioned" : true }
                ycsb.usertable
                        shard key: { "field1" : "hashed" }
                        unique: false
                        balancing: true
                        chunks:
                                08c45835-ceda-41a6-b36e-5f68654a36ab    2
                                b45c54c3-6a9f-4f36-862d-b1ab53e85886    2
                        { "field1" : { "$minKey" : 1 } } -->> { "field1" : NumberLong("-4611686018427387902") } on : 08c45835-ceda-41a6-b36e-5f68654a36ab Timestamp(2, 2) 
                        { "field1" : NumberLong("-4611686018427387902") } -->> { "field1" : NumberLong(0) } on : 08c45835-ceda-41a6-b36e-5f68654a36ab Timestamp(2, 3) 
                        { "field1" : NumberLong(0) } -->> { "field1" : NumberLong("4611686018427387902") } on : b45c54c3-6a9f-4f36-862d-b1ab53e85886 Timestamp(2, 4) 
                        { "field1" : NumberLong("4611686018427387902") } -->> { "field1" : { "$maxKey" : 1 } } on : b45c54c3-6a9f-4f36-862d-b1ab53e85886 Timestamp(2, 5)

同步到目标端的数据:
mongos> show dbs;
admin   0.000GB
config  0.002GB
syd     0.000GB
syn     0.000GB
syx     0.000GB
ycsb    0.089GB
mongos> use syd
switched to db syd
mongos> show tables;
mongos> use syn
switched to db syn
mongos> show tables;
mongos> use syx
switched to db syx
mongos> show tables;
mycol
mycol2
mongos> 

1.mongoshake版本:mongo-shake-v2.8.32 2.mongodb源集群版本:MongoDB server version: 3.6.8 mongodb 目标集群版本:MongoDB server version: 3.6.8 配置文件如下: collector.conf.docx

写了一个测试demo,有和上面同样的问题, 好像是mongo驱动的问题 url为目标端集群的mongos的地址

package main

import (
    "context"
    "fmt"
    "go.mongodb.org/mongo-driver/bson"
    "go.mongodb.org/mongo-driver/mongo"
    "go.mongodb.org/mongo-driver/mongo/options"
    "go.mongodb.org/mongo-driver/mongo/readpref"
    "time"
)

func main(){
    ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)

    url := "mongodb://root:XXX@XXX:27017,XXX:27017/admin"
    client, err := mongo.Connect(ctx, options.Client().ApplyURI(url))
    if err != nil {
        fmt.Println(err)
    }

    err = client.Ping(ctx, readpref.Primary())
    if err != nil {
        fmt.Println(err)
    }

    opts := options.BulkWrite().SetOrdered(false)

    models := []mongo.WriteModel{
        mongo.NewInsertOneModel().SetDocument(bson.D{{"name", "mycol"}}),
    }
    res, err := client.Database("syd").Collection("mycol").BulkWrite(nil,models,opts)
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println("fisrt 1", res)

    models2 := []mongo.WriteModel{
        mongo.NewInsertOneModel().SetDocument(bson.D{{"name", "mycol"}}),
    }
    res, err = client.Database("syx").Collection("mycol").BulkWrite(nil,models2,opts)
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println("fisrt 2", res)

    models3 := []mongo.WriteModel{
        mongo.NewInsertOneModel().SetDocument(bson.D{{"name", "mycol2"}}),
    }
    res, err = client.Database("syx").Collection("mycol2").BulkWrite(nil,models3,opts)
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println("fisrt 3", res)

    cancel()

}
zhangst commented 1 year ago

3.6.8虽然是比较老的mongodb版本,但是正常的写入不应该出问题的。 你的demo依赖的go-deiver是那个版本,写入的时候有报什么错误吗?

zhangst commented 1 year ago

https://www.mongodb.com/docs/drivers/go/current/compatibility/

syd2488951516 commented 1 year ago

同步已经分片的数据没有问题,同步没有分片的集合会出现该问题。 demo依赖的mongo-go-driver-1.9.0,写入的时候无报错, demo写入的数据,mongos只能查到库,看不到库下的集合,但是在分片上可以查到库下的集合,写入到了primary shard ,您可以测一下。

zhangst commented 1 year ago

同步已经分片的数据没有问题,同步没有分片的集合会出现该问题。 demo依赖的mongo-go-driver-1.9.0,写入的时候无报错, demo写入的数据,mongos只能查到库,看不到库下的集合,但是在分片上可以查到库下的集合,写入到了primary shard ,您可以测一下。

直连分片,在分片上可以查到写入的数据吗?

syd2488951516 commented 1 year ago

是的,直连分片可以查到写入的数据

zhangst commented 1 year ago

感觉是MongoDB的问题,跟Shake的Driver没有关系。但建议交叉测试下,2.6.5版本的Shake还是用的老的mgo版本的Driver,辛苦用这个版本测试下。

zuifanchen commented 4 weeks ago

你好 有进展吗