2 * Copyright (c) 2015 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.cluster.datastore.entityownership.selectionstrategy;
11 import akka.actor.ActorRef;
12 import akka.actor.Cancellable;
13 import akka.actor.Scheduler;
14 import com.google.common.base.Preconditions;
15 import java.util.Collection;
16 import java.util.concurrent.TimeUnit;
17 import org.opendaylight.controller.cluster.datastore.entityownership.messages.SelectOwner;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import scala.concurrent.ExecutionContextExecutor;
20 import scala.concurrent.duration.FiniteDuration;
23 * The EntityOwnerSelectionStrategyWrapper is an EntityOwnerSelectionStrategy decorator that adds the ability to
24 * schedule an owner selection job.
26 public class EntityOwnerSelectionStrategyWrapper implements EntityOwnerSelectionStrategy {
27 private final Scheduler scheduler;
28 private final ActorRef shard;
29 private final ExecutionContextExecutor dispatcher;
30 private final EntityOwnerSelectionStrategy strategy;
32 private Cancellable lastScheduledTask;
34 public EntityOwnerSelectionStrategyWrapper(Scheduler scheduler,
36 ExecutionContextExecutor dispatcher,
37 EntityOwnerSelectionStrategy strategy) {
38 this.scheduler = Preconditions.checkNotNull(scheduler);
39 this.shard = Preconditions.checkNotNull(shard);
40 this.dispatcher = Preconditions.checkNotNull(dispatcher);
41 this.strategy = Preconditions.checkNotNull(strategy);
45 * Schedule a new owner selection job. Cancelling any outstanding job if it has not been cancelled.
48 * @param allCandidates
50 public void scheduleOwnerSelection(YangInstanceIdentifier entityPath, Collection<String> allCandidates){
51 if(lastScheduledTask != null && !lastScheduledTask.isCancelled()){
52 lastScheduledTask.cancel();
54 lastScheduledTask = scheduler.scheduleOnce(
55 FiniteDuration.apply(strategy.selectionDelayInMillis(), TimeUnit.MILLISECONDS)
56 , shard, new SelectOwner(entityPath, allCandidates, strategy)
61 public long selectionDelayInMillis(){
62 return strategy.selectionDelayInMillis();
66 public String newOwner(Collection<String> viableCandidates){
67 return strategy.newOwner(viableCandidates);