5ca33ad51d63e268ce905a6cb87c44b9fb84dc81
[controller.git] / opendaylight / md-sal / samples / clustering-test-app / provider / src / main / java / org / opendaylight / controller / clustering / it / provider / impl / FlappingSingletonService.java
1 /*
2  * Copyright (c) 2017 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.clustering.it.provider.impl;
10
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;
23
24 public class FlappingSingletonService implements ClusterSingletonService {
25
26     private static final Logger LOG = LoggerFactory.getLogger(FlappingSingletonService.class);
27
28     private static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER =
29             ServiceGroupIdentifier.create("flapping-singleton-service");
30
31     private final ClusterSingletonServiceProvider singletonServiceProvider;
32
33     private volatile long flapCount = 0;
34     private AtomicBoolean active = new AtomicBoolean(true);
35
36     private volatile ClusterSingletonServiceRegistration registration;
37
38     private static ScheduledExecutorService EXECUTOR = Executors.newSingleThreadScheduledExecutor();
39
40     public FlappingSingletonService(final ClusterSingletonServiceProvider singletonServiceProvider) {
41         LOG.debug("Registering flapping-singleton-service.");
42
43         this.singletonServiceProvider = singletonServiceProvider;
44         registration = singletonServiceProvider.registerClusterSingletonService(this);
45     }
46
47     @Override
48     public void instantiateServiceInstance() {
49         LOG.debug("Instantiating flapping-singleton-service.");
50
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(() -> {
54             try {
55                 registration.close();
56                 registration = null;
57             } catch (final Exception e) {
58                 LOG.warn("There was a problem closing flapping singleton service.", e);
59                 setInactive();
60                 flapCount = -flapCount;
61             }
62         });
63     }
64
65     @Override
66     public ListenableFuture<Void> closeServiceInstance() {
67         LOG.debug("Closing flapping-singleton-service, flapCount: {}", flapCount);
68
69         flapCount++;
70         if (active.get()) {
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");
76                 try {
77                     registration =
78                             singletonServiceProvider.registerClusterSingletonService(this);
79                 } catch (final Exception e) {
80                     LOG.warn("There was a problem re-registering flapping singleton service.", e);
81                     setInactive();
82                     flapCount = -flapCount - 1;
83                 }
84
85             }, 200, TimeUnit.MILLISECONDS);
86         }
87
88         return Futures.immediateFuture(null);
89     }
90
91     @Override
92     public ServiceGroupIdentifier getIdentifier() {
93         return SERVICE_GROUP_IDENTIFIER;
94     }
95
96     public long setInactive() {
97         LOG.debug("Setting flapping-singleton-service to inactive, flap-count: {}", flapCount);
98
99         active.set(false);
100         return flapCount;
101     }
102 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.