Skip to content

Commit

Permalink
fix issue intel-cloud#223 to enable retry logic for auth caching
Browse files Browse the repository at this point in the history
Signed-off-by: ywang19 <[email protected]>
  • Loading branch information
ywang19 committed Dec 8, 2014
1 parent 27a53ab commit f5068bc
Showing 1 changed file with 41 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,47 +44,15 @@ public void setLoginAttempts(int loginAttempts) {
@Override
protected void execute() {
Logger logger = getMissionLogger();
logger.debug("begin to login, will attempt {} times", loginAttempts);
try {
/*
* Here we prepare the storage with the authentication object (e.g.
* a user token) retrieved from an login operation. Some storage
* implementations such as Swift and S3 do require such information,
* others don't. But we will do this anyway.
*/
AuthAPI authApi = workerContext.getAuthApi();
StorageAPI storageApi = workerContext.getStorageApi();
AuthContext import_context = authApi.getParms();
AuthContext auth_context;
String id = import_context.getID();

boolean caching = import_context.getBoolean(AuthConstants.CACHING_KEY, AuthConstants.CACHING_DEFAULT);
logger.debug("input auth context is {} with caching={}", import_context.toString(), caching);
if(caching) { // auth caching is enabled
// check if auth context is already cached.
logger.debug("auth caching is enabled, will query cache pool with id={}", id);
synchronized(AuthCachePool.getInstance()) {
if(AuthCachePool.getInstance().containsKey(id)) {// already cached
auth_context = AuthCachePool.getInstance().get(id);
logger.debug("auth context for id={} is found as {}", id, auth_context);
}
else { // not found
logger.debug("auth context for id={} is not found, will try to login", id);
auth_context = authApi.login();
if(auth_context != null) // the authentication mechanism is embedded into storage adapter
{
logger.debug("login is successful, auth context for id={} will be cacahed as {}", id, auth_context);
AuthCachePool.getInstance().put(id, auth_context);
}else { // the authentication mechanism is embedded into storage adapter
logger.info("no auth context required.");
}
}
}
}
else
auth_context = authApi.login();

storageApi.setAuthContext(auth_context);
storageApi.setAuthContext(login());

} catch (AuthInterruptedException ie) {
throw new AbortedException();
Expand Down Expand Up @@ -112,7 +80,7 @@ private AuthContext login() {
int attempts = 0;
while (attempts++ < loginAttempts - 1)
try {
return authApi.login();
return tryLogin();
} catch (AuthInterruptedException ie) {
throw ie; // do not mask this one
} catch (AuthBadException be) {
Expand All @@ -121,7 +89,45 @@ private AuthContext login() {
logger.error("unable to login, will try again later", e);
sleepForSometime();
}
return authApi.login(); // the very last attempt!
return tryLogin(); // the very last attempt!
}

private AuthContext tryLogin() {
Logger logger = getMissionLogger();
logger.debug("begin to login, will attempt {} times", loginAttempts);

AuthAPI authApi = workerContext.getAuthApi();
AuthContext import_context = authApi.getParms();
AuthContext auth_context;
String id = import_context.getID();

boolean caching = import_context.getBoolean(AuthConstants.CACHING_KEY, AuthConstants.CACHING_DEFAULT);
logger.debug("input auth context is {} with caching={}", import_context.toString(), caching);
if(caching) { // auth caching is enabled
// check if auth context is already cached.
logger.debug("auth caching is enabled, will query cache pool with id={}", id);
synchronized(AuthCachePool.getInstance()) {
if(AuthCachePool.getInstance().containsKey(id)) {// already cached
auth_context = AuthCachePool.getInstance().get(id);
logger.debug("auth context for id={} is found as {}", id, auth_context);
}
else { // not found
logger.debug("auth context for id={} is not found, will try to login", id);
auth_context = authApi.login();
if(auth_context != null) // the authentication mechanism is embedded into storage adapter
{
logger.debug("login is successful, auth context for id={} will be cacahed as {}", id, auth_context);
AuthCachePool.getInstance().put(id, auth_context);
}else { // the authentication mechanism is embedded into storage adapter
logger.info("no auth context required.");
}
}
}
}
else
auth_context = authApi.login();

return auth_context;
}

private void sleepForSometime() {
Expand Down

0 comments on commit f5068bc

Please sign in to comment.