Skip to content

Commit d03ef0d

Browse files
authored
HDDS-14122. Make OmKeyInfo ACL list immutable (#9486)
1 parent d6026b2 commit d03ef0d

13 files changed

Lines changed: 69 additions & 61 deletions

File tree

hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/helpers/OmKeyInfo.java

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.List;
2525
import java.util.Map;
2626
import java.util.Objects;
27-
import java.util.concurrent.CopyOnWriteArrayList;
2827
import java.util.function.Consumer;
2928
import org.apache.commons.lang3.StringUtils;
3029
import org.apache.commons.lang3.tuple.Pair;
@@ -94,7 +93,7 @@ public final class OmKeyInfo extends WithParentObjectId
9493
/**
9594
* ACL Information.
9695
*/
97-
private final CopyOnWriteArrayList<OzoneAcl> acls;
96+
private final ImmutableList<OzoneAcl> acls;
9897

9998
/**
10099
* Used for S3 tags.
@@ -120,7 +119,7 @@ private OmKeyInfo(Builder b) {
120119
this.modificationTime = b.modificationTime;
121120
this.replicationConfig = b.replicationConfig;
122121
this.encInfo = b.encInfo;
123-
this.acls = new CopyOnWriteArrayList<>(b.acls);
122+
this.acls = b.acls.build();
124123
this.fileChecksum = b.fileChecksum;
125124
this.fileName = b.fileName;
126125
this.isFile = b.isFile;
@@ -442,19 +441,7 @@ public FileEncryptionInfo getFileEncryptionInfo() {
442441
}
443442

444443
public List<OzoneAcl> getAcls() {
445-
return ImmutableList.copyOf(acls);
446-
}
447-
448-
public boolean addAcl(OzoneAcl acl) {
449-
return OzoneAclUtil.addAcl(acls, acl);
450-
}
451-
452-
public boolean removeAcl(OzoneAcl acl) {
453-
return OzoneAclUtil.removeAcl(acls, acl);
454-
}
455-
456-
public boolean setAcls(List<OzoneAcl> newAcls) {
457-
return OzoneAclUtil.setAcl(acls, newAcls);
444+
return acls;
458445
}
459446

460447
public void setReplicationConfig(ReplicationConfig repConfig) {
@@ -499,7 +486,7 @@ public static class Builder extends WithParentObjectId.Builder<OmKeyInfo> {
499486
private long modificationTime;
500487
private ReplicationConfig replicationConfig;
501488
private FileEncryptionInfo encInfo;
502-
private final List<OzoneAcl> acls = new ArrayList<>();
489+
private final AclListBuilder acls;
503490
// not persisted to DB. FileName will be the last element in path keyName.
504491
private String fileName;
505492
private FileChecksum fileChecksum;
@@ -509,10 +496,16 @@ public static class Builder extends WithParentObjectId.Builder<OmKeyInfo> {
509496
private Long expectedDataGeneration = null;
510497

511498
public Builder() {
499+
this(AclListBuilder.empty());
500+
}
501+
502+
private Builder(AclListBuilder acls) {
503+
this.acls = acls;
512504
}
513505

514506
public Builder(OmKeyInfo obj) {
515507
super(obj);
508+
this.acls = AclListBuilder.of(obj.acls);
516509
this.volumeName = obj.volumeName;
517510
this.bucketName = obj.bucketName;
518511
this.keyName = obj.keyName;
@@ -529,7 +522,6 @@ public Builder(OmKeyInfo obj) {
529522
if (obj.getTags() != null) {
530523
this.tags.putAll(obj.getTags());
531524
}
532-
this.acls.addAll(obj.getAcls());
533525
obj.keyLocationVersions.forEach(keyLocationVersion ->
534526
this.omKeyLocationInfoGroups.add(
535527
new OmKeyLocationInfoGroup(keyLocationVersion.getVersion(),
@@ -621,11 +613,15 @@ public Builder setFileEncryptionInfo(FileEncryptionInfo feInfo) {
621613

622614
public Builder setAcls(List<OzoneAcl> listOfAcls) {
623615
if (listOfAcls != null) {
624-
this.acls.addAll(listOfAcls);
616+
this.acls.set(listOfAcls);
625617
}
626618
return this;
627619
}
628620

621+
public AclListBuilder acls() {
622+
return acls;
623+
}
624+
629625
public Builder addAcl(OzoneAcl ozoneAcl) {
630626
if (ozoneAcl != null) {
631627
this.acls.add(ozoneAcl);

hadoop-ozone/common/src/test/java/org/apache/hadoop/ozone/om/helpers/TestOmKeyInfo.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,11 @@ private void createdAndTest(boolean isMPU) {
189189
}
190190
}
191191

192-
key.setAcls(Arrays.asList(OzoneAcl.of(
193-
IAccessAuthorizer.ACLIdentityType.USER, "user1",
194-
ACCESS, IAccessAuthorizer.ACLType.WRITE)));
192+
key = key.toBuilder()
193+
.setAcls(Arrays.asList(OzoneAcl.of(
194+
IAccessAuthorizer.ACLIdentityType.USER, "user1",
195+
ACCESS, IAccessAuthorizer.ACLType.WRITE)))
196+
.build();
195197

196198
// Change acls and check.
197199
assertNotEquals(key, cloneKey);

hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/key/acl/OMKeyAclRequest.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,8 @@ public OMClientResponse validateAndUpdateCache(OzoneManager ozoneManager, Execut
106106
throw new OMException(OMException.ResultCodes.KEY_NOT_FOUND);
107107
}
108108

109-
operationResult = apply(omKeyInfo, trxnLogIndex);
110-
omKeyInfo = omKeyInfo.toBuilder()
111-
.setUpdateID(trxnLogIndex)
112-
.build();
109+
OmKeyInfo.Builder builder = omKeyInfo.toBuilder();
110+
operationResult = apply(builder, trxnLogIndex);
113111

114112
// Update the modification time when updating ACLs of Key.
115113
long modificationTime = omKeyInfo.getModificationTime();
@@ -125,7 +123,11 @@ public OMClientResponse validateAndUpdateCache(OzoneManager ozoneManager, Execut
125123
modificationTime = getOmRequest().getRemoveAclRequest()
126124
.getModificationTime();
127125
}
128-
omKeyInfo.setModificationTime(modificationTime);
126+
127+
omKeyInfo = builder
128+
.setModificationTime(modificationTime)
129+
.setUpdateID(trxnLogIndex)
130+
.build();
129131

130132
// update cache.
131133
omMetadataManager.getKeyTable(getBucketLayout())
@@ -244,10 +246,10 @@ abstract void onComplete(Result result, boolean operationResult,
244246
/**
245247
* Apply the acl operation, if successfully completed returns true,
246248
* else false.
247-
* @param omKeyInfo
249+
* @param builder
248250
* @param trxnLogIndex
249251
*/
250-
abstract boolean apply(OmKeyInfo omKeyInfo, long trxnLogIndex);
252+
abstract boolean apply(OmKeyInfo.Builder builder, long trxnLogIndex);
251253

252254
public void setBucketLayout(BucketLayout bucketLayout) {
253255
this.bucketLayout = bucketLayout;

hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/key/acl/OMKeyAclRequestWithFSO.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,19 +97,17 @@ public OMClientResponse validateAndUpdateCache(OzoneManager ozoneManager, Execut
9797
throw new OMException("Key not found. Key:" + key, KEY_NOT_FOUND);
9898
}
9999
omKeyInfo = keyStatus.getKeyInfo();
100-
// Reverting back the full path to key name
101-
// Eg: a/b/c/d/e/file1 -> file1
102-
omKeyInfo.setKeyName(OzoneFSUtils.getFileName(key));
103100
final long volumeId = omMetadataManager.getVolumeId(volume);
104101
final long bucketId = omMetadataManager.getBucketId(volume, bucket);
105102
final String dbKey = omMetadataManager.getOzonePathKey(volumeId, bucketId,
106103
omKeyInfo.getParentObjectID(), omKeyInfo.getFileName());
107104
boolean isDirectory = keyStatus.isDirectory();
108-
operationResult = apply(omKeyInfo, trxnLogIndex);
109-
omKeyInfo = omKeyInfo.toBuilder()
110-
.setUpdateID(trxnLogIndex)
111-
.build();
112105

106+
// Reverting back the full path to key name
107+
// Eg: a/b/c/d/e/file1 -> file1
108+
OmKeyInfo.Builder builder = omKeyInfo.toBuilder()
109+
.setKeyName(OzoneFSUtils.getFileName(key));
110+
operationResult = apply(builder, trxnLogIndex);
113111

114112
// Update the modification time when updating ACLs of Key.
115113
long modificationTime = omKeyInfo.getModificationTime();
@@ -124,7 +122,11 @@ public OMClientResponse validateAndUpdateCache(OzoneManager ozoneManager, Execut
124122
modificationTime =
125123
getOmRequest().getRemoveAclRequest().getModificationTime();
126124
}
127-
omKeyInfo.setModificationTime(modificationTime);
125+
126+
omKeyInfo = builder
127+
.setModificationTime(modificationTime)
128+
.setUpdateID(trxnLogIndex)
129+
.build();
128130

129131
// update cache.
130132
if (isDirectory) {

hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/key/acl/OMKeyAddAclRequest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ void onComplete(Result result, boolean operationResult,
140140
}
141141

142142
@Override
143-
boolean apply(OmKeyInfo omKeyInfo, long trxnLogIndex) {
143+
boolean apply(OmKeyInfo.Builder builder, long trxnLogIndex) {
144144
// No need to check not null here, this will be never called with null.
145-
return omKeyInfo.addAcl(ozoneAcls.get(0));
145+
return builder.acls().add(ozoneAcls.get(0));
146146
}
147147

148148
@Override

hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/key/acl/OMKeyAddAclRequestWithFSO.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ public OMKeyAddAclRequestWithFSO(
126126
getOmRequest().getUserInfo()));
127127
}
128128

129-
@Override boolean apply(OmKeyInfo omKeyInfo, long trxnLogIndex) {
129+
@Override boolean apply(OmKeyInfo.Builder builder, long trxnLogIndex) {
130130
// No need to check not null here, this will be never called with null.
131-
return omKeyInfo.addAcl(ozoneAcls.get(0));
131+
return builder.acls().add(ozoneAcls.get(0));
132132
}
133133

134134
@Override

hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/key/acl/OMKeyRemoveAclRequest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,9 @@ void onComplete(Result result, boolean operationResult,
141141
}
142142

143143
@Override
144-
boolean apply(OmKeyInfo omKeyInfo, long trxnLogIndex) {
144+
boolean apply(OmKeyInfo.Builder builder, long trxnLogIndex) {
145145
// No need to check not null here, this will be never called with null.
146-
return omKeyInfo.removeAcl(ozoneAcls.get(0));
146+
return builder.acls().remove(ozoneAcls.get(0));
147147
}
148148

149149
@Override

hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/key/acl/OMKeyRemoveAclRequestWithFSO.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ void onComplete(Result result, boolean operationResult,
136136
}
137137

138138
@Override
139-
boolean apply(OmKeyInfo omKeyInfo, long trxnLogIndex) {
139+
boolean apply(OmKeyInfo.Builder builder, long trxnLogIndex) {
140140
// No need to check not null here, this will be never called with null.
141-
return omKeyInfo.removeAcl(ozoneAcls.get(0));
141+
return builder.acls().remove(ozoneAcls.get(0));
142142
}
143143

144144
@Override

hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/key/acl/OMKeySetAclRequest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ void onComplete(Result result, boolean operationResult,
137137
}
138138

139139
@Override
140-
boolean apply(OmKeyInfo omKeyInfo, long trxnLogIndex) {
140+
boolean apply(OmKeyInfo.Builder builder, long trxnLogIndex) {
141141
// No need to check not null here, this will be never called with null.
142-
return omKeyInfo.setAcls(ozoneAcls);
142+
return builder.acls().set(ozoneAcls);
143143
}
144144

145145
@Override

hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/key/acl/OMKeySetAclRequestWithFSO.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ void onComplete(Result result, boolean operationResult,
129129
}
130130

131131
@Override
132-
boolean apply(OmKeyInfo omKeyInfo, long trxnLogIndex) {
132+
boolean apply(OmKeyInfo.Builder builder, long trxnLogIndex) {
133133
// No need to check not null here, this will be never called with null.
134-
return omKeyInfo.setAcls(ozoneAcls);
134+
return builder.acls().set(ozoneAcls);
135135
}
136136

137137
@Override

0 commit comments

Comments
 (0)