Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions config/crd/bases/psmdb.percona.com_perconaservermongodbs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -760,11 +760,15 @@ spec:
- local
- available
- majority
- linearizable
- snapshot
type: string
writeConcern:
type: string
properties:
w:
type: string
wtimeout:
minimum: 0
type: integer
type: object
type: object
enableExternalVolumeAutoscaling:
type: boolean
Expand Down
10 changes: 7 additions & 3 deletions deploy/bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2878,11 +2878,15 @@ spec:
- local
- available
- majority
- linearizable
- snapshot
type: string
writeConcern:
type: string
properties:
w:
type: string
wtimeout:
minimum: 0
type: integer
type: object
type: object
enableExternalVolumeAutoscaling:
type: boolean
Expand Down
5 changes: 5 additions & 0 deletions deploy/cr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ spec:
apply: disabled
schedule: "0 2 * * *"
setFCV: false
# defaultRWConcern:
# readConcern: majority
# writeConcern:
# w: majority
# wtimeout: 0
Comment on lines +60 to +64
# vault:
# endpointURL: https://vault-service:8200
# tlsSecret: my-tls-vault-secret
Expand Down
10 changes: 7 additions & 3 deletions deploy/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2878,11 +2878,15 @@ spec:
- local
- available
- majority
- linearizable
- snapshot
type: string
writeConcern:
type: string
properties:
w:
type: string
wtimeout:
minimum: 0
type: integer
type: object
type: object
enableExternalVolumeAutoscaling:
type: boolean
Expand Down
10 changes: 7 additions & 3 deletions deploy/cw-bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2878,11 +2878,15 @@ spec:
- local
- available
- majority
- linearizable
- snapshot
type: string
writeConcern:
type: string
properties:
w:
type: string
wtimeout:
minimum: 0
type: integer
type: object
type: object
enableExternalVolumeAutoscaling:
type: boolean
Expand Down
5 changes: 5 additions & 0 deletions e2e-tests/init-deploy/conf/another-name-rs0.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ spec:
tls: true
tls:
mode: disabled
defaultRWConcern:
readConcern: majority
writeConcern:
w: majority
wtimeout: 5000
backup:
enabled: true
image: perconalab/percona-server-mongodb-operator:1.1.0-backup
Expand Down
22 changes: 22 additions & 0 deletions e2e-tests/init-deploy/run
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,28 @@ desc 'check if service and statefulset created with expected config'
compare_kubectl statefulset/$cluster2
compare_kubectl service/$cluster2

desc 'check defaultRWConcern is applied on the cluster'
rwc=$(run_mongo 'JSON.stringify(db.adminCommand({getDefaultRWConcern:1}))' "clusterAdmin:clusterAdmin123456@$cluster2.$namespace" \
| grep -E '^\{.*\}$')
read_concern_level=$(jq -r '.defaultReadConcern.level' <<<"$rwc")
write_concern_w=$(jq -r '.defaultWriteConcern.w' <<<"$rwc")
write_concern_wtimeout=$(jq -r '.defaultWriteConcern.wtimeout' <<<"$rwc")
if [[ $read_concern_level != "majority" ]]; then
echo "error: expected defaultReadConcern.level to be 'majority', got '$read_concern_level'"
echo "getDefaultRWConcern response: $rwc"
exit 1
fi
if [[ $write_concern_w != "majority" ]]; then
echo "error: expected defaultWriteConcern.w to be 'majority', got '$write_concern_w'"
echo "getDefaultRWConcern response: $rwc"
exit 1
fi
if [[ $write_concern_wtimeout != "5000" ]]; then
echo "error: expected defaultWriteConcern.wtimeout to be '5000', got '$write_concern_wtimeout'"
echo "getDefaultRWConcern response: $rwc"
exit 1
fi

desc 'write data, read from all'
run_mongo \
'db.createUser({user:"myApp",pwd:"myPass",roles:[{db:"myApp",role:"readWrite"}]})' \
Expand Down
10 changes: 7 additions & 3 deletions e2e-tests/version-service/conf/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2878,11 +2878,15 @@ spec:
- local
- available
- majority
- linearizable
- snapshot
type: string
writeConcern:
type: string
properties:
w:
type: string
wtimeout:
minimum: 0
type: integer
type: object
type: object
enableExternalVolumeAutoscaling:
type: boolean
Expand Down
12 changes: 9 additions & 3 deletions pkg/apis/psmdb/v1/psmdb_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,15 @@ type PerconaServerMongoDBSpec struct {
}

