Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -312,14 +312,52 @@ public Builder setUpdateID(long uID) {
}

@Override
public OmLifecycleConfiguration buildObject() {
return new OmLifecycleConfiguration(this);
protected void validate() {
super.validate();
try {
if (StringUtils.isBlank(volume)) {
throw new OMException("Invalid lifecycle configuration: Volume cannot be blank.",
OMException.ResultCodes.INVALID_REQUEST);
}

if (StringUtils.isBlank(bucket)) {
throw new OMException("Invalid lifecycle configuration: Bucket cannot be blank.",
OMException.ResultCodes.INVALID_REQUEST);
}

if (rules.isEmpty()) {
throw new OMException("At least one rules needs to be specified in a lifecycle configuration.",
OMException.ResultCodes.INVALID_REQUEST);
}

if (rules.size() > LC_MAX_RULES) {
throw new OMException("The number of lifecycle rules must not exceed the allowed limit of "
+ LC_MAX_RULES + " rules", OMException.ResultCodes.INVALID_REQUEST);
}

if (!hasNoDuplicateID()) {
throw new OMException("Invalid lifecycle configuration: Duplicate rule IDs found.",
OMException.ResultCodes.INVALID_REQUEST);
}

for (OmLCRule rule : rules) {
rule.valid(bucketLayout, creationTime);
}
} catch (OMException e) {
throw new IllegalArgumentException(e.getMessage(), e);
}
}

private boolean hasNoDuplicateID() {
return rules.size() == rules.stream()
.map(OmLCRule::getId)
.collect(Collectors.toSet())
.size();
}

public OmLifecycleConfiguration buildAndValid() throws OMException {
OmLifecycleConfiguration omLifecycleConfiguration = buildObject();
omLifecycleConfiguration.valid();
return omLifecycleConfiguration;
@Override
protected OmLifecycleConfiguration buildObject() {
return new OmLifecycleConfiguration(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,16 @@ public final class OMLCUtils {

public static void assertOMException(Executable action, OMException.ResultCodes expectedResultCode,
String expectedMessageContent) {
OMException e = assertThrows(OMException.class, action);
Exception thrown = assertThrows(Exception.class, action);
OMException e;
if (thrown instanceof OMException) {
e = (OMException) thrown;
} else if (thrown instanceof IllegalArgumentException
&& thrown.getCause() instanceof OMException) {
e = (OMException) thrown.getCause();
} else {
throw new AssertionError("Expected OMException but got: " + thrown.getClass().getName(), thrown);
}
assertEquals(expectedResultCode, e.getResult());
assertTrue(e.getMessage().contains(expectedMessageContent),
"Expected: " + expectedMessageContent + "\n Actual: " + e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public void testDuplicateRuleIDs() throws OMException {
.setBucket("bucket")
.setRules(rules);

assertOMException(() -> config.buildAndValid(), INVALID_REQUEST, "Duplicate rule IDs found");
assertOMException(config::build, INVALID_REQUEST, "Duplicate rule IDs found");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ public void testCreateInValidLCConfiguration() throws OMException {
List<OmLCRule> rules = Collections.singletonList(rule);

OmLifecycleConfiguration.Builder lcc0 = getOmLifecycleConfiguration(null, "bucket", rules);
assertOMException(() -> lcc0.buildAndValid(), INVALID_REQUEST, "Volume cannot be blank");
assertOMException(lcc0::build, INVALID_REQUEST, "Volume cannot be blank");

OmLifecycleConfiguration.Builder lcc1 = getOmLifecycleConfiguration("volume", null, rules);
assertOMException(() -> lcc1.buildAndValid(), INVALID_REQUEST, "Bucket cannot be blank");
assertOMException(lcc1::build, INVALID_REQUEST, "Bucket cannot be blank");

OmLifecycleConfiguration.Builder lcc3 = getOmLifecycleConfiguration(
"volume", "bucket", Collections.emptyList());
assertOMException(() -> lcc3.buildAndValid(), INVALID_REQUEST,
assertOMException(lcc3::build, INVALID_REQUEST,
"At least one rules needs to be specified in a lifecycle configuration");

List<OmLCRule> rules4 = new ArrayList<>(
Expand All @@ -94,7 +94,7 @@ public void testCreateInValidLCConfiguration() throws OMException {
rules4.add(r);
}
OmLifecycleConfiguration.Builder lcc4 = getOmLifecycleConfiguration("volume", "bucket", rules4);
assertOMException(() -> lcc4.buildAndValid(), INVALID_REQUEST,
assertOMException(lcc4::build, INVALID_REQUEST,
"The number of lifecycle rules must not exceed the allowed limit of");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,16 @@ public OMClientResponse validateAndUpdateCache(OzoneManager ozoneManager, Execut
OmLifecycleConfiguration.Builder lcBuilder =
OmLifecycleConfiguration.getBuilderFromProtobuf(lifecycleConfiguration);
lcBuilder.setUpdateID(transactionLogIndex);
OmLifecycleConfiguration omLifecycleConfiguration =
lcBuilder.setBucketObjectID(bucketInfo.getObjectID()).build();
omLifecycleConfiguration.valid();
OmLifecycleConfiguration omLifecycleConfiguration;
try {
omLifecycleConfiguration =
lcBuilder.setBucketObjectID(bucketInfo.getObjectID()).build();
} catch (IllegalArgumentException e) {
if (e.getCause() instanceof OMException) {
throw (OMException) e.getCause();
}
throw e;
}
auditMap = omLifecycleConfiguration.toAuditMap();

metadataManager.getLifecycleConfigurationTable().addCacheEntry(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1846,21 +1846,29 @@ private OmLifecycleRuleAndOperator.Builder getOmLCAndOperatorBuilder(

private void createLifecyclePolicy(String volume, String bucket, BucketLayout layout, String prefix,
OmLCFilter filter, String date, boolean enabled) throws IOException {
OmLifecycleConfiguration lcc = new OmLifecycleConfiguration.Builder()
.setVolume(volume)
.setBucket(bucket)
.setBucketLayout(layout)
.setBucketObjectID(bucketObjectID)
.setRules(Collections.singletonList(new OmLCRule.Builder()
.setId(String.valueOf(OBJECT_ID_COUNTER.getAndIncrement()))
.setEnabled(enabled)
.setPrefix(prefix)
.setFilter(filter)
.setAction(new OmLCExpiration.Builder()
.setDate(date)
.build())
.build()))
.build();
OmLifecycleConfiguration lcc;
try {
lcc = new OmLifecycleConfiguration.Builder()
.setVolume(volume)
.setBucket(bucket)
.setBucketLayout(layout)
.setBucketObjectID(bucketObjectID)
.setRules(Collections.singletonList(new OmLCRule.Builder()
.setId(String.valueOf(OBJECT_ID_COUNTER.getAndIncrement()))
.setEnabled(enabled)
.setPrefix(prefix)
.setFilter(filter)
.setAction(new OmLCExpiration.Builder()
.setDate(date)
.build())
.build()))
.build();
} catch (IllegalArgumentException e) {
if (e.getCause() instanceof OMException) {
throw (OMException) e.getCause();
}
throw e;
}
String key = "/" + volume + "/" + bucket;
LifecycleConfiguration lcProto = lcc.getProtobuf();
OmLifecycleConfiguration canonicalLcc = OmLifecycleConfiguration.getFromProtobuf(lcProto);
Expand All @@ -1872,13 +1880,21 @@ private void createLifecyclePolicy(String volume, String bucket, BucketLayout la

private void createLifecyclePolicy(String volume, String bucket, BucketLayout layout, List<OmLCRule> ruleList)
throws IOException {
OmLifecycleConfiguration lcc = new OmLifecycleConfiguration.Builder()
.setVolume(volume)
.setBucket(bucket)
.setBucketObjectID(bucketObjectID)
.setBucketLayout(layout)
.setRules(ruleList)
.build();
OmLifecycleConfiguration lcc;
try {
lcc = new OmLifecycleConfiguration.Builder()
.setVolume(volume)
.setBucket(bucket)
.setBucketObjectID(bucketObjectID)
.setBucketLayout(layout)
.setRules(ruleList)
.build();
} catch (IllegalArgumentException e) {
if (e.getCause() instanceof OMException) {
throw (OMException) e.getCause();
}
throw e;
}
String key = "/" + volume + "/" + bucket;
LifecycleConfiguration lcProto = lcc.getProtobuf();
OmLifecycleConfiguration canonicalLcc = OmLifecycleConfiguration.getFromProtobuf(lcProto);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,14 @@ public OmLifecycleConfiguration toOmLifecycleConfiguration(OzoneBucket ozoneBuck
builder.addRule(convertToOmRule(rule));
}

return builder.buildAndValid();
} catch (Exception ex) {
if (ex instanceof IllegalStateException) {
throw S3ErrorTable.newError(S3ErrorTable.INVALID_REQUEST, ozoneBucket.getName(), ex);
return builder.build();
} catch (IllegalArgumentException ex) {
if (ex.getCause() instanceof OMException) {
throw (OMException) ex.getCause();
}
throw ex;
throw S3ErrorTable.newError(S3ErrorTable.INVALID_REQUEST, ozoneBucket.getName(), ex);
} catch (IllegalStateException ex) {
throw S3ErrorTable.newError(S3ErrorTable.INVALID_REQUEST, ozoneBucket.getName(), ex);
}
}

Expand Down