Skip to content

Conversation

@adoroszlai
Copy link
Contributor

What changes were proposed in this pull request?

WithObjectID.Builder and its subclasses currently provide two similar methods for setting objectID and updateID:

  • with...ID: validate input
  • set...ID: no validation

This PR moves validation from with...ID to build() (or rather validate(), called by build()). That is where other properties (in subclasses) are validated, and is common practice for builders.

Benefits:

  1. Users of Builder do not need to remember to use with... for validation, thus we can avoid potential bugs.
  2. getObjectInfo(), used by validation of updateID, did not provide any information about the object since builder does not customise toString(). If validation fails, the message can now reuse a temporary instance of the value object (obtained via buildMaybeInvalid()).

Also make WithParentObjectId abstract.

https://issues.apache.org/jira/browse/HDDS-13964

How was this patch tested?

CI:
https://github.com/adoroszlai/ozone/actions/runs/19629749773

Copy link
Contributor

@swamirishi swamirishi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adoroszlai thanks for the patch I have a few suggestions here design wise. Check and see if it makes sense

}

public OmMultipartKeyInfo build() {
validate();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of each build method calling validate we can call super.build() and that could call validate() method. The changes wouldn't be this big.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel this would be error prone again for newer implementations which could miss this.

Copy link
Contributor Author

@adoroszlai adoroszlai Nov 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of each build method calling validate we can call super.build() and that could call validate() method. The changes wouldn't be this big.

super.build() returns WithObjectID, not the specific subtype. If we need to override to cast the return value, there is no guarantee the implementation calls either super.build() or validate(). Maybe we can add a unit test for that.

I feel this would be error prone again for newer implementations which could miss this.

There are much fewer Builder subclasses than users of Builder.

Copy link
Contributor

@swamirishi swamirishi Nov 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use this
#9356 (comment)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The buildObject would be the previous build() method implementation or the current buildMaybeInvalid() and actual build() object will never get overridden maybe we can make it final

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added generic type to WithObjectID.Builder for this to work.

}

protected Builder(WithObjectID obj) {
super(obj);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit :

protected Builder(WithObjectID obj) {
metadata = new ConcurrentHashMap<>(obj.getMetadata());
}

The parameter type here should be WithMetadata

Copy link
Contributor Author

@adoroszlai adoroszlai Nov 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well spotted, but this was not introduced in this PR (WithMetadata is not changed at all).

Copy link
Contributor

@swamirishi swamirishi Nov 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I know was just wondering if we could fix this along with this patch

}
}

protected abstract WithObjectID buildMaybeInvalid();
Copy link
Contributor

@swamirishi swamirishi Nov 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
protected abstract WithObjectID buildMaybeInvalid();
protected abstract WithObjectID buildObject();
public final WithObjectID build() {
validate();
return buildObject();
}

Copy link
Contributor

@swamirishi swamirishi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thanks for the patch @adoroszlai

@adoroszlai adoroszlai merged commit 7c53356 into apache:master Nov 25, 2025
43 checks passed
@adoroszlai adoroszlai deleted the HDDS-13964 branch November 25, 2025 08:43
@adoroszlai
Copy link
Contributor Author

Thanks @swamirishi for the review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants