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

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


The following commit(s) were added to refs/heads/master by this push:
     new 032ef39  RANGER-3280: Ensure that the policy/tag versions gets 
correctly updated for every change to service/policy/tag
032ef39 is described below

commit 032ef3996a1366480ff72983bd16ce089c6e8798
Author: Abhay Kulkarni <[email protected]>
AuthorDate: Thu May 13 17:26:43 2021 -0700

    RANGER-3280: Ensure that the policy/tag versions gets correctly updated for 
every change to service/policy/tag
---
 .../optimized/current/ranger_core_db_mysql.sql     |  3 +
 .../052-add-unique-constraint-on-change-logs.sql   | 30 ++++++++
 .../optimized/current/ranger_core_db_oracle.sql    |  3 +
 .../052-add-unique-constraint-on-change-logs.sql   | 32 ++++++++
 .../optimized/current/ranger_core_db_postgres.sql  |  7 +-
 .../052-add-unique-constraint-on-change-logs.sql   | 32 ++++++++
 .../current/ranger_core_db_sqlanywhere.sql         |  6 ++
 .../052-add-unique-constraint-on-change-logs.sql   | 21 ++++++
 .../optimized/current/ranger_core_db_sqlserver.sql | 14 ++++
 .../053-add-unique-constraint-on-change-logs.sql   | 30 ++++++++
 .../java/org/apache/ranger/biz/ServiceDBStore.java | 24 +++---
 .../RangerTransactionSynchronizationAdapter.java   | 86 +++++++++++-----------
 12 files changed, 227 insertions(+), 61 deletions(-)

diff --git a/security-admin/db/mysql/optimized/current/ranger_core_db_mysql.sql 
b/security-admin/db/mysql/optimized/current/ranger_core_db_mysql.sql
index a42c2f1..73d5ab7 100644
--- a/security-admin/db/mysql/optimized/current/ranger_core_db_mysql.sql
+++ b/security-admin/db/mysql/optimized/current/ranger_core_db_mysql.sql
@@ -1562,8 +1562,10 @@ primary key (`id`)
 CREATE UNIQUE INDEX x_service_resource_IDX_resource_signature ON 
x_service_resource(resource_signature);
 CREATE INDEX x_tag_change_log_IDX_service_id ON x_tag_change_log(service_id);
 CREATE INDEX x_tag_change_log_IDX_tag_version ON 
x_tag_change_log(service_tags_version);
+CREATE UNIQUE INDEX x_tag_change_log_uk_service_id_service_tags_version ON 
x_tag_change_log(service_id, service_tags_version);
 CREATE INDEX x_policy_change_log_IDX_service_id ON 
x_policy_change_log(service_id);
 CREATE INDEX x_policy_change_log_IDX_policy_version ON 
x_policy_change_log(policy_version);
+CREATE UNIQUE INDEX x_policy_change_log_uk_service_id_policy_version ON 
x_policy_change_log(service_id, policy_version);
 CREATE INDEX x_service_config_def_IDX_def_id ON x_service_config_def(def_id);
 CREATE INDEX x_resource_def_IDX_def_id ON x_resource_def(def_id);
 CREATE INDEX x_access_type_def_IDX_def_id ON x_access_type_def(def_id);
