1
package com.eucalyptus.cluster.callback;
3
import java.util.concurrent.ConcurrentMap;
4
import java.util.concurrent.atomic.AtomicBoolean;
5
import org.apache.log4j.Logger;
6
import com.eucalyptus.cluster.Cluster;
7
import com.eucalyptus.event.ClockTick;
8
import com.eucalyptus.event.Event;
9
import com.eucalyptus.event.EventListener;
10
import com.eucalyptus.records.EventRecord;
11
import com.eucalyptus.records.EventType;
12
import com.google.common.base.Predicate;
13
import com.google.common.collect.Iterables;
14
import com.google.common.collect.Maps;
16
public class StateUpdateHandler implements EventListener {
17
private static Logger LOG = Logger.getLogger( StateUpdateHandler.class );
18
private static final ConcurrentMap<String,StateUpdateHandler> clusterMap = Maps.newConcurrentHashMap( );
19
private final ConcurrentMap<Class,AtomicBoolean> inflightMap = Maps.newConcurrentHashMap( );
20
private final ConcurrentMap<Class,QueuedEventCallback> callbackMap = Maps.newConcurrentHashMap( );
21
private final Cluster cluster;
23
public static void create( Cluster cluster, QueuedEventCallback callback ) {
24
StateUpdateHandler handler = new StateUpdateHandler( cluster );
25
StateUpdateHandler.clusterMap.putIfAbsent( cluster.getName( ), handler );
26
handler = StateUpdateHandler.clusterMap.get( cluster.getName( ) );
27
EventRecord.here( StateUpdateHandler.class, EventType.CLUSTER_STATE_HANDLER_REGISTERED, cluster.getName( ), callback.getClass( ).getCanonicalName( ) );
30
private StateUpdateHandler( Cluster cluster ) {
31
this.cluster = cluster;
34
public boolean timedTrigger( Event c ) {
35
if ( c instanceof ClockTick ) {
36
return ( ( ClockTick ) c ).isBackEdge( );
42
public void addCallback( QueuedEventCallback cb ) {
43
if( this.callbackMap.putIfAbsent( cb.getClass( ), cb ) == null ) {
44
this.inflightMap.put( cb.getClass( ), new AtomicBoolean( false ) );
46
LOG.debug( "Ignoring addition of timed callback for cluster "+ this.cluster.getName( )+ " which is already configured: " + cb.getClass( ) );
51
public void advertiseEvent( Event event ) {}
54
public void fireEvent( Event event ) {
55
if( this.timedTrigger( event ) ) {
56
Iterables.all( callbackMap.keySet( ), new Predicate<Class>() {
59
public boolean apply( Class arg0 ) {
60
if( StateUpdateHandler.this.inflightMap.get( arg0 ).compareAndSet( false, true ) ) {
61
//TODO: RELEASE: wrap this up here.