~ubuntu-branches/ubuntu/maverick/eucalyptus/maverick

« back to all changes in this revision

Viewing changes to clc/modules/msgs/src/main/java/com/eucalyptus/util/TimedEvictionSet.java

  • Committer: Bazaar Package Importer
  • Author(s): Dave Walker (Daviey)
  • Date: 2010-07-21 17:27:10 UTC
  • mfrom: (1.1.38 upstream)
  • Revision ID: james.westby@ubuntu.com-20100721172710-7xv07dmdqgivc3t9
Tags: 2.0~bzr1211-0ubuntu1
* New major upstream version merge, 2.0 (r1211).
* debian/patches/:
  - 01-wsdl-stubs.patch, debian/wsdl.md5sums: wsdl stubs updated.
  - 02-Makefile.patch: Updated to reflect new code layout.
  - 07-local_support_euca_conf-in.patch: Updated to reflect new code layout.
  - 08-ubuntu-default-networking.patch: Refreshed.
  - 09-small-128-192MB.patch: Updated to point to new location.
  - 10-disable-iscsi.patch: Refreshed.
  - 11-state-cleanup-memleakfix.patch: Removed, fixed upstream.
  - 15-fix-default-ramdisk.patch: Updated to point to new location.
  - 16-kvm_libvirt_xml_default_use_kvm.patch: Updated to reflect changes.
  - 17-fix_walrus_OOM_errors.patch: Removed, fixed upstream.
  - 18-priv_security.patch: Updated to reflect upstream changes.
  - 20-brute-force-webui.patch: Updated to reflect upstream changes. 
  - 21-eucalyptus-1.7-with-gwt-1.6.4.patch: New patch, allows 
    eucalyptus-1.7 to be built against gwt 1.6.4. Based on patch courtesy 
    of Dmitrii Zagorodnov, upstream. (LP: #597330)
* debian/eucalyptus-java-common.links: 
  - Changed symlink for groovy, point to groovy.all.jar, making compatiable 
    with groovy versions >1.7. (LP: #595421)
  - Added ant.jar & jetty-rewrite-handler.jar as they are now required.
* debian/control
  - & debian/build-jars: Added libjavassist-java and libjetty-extra-java as 
    build dependencies.
  - Added libjetty-extra-java as a dependency of eucalyptus-java-common
* The binary resulting jar's have been renamed from eucalyptus-*-1.6.2.jar
  to eucalyptus-*-main.jar:    
  - debian/eucalyptus-cc.upstart
  - debian/eucalyptus-cloud.install
  - debian/eucalyptus-common.eucalyptus.upstart
  - debian/eucalyptus-java-common.install
  - debian/eucalyptus-network.upstart
  - debian/eucalyptus-sc.install
  - debian/eucalyptus-walrus.install
* debian/eucalyptus-java-common.install: New upstream jars that have been
  installed:
  - eucalyptus-db-hsqldb-ext-main.jar
  - eucalyptus-component-main.jar
* debian/control:
  - Updated Standards Version to 3.8.4 (no change)
  - Updated the upstream Homepage to: http://open.eucalyptus.com/
  - Changed Vcs-Bzr to reflect new location of Ubuntu hosted development branch.
  - Made the Build Dependency of groovy and the binary eucalyptus-java-common
    package depend on version >=1.7.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package com.eucalyptus.util;
 
2
 
 
3
import java.util.Collection;
 
4
import java.util.Comparator;
 
5
import java.util.Iterator;
 
6
import java.util.NavigableSet;
 
7
import java.util.Set;
 
8
import java.util.SortedSet;
 
9
import java.util.concurrent.ConcurrentSkipListSet;
 
10
import java.util.concurrent.TimeUnit;
 
11
import java.util.concurrent.atomic.AtomicBoolean;
 
12
import org.apache.log4j.Logger;
 
13
 
 
14
public class TimedEvictionSet<E extends Comparable> implements Set<E> {
 
15
  private static Logger LOG = Logger.getLogger( TimedEvictionSet.class );
 
16
  private NavigableSet<E> entries = new ConcurrentSkipListSet<E>();
 
17
  private NavigableSet<TimestampedElement> timestamps = new ConcurrentSkipListSet<TimestampedElement>();
 
18
  private Long evictionMillis = 15*1000*60l;
 
19
  private AtomicBoolean busy = new AtomicBoolean( false );
 
20
  
 
21
  class TimestampedElement implements Comparable<TimestampedElement> {
 
22
    private E element;
 
23
    private Long timeNanos;
 
24
    public TimestampedElement( E element ) {
 
25
      super( );
 
26
      this.element = element;
 
27
      this.timeNanos = System.nanoTime( );
 
28
    }
 
29
    @Override
 
30
    public int hashCode( ) {
 
31
      final int prime = 31;
 
32
      int result = 1;
 
33
      result = prime * result + ( ( this.element == null ) ? 0 : this.element.hashCode( ) );
 
34
      result = prime * result + ( ( this.timeNanos == null ) ? 0 : this.timeNanos.hashCode( ) );
 
35
      return result;
 
36
    }
 
37
    
 
38
    @Override
 
39
    public boolean equals( Object obj ) {
 
40
      if ( this == obj ) return true;
 
41
      if ( obj == null ) return false;
 
42
      if ( getClass( ) != obj.getClass( ) ) return false;
 
43
      TimestampedElement other = ( TimestampedElement ) obj;
 
44
      if ( this.element == null ) {
 
45
        if ( other.element != null ) return false;
 
46
      } else if ( !this.element.equals( other.element ) ) return false;
 
47
      if ( this.timeNanos == null ) {
 
48
        if ( other.timeNanos != null ) return false;
 
49
      } else if ( !this.timeNanos.equals( other.timeNanos ) ) return false;
 
50
      return true;
 
51
    }
 
52
    
 
53
    @Override
 
54
    public int compareTo( TimestampedElement that ) {
 
55
      if( !this.equals( that ) && this.timeNanos.compareTo( that.timeNanos ) == 0 ) {
 
56
        return this.element.compareTo( that.element );
 
57
      } else {
 
58
        return this.timeNanos.compareTo( that.timeNanos );
 
59
      }
 
60
    }
 
61
    public boolean isExpired( ) {
 
62
      return System.nanoTime( ) > ( this.timeNanos + TimedEvictionSet.this.evictionMillis );
 
63
    }
 
64
    public E get( ) {
 
65
      return this.element;
 
66
    }
 
67
    public Long getTimestamp( ) {
 
68
      return this.timeNanos;
 
69
    }
 
70
  }
 
71
  private boolean timestamp( E e ) {
 
72
    this.scavenge( );
 
73
    if( this.entries.add( e ) ) {
 
74
      TimestampedElement elem = new TimestampedElement( e );
 
75
      this.timestamps.add( elem );
 
76
      return true;
 
77
    } else {
 
78
      TimestampedElement elem = new TimestampedElement( e );
 
79
      if( this.timestamps.contains( elem ) && TimeUnit.SECONDS.convert( System.nanoTime( ) - elem.getTimestamp( ), TimeUnit.NANOSECONDS ) < 2 ) {
 
80
        return true;
 
81
      } else {
 
82
        return false;
 
83
      }
 
84
    }
 
85
  }
 
86
  public TimedEvictionSet( Long evictionMillis ) {
 
87
    super( );
 
88
    this.evictionMillis = evictionMillis;
 
89
  }
 
90
  
 
91
  private void scavenge() {
 
92
    if( this.busy.compareAndSet( false, true ) ) {
 
93
      try {
 
94
        TimestampedElement elem = null;        
 
95
        while( !this.timestamps.isEmpty( ) && this.timestamps.first( ).isExpired( ) && ( elem = this.timestamps.pollFirst( ) )!= null ) {
 
96
          this.entries.remove( elem.get( ) );
 
97
          LOG.debug( "Evicting previous entry: " + elem.get( ) + " " + elem.getTimestamp( ) );
 
98
        }
 
99
      } finally {
 
100
        this.busy.lazySet( false );
 
101
      }
 
102
    }
 
103
  }
 
104
  
 
105
  public boolean add( E e ) {
 
106
    return timestamp( e );
 
107
  }
 
108
 
 
109
  @Deprecated
 
110
  public boolean addAll( Collection<? extends E> c ) {
 
111
    return false;
 
112
  }
 
113
 
 
114
  public void clear( ) {
 
115
    this.timestamps.clear( );
 
116
    this.entries.clear( );
 
117
    this.busy.set( false );
 
118
  }
 
119
 
 
120
  public Comparator<? super E> comparator( ) {
 
121
    return this.entries.comparator( );
 
122
  }
 
123
 
 
124
  public boolean contains( Object o ) {
 
125
    this.scavenge( );
 
126
    return this.entries.contains( o );
 
127
  }
 
128
 
 
129
  public boolean containsAll( Collection<?> c ) {
 
130
    return this.entries.containsAll( c );
 
131
  }
 
132
 
 
133
  public E first( ) {
 
134
    return this.entries.first( );
 
135
  }
 
136
 
 
137
  public SortedSet<E> headSet( E toElement ) {
 
138
    return this.entries.headSet( toElement );
 
139
  }
 
140
 
 
141
  public boolean isEmpty( ) {
 
142
    return this.entries.isEmpty( );
 
143
  }
 
144
 
 
145
  public Iterator<E> iterator( ) {
 
146
    return this.entries.iterator( );
 
147
  }
 
148
 
 
149
  public E last( ) {
 
150
    return this.entries.last( );
 
151
  }
 
152
 
 
153
  @Deprecated
 
154
  public boolean remove( Object o ) {
 
155
    return false;
 
156
  }
 
157
 
 
158
  @Deprecated
 
159
  public boolean removeAll( Collection<?> c ) {
 
160
    return false;
 
161
  }
 
162
 
 
163
  @Deprecated
 
164
  public boolean retainAll( Collection<?> c ) {
 
165
    return false;
 
166
  }
 
167
 
 
168
  public int size( ) {
 
169
    return this.entries.size( );
 
170
  }
 
171
 
 
172
  public SortedSet<E> subSet( E fromElement, E toElement ) {
 
173
    return this.entries.subSet( fromElement, toElement );
 
174
  }
 
175
 
 
176
  public SortedSet<E> tailSet( E fromElement ) {
 
177
    return this.entries.tailSet( fromElement );
 
178
  }
 
179
 
 
180
  public Object[] toArray( ) {
 
181
    return this.entries.toArray( );
 
182
  }
 
183
 
 
184
  public <T> T[] toArray( T[] a ) {
 
185
    return this.entries.toArray( a );
 
186
  }
 
187
 
 
188
}