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 public void instantiateServiceInstance() {
47 LOG.debug("Instantiating flapping-singleton-service.");
49 // TODO direct registration/close seem to trigger a bug in singleton state transitions,
50 // remove the whole executor shenanigans after it's fixed.
51 EXECUTOR.submit(() -> {
55 } catch (final Exception e) {
56 LOG.warn("There was a problem closing flapping singleton service.", e);
58 flapCount = -flapCount;
64 public ListenableFuture<Void> closeServiceInstance() {
65 LOG.debug("Closing flapping-singleton-service, flapCount: {}", flapCount);
69 // TODO direct registration/close seem to trigger a bug in singleton state transitions,
70 // remove whole executor shenanigans after it's fixed.
71 // Needs to be delayed slightly otherwise it's triggered as well.
72 EXECUTOR.schedule(() -> {
73 LOG.debug("Running re-registration");
75 registration = singletonServiceProvider.registerClusterSingletonService(this);
76 } catch (final Exception e) {
77 LOG.warn("There was a problem re-registering flapping singleton service.", e);
79 flapCount = -flapCount - 1;
82 }, 200, TimeUnit.MILLISECONDS);
85 return Futures.immediateFuture(null);
89 public ServiceGroupIdentifier getIdentifier() {
90 return SERVICE_GROUP_IDENTIFIER;
93 public long setInactive() {
94 LOG.debug("Setting flapping-singleton-service to inactive, flap-count: {}", flapCount);