OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "sync/engine/apply_updates_command.h" | 5 #include "sync/engine/apply_updates_and_resolve_conflicts_command.h" |
6 | 6 |
7 #include "base/location.h" | 7 #include "base/location.h" |
| 8 #include "sync/engine/conflict_resolver.h" |
8 #include "sync/engine/update_applicator.h" | 9 #include "sync/engine/update_applicator.h" |
9 #include "sync/sessions/sync_session.h" | 10 #include "sync/sessions/sync_session.h" |
10 #include "sync/syncable/directory.h" | 11 #include "sync/syncable/directory.h" |
11 #include "sync/syncable/read_transaction.h" | 12 #include "sync/syncable/read_transaction.h" |
12 #include "sync/syncable/write_transaction.h" | 13 #include "sync/syncable/write_transaction.h" |
13 | 14 |
14 namespace syncer { | 15 namespace syncer { |
15 | 16 |
16 using sessions::SyncSession; | 17 using sessions::SyncSession; |
17 | 18 |
18 ApplyUpdatesCommand::ApplyUpdatesCommand() {} | 19 ApplyUpdatesAndResolveConflictsCommand:: |
19 ApplyUpdatesCommand::~ApplyUpdatesCommand() {} | 20 ApplyUpdatesAndResolveConflictsCommand() {} |
| 21 ApplyUpdatesAndResolveConflictsCommand:: |
| 22 ~ApplyUpdatesAndResolveConflictsCommand() {} |
20 | 23 |
21 std::set<ModelSafeGroup> ApplyUpdatesCommand::GetGroupsToChange( | 24 std::set<ModelSafeGroup> |
| 25 ApplyUpdatesAndResolveConflictsCommand::GetGroupsToChange( |
22 const sessions::SyncSession& session) const { | 26 const sessions::SyncSession& session) const { |
23 std::set<ModelSafeGroup> groups_with_unapplied_updates; | 27 std::set<ModelSafeGroup> groups_with_unapplied_updates; |
24 | 28 |
25 FullModelTypeSet server_types_with_unapplied_updates; | 29 FullModelTypeSet server_types_with_unapplied_updates; |
26 { | 30 { |
27 syncable::Directory* dir = session.context()->directory(); | 31 syncable::Directory* dir = session.context()->directory(); |
28 syncable::ReadTransaction trans(FROM_HERE, dir); | 32 syncable::ReadTransaction trans(FROM_HERE, dir); |
29 server_types_with_unapplied_updates = | 33 server_types_with_unapplied_updates = |
30 dir->GetServerTypesWithUnappliedUpdates(&trans); | 34 dir->GetServerTypesWithUnappliedUpdates(&trans); |
31 } | 35 } |
32 | 36 |
33 for (FullModelTypeSet::Iterator it = | 37 for (FullModelTypeSet::Iterator it = |
34 server_types_with_unapplied_updates.First(); it.Good(); it.Inc()) { | 38 server_types_with_unapplied_updates.First(); it.Good(); it.Inc()) { |
35 groups_with_unapplied_updates.insert( | 39 groups_with_unapplied_updates.insert( |
36 GetGroupForModelType(it.Get(), session.routing_info())); | 40 GetGroupForModelType(it.Get(), session.routing_info())); |
37 } | 41 } |
38 | 42 |
39 return groups_with_unapplied_updates; | 43 return groups_with_unapplied_updates; |
40 } | 44 } |
41 | 45 |
42 SyncerError ApplyUpdatesCommand::ModelChangingExecuteImpl( | 46 SyncerError ApplyUpdatesAndResolveConflictsCommand::ModelChangingExecuteImpl( |
43 SyncSession* session) { | 47 SyncSession* session) { |
| 48 sessions::StatusController* status = session->mutable_status_controller(); |
44 syncable::Directory* dir = session->context()->directory(); | 49 syncable::Directory* dir = session->context()->directory(); |
45 syncable::WriteTransaction trans(FROM_HERE, syncable::SYNCER, dir); | 50 syncable::WriteTransaction trans(FROM_HERE, syncable::SYNCER, dir); |
46 | 51 |
47 // Compute server types with unapplied updates that fall under our | 52 // Compute server types with unapplied updates that fall under our |
48 // group restriction. | 53 // group restriction. |
49 const FullModelTypeSet server_types_with_unapplied_updates = | 54 const FullModelTypeSet server_types_with_unapplied_updates = |
50 dir->GetServerTypesWithUnappliedUpdates(&trans); | 55 dir->GetServerTypesWithUnappliedUpdates(&trans); |
51 FullModelTypeSet server_type_restriction; | 56 FullModelTypeSet server_type_restriction; |
52 for (FullModelTypeSet::Iterator it = | 57 for (FullModelTypeSet::Iterator it = |
53 server_types_with_unapplied_updates.First(); it.Good(); it.Inc()) { | 58 server_types_with_unapplied_updates.First(); it.Good(); it.Inc()) { |
54 if (GetGroupForModelType(it.Get(), session->routing_info()) == | 59 if (GetGroupForModelType(it.Get(), session->routing_info()) == |
55 session->status_controller().group_restriction()) { | 60 status->group_restriction()) { |
56 server_type_restriction.Put(it.Get()); | 61 server_type_restriction.Put(it.Get()); |
57 } | 62 } |
58 } | 63 } |
59 | 64 |
60 // Don't process control type updates here. They will be handled elsewhere. | 65 // Don't process control type updates here. They will be handled elsewhere. |
61 FullModelTypeSet control_types = ToFullModelTypeSet(ControlTypes()); | 66 FullModelTypeSet control_types = ToFullModelTypeSet(ControlTypes()); |
62 server_type_restriction.RemoveAll(control_types); | 67 server_type_restriction.RemoveAll(control_types); |
63 | 68 |
64 std::vector<int64> handles; | 69 std::vector<int64> handles; |
65 dir->GetUnappliedUpdateMetaHandles( | 70 dir->GetUnappliedUpdateMetaHandles( |
66 &trans, server_type_restriction, &handles); | 71 &trans, server_type_restriction, &handles); |
67 | 72 |
| 73 // First set of update application passes. |
68 UpdateApplicator applicator( | 74 UpdateApplicator applicator( |
69 dir->GetCryptographer(&trans), | 75 dir->GetCryptographer(&trans), |
70 session->routing_info(), | 76 session->routing_info(), |
71 session->status_controller().group_restriction()); | 77 status->group_restriction()); |
72 applicator.AttemptApplications(&trans, handles, | 78 applicator.AttemptApplications(&trans, handles); |
73 session->mutable_status_controller()); | 79 status->increment_num_updates_applied_by(applicator.updates_applied()); |
| 80 status->increment_num_hierarchy_conflicts_by( |
| 81 applicator.hierarchy_conflicts()); |
| 82 status->increment_num_encryption_conflicts_by( |
| 83 applicator.encryption_conflicts()); |
| 84 |
| 85 if (applicator.simple_conflict_ids().size() != 0) { |
| 86 // Resolve the simple conflicts we just detected. |
| 87 ConflictResolver resolver; |
| 88 resolver.ResolveConflicts(&trans, |
| 89 dir->GetCryptographer(&trans), |
| 90 applicator.simple_conflict_ids(), |
| 91 status); |
| 92 |
| 93 // Conflict resolution sometimes results in more updates to apply. |
| 94 handles.clear(); |
| 95 dir->GetUnappliedUpdateMetaHandles( |
| 96 &trans, server_type_restriction, &handles); |
| 97 |
| 98 UpdateApplicator conflict_applicator( |
| 99 dir->GetCryptographer(&trans), |
| 100 session->routing_info(), |
| 101 status->group_restriction()); |
| 102 conflict_applicator.AttemptApplications(&trans, handles); |
| 103 |
| 104 // We count the number of updates from both applicator passes. |
| 105 status->increment_num_updates_applied_by( |
| 106 conflict_applicator.updates_applied()); |
| 107 |
| 108 // Encryption conflicts should remain unchanged by the resolution of simple |
| 109 // conflicts. Those can only be solved by updating our nigori key bag. |
| 110 DCHECK_EQ(conflict_applicator.encryption_conflicts(), |
| 111 applicator.encryption_conflicts()); |
| 112 |
| 113 // Hierarchy conflicts should also remain unchanged, for reasons that are |
| 114 // more subtle. Hierarchy conflicts exist when the application of a pending |
| 115 // update from the server would make the local folder hierarchy |
| 116 // inconsistent. The resolution of simple conflicts could never affect the |
| 117 // hierarchy conflicting item directly, because hierarchy conflicts are not |
| 118 // processed by the conflict resolver. It could, in theory, modify the |
| 119 // local hierarchy on which hierarchy conflict detection depends. However, |
| 120 // the conflict resolution algorithm currently in use does not allow this. |
| 121 DCHECK_EQ(conflict_applicator.hierarchy_conflicts(), |
| 122 applicator.hierarchy_conflicts()); |
| 123 |
| 124 // There should be no simple conflicts remaining. We know this because the |
| 125 // resolver should have resolved all the conflicts we detected last time |
| 126 // and, by the two previous assertions, that no conflicts have been |
| 127 // downgraded from encryption or hierarchy down to simple. |
| 128 DCHECK(conflict_applicator.simple_conflict_ids().empty()); |
| 129 } |
74 | 130 |
75 // This might be the first time we've fully completed a sync cycle, for | 131 // This might be the first time we've fully completed a sync cycle, for |
76 // some subset of the currently synced datatypes. | 132 // some subset of the currently synced datatypes. |
77 const sessions::StatusController& status(session->status_controller()); | 133 if (status->ServerSaysNothingMoreToDownload()) { |
78 if (status.ServerSaysNothingMoreToDownload()) { | |
79 for (ModelTypeSet::Iterator it = | 134 for (ModelTypeSet::Iterator it = |
80 status.updates_request_types().First(); it.Good(); it.Inc()) { | 135 status->updates_request_types().First(); it.Good(); it.Inc()) { |
81 // Don't set the flag for control types. We didn't process them here. | 136 // Don't set the flag for control types. We didn't process them here. |
82 if (IsControlType(it.Get())) | 137 if (IsControlType(it.Get())) |
83 continue; | 138 continue; |
84 | 139 |
85 // This gets persisted to the directory's backing store. | 140 // This gets persisted to the directory's backing store. |
86 dir->set_initial_sync_ended_for_type(it.Get(), true); | 141 dir->set_initial_sync_ended_for_type(it.Get(), true); |
87 } | 142 } |
88 } | 143 } |
89 | 144 |
90 return SYNCER_OK; | 145 return SYNCER_OK; |
91 } | 146 } |
92 | 147 |
93 } // namespace syncer | 148 } // namespace syncer |
OLD | NEW |