3ca46b128fdacf6dea475d47d2d6c5f4ec554fc5
[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 java.util.concurrent.atomic.AtomicLong;
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 static final ScheduledExecutorService EXECUTOR = FinalizableScheduledExecutorService.newSingleThread();
32
33     private final ClusterSingletonServiceProvider singletonServiceProvider;
34     private final AtomicBoolean active = new AtomicBoolean(true);
35
36     private final AtomicLong flapCount = new AtomicLong();
37     private volatile ClusterSingletonServiceRegistration registration;
38
39     public FlappingSingletonService(final ClusterSingletonServiceProvider singletonServiceProvider) {
40         LOG.debug("Registering flapping-singleton-service.");
41
42         this.singletonServiceProvider = singletonServiceProvider;
43         registration = singletonServiceProvider.registerClusterSingletonService(this);
44     }
45
46     @Override
47     @SuppressWarnings("checkstyle:IllegalCatch")
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 (Exception e) {
58                 LOG.warn("There was a problem closing flapping singleton service.", e);
59                 setInactive();
60
61                 final long count = flapCount.get();
62                 flapCount.compareAndSet(count, -count);
63             }
64         });
65     }
66
67     @Override
68     @SuppressWarnings("checkstyle:IllegalCatch")
69     public ListenableFuture<Void> closeServiceInstance() {
70         LOG.debug("Closing flapping-singleton-service, flapCount: {}", flapCount);
71
72         flapCount.incrementAndGet();
73         if (active.get()) {
74             // TODO direct registration/close seem to trigger a bug in singleton state transitions,
75             // remove  whole executor shenanigans after it's fixed.
76             // Needs to be delayed slightly otherwise it's triggered as well.
77             EXECUTOR.schedule(() -> {
78                 LOG.debug("Running re-registration");
79                 try {
80                     registration = singletonServiceProvider.registerClusterSingletonService(this);
81                 } catch (RuntimeException e) {
82                     LOG.warn("There was a problem re-registering flapping singleton service.", e);
83                     setInactive();
84
85                     final long count = flapCount.get();
86                     flapCount.compareAndSet(count, -count - 1);
87                 }
88
89             }, 200, TimeUnit.MILLISECONDS);
90         }
91
92         return Futures.immediateFuture(null);
93     }
94
95     @Override
96     public ServiceGroupIdentifier getIdentifier() {
97         return SERVICE_GROUP_IDENTIFIER;
98     }
99
100     public long setInactive() {
101         LOG.debug("Setting flapping-singleton-service to inactive, flap-count: {}", flapCount);
102
103         active.set(false);
104         return flapCount.get();
105     }
106 }

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