1818
1919import java .util .HashMap ;
2020import java .util .Map ;
21+ import java .util .UUID ;
2122
2223import org .apache .log4j .Logger ;
2324import org .apache .log4j .NDC ;
2627import com .cloud .exception .CloudAuthenticationException ;
2728import com .cloud .user .Account ;
2829import com .cloud .user .User ;
30+ import com .cloud .utils .UuidUtils ;
2931import com .cloud .utils .exception .CloudRuntimeException ;
3032
3133/**
@@ -37,7 +39,7 @@ public class CallContext {
3739 private static final Logger s_logger = Logger .getLogger (CallContext .class );
3840 private static ThreadLocal <CallContext > s_currentContext = new ThreadLocal <CallContext >();
3941
40- private String sessionId ;
42+ private String contextId ;
4143 private Account account ;
4244 private long startEventId = 0 ;
4345 private String eventDetails ;
@@ -53,10 +55,10 @@ public static void init(EntityManager entityMgr) {
5355 public CallContext () {
5456 }
5557
56- protected CallContext (User user , Account account , String sessionId ) {
58+ protected CallContext (User user , Account account , String contextId ) {
5759 this .user = user ;
5860 this .account = account ;
59- this .sessionId = sessionId ;
61+ this .contextId = contextId ;
6062 }
6163
6264 public void putContextParameter (String key , Object value ) {
@@ -75,8 +77,8 @@ public User getCallingUser() {
7577 return user ;
7678 }
7779
78- public String getSessionId () {
79- return sessionId ;
80+ public String getContextId () {
81+ return contextId ;
8082 }
8183
8284 public Account getCallingAccount () {
@@ -87,17 +89,18 @@ public static CallContext current() {
8789 return s_currentContext .get ();
8890 }
8991
90- public static CallContext register (User callingUser , Account callingAccount , String sessionId ) {
92+ public static CallContext register (User callingUser , Account callingAccount , String contextId ) {
9193 assert s_currentContext .get () == null : "There's a context already so what does this new register context mean? " + s_currentContext .get ().toString ();
9294 if (s_currentContext .get () != null ) { // FIXME: This should be removed soon. I added this check only to surface all the places that have this problem.
9395 throw new CloudRuntimeException ("There's a context already so what does this new register context mean? " + s_currentContext .get ().toString ());
9496 }
95- CallContext callingContext = new CallContext (callingUser , callingAccount , sessionId );
96- s_currentContext .set (callingContext );
97- if (sessionId != null ) {
98- NDC .push ("job-" + sessionId );
97+ if (contextId == null ) {
98+ contextId = UUID .randomUUID ().toString ();
9999 }
100- s_logger .debug ("Setting calling context: " + s_currentContext .get ());
100+ CallContext callingContext = new CallContext (callingUser , callingAccount , contextId );
101+ s_currentContext .set (callingContext );
102+ NDC .push ("ctx-" + UuidUtils .first (contextId ));
103+ s_logger .debug ("Setting calling context: " + callingContext );
101104 return callingContext ;
102105 }
103106
@@ -111,7 +114,7 @@ public static CallContext registerOnceOnly() {
111114 return context ;
112115 }
113116
114- public static CallContext register (String callingUserUuid , String callingAccountUuid , String sessionId ) {
117+ public static CallContext register (String callingUserUuid , String callingAccountUuid , String contextId ) {
115118 Account account = s_entityMgr .findByUuid (Account .class , callingAccountUuid );
116119 if (account == null ) {
117120 throw new CloudAuthenticationException ("The account is no longer current." ).add (Account .class , callingAccountUuid );
@@ -121,10 +124,10 @@ public static CallContext register(String callingUserUuid, String callingAccount
121124 if (user == null ) {
122125 throw new CloudAuthenticationException ("The user is no longer current." ).add (User .class , callingUserUuid );
123126 }
124- return register (user , account , sessionId );
127+ return register (user , account , contextId );
125128 }
126129
127- public static CallContext register (long callingUserId , long callingAccountId , String sessionId ) throws CloudAuthenticationException {
130+ public static CallContext register (long callingUserId , long callingAccountId , String contextId ) throws CloudAuthenticationException {
128131 Account account = s_entityMgr .findById (Account .class , callingAccountId );
129132 if (account == null ) {
130133 throw new CloudAuthenticationException ("The account is no longer current." ).add (Account .class , Long .toString (callingAccountId ));
@@ -133,15 +136,15 @@ public static CallContext register(long callingUserId, long callingAccountId, St
133136 if (user == null ) {
134137 throw new CloudAuthenticationException ("The user is no longer current." ).add (User .class , Long .toString (callingUserId ));
135138 }
136- return register (user , account , sessionId );
139+ return register (user , account , contextId );
137140 }
138141
139- public static CallContext register (long callingUserId , Account callingAccount , String sessionId , boolean apiServer ) {
142+ public static CallContext register (long callingUserId , Account callingAccount , String contextId , boolean apiServer ) {
140143 User user = s_entityMgr .findById (User .class , callingUserId );
141144 if (user == null ) {
142145 throw new CloudAuthenticationException ("The user is no longer current." ).add (User .class , Long .toString (callingUserId ));
143146 }
144- return register (user , callingAccount , sessionId );
147+ return register (user , callingAccount , contextId );
145148 }
146149
147150 public static CallContext unregister () {
@@ -152,17 +155,15 @@ public static CallContext unregister() {
152155 }
153156 s_currentContext .remove ();
154157 s_logger .debug ("Context removed " + context );
155- String sessionId = context .getSessionId ();
156- if (sessionId != null ) {
157- String sessionIdOnStack = null ;
158- String sessionIdPushedToNDC = "job-" + sessionId ;
159- while ((sessionIdOnStack = NDC .pop ()) != null ) {
160- if (sessionIdPushedToNDC .equals (sessionIdOnStack )) {
161- break ;
162- }
163- if (s_logger .isTraceEnabled ()) {
164- s_logger .trace ("Popping from NDC: " + sessionId );
165- }
158+ String contextId = context .getContextId ();
159+ String sessionIdOnStack = null ;
160+ String sessionIdPushedToNDC = "ctx-" + UuidUtils .first (contextId );
161+ while ((sessionIdOnStack = NDC .pop ()) != null ) {
162+ if (sessionIdPushedToNDC .equals (sessionIdOnStack )) {
163+ break ;
164+ }
165+ if (s_logger .isTraceEnabled ()) {
166+ s_logger .trace ("Popping from NDC: " + contextId );
166167 }
167168 }
168169 return context ;
@@ -200,7 +201,7 @@ public String getEventDetails() {
200201 public String toString () {
201202 return new StringBuffer ("CallContext[acct=" ).append (account .getId ())
202203 .append ("; user=" ).append (user .getId ())
203- .append ("; session=" ).append (sessionId )
204+ .append ("; session=" ).append (contextId )
204205 .append ("]" ).toString ();
205206 }
206207}
0 commit comments