BUG-8618: fix test driver
[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.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;
22
23 public class FlappingSingletonService implements ClusterSingletonService {
24
25     private static final Logger LOG = LoggerFactory.getLogger(FlappingSingletonService.class);
26
27     private static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER =
28             ServiceGroupIdentifier.create("flapping-singleton-service");
29
30     private static final ScheduledExecutorService EXECUTOR = FinalizableScheduledExecutorService.newSingleThread();
31
32     private final ClusterSingletonServiceProvider singletonServiceProvider;
33     private final AtomicBoolean active = new AtomicBoolean(true);
34
35     private volatile long flapCount = 0;
36     private volatile ClusterSingletonServiceRegistration registration;
37
38     public FlappingSingletonService(final ClusterSingletonServiceProvider singletonServiceProvider) {
39         LOG.debug("Registering flapping-singleton-service.");
40
41         this.singletonServiceProvider = singletonServiceProvider;
42         registration = singletonServiceProvider.registerClusterSingletonService(this);
43     }
44
45     @Override
46     public void instantiateServiceInstance() {
47         LOG.debug("Instantiating flapping-singleton-service.");
48
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(() -> {
52             try {
53                 registration.close();
54                 registration = null;
55             } catch (final Exception e) {
56                 LOG.warn("There was a problem closing flapping singleton service.", e);
57                 setInactive();
58                 flapCount = -flapCount;
59             }
60         });
61     }
62
63     @Override
64     public ListenableFuture<Void> closeServiceInstance() {
65         LOG.debug("Closing flapping-singleton-service, flapCount: {}", flapCount);
66
67         flapCount++;
68         if (active.get()) {
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");
74                 try {
75                     registration = singletonServiceProvider.registerClusterSingletonService(this);
76                 } catch (final Exception e) {
77                     LOG.warn("There was a problem re-registering flapping singleton service.", e);
78                     setInactive();
79                     flapCount = -flapCount - 1;
80                 }
81
82             }, 200, TimeUnit.MILLISECONDS);
83         }
84
85         return Futures.immediateFuture(null);
86     }
87
88     @Override
89     public ServiceGroupIdentifier getIdentifier() {
90         return SERVICE_GROUP_IDENTIFIER;
91     }
92
93     public long setInactive() {
94         LOG.debug("Setting flapping-singleton-service to inactive, flap-count: {}", flapCount);
95
96         active.set(false);
97         return flapCount;
98     }
99 }

©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.