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 org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
17 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
18 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
19 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 public class FlappingSingletonService implements ClusterSingletonService {
25 private static final Logger LOG = LoggerFactory.getLogger(FlappingSingletonService.class);
27 private static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER =
28 ServiceGroupIdentifier.create("flapping-singleton-service");
30 private static final ScheduledExecutorService EXECUTOR = FinalizableScheduledExecutorService.newSingleThread();
32 private final ClusterSingletonServiceProvider singletonServiceProvider;
33 private final AtomicBoolean active = new AtomicBoolean(true);
35 private volatile long flapCount = 0;
36 private volatile ClusterSingletonServiceRegistration registration;
38 public FlappingSingletonService(final ClusterSingletonServiceProvider singletonServiceProvider) {
39 LOG.debug("Registering flapping-singleton-service.");
41 this.singletonServiceProvider = singletonServiceProvider;
42 registration = singletonServiceProvider.registerClusterSingletonService(this);
46 @SuppressWarnings("checkstyle:IllegalCatch")
47 public void instantiateServiceInstance() {
48 LOG.debug("Instantiating flapping-singleton-service.");
50 // TODO direct registration/close seem to trigger a bug in singleton state transitions,
51 // remove the whole executor shenanigans after it's fixed.
52 EXECUTOR.submit(() -> {
56 } catch (Exception e) {
57 LOG.warn("There was a problem closing flapping singleton service.", e);
59 flapCount = -flapCount;
65 @SuppressWarnings("checkstyle:IllegalCatch")
66 public ListenableFuture<Void> closeServiceInstance() {
67 LOG.debug("Closing flapping-singleton-service, flapCount: {}", flapCount);
71 // TODO direct registration/close seem to trigger a bug in singleton state transitions,
72 // remove whole executor shenanigans after it's fixed.
73 // Needs to be delayed slightly otherwise it's triggered as well.
74 EXECUTOR.schedule(() -> {
75 LOG.debug("Running re-registration");
77 registration = singletonServiceProvider.registerClusterSingletonService(this);
78 } catch (RuntimeException e) {
79 LOG.warn("There was a problem re-registering flapping singleton service.", e);
81 flapCount = -flapCount - 1;
84 }, 200, TimeUnit.MILLISECONDS);
87 return Futures.immediateFuture(null);
91 public ServiceGroupIdentifier getIdentifier() {
92 return SERVICE_GROUP_IDENTIFIER;
95 public long setInactive() {
96 LOG.debug("Setting flapping-singleton-service to inactive, flap-count: {}", flapCount);