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/verify_updates_command.h" | 5 #include "sync/engine/verify_updates_command.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "sync/engine/syncer.h" | 10 #include "sync/engine/syncer.h" |
11 #include "sync/engine/syncer_proto_util.h" | 11 #include "sync/engine/syncer_proto_util.h" |
12 #include "sync/engine/syncer_types.h" | 12 #include "sync/engine/syncer_types.h" |
13 #include "sync/engine/syncer_util.h" | 13 #include "sync/engine/syncer_util.h" |
14 #include "sync/engine/syncproto.h" | |
15 #include "sync/internal_api/public/engine/model_safe_worker.h" | 14 #include "sync/internal_api/public/engine/model_safe_worker.h" |
16 #include "sync/protocol/bookmark_specifics.pb.h" | 15 #include "sync/protocol/bookmark_specifics.pb.h" |
16 #include "sync/protocol/sync.pb.h" | |
17 #include "sync/syncable/entry.h" | 17 #include "sync/syncable/entry.h" |
18 #include "sync/syncable/mutable_entry.h" | 18 #include "sync/syncable/mutable_entry.h" |
19 #include "sync/syncable/syncable_proto_util.h" | |
19 #include "sync/syncable/write_transaction.h" | 20 #include "sync/syncable/write_transaction.h" |
20 | 21 |
21 namespace syncer { | 22 namespace syncer { |
22 | 23 |
23 using syncable::GET_BY_ID; | 24 using syncable::GET_BY_ID; |
24 using syncer::ModelTypeSet; | 25 using syncer::ModelTypeSet; |
25 using syncable::SYNCER; | 26 using syncable::SYNCER; |
26 using syncable::WriteTransaction; | 27 using syncable::WriteTransaction; |
27 | 28 |
28 namespace { | 29 namespace { |
(...skipping 13 matching lines...) Expand all Loading... | |
42 // following the next restart, we will receive an update from the server that | 43 // following the next restart, we will receive an update from the server that |
43 // updates its local ID. | 44 // updates its local ID. |
44 // - When two attempts to create an item with identical UNIQUE_CLIENT_TAG values | 45 // - When two attempts to create an item with identical UNIQUE_CLIENT_TAG values |
45 // collide at the server. I have seen this in testing. When it happens, the | 46 // collide at the server. I have seen this in testing. When it happens, the |
46 // test server will send one of the clients a response to upate its local ID so | 47 // test server will send one of the clients a response to upate its local ID so |
47 // that both clients will refer to the item using the same ID going forward. In | 48 // that both clients will refer to the item using the same ID going forward. In |
48 // this case, we're right to assume that the update is not a reflection. | 49 // this case, we're right to assume that the update is not a reflection. |
49 // | 50 // |
50 // For more information, see FindLocalIdToUpdate(). | 51 // For more information, see FindLocalIdToUpdate(). |
51 bool UpdateContainsNewVersion(syncable::BaseTransaction *trans, | 52 bool UpdateContainsNewVersion(syncable::BaseTransaction *trans, |
52 const SyncEntity &update) { | 53 const sync_pb::SyncEntity &update) { |
53 int64 existing_version = -1; // The server always sends positive versions. | 54 int64 existing_version = -1; // The server always sends positive versions. |
54 syncable::Entry existing_entry(trans, GET_BY_ID, update.id()); | 55 syncable::Entry existing_entry(trans, GET_BY_ID, |
56 SyncableIdFromProto(update.id_string())); | |
55 if (existing_entry.good()) | 57 if (existing_entry.good()) |
56 existing_version = existing_entry.Get(syncable::BASE_VERSION); | 58 existing_version = existing_entry.Get(syncable::BASE_VERSION); |
57 | 59 |
58 return existing_version < update.version(); | 60 return existing_version < update.version(); |
59 } | 61 } |
60 | 62 |
61 // In the event that IDs match, but tags differ AttemptReuniteClient tag | 63 // In the event that IDs match, but tags differ AttemptReuniteClient tag |
62 // will have refused to unify the update. | 64 // will have refused to unify the update. |
63 // We should not attempt to apply it at all since it violates consistency | 65 // We should not attempt to apply it at all since it violates consistency |
64 // rules. | 66 // rules. |
65 VerifyResult VerifyTagConsistency(const SyncEntity& entry, | 67 VerifyResult VerifyTagConsistency(const sync_pb::SyncEntity& entry, |
66 const syncable::MutableEntry& same_id) { | 68 const syncable::MutableEntry& same_id) { |
67 if (entry.has_client_defined_unique_tag() && | 69 if (entry.has_client_defined_unique_tag() && |
68 entry.client_defined_unique_tag() != | 70 entry.client_defined_unique_tag() != |
69 same_id.Get(syncable::UNIQUE_CLIENT_TAG)) { | 71 same_id.Get(syncable::UNIQUE_CLIENT_TAG)) { |
70 return VERIFY_FAIL; | 72 return VERIFY_FAIL; |
71 } | 73 } |
72 return VERIFY_UNDECIDED; | 74 return VERIFY_UNDECIDED; |
73 } | 75 } |
74 } // namespace | 76 } // namespace |
75 | 77 |
76 VerifyUpdatesCommand::VerifyUpdatesCommand() {} | 78 VerifyUpdatesCommand::VerifyUpdatesCommand() {} |
77 VerifyUpdatesCommand::~VerifyUpdatesCommand() {} | 79 VerifyUpdatesCommand::~VerifyUpdatesCommand() {} |
78 | 80 |
79 std::set<ModelSafeGroup> VerifyUpdatesCommand::GetGroupsToChange( | 81 std::set<ModelSafeGroup> VerifyUpdatesCommand::GetGroupsToChange( |
80 const sessions::SyncSession& session) const { | 82 const sessions::SyncSession& session) const { |
81 std::set<ModelSafeGroup> groups_with_updates; | 83 std::set<ModelSafeGroup> groups_with_updates; |
82 | 84 |
83 const GetUpdatesResponse& updates = | 85 const sync_pb::GetUpdatesResponse& updates = |
84 session.status_controller().updates_response().get_updates(); | 86 session.status_controller().updates_response().get_updates(); |
85 for (int i = 0; i < updates.entries().size(); i++) { | 87 for (int i = 0; i < updates.entries().size(); i++) { |
86 groups_with_updates.insert( | 88 groups_with_updates.insert( |
87 GetGroupForModelType(syncer::GetModelType(updates.entries(i)), | 89 GetGroupForModelType(syncer::GetModelType(updates.entries(i)), |
88 session.routing_info())); | 90 session.routing_info())); |
89 } | 91 } |
90 | 92 |
91 return groups_with_updates; | 93 return groups_with_updates; |
92 } | 94 } |
93 | 95 |
94 SyncerError VerifyUpdatesCommand::ModelChangingExecuteImpl( | 96 SyncerError VerifyUpdatesCommand::ModelChangingExecuteImpl( |
95 sessions::SyncSession* session) { | 97 sessions::SyncSession* session) { |
96 DVLOG(1) << "Beginning Update Verification"; | 98 DVLOG(1) << "Beginning Update Verification"; |
97 syncable::Directory* dir = session->context()->directory(); | 99 syncable::Directory* dir = session->context()->directory(); |
98 WriteTransaction trans(FROM_HERE, SYNCER, dir); | 100 WriteTransaction trans(FROM_HERE, SYNCER, dir); |
99 sessions::StatusController* status = session->mutable_status_controller(); | 101 sessions::StatusController* status = session->mutable_status_controller(); |
100 const GetUpdatesResponse& updates = status->updates_response().get_updates(); | 102 const sync_pb::GetUpdatesResponse& updates = |
103 status->updates_response().get_updates(); | |
101 int update_count = updates.entries().size(); | 104 int update_count = updates.entries().size(); |
102 | 105 |
103 ModelTypeSet requested_types = syncer::GetRoutingInfoTypes( | 106 ModelTypeSet requested_types = syncer::GetRoutingInfoTypes( |
104 session->routing_info()); | 107 session->routing_info()); |
105 | 108 |
106 DVLOG(1) << update_count << " entries to verify"; | 109 DVLOG(1) << update_count << " entries to verify"; |
107 for (int i = 0; i < update_count; i++) { | 110 for (int i = 0; i < update_count; i++) { |
108 const SyncEntity& update = | 111 const sync_pb::SyncEntity& update = |
109 *reinterpret_cast<const SyncEntity *>(&(updates.entries(i))); | 112 *reinterpret_cast<const sync_pb::SyncEntity *>(&(updates.entries(i))); |
akalin
2012/07/11 01:42:22
reinterpret_cast? (urgggh)
rlarocque
2012/07/11 19:22:16
Done.
| |
110 ModelSafeGroup g = GetGroupForModelType(update.GetModelType(), | 113 ModelSafeGroup g = GetGroupForModelType(GetModelType(update), |
111 session->routing_info()); | 114 session->routing_info()); |
112 if (g != status->group_restriction()) | 115 if (g != status->group_restriction()) |
113 continue; | 116 continue; |
114 | 117 |
115 VerifyUpdateResult result = VerifyUpdate(&trans, update, | 118 VerifyUpdateResult result = VerifyUpdate(&trans, update, |
116 requested_types, | 119 requested_types, |
117 session->routing_info()); | 120 session->routing_info()); |
118 status->mutable_update_progress()->AddVerifyResult(result.value, update); | 121 status->mutable_update_progress()->AddVerifyResult(result.value, update); |
119 status->increment_num_updates_downloaded_by(1); | 122 status->increment_num_updates_downloaded_by(1); |
120 if (!UpdateContainsNewVersion(&trans, update)) | 123 if (!UpdateContainsNewVersion(&trans, update)) |
121 status->increment_num_reflected_updates_downloaded_by(1); | 124 status->increment_num_reflected_updates_downloaded_by(1); |
122 if (update.deleted()) | 125 if (update.deleted()) |
123 status->increment_num_tombstone_updates_downloaded_by(1); | 126 status->increment_num_tombstone_updates_downloaded_by(1); |
124 } | 127 } |
125 | 128 |
126 return SYNCER_OK; | 129 return SYNCER_OK; |
127 } | 130 } |
128 | 131 |
129 VerifyUpdatesCommand::VerifyUpdateResult VerifyUpdatesCommand::VerifyUpdate( | 132 VerifyUpdatesCommand::VerifyUpdateResult VerifyUpdatesCommand::VerifyUpdate( |
130 syncable::WriteTransaction* trans, const SyncEntity& entry, | 133 syncable::WriteTransaction* trans, const sync_pb::SyncEntity& entry, |
131 const ModelTypeSet& requested_types, | 134 const ModelTypeSet& requested_types, |
132 const ModelSafeRoutingInfo& routes) { | 135 const ModelSafeRoutingInfo& routes) { |
133 syncable::Id id = entry.id(); | 136 syncable::Id id = SyncableIdFromProto(entry.id_string()); |
134 VerifyUpdateResult result = {VERIFY_FAIL, GROUP_PASSIVE}; | 137 VerifyUpdateResult result = {VERIFY_FAIL, GROUP_PASSIVE}; |
135 | 138 |
136 const bool deleted = entry.has_deleted() && entry.deleted(); | 139 const bool deleted = entry.has_deleted() && entry.deleted(); |
137 const bool is_directory = entry.IsFolder(); | 140 const bool is_directory = IsFolder(entry); |
138 const syncer::ModelType model_type = entry.GetModelType(); | 141 const syncer::ModelType model_type = GetModelType(entry); |
139 | 142 |
140 if (!id.ServerKnows()) { | 143 if (!id.ServerKnows()) { |
141 LOG(ERROR) << "Illegal negative id in received updates"; | 144 LOG(ERROR) << "Illegal negative id in received updates"; |
142 return result; | 145 return result; |
143 } | 146 } |
144 { | 147 { |
145 const std::string name = SyncerProtoUtil::NameFromSyncEntity(entry); | 148 const std::string name = SyncerProtoUtil::NameFromSyncEntity(entry); |
146 if (name.empty() && !deleted) { | 149 if (name.empty() && !deleted) { |
147 LOG(ERROR) << "Zero length name in non-deleted update"; | 150 LOG(ERROR) << "Zero length name in non-deleted update"; |
148 return result; | 151 return result; |
149 } | 152 } |
150 } | 153 } |
151 | 154 |
152 syncable::MutableEntry same_id(trans, GET_BY_ID, id); | 155 syncable::MutableEntry same_id(trans, GET_BY_ID, id); |
153 result.value = VerifyNewEntry(entry, &same_id, deleted); | 156 result.value = VerifyNewEntry(entry, &same_id, deleted); |
154 | 157 |
155 syncer::ModelType placement_type = !deleted ? entry.GetModelType() | 158 syncer::ModelType placement_type = !deleted ? GetModelType(entry) |
156 : same_id.good() ? same_id.GetModelType() : syncer::UNSPECIFIED; | 159 : same_id.good() ? same_id.GetModelType() : syncer::UNSPECIFIED; |
157 result.placement = GetGroupForModelType(placement_type, routes); | 160 result.placement = GetGroupForModelType(placement_type, routes); |
158 | 161 |
159 if (VERIFY_UNDECIDED == result.value) { | 162 if (VERIFY_UNDECIDED == result.value) { |
160 result.value = VerifyTagConsistency(entry, same_id); | 163 result.value = VerifyTagConsistency(entry, same_id); |
161 } | 164 } |
162 | 165 |
163 if (VERIFY_UNDECIDED == result.value) { | 166 if (VERIFY_UNDECIDED == result.value) { |
164 if (deleted) { | 167 if (deleted) { |
165 // For deletes the server could send tombostones for items that | 168 // For deletes the server could send tombostones for items that |
(...skipping 14 matching lines...) Expand all Loading... | |
180 deleted, is_directory, model_type); | 183 deleted, is_directory, model_type); |
181 } | 184 } |
182 | 185 |
183 if (VERIFY_UNDECIDED == result.value) | 186 if (VERIFY_UNDECIDED == result.value) |
184 result.value = VERIFY_SUCCESS; // No news is good news. | 187 result.value = VERIFY_SUCCESS; // No news is good news. |
185 | 188 |
186 return result; // This might be VERIFY_SUCCESS as well | 189 return result; // This might be VERIFY_SUCCESS as well |
187 } | 190 } |
188 | 191 |
189 } // namespace syncer | 192 } // namespace syncer |
OLD | NEW |