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.Executors;
14 import java.util.concurrent.ScheduledExecutorService;
15 import java.util.concurrent.TimeUnit;
16 import java.util.concurrent.atomic.AtomicBoolean;
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 final ClusterSingletonServiceProvider singletonServiceProvider;
33 private volatile long flapCount = 0;
34 private AtomicBoolean active = new AtomicBoolean(true);
36 private volatile ClusterSingletonServiceRegistration registration;
38 private static ScheduledExecutorService EXECUTOR = Executors.newSingleThreadScheduledExecutor();
40 public FlappingSingletonService(final ClusterSingletonServiceProvider singletonServiceProvider) {
41 LOG.debug("Registering flapping-singleton-service.");
43 this.singletonServiceProvider = singletonServiceProvider;
44 registration = singletonServiceProvider.registerClusterSingletonService(this);
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 (final Exception e) {
58 LOG.warn("There was a problem closing flapping singleton service.", e);
59 flapCount = -flapCount;
65 public ListenableFuture<Void> closeServiceInstance() {
66 LOG.debug("Closing flapping-singleton-service, flapCount: {}", flapCount);
70 // TODO direct registration/close seem to trigger a bug in singleton state transitions,
71 // remove whole executor shenanigans after it's fixed.
72 // Needs to be delayed slightly otherwise it's triggered as well.
73 EXECUTOR.schedule(() -> {
74 LOG.debug("Running registration");
76 singletonServiceProvider.registerClusterSingletonService(this);
78 }, 200, TimeUnit.MILLISECONDS);
81 return Futures.immediateFuture(null);
85 public ServiceGroupIdentifier getIdentifier() {
86 return SERVICE_GROUP_IDENTIFIER;
89 public long setInactive() {
90 LOG.debug("Setting flapping-singleton-service to inactive, flap-count: {}", flapCount);