|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectorg.hd.d.pg2k.svrCore.location.LoadBalancingUtils
public final class LoadBalancingUtils
Load-balancing utility functions for stand-alone apps and Web apps.
| Nested Class Summary | |
|---|---|
static class |
LoadBalancingUtils.BWOrder
Our simple sorting comparator for mirrors sorts by bandwidth (or other Long metric) then name. |
private static class |
LoadBalancingUtils.MirrorInfo
Immutable info on interaction with one remote mirror. |
| Field Summary | |
|---|---|
private static java.util.Hashtable<java.lang.String,java.lang.Long> |
_cache_gAM
Private cache for getActiveMirrors(). |
private static long |
_cache_gAM_valid_until
Time _cache_gAM is valid until; initially zero marking the cache as invalid. |
private static java.util.Map<java.lang.String,LoadBalancingUtils.MirrorInfo> |
_cache_tIHSIU_mirrorStatus
Private cache for testIfHTTPServerIsUp() for which mirrors are up. |
private static int |
_MAX_STALE_GET_ACTIVE_MIRRORS_MS
Roughly how stale getActiveMirrors() is prepared for its answers to be, ms. |
private static java.util.SortedMap<java.lang.String,java.lang.Long> |
_NO_LIVE_MIRRORS
Immutable empty Map to indicate no live mirrors. |
private static int |
_tIHSIU_CONN_ATTEMPTS
Number of connection attempts before deciding that remote host is dead; strictly positive. |
private static int |
_tIHSIU_CONN_TIMEOUT_MS
Connection establishment timeout allowed by testIfHTTPServerIsUp(), in ms. |
static boolean |
LOG_BALANCING_DECISIONS
If true then log balancing decisions, etc. |
private static int |
MIRROR_RECHECK_MS
Maximum time before we (re)test that a mirror is working, in ms. |
| Constructor Summary | |
|---|---|
private |
LoadBalancingUtils()
Prevent construction of an instance. |
| Method Summary | |
|---|---|
static java.lang.String |
findFirstWorkingMirror(java.util.List<java.lang.String> orderedMirrorTags)
Try the requested mirrors in turn, returning the full hostname of the first that is up, or null if none is available. |
static java.util.SortedMap<java.lang.String,java.lang.Long> |
getActiveMirrors(SimpleVariablePipelineIF vars,
boolean filterStale)
Get active mirrors listed by mirror tag; may be empty but never null. |
static java.lang.String |
makeMirrorHostnameFromTag(java.lang.String mirrorTag,
java.lang.String domainName)
Make full hostname of site given a mirror tag; never null/empty. |
static java.lang.String |
makeMirrorNameFromTag(java.lang.String tag)
Construct a full mirror host name from the mirror tag. |
static java.util.List<java.lang.String> |
orderMirrorTagsBestBandwidthFirst(java.util.Map<java.lang.String,java.lang.Long> availableMirrors)
Orders mirrors into "fastest first" order, returing a List of mirror tags and dropping any known unusable; never null. |
static java.lang.Boolean |
testIfHTTPServerIsUp(java.lang.String hostAndPort,
boolean checkAsynchronously)
Tests if an HTTP Gallery mirror server is up and running and responding quickly. |
static boolean |
testIfHTTPServerIsUp(java.net.URL serverURL)
Tests if a Web server is up and running and responding quickly. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final boolean LOG_BALANCING_DECISIONS
private static final java.util.Hashtable<java.lang.String,java.lang.Long> _cache_gAM
A Hashtable is used for thread safety, but also a lock on this value is used as a lock by getActiveMirrors() where needed.
private static long _cache_gAM_valid_until
private static final int _MAX_STALE_GET_ACTIVE_MIRRORS_MS
private static final java.util.SortedMap<java.lang.String,java.lang.Long> _NO_LIVE_MIRRORS
private static final int MIRROR_RECHECK_MS
We randomise this a little to help avoid (re)testing collisions between peers.
Testing may be slow and/or consume significant network resources.
We may asymmetrically retain positive results longer than negative ones.
private static final int _tIHSIU_CONN_TIMEOUT_MS
Should be of the order of a few seconds to a few tens of seconds.
private static final int _tIHSIU_CONN_ATTEMPTS
private static final java.util.Map<java.lang.String,LoadBalancingUtils.MirrorInfo> _cache_tIHSIU_mirrorStatus
Uses a thread-safe Map implementation.
| Constructor Detail |
|---|
private LoadBalancingUtils()
| Method Detail |
|---|
public static final java.util.SortedMap<java.lang.String,java.lang.Long> getActiveMirrors(SimpleVariablePipelineIF vars,
boolean filterStale)
Returns a Map from the mirror tag to the per-client bandwidth that the mirror claims to have available.
This does not check names in DNS, or that mirrors are actually responding, just which mirrors claim to be up as recorded in the system variables.
If this cannot retrieve the variable values that it needs, then this routine returns an (immutable) empty result.
This does not re-compute its value on each call, but caches it for a while since it may be expensive to compute.
It is possible to optionally leave in "stale" entries so as to be able to "coast" for a little longer after loss of contact with the master.
vars - the system variables; never nullfilterStale - if true, filter out stale entries;
this result is not cached
public static java.lang.String makeMirrorHostnameFromTag(java.lang.String mirrorTag,
java.lang.String domainName)
No checking is done that the resulting name actually refers to a valid host.
Result is undefined if the tag is not a valid mirror tag.
public static java.lang.String makeMirrorNameFromTag(java.lang.String tag)
public static java.lang.String findFirstWorkingMirror(java.util.List<java.lang.String> orderedMirrorTags)
The cache of mirror state is used, and updated if need be.
This routine may take considerable time to complete if this has to actually test the mirrors and they are down and/or network connectivity is poor.
orderedMirrorTags - valid mirror tags in the order the mirrors should be tried
public static java.util.List<java.lang.String> orderMirrorTagsBestBandwidthFirst(java.util.Map<java.lang.String,java.lang.Long> availableMirrors)
The results will be ordered to be highest-bandwidth first, and then by name.
This will drop mirrors known from the cache to be dead/unreachable,
and any mirrors that are
The result will be smaller when inappropriate/slow/dead mirrors
are dropped from the result.
This does not synchronously attempt
to verify the accuracy of the data that it has been passed,
eg it does not do blocking DNS lookups nor make HTTP connections,
so should always be reasonably fast.
(It does however check the status of remote mirrors asynchrnously,
and won't return a candidiate unless verified to be working.)
availableMirrors - mapping of mirror tags to available per-client bandwidth; never null
public static java.lang.Boolean testIfHTTPServerIsUp(java.lang.String hostAndPort,
boolean checkAsynchronously)
Tests that the web server responds quickly and without error (we must get response code 200), to a request for its top-level page "/null.jsp" (redirections are allowed, eg to an index.html or index.jsp).
If an invalid (non-null) host name is supplied this will return false.
We should only do one of these at a time for a given host in order to avoid overloading any one remote mirror.
Note that even if we check synchronously, we may still record any result in the cache so that another caller may benefit. This also means that this routine can call itself recursively (with the checkAsynchronously argument false) via asynchronous threads.
Note that this may run as many threads as there are different hosts whose status is being requested. We could probably do better with non-blocking I/O and a single worker thread if there are many hosts to check, but the assumption is that there will not be.
hostAndPort - hostname or hostname:port for mirror; never nullcheckAsynchronously - if false we check synchronously
and block until we can return TRUE or FALSE,
else we return immediately and check in the background
public static boolean testIfHTTPServerIsUp(java.net.URL serverURL)
Tests that the web server responds quickly and without error (we must get response code 200), to a HEAD request for a special light-weight page "/null.jsp" (redirections are allowed, eg to an index.html or index.jsp).
If an invalid (non-null) host name is supplied this will return false.
We should only do one of these at a time for a given host in order to avoid overloading any one remote mirror.
serverURL - full URL for remote server (page) to test; never null
|
DHD Multimedia Gallery V1.50.55 | ||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||