Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collection.Save CascadeSave bug #36

Open
wxf4150 opened this issue May 29, 2018 · 4 comments · May be fixed by #40
Open

Collection.Save CascadeSave bug #36

wxf4150 opened this issue May 29, 2018 · 4 comments · May be fixed by #40

Comments

@wxf4150
Copy link

wxf4150 commented May 29, 2018

Collection.Save(document) method invoke "CascadeSave" using goRoutine.
when i change document's field 10 times and save every times.
the function CascadeSave will invoke 10 times use 10 Routines;
the 10 Routines will not ordered, some Routines will concurrency, then parent doc error

@jraede
Copy link
Collaborator

jraede commented May 29, 2018 via email

@wxf4150
Copy link
Author

wxf4150 commented May 30, 2018

var parentColl *bongo.Collection
var childColl *bongo.Collection
type parent struct{
	bongo.DocumentBase `bson:",inline"`
	Childs []*Child
}
type Child struct{
	bongo.DocumentBase `bson:",inline"`
	ParentID bson.ObjectId
	Status int
	diffTracker *bongo.DiffTracker
}

func Test_Bongo(t *testing.T){
	parentColl=utils.Bconn.Collection("parent")
	childColl=utils.Bconn.Collection("child")
	parent:=new(parent)
	parentColl.Save(parent)
	child:=new(Child)
	child.ParentID=parent.Id
	childColl.Save(child)

	for i:=1;i<100;i++{
		child.Status=i
		childColl.Save(child) //will invoke CascadeSave  using (goroutine CascadeSave)
	//code: https://github.com/go-bongo/bongo/blob/master/collection.go#L147
	}

	//**the end**: len(parent.Childs)!=1 or parent.Childs[0].Status!=99
}
func (c *Child) GetCascade(collection *bongo.Collection) []*bongo.CascadeConfig {
	connection := collection.Connection
	rel :=c
	cascadeMulti := &bongo.CascadeConfig{
		Collection:  connection.Collection("parent"),
		//Properties:  []string{"name","orgname","status"},
		Data:rel,
		ThroughProp: "childs",
		RelType:     bongo.REL_MANY,
		Query: bson.M{
			"_id": c.ParentID,
		},
	}
	if c.diffTracker!=nil && c.diffTracker.Modified("ParentID") {
		origId, _ := c.diffTracker.GetOriginalValue("ParentID")
		if origId != nil {
			oldQuery := bson.M{
				"_id": origId,
			}
			cascadeMulti.OldQuery = oldQuery
		}

	}
	return []*bongo.CascadeConfig{ cascadeMulti}
}

the end:
parent doccument error,
len(parent.Childs)!=1 or parent.Childs[0].Status!=99

@wxf4150
Copy link
Author

wxf4150 commented May 30, 2018

or update this way

	for i:=1;i<20;i++{
		cloneChild:=*child
		cloneChild.Status=i
		childColl.Save(&cloneChild) 
	}
	time.Sleep(2*time.Second)

the end:
parent doccument error,
len(parent.Childs)!=1 or parent.Childs[0].Status!=19

@jraede
Copy link
Collaborator

jraede commented Jun 6, 2018

I could see this being a race condition if there are multiple users updating the same record. Solution seems to be just removing the go in front of the CascadeSave here https://github.com/go-bongo/bongo/blob/master/collection.go#L147

Can you submit a PR?

@wxf4150 wxf4150 linked a pull request Feb 28, 2019 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants