cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dar...@apache.org
Subject [1/2] git commit: updated refs/heads/master to c25b583
Date Thu, 24 Oct 2013 06:41:32 GMT
Updated Branches:
  refs/heads/master 8f25ea8c0 -> c25b58356


Make system CallContext load user on demand

Before every thread would hit the database once at the start to
load the system account and user.  The loaded objects are almost never
used.  This change optimizes the behavior and lazy loads the system
user and account object.


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/63e46ad1
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/63e46ad1
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/63e46ad1

Branch: refs/heads/master
Commit: 63e46ad1d0c67e9b0ca80079e1fd190096dcf123
Parents: 8f25ea8
Author: Darren Shepherd <darren.s.shepherd@gmail.com>
Authored: Wed Oct 23 23:09:58 2013 -0700
Committer: Darren Shepherd <darren.s.shepherd@gmail.com>
Committed: Wed Oct 23 23:09:58 2013 -0700

----------------------------------------------------------------------
 .../apache/cloudstack/context/CallContext.java  | 46 +++++++++++++++-----
 1 file changed, 35 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/63e46ad1/api/src/org/apache/cloudstack/context/CallContext.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/context/CallContext.java b/api/src/org/apache/cloudstack/context/CallContext.java
index 73bf355..5439aee 100644
--- a/api/src/org/apache/cloudstack/context/CallContext.java
+++ b/api/src/org/apache/cloudstack/context/CallContext.java
@@ -50,11 +50,13 @@ public class CallContext {
 
     private String contextId;
     private Account account;
+    private long accountId;
     private long startEventId = 0;
     private String eventDescription;
     private String eventDetails;
     private String eventType;
     private User user;
+    private long userId;
     private final Map<Object, Object> context = new HashMap<Object, Object>();
 
     static EntityManager s_entityMgr;
@@ -66,9 +68,17 @@ public class CallContext {
     protected CallContext() {
     }
 
+    protected CallContext(long userId, long accountId, String contextId) {
+        this.userId = userId;
+        this.accountId = accountId;
+        this.contextId = contextId;
+    }
+
     protected CallContext(User user, Account account, String contextId) {
         this.user = user;
+        this.userId = user.getId();
         this.account = account;
+        this.accountId = account.getId();
         this.contextId = contextId;
     }
 
@@ -81,10 +91,13 @@ public class CallContext {
     }
 
     public long getCallingUserId() {
-        return user.getId();
+        return userId;
     }
 
     public User getCallingUser() {
+        if (user == null) {
+            user = s_entityMgr.findById(User.class, userId);
+        }
         return user;
     }
 
@@ -93,6 +106,9 @@ public class CallContext {
     }
 
     public Account getCallingAccount() {
+        if (account == null) {
+            account = s_entityMgr.findById(Account.class, accountId);
+        }
         return account;
     }
 
@@ -110,6 +126,10 @@ public class CallContext {
      * @return CallContext
      */
     public static CallContext register(User callingUser, Account callingAccount, String contextId)
{
+        return register(callingUser, callingAccount, null, null, contextId);
+    }
+
+    protected static CallContext register(User callingUser, Account callingAccount, Long
userId, Long accountId, String contextId) {
         /*
                 Unit tests will have multiple times of setup/tear-down call to this, remove
assertions to all unit test to run
                  
@@ -118,7 +138,12 @@ public class CallContext {
                     throw new CloudRuntimeException("There's a context already so what does
this new register context mean? " + s_currentContext.get().toString());
                 }
         */
-        CallContext callingContext = new CallContext(callingUser, callingAccount, contextId);
+        CallContext callingContext = null;
+        if (userId == null || accountId == null) {
+            callingContext = new CallContext(callingUser, callingAccount, contextId);
+        } else {
+            callingContext = new CallContext(userId, accountId, contextId);
+        }
         s_currentContext.set(callingContext);
         NDC.push("ctx-" + UuidUtils.first(contextId));
         if (s_logger.isTraceEnabled()) {
@@ -138,14 +163,13 @@ public class CallContext {
         try {
             CallContext context = s_currentContext.get();
             if (context == null) {
-                return register(User.UID_SYSTEM, Account.ACCOUNT_ID_SYSTEM);
+                return register(null, null, User.UID_SYSTEM, Account.ACCOUNT_ID_SYSTEM, UUID.randomUUID().toString());
             }
             assert context.getCallingUserId() == User.UID_SYSTEM : "You are calling a very
specific method that registers a one time system context.  This method is meant for background
threads that does processing.";
             return context;
         } catch (Exception e) {
-            s_logger.fatal("Exiting the system because we're unable to register the system
call context.", e);
-            System.exit(1);
-            throw new CloudRuntimeException("Should never hit this");
+            s_logger.error("Failed to register the system call context.", e);
+            throw new CloudRuntimeException("Failed to register system call context", e);
         }
     }
 
@@ -220,15 +244,15 @@ public class CallContext {
     }
 
     public long getCallingAccountId() {
-        return account.getId();
+        return accountId;
     }
 
     public String getCallingAccountUuid() {
-        return account.getUuid();
+        return getCallingAccount().getUuid();
     }
 
     public String getCallingUserUuid() {
-        return user.getUuid();
+        return getCallingUser().getUuid();
     }
 
     public void setEventDetails(String eventDetails) {
@@ -265,8 +289,8 @@ public class CallContext {
 
     @Override
     public String toString() {
-        return new StringBuilder("CCtxt[acct=").append(account.getId())
-                .append("; user=").append(user.getId())
+        return new StringBuilder("CCtxt[acct=").append(getCallingAccountId())
+                .append("; user=").append(getCallingUserId())
                 .append("; id=").append(contextId)
                 .append("]").toString();
     }


Mime
View raw message