|
@@ -21,6 +21,7 @@ package org.apache.hadoop.hdfs.security.token.block;
|
|
|
import java.io.DataInput;
|
|
|
import java.io.DataOutput;
|
|
|
import java.io.IOException;
|
|
|
+import java.util.Arrays;
|
|
|
import java.util.EnumSet;
|
|
|
|
|
|
import org.apache.hadoop.classification.InterfaceAudience;
|
|
@@ -37,17 +38,23 @@ public class BlockTokenIdentifier extends TokenIdentifier {
|
|
|
private long expiryDate;
|
|
|
private int keyId;
|
|
|
private String userId;
|
|
|
- private long blockId;
|
|
|
+ private long [] blockIds;
|
|
|
private EnumSet<AccessMode> modes;
|
|
|
|
|
|
+ private byte [] cache;
|
|
|
+
|
|
|
public BlockTokenIdentifier() {
|
|
|
- this(null, 0, EnumSet.noneOf(AccessMode.class));
|
|
|
+ this(null, new long [] {}, EnumSet.noneOf(AccessMode.class));
|
|
|
}
|
|
|
|
|
|
- public BlockTokenIdentifier(String userId, long blockId,
|
|
|
+ public BlockTokenIdentifier(String userId, long [] blockIds,
|
|
|
EnumSet<AccessMode> modes) {
|
|
|
+ if(blockIds == null)
|
|
|
+ throw new IllegalArgumentException("blockIds can't be null");
|
|
|
+ this.cache = null;
|
|
|
this.userId = userId;
|
|
|
- this.blockId = blockId;
|
|
|
+ this.blockIds = Arrays.copyOf(blockIds, blockIds.length);
|
|
|
+ Arrays.sort(this.blockIds);
|
|
|
this.modes = modes == null ? EnumSet.noneOf(AccessMode.class) : modes;
|
|
|
}
|
|
|
|
|
@@ -59,7 +66,7 @@ public class BlockTokenIdentifier extends TokenIdentifier {
|
|
|
@Override
|
|
|
public UserGroupInformation getUser() {
|
|
|
if (userId == null || "".equals(userId)) {
|
|
|
- return UserGroupInformation.createRemoteUser(Long.toString(blockId));
|
|
|
+ return UserGroupInformation.createRemoteUser(Arrays.toString(blockIds));
|
|
|
}
|
|
|
return UserGroupInformation.createRemoteUser(userId);
|
|
|
}
|
|
@@ -69,6 +76,7 @@ public class BlockTokenIdentifier extends TokenIdentifier {
|
|
|
}
|
|
|
|
|
|
public void setExpiryDate(long expiryDate) {
|
|
|
+ this.cache = null;
|
|
|
this.expiryDate = expiryDate;
|
|
|
}
|
|
|
|
|
@@ -77,6 +85,7 @@ public class BlockTokenIdentifier extends TokenIdentifier {
|
|
|
}
|
|
|
|
|
|
public void setKeyId(int keyId) {
|
|
|
+ this.cache = null;
|
|
|
this.keyId = keyId;
|
|
|
}
|
|
|
|
|
@@ -84,18 +93,36 @@ public class BlockTokenIdentifier extends TokenIdentifier {
|
|
|
return userId;
|
|
|
}
|
|
|
|
|
|
- public long getBlockId() {
|
|
|
- return blockId;
|
|
|
+ /**
|
|
|
+ * Return sorted array of blockIds this {@link BlockTokenIdentifier} includes
|
|
|
+ */
|
|
|
+ public long [] getBlockIds() {
|
|
|
+ return blockIds;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Is specified blockId included in this BlockTokenIdentifier?
|
|
|
+ */
|
|
|
+ public boolean isBlockIncluded(long blockId) {
|
|
|
+ switch(blockIds.length) {
|
|
|
+ case 1:
|
|
|
+ return blockIds[0] == blockId;
|
|
|
+ case 2:
|
|
|
+ return (blockIds[0] == blockId) || (blockIds[1] == blockId);
|
|
|
+ default:
|
|
|
+ return Arrays.binarySearch(blockIds, blockId) >= 0;
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public EnumSet<AccessMode> getAccessModes() {
|
|
|
return modes;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
public String toString() {
|
|
|
return "block_token_identifier (expiryDate=" + this.getExpiryDate()
|
|
|
+ ", keyId=" + this.getKeyId() + ", userId=" + this.getUserId()
|
|
|
- + ", blockId=" + this.getBlockId() + ", access modes="
|
|
|
+ + ", blockId=" + Arrays.toString(blockIds) + ", access modes="
|
|
|
+ this.getAccessModes() + ")";
|
|
|
}
|
|
|
|
|
@@ -111,7 +138,8 @@ public class BlockTokenIdentifier extends TokenIdentifier {
|
|
|
if (obj instanceof BlockTokenIdentifier) {
|
|
|
BlockTokenIdentifier that = (BlockTokenIdentifier) obj;
|
|
|
return this.expiryDate == that.expiryDate && this.keyId == that.keyId
|
|
|
- && isEqual(this.userId, that.userId) && this.blockId == that.blockId
|
|
|
+ && isEqual(this.userId, that.userId)
|
|
|
+ && Arrays.equals(this.blockIds, that.blockIds)
|
|
|
&& isEqual(this.modes, that.modes);
|
|
|
}
|
|
|
return false;
|
|
@@ -119,15 +147,18 @@ public class BlockTokenIdentifier extends TokenIdentifier {
|
|
|
|
|
|
/** {@inheritDoc} */
|
|
|
public int hashCode() {
|
|
|
- return (int) expiryDate ^ keyId ^ (int) blockId ^ modes.hashCode()
|
|
|
+ return (int) expiryDate ^ keyId ^ Arrays.hashCode(blockIds) ^ modes.hashCode()
|
|
|
^ (userId == null ? 0 : userId.hashCode());
|
|
|
}
|
|
|
|
|
|
public void readFields(DataInput in) throws IOException {
|
|
|
+ cache = null;
|
|
|
expiryDate = WritableUtils.readVLong(in);
|
|
|
keyId = WritableUtils.readVInt(in);
|
|
|
userId = WritableUtils.readString(in);
|
|
|
- blockId = WritableUtils.readVLong(in);
|
|
|
+ blockIds = new long[WritableUtils.readVInt(in)];
|
|
|
+ for(int i = 0; i < blockIds.length; i++)
|
|
|
+ blockIds[i] = WritableUtils.readVLong(in);
|
|
|
int length = WritableUtils.readVInt(in);
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
modes.add(WritableUtils.readEnum(in, AccessMode.class));
|
|
@@ -138,10 +169,19 @@ public class BlockTokenIdentifier extends TokenIdentifier {
|
|
|
WritableUtils.writeVLong(out, expiryDate);
|
|
|
WritableUtils.writeVInt(out, keyId);
|
|
|
WritableUtils.writeString(out, userId);
|
|
|
- WritableUtils.writeVLong(out, blockId);
|
|
|
+ WritableUtils.writeVInt(out, blockIds.length);
|
|
|
+ for(int i = 0; i < blockIds.length; i++)
|
|
|
+ WritableUtils.writeVLong(out, blockIds[i]);
|
|
|
WritableUtils.writeVInt(out, modes.size());
|
|
|
for (AccessMode aMode : modes) {
|
|
|
WritableUtils.writeEnum(out, aMode);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public byte[] getBytes() {
|
|
|
+ if(cache == null) cache = super.getBytes();
|
|
|
+
|
|
|
+ return cache;
|
|
|
+ }
|
|
|
}
|