Skip to content

Commit 3d9edb3

Browse files
pinal-shahPinal Shah
authored andcommitted
ATLAS-5187: Fix audit related issues when rdbms backend is selected (#498)
(cherry picked from commit ff6e161)
1 parent bd1681a commit 3d9edb3

File tree

7 files changed

+262
-40
lines changed

7 files changed

+262
-40
lines changed

graphdb/janusgraph-rdbms/src/main/java/org/apache/atlas/repository/audit/rdbms/dao/DbEntityAuditDao.java

Lines changed: 78 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.atlas.repository.audit.rdbms.dao;
1919

2020
import org.apache.atlas.repository.audit.rdbms.entity.DbEntityAudit;
21+
import org.apache.commons.collections.CollectionUtils;
2122

2223
import javax.persistence.EntityManager;
2324
import javax.persistence.NoResultException;
@@ -30,34 +31,52 @@ public DbEntityAuditDao(EntityManager em) {
3031
super(em);
3132
}
3233

33-
public List<DbEntityAudit> getByEntityIdActionStartTimeStartIdx(String entityId, int action, long eventTimeStart, int eventIdxStart, int maxResults) {
34+
public List<DbEntityAudit> getByEntityIdActionStartTimeStartIdx(String entityId, String action, long eventTimeStart, int eventIdxStart, int maxResults) {
3435
try {
35-
return em.createNamedQuery("DbEntityAudit.getByEntityIdActionStartTimeStartIdx", DbEntityAudit.class)
36-
.setParameter("entityId", entityId)
37-
.setParameter("action", action)
38-
.setParameter("eventTimeStart", eventTimeStart)
39-
.setParameter("eventIdxStart", eventIdxStart)
40-
.setMaxResults(maxResults)
41-
.getResultList();
36+
if (action == null) {
37+
return em.createNamedQuery("DbEntityAudit.getByEntityIdStartTimeStartIdx", DbEntityAudit.class)
38+
.setParameter("entityId", entityId)
39+
.setParameter("eventTimeStart", eventTimeStart)
40+
.setParameter("eventIdxStart", eventIdxStart)
41+
.setMaxResults(maxResults)
42+
.getResultList();
43+
} else {
44+
return em.createNamedQuery("DbEntityAudit.getByEntityIdActionStartTimeStartIdx", DbEntityAudit.class)
45+
.setParameter("entityId", entityId)
46+
.setParameter("action", action)
47+
.setParameter("eventTimeStart", eventTimeStart)
48+
.setParameter("eventIdxStart", eventIdxStart)
49+
.setMaxResults(maxResults)
50+
.getResultList();
51+
}
4252
} catch (NoResultException excp) {
4353
// ignore
4454
}
4555

4656
return Collections.emptyList();
4757
}
4858

49-
public List<DbEntityAudit> getByEntityIdAction(String entityId, Integer action, int startIdx, int maxResults) {
59+
public List<DbEntityAudit> getByEntityIdAction(String entityId, String action, List<String> sortByColumn, boolean sortOrder, int startIdx, int maxResults) {
5060
try {
51-
if (action == null) {
52-
return em.createNamedQuery("DbEntityAudit.getByEntityId", DbEntityAudit.class)
61+
StringBuilder query = new StringBuilder("SELECT e FROM DbEntityAudit e WHERE e.entityId = :entityId");
62+
63+
if (action != null) {
64+
query.append(" and e.action = :action");
65+
if (CollectionUtils.isNotEmpty(sortByColumn)) {
66+
query.append(getOrderByQuery(sortByColumn, sortOrder));
67+
}
68+
return em.createQuery(query.toString(), DbEntityAudit.class)
5369
.setParameter("entityId", entityId)
70+
.setParameter("action", action)
5471
.setFirstResult(startIdx)
5572
.setMaxResults(maxResults)
5673
.getResultList();
5774
} else {
58-
return em.createNamedQuery("DbEntityAudit.getByEntityIdAction", DbEntityAudit.class)
75+
if (CollectionUtils.isNotEmpty(sortByColumn)) {
76+
query.append(getOrderByQuery(sortByColumn, sortOrder));
77+
}
78+
return em.createQuery(query.toString(), DbEntityAudit.class)
5979
.setParameter("entityId", entityId)
60-
.setParameter("action", action)
6180
.setFirstResult(startIdx)
6281
.setMaxResults(maxResults)
6382
.getResultList();
@@ -68,4 +87,50 @@ public List<DbEntityAudit> getByEntityIdAction(String entityId, Integer action,
6887

6988
return Collections.emptyList();
7089
}
90+
91+
public String getOrderByQuery(List<String> sortByColumn, boolean sortOrderDesc) {
92+
StringBuilder orderByQuery = new StringBuilder(" ORDER BY ");
93+
for (int i = 0; i < sortByColumn.size(); i++) {
94+
orderByQuery.append("e.").append(sortByColumn.get(i));
95+
orderByQuery.append(sortOrderDesc ? " DESC" : " ASC");
96+
if (i != sortByColumn.size() - 1) {
97+
orderByQuery.append(", ");
98+
}
99+
}
100+
return orderByQuery.toString();
101+
}
102+
103+
public List<DbEntityAudit> getLatestAuditsByEntityIdAction(String entityId, String action, List<String> filterActions) {
104+
try {
105+
StringBuilder query = new StringBuilder("SELECT e FROM DbEntityAudit e WHERE e.entityId = :entityId");
106+
107+
if (action != null) {
108+
query.append(" and e.action = :action");
109+
query.append(" ORDER BY e.eventTime DESC, e.eventIndex DESC");
110+
return em.createQuery(query.toString(), DbEntityAudit.class)
111+
.setParameter("entityId", entityId)
112+
.setParameter("action", action)
113+
.getResultList();
114+
} else {
115+
if (CollectionUtils.isNotEmpty(filterActions)) {
116+
query.append(" and e.action NOT IN (");
117+
for (int i = 0; i < filterActions.size(); i++) {
118+
query.append("'").append(filterActions.get(i)).append("'");
119+
if (i != filterActions.size() - 1) {
120+
query.append(", ");
121+
}
122+
}
123+
query.append(")");
124+
}
125+
query.append(" ORDER BY e.eventTime DESC, e.eventIndex DESC");
126+
return em.createQuery(query.toString(), DbEntityAudit.class)
127+
.setParameter("entityId", entityId)
128+
.getResultList();
129+
}
130+
} catch (NoResultException excp) {
131+
// ignore
132+
}
133+
134+
return Collections.emptyList();
135+
}
71136
}

graphdb/janusgraph-rdbms/src/main/java/org/apache/atlas/repository/audit/rdbms/entity/DbEntityAudit.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ public class DbEntityAudit implements java.io.Serializable {
6161
@Column(name = "user_name", nullable = false, length = 64)
6262
protected String user;
6363

64-
@Column(name = "operation", nullable = false)
65-
protected int action;
64+
@Column(name = "operation", nullable = false, length = 64)
65+
protected String action;
6666

6767
@Column(name = "details")
6868
@Lob
@@ -115,11 +115,11 @@ public void setUser(String user) {
115115
this.user = user;
116116
}
117117

118-
public int getAction() {
118+
public String getAction() {
119119
return action;
120120
}
121121

122-
public void setAction(int action) {
122+
public void setAction(String action) {
123123
this.action = action;
124124
}
125125

@@ -164,7 +164,7 @@ public boolean equals(Object obj) {
164164
eventTime == other.eventTime &&
165165
eventIndex == other.eventIndex &&
166166
Objects.equals(user, other.user) &&
167-
action == other.action &&
167+
Objects.equals(action, other.action) &&
168168
Objects.equals(details, other.details) &&
169169
Objects.equals(entity, other.entity) &&
170170
auditType == other.auditType;

graphdb/janusgraph-rdbms/src/main/resources/META-INF/janus-jpa_named_queries.xml

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,19 @@
1313
xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1414
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd ">
1515

16-
<named-query name="DbEntityAudit.getByEntityId">
17-
<query>SELECT obj FROM DbEntityAudit obj
18-
WHERE obj.entityId = :entityId
19-
ORDER BY obj.eventTime DESC, obj.eventIndex DESC
20-
</query>
21-
</named-query>
22-
23-
<named-query name="DbEntityAudit.getByEntityIdAction">
16+
<named-query name="DbEntityAudit.getByEntityIdActionStartTimeStartIdx">
2417
<query>SELECT obj FROM DbEntityAudit obj
2518
WHERE obj.entityId = :entityId
2619
AND obj.action = :action
20+
AND obj.eventTime >= :eventTimeStart
21+
AND obj.eventIndex >= :eventIdxStart
2722
ORDER BY obj.eventTime DESC, obj.eventIndex DESC
2823
</query>
2924
</named-query>
3025

31-
<named-query name="DbEntityAudit.getByEntityIdActionStartTimeStartIdx">
26+
<named-query name="DbEntityAudit.getByEntityIdStartTimeStartIdx">
3227
<query>SELECT obj FROM DbEntityAudit obj
3328
WHERE obj.entityId = :entityId
34-
AND obj.action = :action
3529
AND obj.eventTime >= :eventTimeStart
3630
AND obj.eventIndex >= :eventIdxStart
3731
ORDER BY obj.eventTime DESC, obj.eventIndex DESC

graphdb/janusgraph-rdbms/src/main/resources/META-INF/postgres/create_schema.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
-- DB objects for Atlas entity audit
1717
CREATE SEQUENCE IF NOT EXISTS atlas_entity_audit_seq INCREMENT BY 1 CACHE 1000;
1818

19-
CREATE TABLE IF NOT EXISTS atlas_entity_audit(id BIGINT, entity_id VARCHAR(64) NOT NULL, event_time BIGINT NOT NULL, event_idx INT NOT NULL, user_name VARCHAR(64) NOT NULL, operation INT NOT NULL, details TEXT DEFAULT NULL, entity TEXT DEFAULT NULL, audit_type INT NOT NULL, PRIMARY KEY(id));
19+
CREATE TABLE IF NOT EXISTS atlas_entity_audit(id BIGINT, entity_id VARCHAR(64) NOT NULL, event_time BIGINT NOT NULL, event_idx INT NOT NULL, user_name VARCHAR(64) NOT NULL, operation VARCHAR(64) NOT NULL, details TEXT DEFAULT NULL, entity TEXT DEFAULT NULL, audit_type INT NOT NULL, PRIMARY KEY(id));
2020

2121
CREATE INDEX IF NOT EXISTS atlas_entity_audit_idx_entity_id ON atlas_entity_audit (entity_id);
2222
CREATE INDEX IF NOT EXISTS atlas_entity_audit_idx_event_time ON atlas_entity_audit (event_time);

repository/src/main/java/org/apache/atlas/repository/audit/AbstractStorageBasedAuditRepository.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
import org.apache.atlas.exception.AtlasBaseException;
2525
import org.apache.atlas.listener.ActiveStateChangeHandler;
2626
import org.apache.atlas.model.audit.EntityAuditEventV2;
27-
import org.apache.atlas.repository.Constants.AtlasAuditAgingType;
2827
import org.apache.atlas.service.Service;
2928
import org.apache.commons.collections.CollectionUtils;
3029
import org.apache.commons.configuration.Configuration;
30+
import org.apache.commons.lang.StringUtils;
3131
import org.apache.hadoop.hbase.util.Bytes;
3232
import org.slf4j.Logger;
3333
import org.slf4j.LoggerFactory;
@@ -36,7 +36,6 @@
3636
import java.util.HashMap;
3737
import java.util.List;
3838
import java.util.Map;
39-
import java.util.Set;
4039

4140
/**
4241
* This abstract base class should be used when adding support for an audit storage backend.
@@ -83,11 +82,6 @@ public void putEventsV2(EntityAuditEventV2... events) throws AtlasBaseException
8382
putEventsV2(Arrays.asList(events));
8483
}
8584

86-
@Override
87-
public List<EntityAuditEventV2> deleteEventsV2(String entityId, Set<EntityAuditEventV2.EntityAuditActionV2> entityAuditActions, short auditCount, int ttlInDays, boolean createEventsAgeoutAllowed, AtlasAuditAgingType auditAgingType) throws AtlasBaseException, AtlasException {
88-
return null;
89-
}
90-
9185
@Override
9286
public List<Object> listEvents(String entityId, String startKey, short maxResults) throws AtlasBaseException {
9387
List ret = listEventsV2(entityId, null, startKey, maxResults);
@@ -168,6 +162,10 @@ protected byte[] getKey(String id, Long ts, int index) {
168162
}
169163

170164
protected long getTimestampFromKey(String key) {
165+
if (StringUtils.isEmpty(key)) {
166+
return 0L;
167+
}
168+
171169
String[] parts = key.split(FIELD_SEPARATOR);
172170

173171
if (parts.length < 3) {
@@ -183,6 +181,10 @@ protected long getTimestampFromKey(String key) {
183181
}
184182

185183
protected int getIndexFromKey(String key) {
184+
if (StringUtils.isEmpty(key)) {
185+
return 0;
186+
}
187+
186188
String[] parts = key.split(FIELD_SEPARATOR);
187189

188190
if (parts.length < 3) {

repository/src/main/java/org/apache/atlas/repository/audit/CassandraBasedAuditRepository.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
import org.apache.atlas.AtlasException;
2929
import org.apache.atlas.EntityAuditEvent;
3030
import org.apache.atlas.annotation.ConditionalOnAtlasProperty;
31+
import org.apache.atlas.exception.AtlasBaseException;
3132
import org.apache.atlas.model.audit.EntityAuditEventV2;
33+
import org.apache.atlas.repository.Constants;
3234
import org.apache.commons.lang3.NotImplementedException;
3335
import org.apache.commons.lang3.StringUtils;
3436
import org.slf4j.Logger;
@@ -38,6 +40,7 @@
3840
import javax.inject.Singleton;
3941

4042
import java.util.ArrayList;
43+
import java.util.Collections;
4144
import java.util.HashMap;
4245
import java.util.List;
4346
import java.util.Map;
@@ -203,6 +206,13 @@ public List<EntityAuditEventV2> listEventsV2(String entityId, EntityAuditEventV2
203206
return listEventsV2(entityId, auditAction, null, limit);
204207
}
205208

209+
@Override
210+
public List<EntityAuditEventV2> deleteEventsV2(String entityId, Set<EntityAuditEventV2.EntityAuditActionV2> entityAuditActions, short auditCount, int ttlInDays, boolean createEventsAgeoutAllowed, Constants.AtlasAuditAgingType auditAgingType)
211+
throws AtlasBaseException, AtlasException
212+
{
213+
return Collections.emptyList();
214+
}
215+
206216
@Override
207217
public Set<String> getEntitiesWithTagChanges(long fromTimestamp, long toTimestamp) {
208218
throw new NotImplementedException();

0 commit comments

Comments
 (0)