Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 352204

Summary: [Legacy] Failing event PREPARE in state CLEAN : state machine issue with legacy mode
Product: [Modeling] EMF Reporter: Alex Lagarde <alex.lagarde>
Component: cdo.legacyAssignee: Martin Fluegge <martin.fluegge>
Status: CLOSED FIXED QA Contact: Eike Stepper <stepper>
Severity: normal    
Priority: P3 CC: alex.lagarde, mariot.chauvin, steve.monnier
Version: 4.1Flags: martin.fluegge: review? (stepper)
Target Milestone: ---   
Hardware: PC   
OS: Windows 7   
Whiteboard:
Bug Depends on:    
Bug Blocks: 359966, 364536    
Attachments:
Description Flags
Transition workaround
none
Transition workaround and test
none
Test v1
none
Metamodel to reproduce the issue
none
model to import on cdo repository
none
JUnit test case of the scenario
none
Test v2
none
Test Case Overview none

Description Alex Lagarde CLA 2011-07-15 08:32:49 EDT
Issue described in the following forum post : http://www.eclipse.org/forums/index.php/t/220818/
Comment 1 Martin Fluegge CLA 2011-07-15 11:33:07 EDT
Alex,

your stack trace does not match the code from HEAD. I assume that you are not using CDO from the repository. Which version are you using? CDO 3.x or 4.0?
Comment 2 Alex Lagarde CLA 2011-07-19 03:51:23 EDT
Hi Martin, I'm using the CDO 4.0 release version (v20110608).
Comment 3 Alex Lagarde CLA 2011-08-31 07:26:22 EDT
Martin, any news about this bug ?
Comment 4 Steve Monnier CLA 2011-09-15 10:22:36 EDT
Created attachment 203415 [details]
Transition workaround

I attached a small patch containing the workaround on CDOStateMachine transitions described by Alex in http://www.eclipse.org/forums/index.php/t/220818/
Comment 5 Martin Fluegge CLA 2011-09-15 12:54:50 EDT
sorry for the late response. I must have overseen this one. Steve, thnaks for the patch. I am usually not a fan of changing CDOs internals if legacy is involved. I would like to find a solution for this in legacy without changing the core. Since you could provide a patch, could one of you provide a small test which reproduces the problem. This you help me a lot. Thanks :)
Comment 6 Steve Monnier CLA 2011-09-16 12:56:10 EDT
Hi Martin,

I will provide a test to reproduce this problem as soon as possible.

(In reply to comment #5)
> sorry for the late response. I must have overseen this one. Steve, thnaks for
> the patch. I am usually not a fan of changing CDOs internals if legacy is
> involved. I would like to find a solution for this in legacy without changing
> the core. Since you could provide a patch, could one of you provide a small
> test which reproduces the problem. This you help me a lot. Thanks :)
Comment 7 Martin Fluegge CLA 2011-09-16 15:47:41 EDT
> I will provide a test to reproduce this problem as soon as possible.
> 

That's great. Thanks a lot. :)
Comment 8 Steve Monnier CLA 2011-09-19 09:35:26 EDT
Created attachment 203586 [details]
Transition workaround and test

Same patch with test cases in addition
Comment 9 Martin Fluegge CLA 2011-09-19 13:33:33 EDT
Created attachment 203617 [details]
Test v1

Unfortunatelly the attached patch only demonstrates that the CDOStaeMachine Works correctly. Attaching or preparing an already clean object must lead to an error. That's why the test also fails in native. 

I tried to write a test besing on the description given in the forum post, but did not succeed yet. Maybe one of you could use this as base and try to reproduce this with our test models?

I also could need some additional information:

- Is the containment relationship a single, or mutli value reference?
- What does "several instances" mean? 10, 100, more?
- Has User2 already openend a session and transaction, or does the error occur when loading the resource? Or both?

I am going back now to the code and continue trying to reproduce the errror.
Comment 10 Steve Monnier CLA 2011-09-22 05:56:40 EDT
Created attachment 203829 [details]
Metamodel to reproduce the issue
Comment 11 Steve Monnier CLA 2011-09-22 06:04:59 EDT
Created attachment 203831 [details]
model to import on cdo repository

Hi Martin,
I finally managed to create a small metamodel to reproduce the issue.

Steps to follow : 
- First user : Import the model on CDO repository
- Second user : connect to this repository and open an editor on this shared resource
- First user : Explore model to the "A" element (containing "D" and "B")
- First user : Copy this "A" element on the same "E" container and commit
- Second user : Receive multiple java.lang.IllegalStateException

