none
How to I resolve conflicting items using latest-writer-wins policy? RRS feed

  • Question

  • Hi there.

    I'm using a FileSyncProvider to carry out my sync jobs. I referred mostly to this page on msdn to get my code up and working. So my code is pretty similar to the one provided on that website.

    My question is, I want to resolve conflicting items using the latest-writer-wins policy, I tried to find such a setting in ConflictResolutionAction, but I couldn't find it. Any ideas on how I can do this? Or is it possible for me to decide for every particular item that is conflicting, I can choose to do DestinationWins or SourceWins for each item?

    I provided the code segment that is relevant to my question.

    private static void OnItemConflicting(object sender, ItemConflictingEventArgs args)

    {

          args.SetResolutionAction(ConflictResolutionAction.<So far, I only can choose between DestinationWIns/Merge/SaveConflict/SkipChange/SourceWins>);

    }

     

    Monday, March 29, 2010 2:30 PM

All replies

  • Sorry, but can I also ask one more thing. What's the difference between Conflict and Constraint items? I tried reading the pages on MSDN, but I couldn't understand.

    From MSDN, it says "Concurrency conflicts occur when the same item or change unit is changed on two different replicas that are later synchronized." For example when I modify the same text files on both replicas and do a synchronization, I only get the OnItemConflicting event raised. So, in what case will I get a OnItemConstraint event raised?

    Monday, March 29, 2010 2:40 PM
  • The difference is that Conflict is whether same item has changed concurrently (outside sync session) in multiple places. For example, one file has been renamed in one machine, and the same file has been deleted in another machine. How would provider resolve this situation?

    Constraint is related to local constraints. For example, while the provider was trying to persist a particular file / change in file, there was an unexpected problem due to a constraint within the local machine. This can be due to a certain folder having changed to read-only / a different file with same name has been created within the same folder. Then, how should the provider handle such situation? Constraint is a problem raised when a certain rule / environment has caused unexpected circumstance such that it is unable to apply changes (e.g. "two files with same name cannot co-exist on same folder").

    So, in your case you described, it would qualify as Conflict (Concurrency) as it is related to concurrent changes in same item.

    Hope this helps,

    Patrick

    Monday, March 29, 2010 4:33 PM
  • Thank you for your prompt reply.

    May I know how I can set the resolution policy to latest writer wins in constraint and conflict situations?

    Monday, March 29, 2010 4:48 PM
  • Hello seahsq,

    File Sync Provider, by default, picks latest-writer as the winner. The event is for case where you want to resolve conflicts differently per conflict (i.e. in your case, you do not need to handle this event)

    Here is an article on File Sync Provider conflict handling. Take a look at the header "Conflict Handling".

    http://msdn.microsoft.com/en-us/sync/bb887623.aspx 

    Hope this helps,

    Patrick

    Monday, March 29, 2010 4:53 PM
  • I have since removed the event handler for OnItemConflicting. Everything (e.g. modifying the same file on both replicas) has been latest writer wins, except for this situation.

    I renamed the text file on both replicas to different name, but somehow it wasn't latest-writer wins, one sides always seems to win only.

    I gave an simple illustration of what I did below. Can you advice me on this issue? Thanks!

     

    Before (FolderA and FolderB) are already synchronized:

    FolderA (test1.txt)

    FolderB (test1.txt)

     

    Changes I made (I renamed test1.txt to A.txt in FolderA followed by test1.txt to B.txt in FolderB)

    FolderA (A.txt)

    FolderB (B.txt)

     

    After synchronization: (expected outcome was both sides will be renamed B.txt, however this is what I get, which is not latest writer wins)

    FolderA (A.txt)

    FolderB (A.txt)

    Tuesday, March 30, 2010 1:40 AM
  • Hi, is there anyone who can help me out?

    Thanks!

    Wednesday, March 31, 2010 10:55 AM
  • You may have stumbled on clock resolution issue on windows file system. In my understanding the last modified timestamp can be off as much as 2 seconds on FAT system. So if your are using FAT and the two files are updated close in time, the timestamps may not be in the order as you thought.

    Thursday, April 1, 2010 1:53 AM
  • That doesn't seem to be the problem. I tried renaming the 2 files with a 10 seconds interval in between, but I still didn't get latest writer wins.

     

    private static void OnItemConflicting(object sender, ItemConflictingEventArgs args)

    {

          args.SetResolutionAction(ConflictResolutionAction.<So far, I only can choose between DestinationWIns/Merge/SaveConflict/SkipChange/SourceWins>);

    }

     

    Is there any way I can use args or sender in the above method to retrieve the name of the files that were in conflict, so I can at least resolve the conflict manually? Or is there a way to solve this problem using some other better solution?


    Thanks.

    Thursday, April 1, 2010 2:23 AM
  • the ItemConflictingEventArgs will have a property DestinationChange and SourceChange which you can access to look at the items in conflict.

    Thursday, April 1, 2010 2:49 AM
    Moderator
  • Hi JuneT,

    Can you provide a short sample code to show how DestinationChange or SourceChange can be used to look at the items in conflict? Thanks.

    Thursday, April 1, 2010 2:57 AM
  • try this inside your OnItemConflicting event:

    IFileDataRetriever localfileDataRetriever = ((IFileDataRetriever)e.SourceChangeData);
    IFileDataRetriever remotefileDataRetriever = ((IFileDataRetriever)e.DestinationChangeData);
    
    if (localfileDataRetriever.FileData.LastWriteTime > remotefileDataRetriever.FileData.LastWriteTime)
    {
        e.SetResolutionAction( ConflictResolutionAction.SourceWins);
    }
    else
    {
         e.SetResolutionAction(ConflictResolutionAction.DestinationWins);
    }
      

     

    Thursday, April 1, 2010 3:20 AM
    Moderator
  • Thanks for that code snippet JuneT.

     

    I tried using your code: modifying (i.e. changing the content inside the file) the same file on both replicas gave the latest writer wins result.

    However, renaming files does not change the LastWriteTime nor LastAccessTime, so the outcome was not latest writer wins. Any idea how I can solve this?

     

    The current problem is that renaming the same files on both replicas does not yield the latest writer wins result (because renaming of files does not change the LastWriteTime nor LastAccessTime).

    Thursday, April 1, 2010 4:08 AM
  • To resolve conflicts as "the last Writer Win", please try e.SetResolutionAction(ConflictResolutionAction.Merge).

    Thanks.


    Leo Zhou ------ This posting is provided "AS IS" with no warranties, and confers no rights.
    Saturday, April 3, 2010 12:55 AM
    Answerer
  • I have since removed the event handler for OnItemConflicting. Everything (e.g. modifying the same file on both replicas) has been latest writer wins, except for this situation.

    I renamed the text file on both replicas to different name, but somehow it wasn't latest-writer wins, one sides always seems to win only.

    I gave an simple illustration of what I did below. Can you advice me on this issue? Thanks!

     

    Before (FolderA and FolderB) are already synchronized:

    FolderA (test1.txt)

    FolderB (test1.txt)

     

    Changes I made (I renamed test1.txt to A.txt in FolderA followed by test1.txt to B.txt in FolderB)

    FolderA (A.txt)

    FolderB (B.txt)

     

    After synchronization: (expected outcome was both sides will be renamed B.txt, however this is what I get, which is not latest writer wins)

    FolderA (A.txt)

    FolderB (A.txt)

    Hi,

    I tried using e.SetResolutionAction(ConflictResolutionAction.Merge) to get the latest writer wins policy.

    But the problem here is (which I've describe above) when I rename the same file on both replicas, the result of the synchronization does not gives me latest writer wins. However, when I modify the same file on both replicas, I get latest writer wins.

    So the question is, how do I get latest writer wins for renaming the same files on both replicas. This is the problem I'm trying to solve. Thanks.

    Saturday, April 3, 2010 1:44 AM
  • Hi, anyone able to offer a solution? Thanks :)
    Sunday, April 4, 2010 5:12 PM
  • I doubt it will help, but are you using FAT or NTFS?  Doing a quick check, it doesn't appear that renaming affects the modified time (at least on NTFS), so there is no way for the built-in resolution to determine the last writer.  You may be able to do a little bit better if you're using NTFS by using the change journal, but this won't work on FAT or if you're going over WCF or a network.

     

    Aaron


    SDE, Microsoft Sync Framework
    Monday, April 5, 2010 8:51 PM
    Answerer
  • I'm using NTFS. I will look into change journal and see if it's suitable. Thanks.

     

    If anyone else has encounter similar issue and have a solution to offer, please do let me know. Thanks for your help!

    Tuesday, April 6, 2010 1:50 AM
  • Any update on this?
    Wednesday, August 3, 2011 4:59 PM