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
9 package org.opendaylight.controller.clustering.it.provider.impl;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.concurrent.ScheduledExecutorService;
14 import java.util.concurrent.TimeUnit;
15 import java.util.concurrent.atomic.AtomicBoolean;
16 import java.util.concurrent.atomic.AtomicLong;
17 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
18 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
19 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
20 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 public class FlappingSingletonService implements ClusterSingletonService {
26 private static final Logger LOG = LoggerFactory.getLogger(FlappingSingletonService.class);
28 private static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER =
29 ServiceGroupIdentifier.create("flapping-singleton-service");
31 private static final ScheduledExecutorService EXECUTOR = FinalizableScheduledExecutorService.newSingleThread();
33 private final ClusterSingletonServiceProvider singletonServiceProvider;
34 private final AtomicBoolean active = new AtomicBoolean(true);
36 private final AtomicLong flapCount = new AtomicLong();
37 private volatile ClusterSingletonServiceRegistration registration;
39 public FlappingSingletonService(final ClusterSingletonServiceProvider singletonServiceProvider) {
40 LOG.debug("Registering flapping-singleton-service.");
42 this.singletonServiceProvider = singletonServiceProvider;
43 registration = singletonServiceProvider.registerClusterSingletonService(this);
47 @SuppressWarnings("checkstyle:IllegalCatch")
48 public void instantiateServiceInstance() {
49 LOG.debug("Instantiating flapping-singleton-service.");
51 // TODO direct registration/close seem to trigger a bug in singleton state transitions,
52 // remove the whole executor shenanigans after it's fixed.
53 EXECUTOR.submit(() -> {
57 } catch (Exception e) {
58 LOG.warn("There was a problem closing flapping singleton service.", e);
61 final long count = flapCount.get();
62 flapCount.compareAndSet(count, -count);
68 @SuppressWarnings("checkstyle:IllegalCatch")
69 public ListenableFuture<Void> closeServiceInstance() {
70 LOG.debug("Closing flapping-singleton-service, flapCount: {}", flapCount);
72 flapCount.incrementAndGet();
74 // TODO direct registration/close seem to trigger a bug in singleton state transitions,
75 // remove whole executor shenanigans after it's fixed.
76 // Needs to be delayed slightly otherwise it's triggered as well.
77 EXECUTOR.schedule(() -> {
78 LOG.debug("Running re-registration");
80 registration = singletonServiceProvider.registerClusterSingletonService(this);
81 } catch (RuntimeException e) {
82 LOG.warn("There was a problem re-registering flapping singleton service.", e);
85 final long count = flapCount.get();
86 flapCount.compareAndSet(count, -count - 1);
89 }, 200, TimeUnit.MILLISECONDS);
92 return Futures.immediateFuture(null);
96 public ServiceGroupIdentifier getIdentifier() {
97 return SERVICE_GROUP_IDENTIFIER;
100 public long setInactive() {
101 LOG.debug("Setting flapping-singleton-service to inactive, flap-count: {}", flapCount);
104 return flapCount.get();