2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.protocol.bgp.rib.spi.util;
11 import com.google.common.util.concurrent.Uninterruptibles;
12 import java.util.concurrent.TimeUnit;
13 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
14 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
15 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
20 * Utility class which provides helper functionality for ClusterSingletonService.
22 public final class ClusterSingletonServiceRegistrationHelper {
24 private static final Logger LOG = LoggerFactory.getLogger(ClusterSingletonServiceRegistrationHelper.class);
25 private static final int MAX_REGISTRATION_ATTEMPTS = 10;
26 private static final int SLEEP_TIME_MILLIS = MAX_REGISTRATION_ATTEMPTS;
28 private ClusterSingletonServiceRegistrationHelper() {
29 throw new UnsupportedOperationException();
33 * This helper function wraps
34 * {@link ClusterSingletonServiceProvider#registerClusterSingletonService(ClusterSingletonService)} in order to
35 * execute repeated registration attempts while catching RuntimeException. If registration is not successful,
36 * RuntimeException is re-thrown.
38 * @param singletonProvider Cluster Singleton Service Provider
39 * @param clusterSingletonService Cluster Singleton Service
40 * @param maxAttempts Upper bound for registration retries count.
41 * @param sleepTime Sleep time between registration retries in milliseconds.
42 * @return Registration
44 @SuppressWarnings("checkstyle:IllegalCatch")
45 public static ClusterSingletonServiceRegistration registerSingletonService(
46 final ClusterSingletonServiceProvider singletonProvider,
47 final ClusterSingletonService clusterSingletonService, final int maxAttempts, final int sleepTime) {
48 int attempts = maxAttempts;
51 return singletonProvider.registerClusterSingletonService(clusterSingletonService);
52 } catch (final RuntimeException e) {
54 LOG.error("Giving up after {} registration attempts for service {}.", maxAttempts,
55 clusterSingletonService, e);
59 LOG.warn("Failed to register {} service to ClusterSingletonServiceProvider. Try again in {} ms.",
60 clusterSingletonService, sleepTime, e);
61 Uninterruptibles.sleepUninterruptibly(sleepTime, TimeUnit.MILLISECONDS);
67 * This helper function wraps
68 * {@link ClusterSingletonServiceProvider#registerClusterSingletonService(ClusterSingletonService)} in order to
69 * execute repeated registration attempts while catching RuntimeException. If registration is not successful,
70 * RuntimeException is re-thrown. 10 registration attempts will be tried with 10 ms pause between each
73 * @param singletonProvider Cluster Singleton Service Provider
74 * @param clusterSingletonService Cluster Singleton Service
75 * @return Registration
77 public static ClusterSingletonServiceRegistration registerSingletonService(final ClusterSingletonServiceProvider
78 singletonProvider, final ClusterSingletonService clusterSingletonService) {
79 return ClusterSingletonServiceRegistrationHelper.registerSingletonService(singletonProvider,
80 clusterSingletonService, MAX_REGISTRATION_ATTEMPTS, SLEEP_TIME_MILLIS);