blob: 91dcca52a2ab63577e1756354a2676dffda926ce [file] [log] [blame]
Jordan Haltermanaf35f7a2018-01-24 16:46:55 -08001/*
2 * Copyright 2018-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.store.primitives.impl;
17
18import java.util.concurrent.CompletableFuture;
19import java.util.function.Consumer;
20
21import com.google.common.cache.CacheBuilder;
22import com.google.common.cache.CacheLoader;
23import com.google.common.cache.LoadingCache;
24import org.onosproject.cluster.Leadership;
25import org.onosproject.cluster.NodeId;
26import org.onosproject.event.Change;
27import org.onosproject.store.service.AsyncLeaderElector;
28
29/**
30 * Caching async leader elector.
31 */
32public class CachingAsyncLeaderElector extends DelegatingAsyncLeaderElector {
33 private final LoadingCache<String, CompletableFuture<Leadership>> cache;
34 private final Consumer<Change<Leadership>> cacheUpdater;
35 private final Consumer<Status> statusListener;
36
37 public CachingAsyncLeaderElector(AsyncLeaderElector delegateLeaderElector) {
38 super(delegateLeaderElector);
39 cache = CacheBuilder.newBuilder()
40 .maximumSize(1000)
41 .build(CacheLoader.from(super::getLeadership));
42
43 cacheUpdater = change -> {
44 Leadership leadership = change.newValue();
45 cache.put(leadership.topic(), CompletableFuture.completedFuture(leadership));
46 };
47 statusListener = status -> {
48 if (status == Status.SUSPENDED || status == Status.INACTIVE) {
49 cache.invalidateAll();
50 }
51 };
52 addChangeListener(cacheUpdater);
53 addStatusChangeListener(statusListener);
54 }
55
56 @Override
57 public CompletableFuture<Leadership> getLeadership(String topic) {
58 return cache.getUnchecked(topic)
59 .whenComplete((r, e) -> {
60 if (e != null) {
61 cache.invalidate(topic);
62 }
63 });
64 }
65
66 @Override
67 public CompletableFuture<Leadership> run(String topic, NodeId nodeId) {
68 return super.run(topic, nodeId).whenComplete((r, e) -> cache.invalidate(topic));
69 }
70
71 @Override
72 public CompletableFuture<Void> withdraw(String topic) {
73 return super.withdraw(topic).whenComplete((r, e) -> cache.invalidate(topic));
74 }
75
76 @Override
77 public CompletableFuture<Boolean> anoint(String topic, NodeId nodeId) {
78 return super.anoint(topic, nodeId).whenComplete((r, e) -> cache.invalidate(topic));
79 }
80
81 @Override
82 public CompletableFuture<Boolean> promote(String topic, NodeId nodeId) {
83 return super.promote(topic, nodeId).whenComplete((r, e) -> cache.invalidate(topic));
84 }
85
86 @Override
87 public CompletableFuture<Void> evict(NodeId nodeId) {
88 return super.evict(nodeId).whenComplete((r, e) -> cache.invalidateAll());
89 }
90
91 @Override
92 public CompletableFuture<Void> destroy() {
93 removeStatusChangeListener(statusListener);
94 return removeChangeListener(cacheUpdater);
95 }
96}