You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ranger.apache.org by ab...@apache.org on 2021/05/14 01:27:48 UTC

[ranger] branch ranger-2.2 updated: RANGER-3284: Simplify processing of tasks scheduled to execute after current transaction is completed

This is an automated email from the ASF dual-hosted git repository.

abhay pushed a commit to branch ranger-2.2
in repository https://gitbox.apache.org/repos/asf/ranger.git


The following commit(s) were added to refs/heads/ranger-2.2 by this push:
     new 7a2e6d9  RANGER-3284: Simplify processing of tasks scheduled to execute after current transaction is completed
7a2e6d9 is described below

commit 7a2e6d92a0aa7344509b1b4aed9846ac9118c472
Author: Abhay Kulkarni <ab...@apache.org>
AuthorDate: Thu May 13 18:11:23 2021 -0700

    RANGER-3284: Simplify processing of tasks scheduled to execute after current transaction is completed
---
 .../org/apache/ranger/biz/PolicyRefUpdater.java    |  99 ++++++------------
 .../java/org/apache/ranger/biz/RoleRefUpdater.java | 116 ++++++++-------------
 .../java/org/apache/ranger/rest/ServiceREST.java   |  55 ++++------
 3 files changed, 99 insertions(+), 171 deletions(-)

diff --git a/security-admin/src/main/java/org/apache/ranger/biz/PolicyRefUpdater.java b/security-admin/src/main/java/org/apache/ranger/biz/PolicyRefUpdater.java
index 115e556..4452676 100644
--- a/security-admin/src/main/java/org/apache/ranger/biz/PolicyRefUpdater.java
+++ b/security-admin/src/main/java/org/apache/ranger/biz/PolicyRefUpdater.java
@@ -63,6 +63,7 @@ import org.apache.ranger.service.XGroupService;
 import org.apache.ranger.service.XUserService;
 import org.apache.ranger.view.VXGroup;
 import org.apache.ranger.view.VXResponse;