@@ -1749,6 +1751,7 @@ INSERT INTO x_db_version_h 
(version,inst_at,inst_by,updated_at,updated_by,active
 INSERT INTO x_db_version_h 
(version,inst_at,inst_by,updated_at,updated_by,active) VALUES 
('049',UTC_TIMESTAMP(),'Ranger 1.0.0',UTC_TIMESTAMP(),'localhost','Y');
 INSERT INTO x_db_version_h 
(version,inst_at,inst_by,updated_at,updated_by,active) VALUES 
('050',UTC_TIMESTAMP(),'Ranger 1.0.0',UTC_TIMESTAMP(),'localhost','Y');
 INSERT INTO x_db_version_h 
(version,inst_at,inst_by,updated_at,updated_by,active) VALUES 
('051',UTC_TIMESTAMP(),'Ranger 1.0.0',UTC_TIMESTAMP(),'localhost','Y');
+INSERT INTO x_db_version_h 
(version,inst_at,inst_by,updated_at,updated_by,active) VALUES 
('052',UTC_TIMESTAMP(),'Ranger 1.0.0',UTC_TIMESTAMP(),'localhost','Y');
 INSERT INTO x_db_version_h 
(version,inst_at,inst_by,updated_at,updated_by,active) VALUES 
('DB_PATCHES',UTC_TIMESTAMP(),'Ranger 1.0.0',UTC_TIMESTAMP(),'localhost','Y');
 
 INSERT INTO x_user_module_perm 
(user_id,module_id,create_time,update_time,added_by_id,upd_by_id,is_allowed)
diff --git 
a/security-admin/db/mysql/patches/052-add-unique-constraint-on-change-logs.sql 
b/security-admin/db/mysql/patches/052-add-unique-constraint-on-change-logs.sql
new file mode 100644
index 0000000..d4c7fc9
--- /dev/null
+++ 
b/security-admin/db/mysql/patches/052-add-unique-constraint-on-change-logs.sql
@@ -0,0 +1,30 @@
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+
+drop procedure if exists create_unique_constraint_on_change_logs;
+
+delimiter ;;
+create procedure create_unique_constraint_on_change_logs() begin
+       ALTER TABLE x_policy_change_log ADD INDEX 
x_policy_change_log_uk_service_id_policy_version(service_id, policy_version);
+       ALTER TABLE x_tag_change_log ADD INDEX 
x_tag_change_log_uk_service_id_service_tags_version(service_id, 
service_tags_version);
+end;;
+
+delimiter ;
+
+truncate x_policy_change_log;
+truncate x_tag_change_log;
+call create_unique_constraint_on_change_logs();
+
+drop procedure if exists create_unique_constraint_on_change_logs;
diff --git 
a/security-admin/db/oracle/optimized/current/ranger_core_db_oracle.sql 
b/security-admin/db/oracle/optimized/current/ranger_core_db_oracle.sql
index 149d94d..8e76bcd 100644
--- a/security-admin/db/oracle/optimized/current/ranger_core_db_oracle.sql
+++ b/security-admin/db/oracle/optimized/current/ranger_core_db_oracle.sql
@@ -1509,6 +1509,7 @@ policy_id NUMBER(20) DEFAULT NULL NULL,
 );
 CREATE INDEX x_plcy_chng_log_IDX_service_id ON x_policy_change_log(service_id);
 CREATE INDEX x_plcy_chng_log_IDX_policy_ver ON 
x_policy_change_log(policy_version);
+CREATE UNIQUE INDEX X_POLICY_CHANGE_LOG_UK_SERVICE_ID_POLICY_VERSION ON 
x_policy_change_log(service_id, policy_version);
 COMMIT;
 
 CREATE TABLE x_tag_change_log (
@@ -1523,6 +1524,7 @@ primary key (id)
 );
 CREATE INDEX x_tag_chng_log_IDX_service_id ON x_tag_change_log(service_id);
 CREATE INDEX x_tag_chng_log_IDX_tag_ver ON 