java.lang.IllegalStateException: Failing event PREPARE in state CLEAN for CDOLegacyWrapper[CImpl@OID18] (data=Pair[Transaction 1 [MAIN], []])
at org.eclipse.net4j.util.fsm.FiniteStateMachine.process(FiniteStateMachine.java:153)
at org.eclipse.emf.internal.cdo.view.CDOStateMachine.prepare(CDOStateMachine.java:227)
at org.eclipse.emf.internal.cdo.view.CDOStateMachine.attach(CDOStateMachine.java:191)
at org.eclipse.emf.cdo.eresource.impl.CDOResourceImpl.attached(CDOResourceImpl.java:1228)
at org.eclipse.emf.cdo.eresource.impl.CDOResourceImpl.attached(CDOResourceImpl.java:1219)
at org.eclipse.emf.ecore.impl.BasicEObjectImpl.eBasicSetContainer(BasicEObjectImpl.java:1342)
at org.eclipse.emf.ecore.impl.BasicEObjectImpl.eInverseAdd(BasicEObjectImpl.java:1423)
at bug352204.impl.BImpl.setOwnedC(BImpl.java:100)
at bug352204.impl.BImpl.eSet(BImpl.java:145)
at org.eclipse.emf.ecore.impl.BasicEObjectImpl.eSet(BasicEObjectImpl.java:1081)
at org.eclipse.emf.internal.cdo.object.CDOLegacyWrapper.revisionToInstanceFeature(CDOLegacyWrapper.java:544)
at org.eclipse.emf.internal.cdo.object.CDOLegacyWrapper.revisionToInstance(CDOLegacyWrapper.java:391)
at org.eclipse.emf.internal.cdo.object.CDOLegacyWrapper.cdoInternalPostLoad(CDOLegacyWrapper.java:278)


In the stack trace, it presents that the issue comes from "setOwnedC" relation on "B". However, you will not have the exceptions if you remove the relation of the last GMF Node element to "C".
Comment 12 Steve Monnier CLA 2011-09-27 09:05:44 EDT
Hi Martin,

I forgot to add in my previous comment, that I reproduce this issue on branch 4.0 Maintenance. Did you manage to reproduce it as well?

