1
/*******************************************************************************
2
*Copyright (c) 2009 Eucalyptus Systems, Inc.
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation, only version 3 of the License.
9
* This file is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* You should have received a copy of the GNU General Public License along
15
* with this program. If not, see <http://www.gnu.org/licenses/>.
17
* Please contact Eucalyptus Systems, Inc., 130 Castilian
18
* Dr., Goleta, CA 93101 USA or visit <http://www.eucalyptus.com/licenses/>
19
* if you need additional information or have any questions.
21
* This file may incorporate work covered under the following copyright and
24
* Software License Agreement (BSD License)
26
* Copyright (c) 2008, Regents of the University of California
27
* All rights reserved.
29
* Redistribution and use of this software in source and binary forms, with
30
* or without modification, are permitted provided that the following
33
* Redistributions of source code must retain the above copyright notice,
34
* this list of conditions and the following disclaimer.
36
* Redistributions in binary form must reproduce the above copyright
37
* notice, this list of conditions and the following disclaimer in the
38
* documentation and/or other materials provided with the distribution.
40
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
41
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
42
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
43
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
44
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
46
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
47
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
48
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
49
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
50
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. USERS OF
51
* THIS SOFTWARE ACKNOWLEDGE THE POSSIBLE PRESENCE OF OTHER OPEN SOURCE
52
* LICENSED MATERIAL, COPYRIGHTED MATERIAL OR PATENTED MATERIAL IN THIS
53
* SOFTWARE, AND IF ANY SUCH MATERIAL IS DISCOVERED THE PARTY DISCOVERING
54
* IT MAY INFORM DR. RICH WOLSKI AT THE UNIVERSITY OF CALIFORNIA, SANTA
55
* BARBARA WHO WILL THEN ASCERTAIN THE MOST APPROPRIATE REMEDY, WHICH IN
56
* THE REGENTS’ DISCRETION MAY INCLUDE, WITHOUT LIMITATION, REPLACEMENT
57
* OF THE CODE SO IDENTIFIED, LICENSING OF THE CODE SO IDENTIFIED, OR
58
* WITHDRAWAL OF THE CODE CAPABILITY TO THE EXTENT NEEDED TO COMPLY WITH
59
* ANY SUCH LICENSES OR RIGHTS.
60
*******************************************************************************/
62
* Author: chris grzegorczyk <grze@eucalyptus.com>
64
package com.eucalyptus.ws.util;
66
import java.util.concurrent.ConcurrentHashMap;
68
import org.apache.log4j.Logger;
70
import edu.ucsb.eucalyptus.msgs.EucalyptusErrorMessageType;
71
import edu.ucsb.eucalyptus.msgs.EucalyptusMessage;
73
public class ReplyCoordinator {
75
private static Logger LOG = Logger.getLogger( ReplyCoordinator.class );
76
private static int MAP_CAPACITY = 64;
77
private static int MAP_NUM_CONCURRENT = MAP_CAPACITY / 2;
78
private static float MAP_BIN_AVG_THRESHOLD = 1.0f;
79
private static long MAP_GET_WAIT_MS = 10;
80
private static long MAP_SUBMIT_SLEEP_MS = MAP_GET_WAIT_MS;
81
private long MAP_TIMEOUT_MS = 15000;
82
private ConcurrentHashMap<String, EucalyptusMessage> replyMap;
83
private ConcurrentHashMap<String, String> waitList;
85
public ReplyCoordinator( ) {
86
this.MAP_TIMEOUT_MS = 360000;//15000;
87
this.replyMap = new ConcurrentHashMap<String, EucalyptusMessage>( MAP_CAPACITY, MAP_BIN_AVG_THRESHOLD, MAP_NUM_CONCURRENT );
88
this.waitList = new ConcurrentHashMap<String, String>( MAP_CAPACITY, MAP_BIN_AVG_THRESHOLD, MAP_NUM_CONCURRENT );
91
public ReplyCoordinator( long user_timeout ) {
93
this.MAP_TIMEOUT_MS = user_timeout;
96
public void putMessage( EucalyptusMessage msg ) {
97
long startTime = System.currentTimeMillis( );
98
String msgCorId = msg.getCorrelationId( );
99
while ( !this.waitList.containsKey( msgCorId ) && ( System.currentTimeMillis( ) - startTime ) < MAP_TIMEOUT_MS ) {
101
Thread.sleep( MAP_SUBMIT_SLEEP_MS );
102
} catch ( InterruptedException e ) {
106
if ( this.waitList.containsKey( msgCorId ) ) {
107
String corId = this.waitList.get( msgCorId );
108
synchronized ( corId ) {
109
this.replyMap.put( corId, msg );
113
LOG.error( "TIMEOUT: A message was returned by the system but the requesting client cannot be found, most likely timed out. " );
114
LOG.error( "TIMEOUT: Waited for msec=" + ( System.currentTimeMillis( ) - startTime ) );
115
LOG.error( "TIMEOUT: Message:" + msg );
119
public EucalyptusMessage getMessage( String corId ) {
120
long startTime = System.currentTimeMillis( );
121
this.waitList.put( corId, corId );
122
EucalyptusMessage reply = null;
123
synchronized ( corId ) {
124
while ( !this.replyMap.containsKey( corId ) && ( System.currentTimeMillis( ) - startTime ) < MAP_TIMEOUT_MS )
126
corId.wait( MAP_GET_WAIT_MS );
127
} catch ( InterruptedException e ) {
129
this.waitList.remove( corId );
130
if ( this.replyMap.containsKey( corId ) ) reply = this.replyMap.remove( corId );
132
LOG.error( "TIMEOUT: Requesting client has waited for msec=" + ( System.currentTimeMillis( ) - startTime ) );
133
LOG.error( "TIMEOUT: Returning a message to indicate that the system hasn't finished processing yet" );
134
LOG.error( "TIMEOUT: correlationId:" + corId );
135
return new EucalyptusErrorMessageType( "Looks like you are going to timeout, but we aren't done processing your request yet. Might be a slow network -- or a bug :(", "Raise the timeout value used by your client software." );