DeviceManager fix
- relinquish Mastership on disconnecregardless of store response - update TODO memos Change-Id: Idece0326fdc780586801fbeb19004c4e88ac8c92
Showing
1 changed file
with
33 additions
and
12 deletions
... | @@ -203,31 +203,43 @@ public class DeviceManager | ... | @@ -203,31 +203,43 @@ public class DeviceManager |
203 | MastershipRole role = mastershipService.requestRoleFor(deviceId); | 203 | MastershipRole role = mastershipService.requestRoleFor(deviceId); |
204 | 204 | ||
205 | if (role != MastershipRole.MASTER) { | 205 | if (role != MastershipRole.MASTER) { |
206 | - // TODO: Do we need to tell the Provider that | 206 | + // TODO: Do we need to explicitly tell the Provider that |
207 | - // I am no longer the MASTER? | 207 | + // this instance is no longer the MASTER? probably not |
208 | return; | 208 | return; |
209 | } | 209 | } |
210 | 210 | ||
211 | - // Master: | ||
212 | MastershipTerm term = mastershipService.requestTermService() | 211 | MastershipTerm term = mastershipService.requestTermService() |
213 | .getMastershipTerm(deviceId); | 212 | .getMastershipTerm(deviceId); |
214 | if (!term.master().equals(clusterService.getLocalNode().id())) { | 213 | if (!term.master().equals(clusterService.getLocalNode().id())) { |
215 | - // I've lost mastership after I thought I was MASTER. | 214 | + // lost mastership after requestRole told this instance was MASTER. |
216 | return; | 215 | return; |
217 | } | 216 | } |
217 | + // tell clock provider if this instance is the master | ||
218 | clockProviderService.setMastershipTerm(deviceId, term); | 218 | clockProviderService.setMastershipTerm(deviceId, term); |
219 | 219 | ||
220 | DeviceEvent event = store.createOrUpdateDevice(provider().id(), | 220 | DeviceEvent event = store.createOrUpdateDevice(provider().id(), |
221 | deviceId, deviceDescription); | 221 | deviceId, deviceDescription); |
222 | 222 | ||
223 | // If there was a change of any kind, tell the provider | 223 | // If there was a change of any kind, tell the provider |
224 | - // I am the master. | 224 | + // that this instance is the master. |
225 | - // Note: can be null, if mastership was lost. | 225 | + // Note: event can be null, if mastership was lost between |
226 | + // roleRequest and store update calls. | ||
226 | if (event != null) { | 227 | if (event != null) { |
227 | - // TODO: Check switch reconnected case, is it assured that | 228 | + // TODO: Check switch reconnected case. Is it assured that |
228 | - // event will not be null? | 229 | + // event will never be null? |
229 | - | 230 | + // Could there be a situation MastershipService told this |
230 | - // FIXME: 1st argument should be deviceId | 231 | + // instance is the new Master, but |
232 | + // event returned from the store is null? | ||
233 | + | ||
234 | + // TODO: Confirm: Mastership could be lost after requestRole | ||
235 | + // and createOrUpdateDevice call. | ||
236 | + // In that case STANDBY node can | ||
237 | + // claim itself to be master against the Device. | ||
238 | + // Will the Node, chosen by the MastershipService, retry | ||
239 | + // to get the MASTER role when that happen? | ||
240 | + | ||
241 | + // FIXME: 1st argument should be deviceId, to allow setting | ||
242 | + // certain roles even if the store returned null. | ||
231 | provider().roleChanged(event.subject(), role); | 243 | provider().roleChanged(event.subject(), role); |
232 | post(event); | 244 | post(event); |
233 | } | 245 | } |
... | @@ -237,16 +249,22 @@ public class DeviceManager | ... | @@ -237,16 +249,22 @@ public class DeviceManager |
237 | public void deviceDisconnected(DeviceId deviceId) { | 249 | public void deviceDisconnected(DeviceId deviceId) { |
238 | checkNotNull(deviceId, DEVICE_ID_NULL); | 250 | checkNotNull(deviceId, DEVICE_ID_NULL); |
239 | checkValidity(); | 251 | checkValidity(); |
252 | + | ||
253 | + // FIXME: only the MASTER should be marking off-line in normal cases, | ||
254 | + // but if I was the last STANDBY connection, etc. and no one else | ||
255 | + // was there to mark the device offline, this instance may need to | ||
256 | + // temporarily request for Master Role and mark offline. | ||
240 | if (!mastershipService.getLocalRole(deviceId).equals(MastershipRole.MASTER)) { | 257 | if (!mastershipService.getLocalRole(deviceId).equals(MastershipRole.MASTER)) { |
241 | log.debug("Device {} disconnected, but I am not the master", deviceId); | 258 | log.debug("Device {} disconnected, but I am not the master", deviceId); |
242 | return; | 259 | return; |
243 | } | 260 | } |
244 | DeviceEvent event = store.markOffline(deviceId); | 261 | DeviceEvent event = store.markOffline(deviceId); |
245 | 262 | ||
263 | + mastershipService.relinquishMastership(deviceId); | ||
264 | + | ||
246 | //we're no longer capable of mastership. | 265 | //we're no longer capable of mastership. |
247 | if (event != null) { | 266 | if (event != null) { |
248 | log.info("Device {} disconnected", deviceId); | 267 | log.info("Device {} disconnected", deviceId); |
249 | - mastershipService.relinquishMastership(deviceId); | ||
250 | post(event); | 268 | post(event); |
251 | } | 269 | } |
252 | } | 270 | } |
... | @@ -286,6 +304,9 @@ public class DeviceManager | ... | @@ -286,6 +304,9 @@ public class DeviceManager |
286 | // FIXME: implement response to this notification | 304 | // FIXME: implement response to this notification |
287 | log.warn("Failed to assert role [{}] onto Device {}", role, | 305 | log.warn("Failed to assert role [{}] onto Device {}", role, |
288 | deviceId); | 306 | deviceId); |
307 | + if (role == MastershipRole.MASTER) { | ||
308 | + mastershipService.relinquishMastership(deviceId); | ||
309 | + } | ||
289 | } | 310 | } |
290 | } | 311 | } |
291 | 312 | ||
... | @@ -307,7 +328,7 @@ public class DeviceManager | ... | @@ -307,7 +328,7 @@ public class DeviceManager |
307 | .getMastershipTerm(event.subject()); | 328 | .getMastershipTerm(event.subject()); |
308 | 329 | ||
309 | if (term.master().equals(clusterService.getLocalNode().id())) { | 330 | if (term.master().equals(clusterService.getLocalNode().id())) { |
310 | - // only set if I am the master | 331 | + // only set the new term if I am the master |
311 | clockProviderService.setMastershipTerm(event.subject(), term); | 332 | clockProviderService.setMastershipTerm(event.subject(), term); |
312 | } | 333 | } |
313 | 334 | ... | ... |
-
Please register or login to post a comment