type DefaultRWConcern struct {
// +kubebuilder:validation:Enum={local,available,majority,linearizable,snapshot}
ReadConcern string `json:"readConcern,omitempty"`
WriteConcern string `json:"writeConcern,omitempty"`
// +kubebuilder:validation:Enum={local,available,majority}
ReadConcern string `json:"readConcern,omitempty"`
WriteConcern *DefaultWriteConcernSpec `json:"writeConcern,omitempty"`
}
Comment on lines 113 to +117

type DefaultWriteConcernSpec struct {
W string `json:"w,omitempty"`
// +kubebuilder:validation:Minimum=0
WTimeout int `json:"wtimeout,omitempty"`
}
Comment thread
Copilot marked this conversation as resolved.

type UserRole struct {
Expand Down
22 changes: 21 additions & 1 deletion pkg/apis/psmdb/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 18 additions & 13 deletions pkg/controller/perconaservermongodb/mgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,22 @@ import (

var errReplsetLimit = fmt.Errorf("maximum replset member (%d) count reached", mongo.MaxMembers)

func defaultRWConcern(cr *api.PerconaServerMongoDB) (string, string) {
readConcern, writeConcern := mongo.DefaultReadConcern, mongo.DefaultWriteConcern
if c := cr.Spec.DefaultRWConcern; c != nil {
if c.ReadConcern != "" {
readConcern = c.ReadConcern
}
if c.WriteConcern != "" {
writeConcern = c.WriteConcern
func defaultRWConcern(cr *api.PerconaServerMongoDB) (string, string, int) {
readConcern, writeConcernW, writeConcernWTimeout := mongo.DefaultReadConcern, mongo.DefaultWriteConcern, 0
c := cr.Spec.DefaultRWConcern
if c == nil {
return readConcern, writeConcernW, writeConcernWTimeout
}
if c.ReadConcern != "" {
readConcern = c.ReadConcern
}
if c.WriteConcern != nil {
if c.WriteConcern.W != "" {
writeConcernW = c.WriteConcern.W
}
writeConcernWTimeout = c.WriteConcern.WTimeout
}
return readConcern, writeConcern
return readConcern, writeConcernW, writeConcernWTimeout
}

// PSA replsets need the explicit push since MongoDB's implicit default is w:1; user-set
Expand Down Expand Up @@ -227,8 +232,8 @@ func (r *ReconcilePerconaServerMongoDB) reconcileCluster(ctx context.Context, cr
}
}()

readConcern, writeConcern := defaultRWConcern(cr)
err = mongosSession.SetDefaultRWConcern(ctx, readConcern, writeConcern)
readConcern, writeConcernW, writeConcernWTimeout := defaultRWConcern(cr)
err = mongosSession.SetDefaultRWConcern(ctx, readConcern, writeConcernW, writeConcernWTimeout)
// SetDefaultRWConcern introduced in MongoDB 4.4
if err != nil && !strings.Contains(err.Error(), "CommandNotFound") {
return api.AppStateError, nil, errors.Wrap(err, "set default RW concern")
Expand Down Expand Up @@ -263,8 +268,8 @@ func (r *ReconcilePerconaServerMongoDB) reconcileCluster(ctx context.Context, cr
}

if shouldSetDefaultRWConcern(cr, replset) {
readConcern, writeConcern := defaultRWConcern(cr)
err := cli.SetDefaultRWConcern(ctx, readConcern, writeConcern)
readConcern, writeConcernW, writeConcernWTimeout := defaultRWConcern(cr)
err := cli.SetDefaultRWConcern(ctx, readConcern, writeConcernW, writeConcernWTimeout)
// SetDefaultRWConcern introduced in MongoDB 4.4
if err != nil && !strings.Contains(err.Error(), "CommandNotFound") {
return api.AppStateError, nil, errors.Wrap(err, "set default RW concern")
Expand Down
39 changes: 28 additions & 11 deletions pkg/controller/perconaservermongodb/mgo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ func TestDefaultRWConcern(t *testing.T) {
t.Parallel()

tests := map[string]struct {
spec *api.DefaultRWConcern
wantReadConcern string
wantWriteConcern string
spec *api.DefaultRWConcern
wantReadConcern string
wantWriteConcern string
wantWTimeout int
}{
"nil spec falls back to majority": {
spec: nil,
Expand All @@ -82,15 +83,30 @@ func TestDefaultRWConcern(t *testing.T) {
wantReadConcern: "local",
wantWriteConcern: mongo.DefaultWriteConcern,
},
"only write overridden": {
spec: &api.DefaultRWConcern{WriteConcern: "1"},
"only write w overridden": {
spec: &api.DefaultRWConcern{WriteConcern: &api.DefaultWriteConcernSpec{W: "1"}},
wantReadConcern: mongo.DefaultReadConcern,
wantWriteConcern: "1",
},
"both overridden": {
spec: &api.DefaultRWConcern{ReadConcern: "local", WriteConcern: "1"},
"wtimeout overridden": {
spec: &api.DefaultRWConcern{WriteConcern: &api.DefaultWriteConcernSpec{W: "majority", WTimeout: 5000}},
wantReadConcern: mongo.DefaultReadConcern,
wantWriteConcern: "majority",
wantWTimeout: 5000,
},
"empty writeConcern struct keeps defaults": {
spec: &api.DefaultRWConcern{WriteConcern: &api.DefaultWriteConcernSpec{}},
wantReadConcern: mongo.DefaultReadConcern,
wantWriteConcern: mongo.DefaultWriteConcern,
},
"all overridden": {
spec: &api.DefaultRWConcern{
ReadConcern: "local",
WriteConcern: &api.DefaultWriteConcernSpec{W: "1", WTimeout: 250},
},
wantReadConcern: "local",
wantWriteConcern: "1",
wantWTimeout: 250,
},
}

Expand All @@ -99,9 +115,10 @@ func TestDefaultRWConcern(t *testing.T) {
cr := &api.PerconaServerMongoDB{
Spec: api.PerconaServerMongoDBSpec{DefaultRWConcern: tt.spec},
}
gotRead, gotWrite := defaultRWConcern(cr)
gotRead, gotWrite, gotWTimeout := defaultRWConcern(cr)
assert.Equal(t, tt.wantReadConcern, gotRead)
assert.Equal(t, tt.wantWriteConcern, gotWrite)
assert.Equal(t, tt.wantWTimeout, gotWTimeout)
})
}
}
Expand All @@ -123,12 +140,12 @@ func TestShouldSetDefaultRWConcern(t *testing.T) {
want: true,
},
"PSS, custom concern": {
rwConcern: &api.DefaultRWConcern{WriteConcern: "1"},
rwConcern: &api.DefaultRWConcern{WriteConcern: &api.DefaultWriteConcernSpec{W: "1"}},
want: true,
},
"PSA, custom concern": {
arbiterEnabled: true,
rwConcern: &api.DefaultRWConcern{WriteConcern: "1"},
rwConcern: &api.DefaultRWConcern{WriteConcern: &api.DefaultWriteConcernSpec{W: "1"}},
want: true,
},
"sharded, PSA": {
Expand All @@ -138,7 +155,7 @@ func TestShouldSetDefaultRWConcern(t *testing.T) {
},
"sharded, custom concern": {
shardingEnabled: true,
rwConcern: &api.DefaultRWConcern{WriteConcern: "1"},
rwConcern: &api.DefaultRWConcern{WriteConcern: &api.DefaultWriteConcernSpec{W: "1"}},
want: false,
},
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/psmdb/mongo/fake/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (c *fakeMongoClient) Ping(ctx context.Context, rp *readpref.ReadPref) error
return nil
}

func (c *fakeMongoClient) SetDefaultRWConcern(ctx context.Context, readConcern, writeConcern string) error {
func (c *fakeMongoClient) SetDefaultRWConcern(ctx context.Context, readConcern, writeConcernW string, writeConcernWTimeout int) error {
return nil
}

Expand Down
9 changes: 6 additions & 3 deletions pkg/psmdb/mongo/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ type Client interface {
Database(name string, opts ...options.Lister[options.DatabaseOptions]) ClientDatabase
Ping(ctx context.Context, rp *readpref.ReadPref) error

SetDefaultRWConcern(ctx context.Context, readConcern, writeConcern string) error
SetDefaultRWConcern(ctx context.Context, readConcern, writeConcernW string, writeConcernWTimeout int) error
ReadConfig(ctx context.Context) (RSConfig, error)
CreateRole(ctx context.Context, db string, role Role) error
UpdateRole(ctx context.Context, db string, role Role) error
Expand Down Expand Up @@ -181,11 +181,14 @@ func Dial(ctx context.Context, conf *Config) (Client, error) {
return ToInterface(client), nil
}

func (client *mongoClient) SetDefaultRWConcern(ctx context.Context, readConcern, writeConcern string) error {
func (client *mongoClient) SetDefaultRWConcern(ctx context.Context, readConcern, writeConcernW string, writeConcernWTimeout int) error {
cmd := bson.D{
{Key: "setDefaultRWConcern", Value: 1},
{Key: "defaultReadConcern", Value: bson.D{{Key: "level", Value: readConcern}}},
{Key: "defaultWriteConcern", Value: bson.D{{Key: "w", Value: parseWriteConcernW(writeConcern)}}},
{Key: "defaultWriteConcern", Value: bson.D{
{Key: "w", Value: parseWriteConcernW(writeConcernW)},
{Key: "wtimeout", Value: writeConcernWTimeout},
}},
}

res := client.Database("admin").RunCommand(ctx, cmd)
Expand Down
Loading