2 * Copyright (c) 2017 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
8 package org.opendaylight.controller.clustering.it.provider.impl;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.concurrent.atomic.AtomicBoolean;
15 import java.util.concurrent.atomic.AtomicLong;
16 import org.opendaylight.mdsal.singleton.api.ClusterSingletonService;
17 import org.opendaylight.mdsal.singleton.api.ClusterSingletonServiceProvider;
18 import org.opendaylight.mdsal.singleton.api.ServiceGroupIdentifier;
19 import org.opendaylight.yangtools.concepts.Registration;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 public final class FlappingSingletonService implements ClusterSingletonService {
24 private static final Logger LOG = LoggerFactory.getLogger(FlappingSingletonService.class);
25 private static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER =
26 new ServiceGroupIdentifier("flapping-singleton-service");
28 private final ClusterSingletonServiceProvider singletonServiceProvider;
29 private final AtomicBoolean active = new AtomicBoolean(true);
30 private final AtomicLong flapCount = new AtomicLong();
32 private volatile Registration registration;
34 public FlappingSingletonService(final ClusterSingletonServiceProvider singletonServiceProvider) {
35 LOG.debug("Registering flapping-singleton-service.");
36 this.singletonServiceProvider = requireNonNull(singletonServiceProvider);
37 registration = singletonServiceProvider.registerClusterSingletonService(this);
41 @SuppressWarnings("checkstyle:IllegalCatch")
42 public void instantiateServiceInstance() {
43 LOG.debug("Instantiating flapping-singleton-service.");
47 } catch (Exception e) {
48 LOG.warn("There was a problem closing flapping singleton service.", e);
51 final long count = flapCount.get();
52 flapCount.compareAndSet(count, -count);
57 @SuppressWarnings("checkstyle:IllegalCatch")
58 public ListenableFuture<Void> closeServiceInstance() {
59 LOG.debug("Closing flapping-singleton-service, flapCount: {}", flapCount);
61 flapCount.incrementAndGet();
63 LOG.debug("Running re-registration");
65 registration = singletonServiceProvider.registerClusterSingletonService(this);
66 } catch (RuntimeException e) {
67 LOG.warn("There was a problem re-registering flapping singleton service.", e);
70 final long count = flapCount.get();
71 flapCount.compareAndSet(count, -count - 1);
75 return Futures.immediateFuture(null);
79 public ServiceGroupIdentifier getIdentifier() {
80 return SERVICE_GROUP_IDENTIFIER;
83 public long setInactive() {
84 LOG.debug("Setting flapping-singleton-service to inactive, flap-count: {}", flapCount);
87 return flapCount.get();