+import org.apache.ranger.view.VXUser;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
@@ -305,19 +306,13 @@ public class PolicyRefUpdater {
 	private void createUserForPolicy(String user, XXPolicy xPolicy) {
 		LOG.warn("User specified in policy does not exist in ranger admin, creating new user, User = " + user);
 		final PolicyUserCreateContext policyUserCreateContext = new PolicyUserCreateContext(user, xPolicy);
-		Runnable CreateAndAssociateUser = new Runnable () {
+		Runnable createAndAssociateUser = new Runnable () {
 			@Override
 			public void run() {
-				Runnable realTask = new Runnable () {
-					@Override
-					public void run() {
-						doCreateAndAssociatePolicyUser(policyUserCreateContext);
-					}
-				};
-				transactionService.scheduleToExecuteInOwnTransaction(realTask, 0L);
+				doCreateAndAssociatePolicyUser(policyUserCreateContext);
 			}
 		};
-		rangerTransactionSynchronizationAdapter.executeOnTransactionCommit(CreateAndAssociateUser);
+		rangerTransactionSynchronizationAdapter.executeOnTransactionCommit(createAndAssociateUser);
 	}
 
 	private void createGroupForPolicy(String group, XXPolicy xPolicy) {
@@ -330,13 +325,7 @@ public class PolicyRefUpdater {
 		Runnable createAndAssociatePolicyGroup = new Runnable() {
 			@Override
 			public void run() {
-				Runnable realTask = new Runnable() {
-					@Override
-					public void run() {
-						doCreateAndAssociatePolicyGroup(policyGroupCreateContext);
-					}
-				};
-				transactionService.scheduleToExecuteInOwnTransaction(realTask, 0L);
+				doCreateAndAssociatePolicyGroup(policyGroupCreateContext);
 			}
 		};
 		rangerTransactionSynchronizationAdapter.executeOnTransactionCommit(createAndAssociatePolicyGroup);
@@ -462,40 +451,30 @@ public class PolicyRefUpdater {
 		if (xGroup != null) {
 			groupPolicyAssociation(context.xPolicy, xGroup.getId(), context.group.getName());
 		} else {
-			try {
-				// Create group
-				VXGroup vXGroup = xGroupService.createXGroupWithOutLogin(context.group);
-				if (null != vXGroup) {
+			// Create group
+			VXGroup vXGroup = xGroupService.createXGroupWithOutLogin(context.group);
+			if (vXGroup != null) {
+				try {
 					List<XXTrxLog> trxLogList = xGroupService.getTransactionLog(vXGroup, "create");
-					for(XXTrxLog xTrxLog : trxLogList) {
+					for (XXTrxLog xTrxLog : trxLogList) {
 						xTrxLog.setAddedByUserId(context.xPolicy.getAddedByUserId());
 						xTrxLog.setUpdatedByUserId(context.xPolicy.getAddedByUserId());
 					}
 					rangerBizUtil.createTrxLog(trxLogList);
+				} catch (Throwable t) {
+					// Ignore
+				}
+				doAssociatePolicyGroup(context);
+			} else {
+				if (LOG.isDebugEnabled()) {
+					LOG.debug("Group:[" + context.group + "] creation failed!");
+					throw new RuntimeException("Group:[" + context.group + "] creation failed!");
 				}
-			} catch (Exception exception) {
-				LOG.error("Failed to create Group or to associate group and policy, PolicyGroupContext:[" + context + "]",
-						exception);
-			} finally {
-				// This transaction may still fail at commit time because another transaction
-				// has already created the group
-				// So, associate the group to policy in a different transaction
-				Runnable associatePolicyGroup = new Runnable() {
-					@Override
-					public void run() {
-						Runnable realTask = new Runnable() {
-							@Override
-							public void run() {
-								doAssociatePolicyGroup(context);
-							}
-						};
-						transactionService.scheduleToExecuteInOwnTransaction(realTask, 0L);
-					}
-				};
-				rangerTransactionSynchronizationAdapter.executeOnTransactionCompletion(associatePolicyGroup);
-
 			}
 		}
+		if (LOG.isDebugEnabled()) {
+			LOG.debug("<=== PolicyRefUpdater.doCreateAndAssociatePolicyGroup()");
+		}
 	}
 
 	private static final class PolicyUserCreateContext {
@@ -555,31 +534,19 @@ public class PolicyRefUpdater {
 		if (xUser != null) {
 			userPolicyAssociation(context.xPolicy, xUser.getId(), context.userName);
 		} else {
-			try {
-				// Create External user
-				xUserMgr.createServiceConfigUser(context.userName);
-			} catch (Exception exception) {
-				LOG.error("Failed to create User or to associate user and policy, PolicyUserContext:[" + context + "]",
-						exception);
-			} finally {
-				// This transaction may still fail at commit time because another transaction
-				// has already created the user
-				// So, associate the user to policy in a different transaction
-				Runnable associatePolicyUser = new Runnable() {
-					@Override
-					public void run() {
-						Runnable realTask = new Runnable() {
-							@Override
-							public void run() {
-								doAssociatePolicyUser(context);
-							}
-						};
-						transactionService.scheduleToExecuteInOwnTransaction(realTask, 0L);
-					}
-				};
-				rangerTransactionSynchronizationAdapter.executeOnTransactionCompletion(associatePolicyUser);
+			// Create External user
+			VXUser vXUser = xUserMgr.createServiceConfigUser(context.userName);
+			if (vXUser != null) {
+				doAssociatePolicyUser(context);
+			} else {
+				if (LOG.isDebugEnabled()) {
+					LOG.debug("serviceConfigUser:[" + context.userName + "] creation failed");
+					throw new RuntimeException("serviceConfigUser:[" + context.userName + "] creation failed");
+				}
 			}
 		}
-
+		if (LOG.isDebugEnabled()) {
+			LOG.debug("<=== PolicyRefUpdater.doCreateAndAssociatePolicyUser()");
+		}
 	}
 }
diff --git a/security-admin/src/main/java/org/apache/ranger/biz/RoleRefUpdater.java b/security-admin/src/main/java/org/apache/ranger/biz/RoleRefUpdater.java
index 10a7625..4c50d81 100644
--- a/security-admin/src/main/java/org/apache/ranger/biz/RoleRefUpdater.java
+++ b/security-admin/src/main/java/org/apache/ranger/biz/RoleRefUpdater.java
@@ -48,6 +48,7 @@ import org.apache.ranger.service.RangerTransactionService;
 import org.apache.ranger.service.XGroupService;
 import org.apache.ranger.service.XUserService;
 import org.apache.ranger.view.VXGroup;
+import org.apache.ranger.view.VXUser;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
@@ -118,21 +119,15 @@ public class RoleRefUpdater {
 					if (createNonExistUserGroup && xaBizUtil.checkAdminAccess()) {
 						LOG.warn("User specified in role does not exist in ranger admin, creating new user, User = "
 								+ roleUser);
-						// Schedule another transaction and let this transaction complete (and commit) successfully!
+
 						final RoleUserCreateContext roleUserCreateContext = new RoleUserCreateContext(roleUser, roleId);
-						Runnable CreateAndAssociateUser = new Runnable () {
+						Runnable createAndAssociateUser = new Runnable() {
 							@Override
 							public void run() {
-								Runnable realTask = new Runnable () {
-									@Override
-									public void run() {
-										doCreateAndAssociateRoleUser(roleUserCreateContext);
-									}
-								};
-								transactionService.scheduleToExecuteInOwnTransaction(realTask, 0L);
+								doCreateAndAssociateRoleUser(roleUserCreateContext);
 							}
-                        };
-						rangerTransactionSynchronizationAdapter.executeOnTransactionCommit(CreateAndAssociateUser);
+						};
+						rangerTransactionSynchronizationAdapter.executeOnTransactionCommit(createAndAssociateUser);
 
 					} else {
 						throw restErrorUtil.createRESTException("user with name: " + roleUser + " does not exist ",
@@ -165,21 +160,15 @@ public class RoleRefUpdater {
 						vxGroupNew.setName(roleGroup);
 						vxGroupNew.setDescription(roleGroup);
 						vxGroupNew.setGroupSource(RangerCommonEnums.GROUP_EXTERNAL);
-						// Schedule another transaction and let this transaction complete (and commit) successfully!
+
 						final RoleGroupCreateContext roleGroupCreateContext = new RoleGroupCreateContext(vxGroupNew, roleId);
 
 						Runnable createAndAssociateRoleGroup = new Runnable() {
-                            @Override
-                            public void run() {
-                                Runnable realTask = new Runnable() {
-                                    @Override
-                                    public void run() {
-                                        doCreateAndAssociateRoleGroup(roleGroupCreateContext);
-                                    }
-                                };
-                                transactionService.scheduleToExecuteInOwnTransaction(realTask, 0L);
-                            }
-                        };
+							@Override
+							public void run() {
+								doCreateAndAssociateRoleGroup(roleGroupCreateContext);
+							}
+						};
 						rangerTransactionSynchronizationAdapter.executeOnTransactionCommit(createAndAssociateRoleGroup);
 
 					} else {
@@ -284,35 +273,26 @@ public class RoleRefUpdater {
 		if (xGroup != null) {
 			groupRoleAssociation(context.roleId, xGroup.getId(), context.group.getName());
 		} else {
-			try {
-				// Create group
-				VXGroup vXGroup = xGroupService.createXGroupWithOutLogin(context.group);
-				if (null != vXGroup) {
+			// Create group
+			VXGroup vXGroup = xGroupService.createXGroupWithOutLogin(context.group);
+			if (null != vXGroup) {
+				try {
 					List<XXTrxLog> trxLogList = xGroupService.getTransactionLog(vXGroup, "create");
 					xaBizUtil.createTrxLog(trxLogList);
+				} catch (Throwable t) {
+					// Ignore
+				}
+				doAssociateRoleGroup(context);
+			} else {
+				if (LOG.isDebugEnabled()) {
+					LOG.debug("Group:[" + context.group + "] creation failed!");
+					throw new RuntimeException("Group:[" + context.group + "] creation failed!");
 				}
-			} catch (Exception exception) {
-				LOG.error("Failed to create Group or to associate group and role, RoleGroupContext:[" + context + "]",
-						exception);
-			} finally {
-				// This transaction may still fail at commit time because another transaction
-				// has already created the group
-				// So, associate the group to role in a different transaction
-				Runnable associateRoleGroup = new Runnable() {
-                    @Override
-					public void run() {
-						Runnable realTask = new Runnable() {
-							@Override
-							public void run() {
-								doAssociateRoleGroup(context);
-							}
-						};
-						transactionService.scheduleToExecuteInOwnTransaction(realTask, 0L);
-					}
-                };
-				rangerTransactionSynchronizationAdapter.executeOnTransactionCompletion(associateRoleGroup);
 			}
 		}
+		if (LOG.isDebugEnabled()) {
+			LOG.debug("<=== doCreateAndAssociateRoleGroup()");
+		}
 	}
 
 	void doAssociateRoleGroup(final RoleGroupCreateContext context) {
@@ -330,6 +310,9 @@ public class RoleRefUpdater {
 				LOG.error("Failed to associate group and role, RoleGroupContext:[" + context + "]", exception);
 			}
 		}
+		if(LOG.isDebugEnabled()) {
+			LOG.debug("<=== doAssociateRoleGroup()");
+		}
 	}
 
 	private static final class RoleUserCreateContext {
@@ -371,32 +354,20 @@ public class RoleRefUpdater {
 		if (xUser != null) {
 			userRoleAssociation(context.roleId, xUser.getId(), context.userName);
 		} else {
-			try {
-				// Create External user
-				xUserMgr.createServiceConfigUser(context.userName);
-			} catch (Exception exception) {
-				LOG.error("Failed to create User or to associate user and role, RoleUserContext:[" + context + "]",
-						exception);
-			} finally {
-				// This transaction may still fail at commit time because another transaction
-				// has already created the user
-				// So, associate the user to role in a different transaction
-				Runnable associateRoleUser = new Runnable() {
-					@Override
-					public void run() {
-						Runnable realTask = new Runnable() {
-							@Override
-							public void run() {
-								doAssociateRoleUser(context);
-							}
-						};
-						transactionService.scheduleToExecuteInOwnTransaction(realTask, 0L);
-					}
-                };
-				rangerTransactionSynchronizationAdapter.executeOnTransactionCompletion(associateRoleUser);
+			// Create External user
+			VXUser vXUser = xUserMgr.createServiceConfigUser(context.userName);
+			if (vXUser != null) {
+				doAssociateRoleUser(context);
+			} else {
+				if (LOG.isDebugEnabled()) {
+					LOG.debug("ServiceConfigUser:[" + context.userName + "] creation failed!");
+				}
+				throw new RuntimeException("ServiceConfigUser:[" + context.userName + "] creation failed!");
 			}
 		}
-
+		if (LOG.isDebugEnabled()) {
+			LOG.debug("<=== doCreateAndAssociateRoleUser()");
+		}
 	}
 
 	void doAssociateRoleUser(final RoleUserCreateContext context) {
@@ -414,6 +385,9 @@ public class RoleRefUpdater {
 				LOG.error("Failed to associate user and role, RoleUserContext:[" + context + "]", exception);
 			}
 		}
+		if(LOG.isDebugEnabled()) {
+			LOG.debug("<=== doAssociateRoleUser()");
+		}
 	}
 
 }
diff --git a/security-admin/src/main/java/org/apache/ranger/rest/ServiceREST.java b/security-admin/src/main/java/org/apache/ranger/rest/ServiceREST.java
index fe8de41..b4bc1d9 100644
--- a/security-admin/src/main/java/org/apache/ranger/rest/ServiceREST.java
+++ b/security-admin/src/main/java/org/apache/ranger/rest/ServiceREST.java
@@ -760,7 +760,7 @@ public class ServiceREST {
 				if (LOG.isDebugEnabled()) {
 					LOG.debug("Tag service may need to be created and linked with this service:[" + service.getName() + "]");
 				}
-				scheduleCreateOrGetTagService(service);
+				createOrGetTagService(service);
 			}
 
 			ret = svcStore.createService(service);
@@ -3933,9 +3933,9 @@ public class ServiceREST {
 		}
 	}
 
-	private void scheduleCreateOrGetTagService(RangerService resourceService) {
+	private void createOrGetTagService(RangerService resourceService) {
 		if (LOG.isDebugEnabled()) {
-			LOG.debug("==> scheduleCreateOrGetTagService(resourceService=" + resourceService.getName() + ")");
+			LOG.debug("==> createOrGetTagService(resourceService=" + resourceService.getName() + ")");
 		}
 		final boolean isAutoCreateTagService = config.getBoolean("ranger.tagservice.auto.create", true);
 
@@ -3967,13 +3967,7 @@ public class ServiceREST {
 					Runnable createAndLinkTagServiceTask = new Runnable() {
 						@Override
 						public void run() {
-							Runnable realTask = new Runnable() {
-								@Override
-								public void run() {
-									doCreateAndLinkTagService(context);
-								}
-							};
-							transactionService.scheduleToExecuteInOwnTransaction(realTask, 0L);
+							doCreateAndLinkTagService(context);
 						}
 					};
 
@@ -3985,7 +3979,7 @@ public class ServiceREST {
 			}
 		}
 		if (LOG.isDebugEnabled()) {
-			LOG.debug("<== scheduleCreateOrGetTagService(resourceService=" + resourceService.getName() + ")");
+			LOG.debug("<== createOrGetTagService(resourceService=" + resourceService.getName() + ")");
 		}
 	}
 
@@ -4030,39 +4024,32 @@ public class ServiceREST {
 		}
 
 		if (resourceService != null) {
+			try {
+				String tagServiceName = context.tagServiceName;
 
-			RangerService tagService = new RangerService();
+				RangerService tagService = svcStore.getServiceByName(tagServiceName);
 
-			tagService.setName(context.tagServiceName);
-			tagService.setDisplayName(context.tagServiceName);//set DEFAULT display name
-			tagService.setType(EmbeddedServiceDefsUtil.EMBEDDED_SERVICEDEF_TAG_NAME);
+				if (tagService == null) {
+					tagService = new RangerService();
 
-			LOG.info("creating tag-service [" + context.tagServiceName + "]");
+					tagService.setName(context.tagServiceName);
+					tagService.setDisplayName(context.tagServiceName);//set DEFAULT display name
+					tagService.setType(EmbeddedServiceDefsUtil.EMBEDDED_SERVICEDEF_TAG_NAME);
 
-			RangerService service;
+					LOG.info("creating tag-service [" + context.tagServiceName + "]");
 
-			try {
-				service = svcStore.createService(tagService);
-				LOG.info("Created tag-service:[" + service.getName() + "]");
+					svcStore.createService(tagService);
+				}
 			} catch (Exception e) {
-				LOG.info("Failed to create tag-service " + context.tagServiceName, e);
+				throw new RuntimeException(e);
 			}
 
 			if (context.isAutoLinkTagService) {
-				Runnable linkTagServiceTask = new Runnable() {
-					@Override
-					public void run() {
-						Runnable realTask = new Runnable() {
-							@Override
-							public void run() {
-								doLinkTagService(context);
-							}
-						};
-						transactionService.scheduleToExecuteInOwnTransaction(realTask, 0L);
-					}
-				};
-				rangerTransactionSynchronizationAdapter.executeOnTransactionCompletion(linkTagServiceTask);
+				doLinkTagService(context);
 			}
+
+		} else {
+			LOG.info("Resource service :[" + context.resourceServiceName + "] not found! Returning without linking tag service!!");
 		}
 
 		if (LOG.isDebugEnabled()) {