Committed by
Gerrit Code Review
Add basic error handling to gRPC Device SB service
Change-Id: Idd19bd4e0d35d8eda06acb4cb631c5d4bcd21980
Showing
2 changed files
with
60 additions
and
12 deletions
... | @@ -65,6 +65,8 @@ final class DeviceProviderServiceClientProxy | ... | @@ -65,6 +65,8 @@ final class DeviceProviderServiceClientProxy |
65 | 65 | ||
66 | private final Channel channel; | 66 | private final Channel channel; |
67 | 67 | ||
68 | + private Throwable error; | ||
69 | + | ||
68 | DeviceProviderServiceClientProxy(DeviceProvider provider, Channel channel) { | 70 | DeviceProviderServiceClientProxy(DeviceProvider provider, Channel channel) { |
69 | super(provider); | 71 | super(provider); |
70 | this.channel = channel; | 72 | this.channel = channel; |
... | @@ -194,8 +196,26 @@ final class DeviceProviderServiceClientProxy | ... | @@ -194,8 +196,26 @@ final class DeviceProviderServiceClientProxy |
194 | log.error("Shutting down session over {}", channel.authority()); | 196 | log.error("Shutting down session over {}", channel.authority()); |
195 | // initiate abnormal termination from client | 197 | // initiate abnormal termination from client |
196 | devProvService.onError(t); | 198 | devProvService.onError(t); |
197 | - invalidate(); | 199 | + invalidate(t); |
200 | + } | ||
201 | + } | ||
202 | + | ||
203 | + /** | ||
204 | + * Invalidates the ProviderService indicating Failure. | ||
205 | + * @param t {@link Throwable} describing last failure | ||
206 | + */ | ||
207 | + private void invalidate(Throwable t) { | ||
208 | + this.error = t; | ||
209 | + invalidate(); | ||
210 | + } | ||
211 | + | ||
212 | + @Override | ||
213 | + public void checkValidity() { | ||
214 | + if (error != null) { | ||
215 | + throw new IllegalStateException("DeviceProviderService no longer valid", | ||
216 | + error); | ||
198 | } | 217 | } |
218 | + super.checkValidity(); | ||
199 | } | 219 | } |
200 | 220 | ||
201 | @Override | 221 | @Override |
... | @@ -272,17 +292,14 @@ final class DeviceProviderServiceClientProxy | ... | @@ -272,17 +292,14 @@ final class DeviceProviderServiceClientProxy |
272 | public void onCompleted() { | 292 | public void onCompleted() { |
273 | log.info("DeviceProviderClientProxy completed"); | 293 | log.info("DeviceProviderClientProxy completed"); |
274 | // session terminated from remote | 294 | // session terminated from remote |
275 | - // TODO unregister...? how? | 295 | + invalidate(); |
276 | - | ||
277 | - //devProvService.onCompleted(); | ||
278 | } | 296 | } |
279 | 297 | ||
280 | @Override | 298 | @Override |
281 | public void onError(Throwable t) { | 299 | public void onError(Throwable t) { |
282 | log.error("DeviceProviderClientProxy#onError", t); | 300 | log.error("DeviceProviderClientProxy#onError", t); |
283 | // session terminated from remote | 301 | // session terminated from remote |
284 | - // TODO unregister...? how? | 302 | + invalidate(t); |
285 | - //devProvService.onError(t); | ||
286 | } | 303 | } |
287 | 304 | ||
288 | @Override | 305 | @Override | ... | ... |
... | @@ -397,6 +397,16 @@ public class GrpcRemoteServiceServer { | ... | @@ -397,6 +397,16 @@ public class GrpcRemoteServiceServer { |
397 | 397 | ||
398 | @Override | 398 | @Override |
399 | public void triggerProbe(DeviceId deviceId) { | 399 | public void triggerProbe(DeviceId deviceId) { |
400 | + try { | ||
401 | + onTriggerProbe(deviceId); | ||
402 | + } catch (Exception e) { | ||
403 | + log.error("Exception caught handling triggerProbe({})", | ||
404 | + deviceId, e); | ||
405 | + toDeviceProvider.onError(e); | ||
406 | + } | ||
407 | + } | ||
408 | + | ||
409 | + private void onTriggerProbe(DeviceId deviceId) { | ||
400 | log.trace("triggerProbe({})", deviceId); | 410 | log.trace("triggerProbe({})", deviceId); |
401 | DeviceProviderMsg.Builder msgBuilder = DeviceProviderMsg.newBuilder(); | 411 | DeviceProviderMsg.Builder msgBuilder = DeviceProviderMsg.newBuilder(); |
402 | msgBuilder.setTriggerProbe(msgBuilder.getTriggerProbeBuilder() | 412 | msgBuilder.setTriggerProbe(msgBuilder.getTriggerProbeBuilder() |
... | @@ -404,11 +414,20 @@ public class GrpcRemoteServiceServer { | ... | @@ -404,11 +414,20 @@ public class GrpcRemoteServiceServer { |
404 | .build()); | 414 | .build()); |
405 | DeviceProviderMsg triggerProbeMsg = msgBuilder.build(); | 415 | DeviceProviderMsg triggerProbeMsg = msgBuilder.build(); |
406 | toDeviceProvider.onNext(triggerProbeMsg); | 416 | toDeviceProvider.onNext(triggerProbeMsg); |
407 | - // TODO Catch Exceptions and call onError() | ||
408 | } | 417 | } |
409 | 418 | ||
410 | @Override | 419 | @Override |
411 | public void roleChanged(DeviceId deviceId, MastershipRole newRole) { | 420 | public void roleChanged(DeviceId deviceId, MastershipRole newRole) { |
421 | + try { | ||
422 | + onRoleChanged(deviceId, newRole); | ||
423 | + } catch (Exception e) { | ||
424 | + log.error("Exception caught handling onRoleChanged({}, {})", | ||
425 | + deviceId, newRole, e); | ||
426 | + toDeviceProvider.onError(e); | ||
427 | + } | ||
428 | + } | ||
429 | + | ||
430 | + private void onRoleChanged(DeviceId deviceId, MastershipRole newRole) { | ||
412 | log.trace("roleChanged({}, {})", deviceId, newRole); | 431 | log.trace("roleChanged({}, {})", deviceId, newRole); |
413 | DeviceProviderMsg.Builder msgBuilder = DeviceProviderMsg.newBuilder(); | 432 | DeviceProviderMsg.Builder msgBuilder = DeviceProviderMsg.newBuilder(); |
414 | msgBuilder.setRoleChanged(msgBuilder.getRoleChangedBuilder() | 433 | msgBuilder.setRoleChanged(msgBuilder.getRoleChangedBuilder() |
... | @@ -416,11 +435,22 @@ public class GrpcRemoteServiceServer { | ... | @@ -416,11 +435,22 @@ public class GrpcRemoteServiceServer { |
416 | .setNewRole(translate(newRole)) | 435 | .setNewRole(translate(newRole)) |
417 | .build()); | 436 | .build()); |
418 | toDeviceProvider.onNext(msgBuilder.build()); | 437 | toDeviceProvider.onNext(msgBuilder.build()); |
419 | - // TODO Catch Exceptions and call onError() | ||
420 | } | 438 | } |
421 | 439 | ||
422 | @Override | 440 | @Override |
423 | public boolean isReachable(DeviceId deviceId) { | 441 | public boolean isReachable(DeviceId deviceId) { |
442 | + try { | ||
443 | + return onIsReachable(deviceId); | ||
444 | + } catch (Exception e) { | ||
445 | + log.error("Exception caught handling onIsReachable({})", | ||
446 | + deviceId, e); | ||
447 | + toDeviceProvider.onError(e); | ||
448 | + return false; | ||
449 | + } | ||
450 | + } | ||
451 | + | ||
452 | + private boolean onIsReachable(DeviceId deviceId) { | ||
453 | + | ||
424 | log.trace("isReachable({})", deviceId); | 454 | log.trace("isReachable({})", deviceId); |
425 | CompletableFuture<Boolean> result = new CompletableFuture<>(); | 455 | CompletableFuture<Boolean> result = new CompletableFuture<>(); |
426 | final int xid = xidPool.incrementAndGet(); | 456 | final int xid = xidPool.incrementAndGet(); |
... | @@ -450,10 +480,10 @@ public class GrpcRemoteServiceServer { | ... | @@ -450,10 +480,10 @@ public class GrpcRemoteServiceServer { |
450 | log.warn("isReachable({}) Timed out", deviceId, e); | 480 | log.warn("isReachable({}) Timed out", deviceId, e); |
451 | } catch (ExecutionException e) { | 481 | } catch (ExecutionException e) { |
452 | log.error("isReachable({}) Execution failed", deviceId, e); | 482 | log.error("isReachable({}) Execution failed", deviceId, e); |
453 | - // close session? | 483 | + // close session |
484 | + toDeviceProvider.onError(e); | ||
454 | } | 485 | } |
455 | return false; | 486 | return false; |
456 | - // TODO Catch Exceptions and call onError() | ||
457 | } | 487 | } |
458 | 488 | ||
459 | @Override | 489 | @Override |
... | @@ -464,8 +494,9 @@ public class GrpcRemoteServiceServer { | ... | @@ -464,8 +494,9 @@ public class GrpcRemoteServiceServer { |
464 | @Override | 494 | @Override |
465 | public void changePortState(DeviceId deviceId, PortNumber portNumber, | 495 | public void changePortState(DeviceId deviceId, PortNumber portNumber, |
466 | boolean enable) { | 496 | boolean enable) { |
467 | - // TODO if required | 497 | + // TODO Implement if required |
468 | - | 498 | + log.error("changePortState not supported yet"); |
499 | + toDeviceProvider.onError(new UnsupportedOperationException("not implemented yet")); | ||
469 | } | 500 | } |
470 | 501 | ||
471 | } | 502 | } | ... | ... |
-
Please register or login to post a comment