Thanks,
Steve
Comment 13 Martin Fluegge CLA 2011-09-27 09:26:05 EDT
(In reply to comment #12)
> Hi Martin,
> 
> I forgot to add in my previous comment, that I reproduce this issue on branch
> 4.0 Maintenance. Did you manage to reproduce it as well?
> 

Does this mean that you only tested on 4.0 or that you were not able to reproduce it on the CDO 4.1 stream?

I'll try to set up your environment this evening.
Comment 14 Steve Monnier CLA 2011-09-27 09:39:04 EDT
(In reply to comment #13)
> (In reply to comment #12)
> > Hi Martin,
> > 
> > I forgot to add in my previous comment, that I reproduce this issue on branch
> > 4.0 Maintenance. Did you manage to reproduce it as well?
> > 
> 
> Does this mean that you only tested on 4.0 or that you were not able to
> reproduce it on the CDO 4.1 stream?
> 
> I'll try to set up your environment this evening.

Indeed, I only tried on 4.0.

Thanks
Comment 15 Martin Fluegge CLA 2011-09-27 13:12:55 EDT
Hi Steve,

I successfully imported the models, but somehow did not find a test class. The test_bug352205 project only contains a resource file. Where can I find the specific java file?

Cheers, 

Martin
Comment 16 Steve Monnier CLA 2011-09-28 03:32:54 EDT
Hi Martin,

Actually, I thought it would be quicker to provide a test project and the previous scenario in order that you can reproduce the issue as soon as possible :).

Regards
Comment 17 Martin Fluegge CLA 2011-09-28 04:16:25 EDT
Hi Steve,

but the test project is missing some file to execute the scenario you described. Since you showed the exception I guess you already have some piece of code which leads to the exception. 

This would be helpful. If I would have something where I can debug through it would be easier to understand the problem and to provide a solution.

I could write an own test based on your description, but if you already have one this would lead to unnecessary effort. I currently busy preparing the Eclipse Con so writing an own test would take some time ;)
Comment 18 Steve Monnier CLA 2011-09-28 04:54:14 EDT
Ok I see the misunderstanding, I reproduced the scenario manually not programmatically.

I'm not using any other file, I just opened a session with the cdo session view, created a new resource and imported the model that is in the test project and open a CDO Editor on this imported resource. 

Then a second user (another eclipse) opens a CDO Editor on this imported resource.

Next one of these users Explore model to the "A" element (containing "D" and "B"), Copy this "A" element on the same "E" container and commit.

As a result the other user Receive multiple java.lang.IllegalStateException.

I'm looking into the cdo test framework to create a java test case.
Comment 19 Steve Monnier CLA 2011-09-29 10:04:58 EDT
Created attachment 204294 [details]
JUnit test case of the scenario

Hi Martin,

I managed to write a test case (might be a bit tricky) that reproduces my previous scenario and fails with the same exception with CDO 4.0 maintenance branch.
Comment 20 Martin Fluegge CLA 2011-09-30 05:39:01 EDT
Created attachment 204360 [details]
Test v2

That's really a strange bug and a complex scenario. Legacy, multiple session, differnt threads, importing resources and GMF. That's tough. 

I simplified the test to exclude some potentiel problems. It occurs to me that the bug only occurs if the second user reads the first object from the resource. 

Not sure why this can influence the second thread. Also the timing in the test seems to be related to the problem.

progress is ongoing...
Comment 21 Alex Lagarde CLA 2011-10-05 07:58:47 EDT
You can also use the test case written for https://bugs.eclipse.org/bugs/show_bug.cgi?id=359966 to reproduce the issue.
Comment 22 Eike Stepper CLA 2011-11-23 01:05:25 EST
These are disabled until this bug is fixed (they demo the same effect):
org.eclipse.emf.cdo.tests.bugzilla.Bugzilla_359966_Test._testChangesImportWithNewLegacyElementsWithCustomFileAndReconstructSavePoints()
org.eclipse.emf.cdo.tests.bugzilla.Bugzilla_359966_Test._testChangesImportWithNewLegacyElementsWithCustomFileAndNotReconstructSavePoints()
Comment 23 Eike Stepper CLA 2011-11-25 03:24:33 EST
(In reply to comment #21)
> You can also use the test case written for
> https://bugs.eclipse.org/bugs/show_bug.cgi?id=359966 to reproduce the issue.

See also diagram https://bugs.eclipse.org/bugs/attachment.cgi?id=207400
Comment 24 Martin Fluegge CLA 2011-11-25 03:39:48 EST
Start investigating
Comment 25 Martin Fluegge CLA 2011-11-26 03:29:58 EST
Created attachment 207564 [details]
Test Case Overview

I managed to reduce the complexity of the test (see attached figure). Still this one was not that trivial to detect. The problem comes (again) from the recursive nature of legacy. In fact, when processing the object tree to fill the legacy objects the Diagram object in the tree is created and initialize as while processing the container of the NodeImpl (which is the starting point in the test). Doing this, the call of AbstractCDOView.cleanup() sets the Diagram into the CDOState CLEAN before calling cdoInternalPostLoad and continuing with the processing. 
The problem occurs if the container of the Diagram (DImpl) is processed in the next step. While all references of DImpl are handled also the containment reference (data) to the DiagramImpl is set. 

But containment references are currently only handled with a call to eSet(), which unfortunately invokes a reverses setting (eBasicSetContainer) for the parent (DImpl). 

If now the object is attached to the resource it is already in state CLEAN, wich leads to the exception. 

The problem comes from the call of eSet since there is no reflective way to call only one site of this operation (in this case basicSetData(EObject)).
I have different ideas how to face this problem. But I am optimistic that I can provide a patch soon.
Comment 26 Martin Fluegge CLA 2011-11-26 05:25:10 EST
Currently the best way is to avoid the multiple addition to the resource in case of Legacy. I provided the changes under bugs/352204.
Comment 27 Eike Stepper CLA 2011-11-27 03:31:31 EST
Changed slightly, merged to master, pushed to upstream:

commit 97b7955f6e86c473559a920869603e6039994879
Author: Eike Stepper <stepper@esc-net.de> 2011-11-27 09:12:12
Committer: Eike Stepper <stepper@esc-net.de> 2011-11-27 09:12:12
Parent: 5e576c5f2975be19b3d81627aa163c7022b5329f ([352204] [Legacy] Failing event PREPARE in state CLEAN : state machine issue with legacy mode https://bugs.eclipse.org/bugs/show_bug.cgi?id=352204)
Branches: origin/bugs/352204, origin/master, master, bugs/352204

[352204] [Legacy] Failing event PREPARE in state CLEAN : state machine issue with legacy mode 
https://bugs.eclipse.org/bugs/show_bug.cgi?id=352204
Comment 28 Eike Stepper CLA 2011-11-27 03:32:15 EST
Port to 4.0 via bug 364904.
Comment 29 Eike Stepper CLA 2012-09-21 07:17:36 EDT
Closing.