BUG 2138 - Do not fail on module-based default shard
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / sharding / LookupTask.java
1 /*
2  * Copyright (c) 2017 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.cluster.sharding;
10
11 import static akka.actor.ActorRef.noSender;
12
13 import akka.actor.ActorRef;
14 import akka.actor.Status;
15 import javax.annotation.Nullable;
16 import javax.annotation.concurrent.NotThreadSafe;
17
18 /**
19  * Base class for lookup tasks. Lookup tasks are supposed to run repeatedly
20  * until successful lookup or maximum retries are hit.
21  */
22 @NotThreadSafe
23 abstract class LookupTask implements Runnable {
24     private final int maxRetries;
25     private final ActorRef replyTo;
26     private int retries = 0;
27
28     LookupTask(final ActorRef replyTo, final int maxRetries) {
29         this.replyTo = replyTo;
30         this.maxRetries = maxRetries;
31     }
32
33     abstract void reschedule(int retries);
34
35     void tryReschedule(@Nullable final Throwable throwable) {
36         if (retries <= maxRetries) {
37             retries++;
38             reschedule(retries);
39         } else {
40             fail(throwable);
41         }
42     }
43
44     void fail(@Nullable final Throwable throwable) {
45         if (throwable == null) {
46             replyTo.tell(new Status.Failure(
47                     new DOMDataTreeShardCreationFailedException("Unable to find the backend shard."
48                             + "Failing..")), noSender());
49         } else {
50             replyTo.tell(new Status.Failure(
51                     new DOMDataTreeShardCreationFailedException("Unable to find the backend shard."
52                             + "Failing..", throwable)), noSender());
53         }
54     }
55 }