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 com.google.common.util.concurrent.Futures;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import java.util.concurrent.atomic.AtomicBoolean;
13 import java.util.concurrent.atomic.AtomicLong;
14 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
15 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
16 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
17 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
21 public final class FlappingSingletonService implements ClusterSingletonService {
22 private static final Logger LOG = LoggerFactory.getLogger(FlappingSingletonService.class);
23 private static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER =
24 ServiceGroupIdentifier.create("flapping-singleton-service");
26 private final ClusterSingletonServiceProvider singletonServiceProvider;
27 private final AtomicBoolean active = new AtomicBoolean(true);
29 private final AtomicLong flapCount = new AtomicLong();
30 private volatile ClusterSingletonServiceRegistration registration;
32 public FlappingSingletonService(final ClusterSingletonServiceProvider singletonServiceProvider) {
33 LOG.debug("Registering flapping-singleton-service.");
35 this.singletonServiceProvider = singletonServiceProvider;
36 registration = singletonServiceProvider.registerClusterSingletonService(this);
40 @SuppressWarnings("checkstyle:IllegalCatch")
41 public void instantiateServiceInstance() {
42 LOG.debug("Instantiating flapping-singleton-service.");
46 } catch (Exception e) {
47 LOG.warn("There was a problem closing flapping singleton service.", e);
50 final long count = flapCount.get();
51 flapCount.compareAndSet(count, -count);
56 @SuppressWarnings("checkstyle:IllegalCatch")
57 public ListenableFuture<Void> closeServiceInstance() {
58 LOG.debug("Closing flapping-singleton-service, flapCount: {}", flapCount);
60 flapCount.incrementAndGet();
62 LOG.debug("Running re-registration");
64 registration = singletonServiceProvider.registerClusterSingletonService(this);
65 } catch (RuntimeException e) {
66 LOG.warn("There was a problem re-registering flapping singleton service.", e);
69 final long count = flapCount.get();
70 flapCount.compareAndSet(count, -count - 1);
74 return Futures.immediateFuture(null);
78 public ServiceGroupIdentifier getIdentifier() {
79 return SERVICE_GROUP_IDENTIFIER;
82 public long setInactive() {
83 LOG.debug("Setting flapping-singleton-service to inactive, flap-count: {}", flapCount);
86 return flapCount.get();