x_tag_change_log(service_tags_version);
+CREATE UNIQUE INDEX X_TAG_CHANGE_LOG_UK_SERVICE_ID_SERVICE_TAGS_VERSION ON 
x_tag_change_log(service_id, service_tags_version);
 COMMIT;
 
 CREATE TABLE x_role(
@@ -1985,6 +1987,7 @@ INSERT INTO x_db_version_h 
(id,version,inst_at,inst_by,updated_at,updated_by,act
 INSERT INTO x_db_version_h 
(id,version,inst_at,inst_by,updated_at,updated_by,active) VALUES 
(X_DB_VERSION_H_SEQ.nextval, '049',sys_extract_utc(systimestamp),'Ranger 
1.0.0',sys_extract_utc(systimestamp),'localhost','Y');
 INSERT INTO x_db_version_h 
(id,version,inst_at,inst_by,updated_at,updated_by,active) VALUES 
(X_DB_VERSION_H_SEQ.nextval, '050',sys_extract_utc(systimestamp),'Ranger 
1.0.0',sys_extract_utc(systimestamp),'localhost','Y');
 INSERT INTO x_db_version_h 
(id,version,inst_at,inst_by,updated_at,updated_by,active) VALUES 
(X_DB_VERSION_H_SEQ.nextval, '051',sys_extract_utc(systimestamp),'Ranger 
1.0.0',sys_extract_utc(systimestamp),'localhost','Y');
+INSERT INTO x_db_version_h 
(id,version,inst_at,inst_by,updated_at,updated_by,active) VALUES 
(X_DB_VERSION_H_SEQ.nextval, '052',sys_extract_utc(systimestamp),'Ranger 
1.0.0',sys_extract_utc(systimestamp),'localhost','Y');
 INSERT INTO x_db_version_h 
(id,version,inst_at,inst_by,updated_at,updated_by,active) VALUES 
(X_DB_VERSION_H_SEQ.nextval, 'DB_PATCHES',sys_extract_utc(systimestamp),'Ranger 
1.0.0',sys_extract_utc(systimestamp),'localhost','Y');
 
 INSERT INTO x_user_module_perm 
(id,user_id,module_id,create_time,update_time,added_by_id,upd_by_id,is_allowed) 
VALUES 
(X_USER_MODULE_PERM_SEQ.nextval,getXportalUIdByLoginId('admin'),getModulesIdByName('Reports'),sys_extract_utc(systimestamp),sys_extract_utc(systimestamp),getXportalUIdByLoginId('admin'),getXportalUIdByLoginId('admin'),1);
diff --git 
a/security-admin/db/oracle/patches/052-add-unique-constraint-on-change-logs.sql 
b/security-admin/db/oracle/patches/052-add-unique-constraint-on-change-logs.sql
new file mode 100644
index 0000000..ed24e6e
--- /dev/null
+++ 
b/security-admin/db/oracle/patches/052-add-unique-constraint-on-change-logs.sql
@@ -0,0 +1,32 @@
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+
+DECLARE
+       v_count number:=0;
+BEGIN
+       select count(*) into v_count from user_ind_columns where 
table_name='X_POLICY_CHANGE_LOG' and 
index_name='X_POLICY_CHANGE_LOG_UK_SERVICE_ID_POLICY_VERSION';
+       if (v_count = 0) THEN
+               execute immediate 'TRUNCATE TABLE X_POLICY_CHANGE_LOG';
+               execute immediate 'CREATE UNIQUE INDEX 
X_POLICY_CHANGE_LOG_UK_SERVICE_ID_POLICY_VERSION ON 
X_POLICY_CHANGE_LOG(service_id, policy_version)';
+               commit;
+       end if;
+
+       select count(*) into v_count from user_ind_columns where 
table_name='X_TAG_CHANGE_LOG' and 
index_name='X_TAG_CHANGE_LOG_UK_SERVICE_ID_SERVICE_TAGS_VERSION';
+       if (v_count = 0) THEN
+               execute immediate 'TRUNCATE TABLE X_TAG_CHANGE_LOG';
+               execute immediate 'CREATE UNIQUE INDEX 
X_TAG_CHANGE_LOG_UK_SERVICE_ID_SERVICE_TAGS_VERSION ON 
X_POLICY_CHANGE_LOG(service_id, service_tags_version)';
+               commit;
+       end if;
+END;/
diff --git 
a/security-admin/db/postgres/optimized/current/ranger_core_db_postgres.sql 
b/security-admin/db/postgres/optimized/current/ranger_core_db_postgres.sql
index 867bed5..1f181b2 100644
--- a/security-admin/db/postgres/optimized/current/ranger_core_db_postgres.sql
+++ b/security-admin/db/postgres/optimized/current/ranger_core_db_postgres.sql
@@ -1476,7 +1476,8 @@ service_type varchar(256) DEFAULT NULL NULL,
 policy_type int DEFAULT NULL NULL,
 zone_name varchar(256) DEFAULT NULL NULL,
 policy_id bigint DEFAULT NULL NULL,
-primary key (id)
+primary key (id),
+CONSTRAINT x_policy_change_log_uk_service_id_policy_version UNIQUE(service_id, 
policy_version)
 );
 commit;
 
@@ -1584,7 +1585,8 @@ change_type int NOT NULL,
 service_tags_version bigint DEFAULT '0' NOT NULL,
 service_resource_id bigint DEFAULT NULL NULL,
 tag_id bigint DEFAULT NULL NULL,
-primary key (id)
+primary key (id),
+CONSTRAINT x_tag_change_log_uk_service_id_service_tags_version 
UNIQUE(service_id, service_tags_version)
 );
 commit;
 
@@ -1888,6 +1890,7 @@ INSERT INTO x_db_version_h 
(version,inst_at,inst_by,updated_at,updated_by,active
 INSERT INTO x_db_version_h 
(version,inst_at,inst_by,updated_at,updated_by,active) VALUES 
('049',current_timestamp,'Ranger 1.0.0',current_timestamp,'localhost','Y');
 INSERT INTO x_db_version_h 
(version,inst_at,inst_by,updated_at,updated_by,active) VALUES 
('050',current_timestamp,'Ranger 1.0.0',current_timestamp,'localhost','Y');
 INSERT INTO x_db_version_h 
(version,inst_at,inst_by,updated_at,updated_by,active) VALUES 
('051',current_timestamp,'Ranger 1.0.0',current_timestamp,'localhost','Y');
+INSERT INTO x_db_version_h 
(version,inst_at,inst_by,updated_at,updated_by,active) VALUES 
('052',current_timestamp,'Ranger 1.0.0',current_timestamp,'localhost','Y');
 INSERT INTO x_db_version_h 
(version,inst_at,inst_by,updated_at,updated_by,active) VALUES 
('DB_PATCHES',current_timestamp,'Ranger 
1.0.0',current_timestamp,'localhost','Y');
 
 INSERT INTO x_user_module_perm 
(user_id,module_id,create_time,update_time,added_by_id,upd_by_id,is_allowed) 
VALUES
diff --git 
a/security-admin/db/postgres/patches/052-add-unique-constraint-on-change-logs.sql
 
b/security-admin/db/postgres/patches/052-add-unique-constraint-on-change-logs.sql
new file mode 100644
index 0000000..d226b59
--- /dev/null
+++ 
b/security-admin/db/postgres/patches/052-add-unique-constraint-on-change-logs.sql
@@ -0,0 +1,32 @@
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+
+select 'delimiter start';
+CREATE OR REPLACE FUNCTION create_unique_constraint_on_change_logs()
+RETURNS void AS $$
+BEGIN
+       IF not exists (select * from pg_constraint where conrelid in(select oid 
from pg_class where relname='x_policy_change_log') and 
conname='x_policy_change_log_uk_service_id_policy_version' and contype='u') THEN
+               ALTER TABLE x_policy_change_log ADD CONSTRAINT 
x_policy_change_log_uk_service_id_policy_version UNIQUE(service_id, 
policy_version);
+       END IF;
+       IF not exists (select * from pg_constraint where conrelid in(select oid 
from pg_class where relname='x_tag_change_log') and 
conname='x_tag_change_log_uk_service_id_service_tags_version' and contype='u') 
THEN
+               ALTER TABLE x_tag_change_log ADD CONSTRAINT 
x_tag_change_log_uk_service_id_service_tags_version UNIQUE(service_id, 
service_tags_version);
+       END IF;
+
+END;
+$$ LANGUAGE plpgsql;
+truncate x_policy_change_log;
+truncate x_tag_change_log;
+select create_unique_constraint_on_change_logs();
+select 'delimiter end';
diff --git 
a/security-admin/db/sqlanywhere/optimized/current/ranger_core_db_sqlanywhere.sql
 
b/security-admin/db/sqlanywhere/optimized/current/ranger_core_db_sqlanywhere.sql
index fd30f96..7c864ad 100644
--- 
a/security-admin/db/sqlanywhere/optimized/current/ranger_core_db_sqlanywhere.sql
+++ 
b/security-admin/db/sqlanywhere/optimized/current/ranger_core_db_sqlanywhere.sql
@@ -2054,10 +2054,14 @@ CREATE NONCLUSTERED INDEX 
x_policy_change_log_IDX_service_id ON dbo.x_policy_cha
 GO
 CREATE NONCLUSTERED INDEX x_policy_change_log_IDX_policy_version ON 
dbo.x_policy_change_log(policy_version ASC)
 GO
+CREATE NONCLUSTERED UNIQUE INDEX 
x_policy_change_log_uk_service_id_policy_version ON 
dbo.x_policy_change_log((service_id, policy_version) ASC)
+GO
 CREATE NONCLUSTERED INDEX x_tag_change_log_IDX_service_id ON 
dbo.x_tag_change_log(service_id ASC);
 GO
 CREATE NONCLUSTERED INDEX x_tag_change_log_IDX_tag_version ON 
dbo.x_tag_change_log(service_tags_version ASC);
 GO
+CREATE NONCLUSTERED INDEX x_tag_change_log_uk_service_id_service_tags_version 
ON dbo.x_tag_change_log((service_id, service_tags_version) ASC);
+GO
 
 CREATE OR REPLACE PROCEDURE dbo.removeForeignKeysAndTable (IN table_name 
varchar(100))
 AS
@@ -2270,6 +2274,8 @@ INSERT INTO x_db_version_h 
(version,inst_at,inst_by,updated_at,updated_by,active
 GO
 INSERT INTO x_db_version_h 
(version,inst_at,inst_by,updated_at,updated_by,active) VALUES 
('051',CURRENT_TIMESTAMP,'Ranger 1.0.0',CURRENT_TIMESTAMP,'localhost','Y');
 GO
+INSERT INTO x_db_version_h 
(version,inst_at,inst_by,updated_at,updated_by,active) VALUES 
('052',CURRENT_TIMESTAMP,'Ranger 1.0.0',CURRENT_TIMESTAMP,'localhost','Y');
+GO
 INSERT INTO x_db_version_h 
(version,inst_at,inst_by,updated_at,updated_by,active) VALUES 
('DB_PATCHES',CURRENT_TIMESTAMP,'Ranger 
1.0.0',CURRENT_TIMESTAMP,'localhost','Y');
 GO
 INSERT INTO x_user_module_perm 
(user_id,module_id,create_time,update_time,added_by_id,upd_by_id,is_allowed) 
VALUES 
(dbo.getXportalUIdByLoginId('admin'),dbo.getModulesIdByName('Reports'),CURRENT_TIMESTAMP,CURRENT_TIMESTAMP,dbo.getXportalUIdByLoginId('admin'),dbo.getXportalUIdByLoginId('admin'),1);
diff --git 
a/security-admin/db/sqlanywhere/patches/052-add-unique-constraint-on-change-logs.sql
 
b/security-admin/db/sqlanywhere/patches/052-add-unique-constraint-on-change-logs.sql
new file mode 100644
index 0000000..3ae5a94
--- /dev/null
+++ 
b/security-admin/db/sqlanywhere/patches/052-add-unique-constraint-on-change-logs.sql
@@ -0,0 +1,21 @@
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+BEGIN
+       TRUNCATE x_policy_change_log;
+       TRUNCATE x_tag_change_log;
+       CREATE NONCLUSTERED UNIQUE INDEX 
x_policy_change_log_uk_service_id_policy_version ON 
dbo.x_policy_change_log((service_id, policy_version) ASC);
+       CREATE NONCLUSTERED UNIQUE INDEX 
x_tag_change_log_uk_service_id_service_tags_version ON 
dbo.x_tag_change_log((service_id, service_tags_version) ASC);
+END
+GO
diff --git 
a/security-admin/db/sqlserver/optimized/current/ranger_core_db_sqlserver.sql 
b/security-admin/db/sqlserver/optimized/current/ranger_core_db_sqlserver.sql
index 276a57c..79d6202 100644
--- a/security-admin/db/sqlserver/optimized/current/ranger_core_db_sqlserver.sql
+++ b/security-admin/db/sqlserver/optimized/current/ranger_core_db_sqlserver.sql
@@ -2443,6 +2443,13 @@ CREATE NONCLUSTERED INDEX 
[x_policy_change_log_IDX_policy_version] ON [x_policy_
 WITH (SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, IGNORE_DUP_KEY = OFF, ONLINE 
= OFF) ON [PRIMARY]
 
 GO
+CREATE NONCLUSTERED UNIQUE INDEX 
[x_policy_change_log_uk_service_id_policy_version] ON [x_policy_change_log]
+(
+   [service_id,policy_version] ASC
+)
+WITH (SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, IGNORE_DUP_KEY = OFF, ONLINE 
= OFF) ON [PRIMARY]
+
+GO
 SET ANSI_NULLS ON
 SET QUOTED_IDENTIFIER ON
 SET ANSI_PADDING ON
@@ -2614,6 +2621,12 @@ CREATE NONCLUSTERED INDEX 
[x_tag_change_log_IDX_tag_version] ON [x_tag_change_lo
 )
 
 WITH (SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, IGNORE_DUP_KEY = OFF, ONLINE 
= OFF) ON [PRIMARY]
+CREATE NONCLUSTERED UNIQUE INDEX 
[x_tag_change_log_uk_service_id_service_tags_version] ON [x_tag_change_log]
+(
+   [service_id, service_tags_version] ASC
+)
+
+WITH (SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, IGNORE_DUP_KEY = OFF, ONLINE 
= OFF) ON [PRIMARY]
 SET ANSI_NULLS ON
 SET QUOTED_IDENTIFIER ON
 SET ANSI_PADDING ON
@@ -4110,6 +4123,7 @@ INSERT INTO x_db_version_h 
(version,inst_at,inst_by,updated_at,updated_by,active
 INSERT INTO x_db_version_h 
(version,inst_at,inst_by,updated_at,updated_by,active) VALUES 
('050',CURRENT_TIMESTAMP,'Ranger 1.0.0',CURRENT_TIMESTAMP,'localhost','Y');
 INSERT INTO x_db_version_h 
(version,inst_at,inst_by,updated_at,updated_by,active) VALUES 
('051',CURRENT_TIMESTAMP,'Ranger 1.0.0',CURRENT_TIMESTAMP,'localhost','Y');
 INSERT INTO x_db_version_h 
(version,inst_at,inst_by,updated_at,updated_by,active) VALUES 
('052',CURRENT_TIMESTAMP,'Ranger 1.0.0',CURRENT_TIMESTAMP,'localhost','Y');
+INSERT INTO x_db_version_h 
(version,inst_at,inst_by,updated_at,updated_by,active) VALUES 
('053',CURRENT_TIMESTAMP,'Ranger 1.0.0',CURRENT_TIMESTAMP,'localhost','Y');
 INSERT INTO x_db_version_h 
(version,inst_at,inst_by,updated_at,updated_by,active) VALUES 
('DB_PATCHES',CURRENT_TIMESTAMP,'Ranger 
1.0.0',CURRENT_TIMESTAMP,'localhost','Y');
 INSERT INTO x_user_module_perm 
(user_id,module_id,create_time,update_time,added_by_id,upd_by_id,is_allowed) 
VALUES 
(dbo.getXportalUIdByLoginId('admin'),dbo.getModulesIdByName('Reports'),CURRENT_TIMESTAMP,CURRENT_TIMESTAMP,dbo.getXportalUIdByLoginId('admin'),dbo.getXportalUIdByLoginId('admin'),1);
 INSERT INTO x_user_module_perm 
(user_id,module_id,create_time,update_time,added_by_id,upd_by_id,is_allowed) 
VALUES (dbo.getXportalUIdByLoginId('admin'),dbo.getModulesIdByName('Resource 
Based 
Policies'),CURRENT_TIMESTAMP,CURRENT_TIMESTAMP,dbo.getXportalUIdByLoginId('admin'),dbo.getXportalUIdByLoginId('admin'),1);
diff --git 
a/security-admin/db/sqlserver/patches/053-add-unique-constraint-on-change-logs.sql
 
b/security-admin/db/sqlserver/patches/053-add-unique-constraint-on-change-logs.sql
new file mode 100644
index 0000000..f3b5709
--- /dev/null
+++ 
b/security-admin/db/sqlserver/patches/053-add-unique-constraint-on-change-logs.sql
@@ -0,0 +1,30 @@
+
+
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+
+IF NOT EXISTS(select * from sys.indexes where 
name='x_policy_change_log_uk_service_id_policy_version')
+BEGIN
+       TRUNCATE x_policy_change_log;
+       CREATE NONCLUSTERED UNIQUE INDEX 
[x_policy_change_log_uk_service_id_policy_version] ON 
[x_policy_change_log]([service_id, policy_version] ASC)WITH (SORT_IN_TEMPDB = 
OFF, DROP_EXISTING = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF) ON [PRIMARY];
+END
+GO
+IF NOT EXISTS(select * from sys.indexes where 
name='x_tag_change_log_uk_service_id_service_tags_version')
+BEGIN
+       TRUNCATE x_tag_change_log;
+       CREATE NONCLUSTERED UNIQUE INDEX 
[x_tag_change_log_uk_service_id_service_tags_version] ON 
[x_tag_change_log]([service_id, service_tags_version] ASC)WITH (SORT_IN_TEMPDB 
= OFF, DROP_EXISTING = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF) ON [PRIMARY];
+END
+GO
+exit
diff --git 
a/security-admin/src/main/java/org/apache/ranger/biz/ServiceDBStore.java 
b/security-admin/src/main/java/org/apache/ranger/biz/ServiceDBStore.java
index e3c5b54..50eeb20 100644
--- a/security-admin/src/main/java/org/apache/ranger/biz/ServiceDBStore.java
+++ b/security-admin/src/main/java/org/apache/ranger/biz/ServiceDBStore.java
@@ -3480,7 +3480,7 @@ public class ServiceDBStore extends AbstractServiceStore {
                updatePolicyVersion(service, policyDeltaType, policy, 
updateServiceInfoRoleVersion);
        }
 
-       public enum VERSION_TYPE { POLICY_VERSION, TAG_VERSION, 
POLICY_AND_TAG_VERSION, ROLE_VERSION }
+       public enum VERSION_TYPE { POLICY_VERSION, TAG_VERSION, ROLE_VERSION }
 
        private void updatePolicyVersion(RangerService service, Integer 
policyDeltaType, RangerPolicy policy, boolean updateServiceInfoRoleVersion) 
throws Exception {
                if(service == null || service.getId() == null) {
@@ -3543,13 +3543,13 @@ public class ServiceDBStore extends 
AbstractServiceStore {
                Date now = new Date();
 
                if (serviceVersionInfoDbObj != null) {
-                       if (versionType == VERSION_TYPE.POLICY_VERSION || 
versionType == VERSION_TYPE.POLICY_AND_TAG_VERSION) {
+                       if (versionType == VERSION_TYPE.POLICY_VERSION) {
                                nextPolicyVersion = 
getNextVersion(serviceVersionInfoDbObj.getPolicyVersion());
 
                                
serviceVersionInfoDbObj.setPolicyVersion(nextPolicyVersion);
                                
serviceVersionInfoDbObj.setPolicyUpdateTime(now);
                        }
-                       if (versionType == VERSION_TYPE.TAG_VERSION || 
versionType == VERSION_TYPE.POLICY_AND_TAG_VERSION) {
+                       if (versionType == VERSION_TYPE.TAG_VERSION) {
                                
serviceVersionInfoDbObj.setTagVersion(getNextVersion(serviceVersionInfoDbObj.getTagVersion()));
                                serviceVersionInfoDbObj.setTagUpdateTime(now);
                        }
@@ -3584,7 +3584,11 @@ public class ServiceDBStore extends AbstractServiceStore 
{
                }
 
                if (service != null) {
-                       persistChangeLog(service, versionType, versionType == 
VERSION_TYPE.TAG_VERSION ? serviceVersionInfoDbObj.getTagVersion() : 
serviceVersionInfoDbObj.getPolicyVersion(), serviceVersionUpdater);
+                       if (versionType == VERSION_TYPE.POLICY_VERSION) {
+                               persistChangeLog(service, versionType, 
serviceVersionInfoDbObj.getPolicyVersion(), serviceVersionUpdater);
+                       } else if (versionType == VERSION_TYPE.TAG_VERSION) {
+                               persistChangeLog(service, versionType, 
serviceVersionInfoDbObj.getTagVersion(), serviceVersionUpdater);
+                       }
                }
        }
 
@@ -4974,11 +4978,7 @@ public class ServiceDBStore extends AbstractServiceStore 
{
                if (CollectionUtils.isNotEmpty(allServiceIds)) {
                        for (Long serviceId : allServiceIds) {
                                ServiceVersionUpdater updater = new 
ServiceVersionUpdater(daoMgr, serviceId, VERSION_TYPE.POLICY_VERSION, null, 
policyChangeType, null);
-                               if (policyChangeType == 
RangerPolicyDelta.CHANGE_TYPE_RANGER_ADMIN_START) {
-                                       persistChangeLog(updater);
-                               } else {
-                                       persistVersionChange(updater);
-                               }
+                               persistVersionChange(updater);
                        }
                }
 
@@ -4998,11 +4998,7 @@ public class ServiceDBStore extends AbstractServiceStore 
{
                if (CollectionUtils.isNotEmpty(allServiceIds)) {
                        for (Long serviceId : allServiceIds) {
                                ServiceVersionUpdater updater = new 
ServiceVersionUpdater(daoMgr, serviceId, VERSION_TYPE.TAG_VERSION, 
tagChangeType, null, null);
-                               if (tagChangeType == 
ServiceTags.TagsChangeType.RANGER_ADMIN_START) {
-                                       persistChangeLog(updater);
-                               } else {
-                                       persistVersionChange(updater);
-                               }
+                               persistVersionChange(updater);
                        }
                }
 
diff --git 
a/security-admin/src/main/java/org/apache/ranger/common/db/RangerTransactionSynchronizationAdapter.java
 
b/security-admin/src/main/java/org/apache/ranger/common/db/RangerTransactionSynchronizationAdapter.java
index 4bee929..7349898 100644
--- 
a/security-admin/src/main/java/org/apache/ranger/common/db/RangerTransactionSynchronizationAdapter.java
+++ 
b/security-admin/src/main/java/org/apache/ranger/common/db/RangerTransactionSynchronizationAdapter.java
@@ -115,28 +115,16 @@ public class RangerTransactionSynchronizationAdapter 
extends TransactionSynchron
             LOG.debug("==> 
RangerTransactionSynchronizationAdapter.afterCompletion(status=" + (status == 
STATUS_COMMITTED ? "COMMITTED" : "ROLLED_BACK") + ")");
         }
 
-        List<Runnable> allRunnables = null;
+        final boolean isParentTransactionCommitted = status == 
STATUS_COMMITTED;
 
-        if (status == STATUS_COMMITTED) {
-            final List<Runnable> postCommitRunnables = 
RUNNABLES_AFTER_COMMIT.get();
-            if (CollectionUtils.isNotEmpty(postCommitRunnables)) {
-                allRunnables = postCommitRunnables;
-            }
-        }
-
-        final List<Runnable> postCompletionRunnables = RUNNABLES.get();
-
-        if (CollectionUtils.isNotEmpty(postCompletionRunnables)) {
-            if (allRunnables == null) {
-                allRunnables = postCompletionRunnables;
-            } else {
-                allRunnables.addAll(postCompletionRunnables);
-            }
+        if (isParentTransactionCommitted) {
+            // Run tasks scheduled to run after transaction is successfully 
committed
+            runRunnables(RUNNABLES_AFTER_COMMIT.get(), true);
+            RUNNABLES_AFTER_COMMIT.remove();
         }
 
-        runRunnables(allRunnables);
-
-        RUNNABLES_AFTER_COMMIT.remove();
+        // Run other tasks scheduled to run after transaction completes
+        runRunnables(RUNNABLES.get(), false);
         RUNNABLES.remove();
 
         if (LOG.isDebugEnabled()) {
@@ -156,42 +144,50 @@ public class RangerTransactionSynchronizationAdapter 
extends TransactionSynchron
         return ret;
     }
 
-    private void runRunnables(final List<Runnable> runnables) {
+    private void runRunnables(final List<Runnable> runnables, final boolean 
isParentTransactionCommitted) {
         if (LOG.isDebugEnabled()) {
             LOG.debug("==> 
RangerTransactionSynchronizationAdapter.runRunnables()");
         }
 
-        if (runnables != null) {
+        if (CollectionUtils.isNotEmpty(runnables)) {
             if (LOG.isDebugEnabled()) {
                 LOG.debug("Executing {" + runnables.size() + "} runnables");
             }
-            try {
-                //Create new  transaction
-                TransactionTemplate txTemplate = new 
TransactionTemplate(txManager);
-                
txTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
-
-                txTemplate.execute(new TransactionCallback<Object>() {
-                    public Object doInTransaction(TransactionStatus status) {
-                        for (Runnable runnable : runnables) {
-                            if (LOG.isDebugEnabled()) {
-                                LOG.debug("Executing runnable {" + runnable + 
"}");
-                            }
-                            try {
-                                runnable.run();
-                            } catch (RuntimeException e) {
-                                LOG.error("Failed to execute runnable " + 
runnable, e);
-                                break;
+            for (Runnable runnable : runnables) {
+                boolean isThisTransactionCommitted;
+                do {
+                    try {
+                        //Create new  transaction
+                        TransactionTemplate txTemplate = new 
TransactionTemplate(txManager);
+                        
txTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
+
+                        Object result = txTemplate.execute(new 
TransactionCallback<Object>() {
+                            public Object doInTransaction(TransactionStatus 
status) {
+                                Object result;
+                                if (LOG.isDebugEnabled()) {
+                                    LOG.debug("Executing runnable {" + 
runnable + "}");
+                                }
+                                try {
+                                    runnable.run();
+                                    result = runnable;
+                                } catch (Throwable e) {
+                                    LOG.error("Failed to execute runnable " + 
runnable, e);
+                                    result = null;
+                                }
+                                return result;
                             }
-                        }
+                        });
 
-                        return null;
+                        isThisTransactionCommitted = result == runnable;
+
+                    } catch (Exception e) {
+                        if (LOG.isDebugEnabled()) {
+                            LOG.debug("Failed to commit TransactionService 
transaction for runnable:[" + runnable + "]", e);
+                        }
+                        LOG.warn("Failed to commit TransactionService 
transaction for runnable:[" + runnable + "]");
+                        isThisTransactionCommitted = false;
                     }
-                });
-            } catch (Exception e) {
-               if(LOG.isDebugEnabled()) {
-                       LOG.debug("Failed to commit TransactionService 
transaction", e);
-               }
-                LOG.warn("Failed to commit TransactionService transaction. 
Ignoring...");
+                } while (isParentTransactionCommitted && 
!isThisTransactionCommitted);
             }
         } else {
             if (LOG.isDebugEnabled()) {

Reply via email to