From c73ba3949757372c3b52e37f36eca3e565d58dbf Mon Sep 17 00:00:00 2001 From: Monet Lee Date: Fri, 20 Jun 2025 17:20:00 +0800 Subject: [PATCH 1/2] feat: support mongo replicaset mode. --- config/mongodb.yml | 39 +++++++++++++++++++++++-- go.mod | 2 +- go.sum | 4 +-- pkg/common/config/config.go | 57 +++++++++++++++++++++++++++++++------ 4 files changed, 89 insertions(+), 13 deletions(-) diff --git a/config/mongodb.yml b/config/mongodb.yml index 072cb4b8f..519645128 100644 --- a/config/mongodb.yml +++ b/config/mongodb.yml @@ -1,7 +1,7 @@ # URI for database connection, leave empty if using address and credential settings directly -uri: +uri: # List of MongoDB server addresses -address: [ localhost:37017 ] +address: [localhost:37017] # Name of the database database: openim_v3 # Username for database authentication @@ -14,3 +14,38 @@ authSource: openim_v3 maxPoolSize: 100 # Maximum number of retry attempts for a failed database connection maxRetry: 10 +# MongoDB Mode, including "standalone", "replicaSet" +mongoMode: "replicaSet" + +# The following configurations only take effect when mongoMode is set to "replicaSet" +replicaSet: + name: rs0 + hosts: [192.168.1.217:37011, 192.168.1.217:370180, 192.168.1.217:37019] + # Read concern level: "local", "available", "majority", "linearizable", "snapshot" + readConcern: majority + # maximum staleness of data in seconds + maxStaleness: 90s + +# The following configurations only take effect when mongoMode is set to "replicaSet" +readPreference: + # Read preference mode, can be "primary", "primaryPreferred", "secondary", "secondaryPreferred", "nearest" + mode: primary + maxStaleness: 90s + # TagSets is an array of maps with priority based on order, empty map must be placed last for fallback tagSets + tagSets: + - datacenter: "cn-east" + rack: "1" + storage: "ssd" + - datacenter: "cn-east" + storage: "ssd" + - datacenter: "cn-east" + - {} # Empty map, indicates any node + +# The following configurations only take effect when mongoMode is set to "replicaSet" +writeConcern: + # Write node count or tag (int, "majority", or custom tag) + w: majority + # Whether to wait for journal confirmation + j: true + # Write timeout duration + wtimeout: 30s diff --git a/go.mod b/go.mod index 2a05b14b6..2c9d32009 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 github.com/mitchellh/mapstructure v1.5.0 github.com/openimsdk/protocol v0.0.73-alpha.12 - github.com/openimsdk/tools v0.0.50-alpha.92 + github.com/openimsdk/tools v0.0.50-alpha.96 github.com/pkg/errors v0.9.1 // indirect github.com/prometheus/client_golang v1.18.0 github.com/stretchr/testify v1.9.0 diff --git a/go.sum b/go.sum index db68ca960..5d6f18b40 100644 --- a/go.sum +++ b/go.sum @@ -349,8 +349,8 @@ github.com/openimsdk/gomake v0.0.15-alpha.5 h1:eEZCEHm+NsmcO3onXZPIUbGFCYPYbsX5b github.com/openimsdk/gomake v0.0.15-alpha.5/go.mod h1:PndCozNc2IsQIciyn9mvEblYWZwJmAI+06z94EY+csI= github.com/openimsdk/protocol v0.0.73-alpha.12 h1:2NYawXeHChYUeSme6QJ9pOLh+Empce2WmwEtbP4JvKk= github.com/openimsdk/protocol v0.0.73-alpha.12/go.mod h1:WF7EuE55vQvpyUAzDXcqg+B+446xQyEba0X35lTINmw= -github.com/openimsdk/tools v0.0.50-alpha.92 h1:hWfykMhmi7EQEiwgQccJqbgggIuhun/PrVkBnjmj9Ec= -github.com/openimsdk/tools v0.0.50-alpha.92/go.mod h1:n2poR3asX1e1XZce4O+MOWAp+X02QJRFvhcLCXZdzRo= +github.com/openimsdk/tools v0.0.50-alpha.96 h1:U44Fq2jHiEvGi9zuYAnTRNx3Xd9T7P/kBAZLHvQ8xg4= +github.com/openimsdk/tools v0.0.50-alpha.96/go.mod h1:n2poR3asX1e1XZce4O+MOWAp+X02QJRFvhcLCXZdzRo= github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= github.com/pierrec/lz4/v4 v4.1.21 h1:yOVMLb6qSIDP67pl/5F7RepeKYu/VmTyEXvuMI5d9mQ= diff --git a/pkg/common/config/config.go b/pkg/common/config/config.go index 619571064..4ff4052e3 100644 --- a/pkg/common/config/config.go +++ b/pkg/common/config/config.go @@ -71,15 +71,39 @@ type Minio struct { } type Mongo struct { - URI string `yaml:"uri"` - Address []string `yaml:"address"` - Database string `yaml:"database"` - Username string `yaml:"username"` - Password string `yaml:"password"` - AuthSource string `yaml:"authSource"` - MaxPoolSize int `yaml:"maxPoolSize"` - MaxRetry int `yaml:"maxRetry"` + URI string `yaml:"uri"` + Address []string `yaml:"address"` + Database string `yaml:"database"` + Username string `yaml:"username"` + Password string `yaml:"password"` + AuthSource string `yaml:"authSource"` + MaxPoolSize int `yaml:"maxPoolSize"` + MaxRetry int `yaml:"maxRetry"` + MongoMode string `yaml:"mongoMode"` + ReplicaSet ReplicaSetConfig + ReadPreference ReadPrefConfig + WriteConcern WriteConcernConfig } + +type ReplicaSetConfig struct { + Name string `yaml:"name"` + Hosts []string `yaml:"hosts"` + ReadConcern string `yaml:"readConcern"` + MaxStaleness time.Duration `yaml:"maxStaleness"` +} + +type ReadPrefConfig struct { + Mode string `yaml:"mode"` + TagSets []map[string]string `yaml:"tagSets"` + MaxStaleness time.Duration `yaml:"maxStaleness"` +} + +type WriteConcernConfig struct { + W any `yaml:"w"` + J bool `yaml:"j"` + WTimeout time.Duration `yaml:"wtimeout"` +} + type Kafka struct { Username string `yaml:"username"` Password string `yaml:"password"` @@ -490,6 +514,23 @@ func (m *Mongo) Build() *mongoutil.Config { AuthSource: m.AuthSource, MaxPoolSize: m.MaxPoolSize, MaxRetry: m.MaxRetry, + MongoMode: m.MongoMode, + ReplicaSet: &mongoutil.ReplicaSetConfig{ + Name: m.ReplicaSet.Name, + Hosts: m.ReplicaSet.Hosts, + ReadConcern: m.ReplicaSet.ReadConcern, + MaxStaleness: m.ReplicaSet.MaxStaleness, + }, + ReadPreference: &mongoutil.ReadPrefConfig{ + Mode: m.ReadPreference.Mode, + TagSets: m.ReadPreference.TagSets, + MaxStaleness: m.ReadPreference.MaxStaleness, + }, + WriteConcern: &mongoutil.WriteConcernConfig{ + W: m.WriteConcern.W, + J: m.WriteConcern.J, + WTimeout: m.WriteConcern.WTimeout, + }, } } From e2c1bb0eeea43a036d0af05c38313ad01919767d Mon Sep 17 00:00:00 2001 From: Monet Lee Date: Fri, 20 Jun 2025 17:33:27 +0800 Subject: [PATCH 2/2] fix mongo config. --- config/mongodb.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/mongodb.yml b/config/mongodb.yml index 519645128..ca45fea6f 100644 --- a/config/mongodb.yml +++ b/config/mongodb.yml @@ -15,12 +15,12 @@ maxPoolSize: 100 # Maximum number of retry attempts for a failed database connection maxRetry: 10 # MongoDB Mode, including "standalone", "replicaSet" -mongoMode: "replicaSet" +mongoMode: "standalone" # The following configurations only take effect when mongoMode is set to "replicaSet" replicaSet: name: rs0 - hosts: [192.168.1.217:37011, 192.168.1.217:370180, 192.168.1.217:37019] + hosts: [127.0.0.1:37017, 127.0.0.1:37018, 127.0.0.1:37019] # Read concern level: "local", "available", "majority", "linearizable", "snapshot" readConcern: majority # maximum staleness of data in seconds