Skip to content

Commit 0576578

Browse files
committed
refactor: avoid redundant DB calls when checking for root admin account
AccountService.isRootAdmin(Long) is currently invoked each time to check if an account or caller is a root admin, leading to repeated DB lookups. In most cases, the Account object is already available and should be used directly. For the caller, the root admin flag is now cached in CallContext to avoid repeated evaluations. Signed-off-by: Abhishek Kumar <[email protected]>
1 parent 5cac4f6 commit 0576578

File tree

84 files changed

+699
-755
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+699
-755
lines changed

api/src/main/java/com/cloud/user/AccountService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ User createUser(String userName, String password, String firstName, String lastN
8585

8686
boolean isRootAdmin(Long accountId);
8787

88+
boolean isRootAdmin(Account account);
89+
8890
boolean isDomainAdmin(Long accountId);
8991

9092
boolean isResourceDomainAdmin(Long accountId);

api/src/main/java/org/apache/cloudstack/api/command/user/affinitygroup/CreateAffinityGroupCmd.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ public long getEntityOwnerId() {
106106
Account caller = CallContext.current().getCallingAccount();
107107

108108
//For domain wide affinity groups (if the affinity group processor type allows it)
109-
if(projectId == null && domainId != null && accountName == null && _accountService.isRootAdmin(caller.getId())){
109+
if(projectId == null && domainId != null && accountName == null &&
110+
CallContext.current().isCallingAccountRootAdmin()){
110111
return Account.ACCOUNT_ID_SYSTEM;
111112
}
112113
Account owner = _accountService.finalizeOwner(caller, accountName, domainId, projectId);

api/src/main/java/org/apache/cloudstack/api/command/user/affinitygroup/DeleteAffinityGroupCmd.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ public long getEntityOwnerId() {
9595
Account caller = CallContext.current().getCallingAccount();
9696

9797
//For domain wide affinity groups (if the affinity group processor type allows it)
98-
if(projectId == null && domainId != null && accountName == null && _accountService.isRootAdmin(caller.getId())){
98+
if(projectId == null && domainId != null && accountName == null &&
99+
CallContext.current().isCallingAccountRootAdmin()){
99100
return Account.ACCOUNT_ID_SYSTEM;
100101
}
101102
Account owner = _accountService.finalizeOwner(caller, accountName, domainId, projectId);

api/src/main/java/org/apache/cloudstack/api/command/user/storage/sharedfs/ChangeSharedFSDiskOfferingCmd.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535

3636
import com.cloud.event.EventTypes;
3737
import com.cloud.exception.ResourceAllocationException;
38-
import com.cloud.user.Account;
3938

4039
@APICommand(name = "changeSharedFileSystemDiskOffering",
4140
responseObject= SharedFSResponse.class,
@@ -130,8 +129,7 @@ public void execute() throws ResourceAllocationException {
130129
SharedFS sharedFS = sharedFSService.changeSharedFSDiskOffering(this);
131130
if (sharedFS != null) {
132131
ResponseObject.ResponseView respView = getResponseView();
133-
Account caller = CallContext.current().getCallingAccount();
134-
if (_accountService.isRootAdmin(caller.getId())) {
132+
if (CallContext.current().isCallingAccountRootAdmin()) {
135133
respView = ResponseObject.ResponseView.Full;
136134
}
137135
SharedFSResponse response = _responseGenerator.createSharedFSResponse(respView, sharedFS);

api/src/main/java/org/apache/cloudstack/api/command/user/storage/sharedfs/ChangeSharedFSServiceOfferingCmd.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
import org.apache.cloudstack.api.ResponseObject;
2828
import org.apache.cloudstack.api.ServerApiException;
2929
import org.apache.cloudstack.api.command.user.UserCmd;
30-
import org.apache.cloudstack.api.response.SharedFSResponse;
3130
import org.apache.cloudstack.api.response.ServiceOfferingResponse;
31+
import org.apache.cloudstack.api.response.SharedFSResponse;
3232
import org.apache.cloudstack.context.CallContext;
3333
import org.apache.cloudstack.storage.sharedfs.SharedFS;
3434
import org.apache.cloudstack.storage.sharedfs.SharedFSService;
@@ -39,7 +39,6 @@
3939
import com.cloud.exception.OperationTimedoutException;
4040
import com.cloud.exception.ResourceUnavailableException;
4141
import com.cloud.exception.VirtualMachineMigrationException;
42-
import com.cloud.user.Account;
4342
import com.cloud.utils.exception.CloudRuntimeException;
4443

4544
@APICommand(name = "changeSharedFileSystemServiceOffering",
@@ -132,8 +131,7 @@ public void execute() {
132131

133132
if (sharedFS != null) {
134133
ResponseObject.ResponseView respView = getResponseView();
135-
Account caller = CallContext.current().getCallingAccount();
136-
if (_accountService.isRootAdmin(caller.getId())) {
134+
if (CallContext.current().isCallingAccountRootAdmin()) {
137135
respView = ResponseObject.ResponseView.Full;
138136
}
139137
SharedFSResponse response = _responseGenerator.createSharedFSResponse(respView, sharedFS);

api/src/main/java/org/apache/cloudstack/api/command/user/storage/sharedfs/CreateSharedFSCmd.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,6 @@
1818

1919
import javax.inject.Inject;
2020

21-
import com.cloud.event.EventTypes;
22-
import com.cloud.exception.ConcurrentOperationException;
23-
import com.cloud.exception.InsufficientCapacityException;
24-
import com.cloud.exception.OperationTimedoutException;
25-
import com.cloud.exception.ResourceAllocationException;
26-
import com.cloud.exception.ResourceUnavailableException;
27-
import com.cloud.user.Account;
28-
import com.cloud.utils.exception.CloudRuntimeException;
29-
3021
import org.apache.cloudstack.acl.RoleType;
3122
import org.apache.cloudstack.api.APICommand;
3223
import org.apache.cloudstack.api.ApiCommandResourceType;
@@ -40,16 +31,24 @@
4031
import org.apache.cloudstack.api.command.user.UserCmd;
4132
import org.apache.cloudstack.api.response.DiskOfferingResponse;
4233
import org.apache.cloudstack.api.response.DomainResponse;
43-
import org.apache.cloudstack.api.response.SharedFSResponse;
4434
import org.apache.cloudstack.api.response.NetworkResponse;
4535
import org.apache.cloudstack.api.response.ProjectResponse;
4636
import org.apache.cloudstack.api.response.ServiceOfferingResponse;
37+
import org.apache.cloudstack.api.response.SharedFSResponse;
4738
import org.apache.cloudstack.api.response.ZoneResponse;
4839
import org.apache.cloudstack.context.CallContext;
4940
import org.apache.cloudstack.storage.sharedfs.SharedFS;
5041
import org.apache.cloudstack.storage.sharedfs.SharedFSProvider;
5142
import org.apache.cloudstack.storage.sharedfs.SharedFSService;
5243

44+
import com.cloud.event.EventTypes;
45+
import com.cloud.exception.ConcurrentOperationException;
46+
import com.cloud.exception.InsufficientCapacityException;
47+
import com.cloud.exception.OperationTimedoutException;
48+
import com.cloud.exception.ResourceAllocationException;
49+
import com.cloud.exception.ResourceUnavailableException;
50+
import com.cloud.utils.exception.CloudRuntimeException;
51+
5352
@APICommand(name = "createSharedFileSystem",
5453
responseObject= SharedFSResponse.class,
5554
description = "Create a new Shared File System of specified size and disk offering, attached to the given network",
@@ -289,8 +288,7 @@ public void execute() {
289288

290289
if (sharedFS != null) {
291290
ResponseObject.ResponseView respView = getResponseView();
292-
Account caller = CallContext.current().getCallingAccount();
293-
if (_accountService.isRootAdmin(caller.getId())) {
291+
if (CallContext.current().isCallingAccountRootAdmin()) {
294292
respView = ResponseObject.ResponseView.Full;
295293
}
296294
SharedFSResponse response = _responseGenerator.createSharedFSResponse(respView, sharedFS);

api/src/main/java/org/apache/cloudstack/api/command/user/storage/sharedfs/RestartSharedFSCmd.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import com.cloud.exception.OperationTimedoutException;
4040
import com.cloud.exception.ResourceAllocationException;
4141
import com.cloud.exception.ResourceUnavailableException;
42-
import com.cloud.user.Account;
4342
import com.cloud.utils.exception.CloudRuntimeException;
4443

4544
@APICommand(name = "restartSharedFileSystem",
@@ -130,8 +129,7 @@ public void execute() {
130129

131130
if (sharedFS != null) {
132131
ResponseObject.ResponseView respView = getResponseView();
133-
Account caller = CallContext.current().getCallingAccount();
134-
if (_accountService.isRootAdmin(caller.getId())) {
132+
if (CallContext.current().isCallingAccountRootAdmin()) {
135133
respView = ResponseObject.ResponseView.Full;
136134
}
137135
SharedFSResponse response = _responseGenerator.createSharedFSResponse(respView, sharedFS);

api/src/main/java/org/apache/cloudstack/api/command/user/storage/sharedfs/StartSharedFSCmd.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import com.cloud.exception.OperationTimedoutException;
3939
import com.cloud.exception.ResourceAllocationException;
4040
import com.cloud.exception.ResourceUnavailableException;
41-
import com.cloud.user.Account;
4241
import com.cloud.utils.exception.CloudRuntimeException;
4342

4443
@APICommand(name = "startSharedFileSystem",
@@ -120,8 +119,7 @@ public void execute() {
120119

121120
if (sharedFS != null) {
122121
ResponseObject.ResponseView respView = getResponseView();
123-
Account caller = CallContext.current().getCallingAccount();
124-
if (_accountService.isRootAdmin(caller.getId())) {
122+
if (CallContext.current().isCallingAccountRootAdmin()) {
125123
respView = ResponseObject.ResponseView.Full;
126124
}
127125
SharedFSResponse response = _responseGenerator.createSharedFSResponse(respView, sharedFS);

api/src/main/java/org/apache/cloudstack/api/command/user/storage/sharedfs/StopSharedFSCmd.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.apache.cloudstack.storage.sharedfs.SharedFSService;
3434

3535
import com.cloud.event.EventTypes;
36-
import com.cloud.user.Account;
3736

3837
@APICommand(name = "stopSharedFileSystem",
3938
responseObject= SharedFSResponse.class,
@@ -100,8 +99,7 @@ public void execute() {
10099
SharedFS sharedFS = sharedFSService.stopSharedFS(this.getId(), this.isForced());
101100
if (sharedFS != null) {
102101
ResponseObject.ResponseView respView = getResponseView();
103-
Account caller = CallContext.current().getCallingAccount();
104-
if (_accountService.isRootAdmin(caller.getId())) {
102+
if (CallContext.current().isCallingAccountRootAdmin()) {
105103
respView = ResponseObject.ResponseView.Full;
106104
}
107105
SharedFSResponse response = _responseGenerator.createSharedFSResponse(respView, sharedFS);

api/src/main/java/org/apache/cloudstack/api/command/user/storage/sharedfs/UpdateSharedFSCmd.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
// under the License.
1717
package org.apache.cloudstack.api.command.user.storage.sharedfs;
1818

19+
import javax.inject.Inject;
20+
1921
import org.apache.cloudstack.acl.RoleType;
2022
import org.apache.cloudstack.api.APICommand;
2123
import org.apache.cloudstack.api.ApiConstants;
@@ -30,10 +32,6 @@
3032
import org.apache.cloudstack.storage.sharedfs.SharedFS;
3133
import org.apache.cloudstack.storage.sharedfs.SharedFSService;
3234

33-
import javax.inject.Inject;
34-
35-
import com.cloud.user.Account;
36-
3735
@APICommand(name = "updateSharedFileSystem",
3836
responseObject= SharedFSResponse.class,
3937
description = "Update a Shared FileSystem",
@@ -98,8 +96,7 @@ public void execute() {
9896
SharedFS sharedFS = sharedFSService.updateSharedFS(this);
9997
if (sharedFS != null) {
10098
ResponseObject.ResponseView respView = getResponseView();
101-
Account caller = CallContext.current().getCallingAccount();
102-
if (_accountService.isRootAdmin(caller.getId())) {
99+
if (CallContext.current().isCallingAccountRootAdmin()) {
103100
respView = ResponseObject.ResponseView.Full;
104101
}
105102
SharedFSResponse response = _responseGenerator.createSharedFSResponse(respView, sharedFS);

0 commit comments

Comments
 (0)