Skip to content

Conversation

@kezhuw
Copy link
Member

@kezhuw kezhuw commented May 30, 2023

Currently, there are 10 constructor variants for ZooKeeper and 4 for ZooKeeperAdmin. It is enough for us to resort to a builder.

The build method throws IOException to make it a drop-in replacement of existing constructors of ZooKeeper.

This pr also unify body of ZooKeeper constructor to one. Previously, there are diverged to two. One has sessionId and sessionPasswd, and another doesn't have. This pr uses sessionId == 0 to differentiate the two as it is used in server side to differentiate session create and reconnect.

Currently, there are 10 constructor variants for `ZooKeeper` and 4 for
`ZooKeeperAdmin`. It is enough for us to resort to a builder.

The `build` method throws `IOException` to make it a drop-in replacement
of existing constructors of `ZooKeeper`.

This pr also unify body of `ZooKeeper` constructor to one. Previously,
there are diverged to two. One has `sessionId` and `sessionPasswd`, and
another doesn't have. This pr uses `sessionId == 0` to differentiate the
two as it is used in server side to differentiate session create and
reconnect.
@kezhuw
Copy link
Member Author

kezhuw commented May 31, 2023

@eolivelli @jowiho Can you please take a look at this ? I think it supersedes ZOOKEEPER-4656(#1947) though I did not realize this in creating ZOOKEEPER-4697.

@tisonkun tisonkun requested review from eolivelli and tisonkun May 31, 2023 10:38
@tisonkun
Copy link
Member

I like this proposal. Will check it in this week.

@jowiho
Copy link

jowiho commented May 31, 2023

@kezhuw, I agree that your solution supersedes ZOOKEEPER-4656(#1947). I never got round to writing a builder for ZookeeperAdmin, mainly because the complexity of testing it. So I'm happy to see that you did just that. Thanks!

I've closed my PR (#1947) in favor of this PR.

* @throws IOException from constructor of ZooKeeper instance or wrapper of no IO exception
*/
@SuppressWarnings("unchecked")
public <T extends ZooKeeper> T build(Class<T> clazz) throws IOException {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please don't go this way.

This is totally out of our control.
If developers know the class they can instantiate it explicitly.

It it fine to add a buildZooKeeperAdmin() method

Copy link
Member Author

Choose a reason for hiding this comment

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

I felt similar(multiple ways to construct). I think there are candidates for us to go:

  1. Only build and buildAdmin for ZooKeeper and ZooKeeperAdmin. For other derivations, they should resort to ZooKeeperBuilder.toOptions and CustomZooKeeper(ZooKeeperOptions) for full options customization.
  2. Restrict ZooKeeperBuilder.toOptions to some level of private, so CustomZooKeeper(ZooKeeperOptions) is a simple hook for ZooKeeperBuilder. This way clients are encouraged(or forced) to use ZooKeeperBuilder::build(Class<T> class) to construct CustomZooKeeper.
  3. Combine above two and support only ZooKeeper and ZooKeeperAdmin. No third party derivations are supported or encouraged.

Maybe we can go 3 and unleash restriction in future if requested ? Currently, all other derivations are test purpose.

For "some level of private", I means @InterfaceAudience.Private or moving to package where ZooKeeper resides in.

Copy link
Member Author

Choose a reason for hiding this comment

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

I have added a follow up commit to build only ZooKeeper and ZooKeeperAdmin(e.g. candidate 3 from above).

I limited ZooKeeper(ZooKeeperOptions options), ZooKeeperAdmin(ZooKeeperOptions options) and ZooKeeperOptions to private using InterfaceAudience.Private now. ZooKeeperAdmin resides in different package than ZooKeeper, so I have resorted to InterfaceAudience.Private somewhere.

Copy link
Member

@tisonkun tisonkun Jun 7, 2023

Choose a reason for hiding this comment

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

No third party derivations are supported or encouraged

Anyone inherits ZooKeeper should be fine to build their own constructing method. And yes, we don't encourage that. If it's a common use case, it can go to upstream; if it's not, the upstream can be refactored to accept combination over inheritance. ZooKeeper is one of the key abstractions we deliver to users.

@kezhuw kezhuw changed the title ZOOKEEPER-4697: Add Builder to construct ZooKeeper and its derivations ZOOKEEPER-4697: Add Builder to construct ZooKeeper and ZooKeeperAdmin Jun 2, 2023
@tisonkun tisonkun requested a review from eolivelli June 7, 2023 03:35
Copy link
Member

@tisonkun tisonkun left a comment

Choose a reason for hiding this comment

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

The currrent implementation looks good.

Thank you!

@tisonkun
Copy link
Member

cc @eolivelli may you give another look on this PR?

Copy link
Contributor

@eolivelli eolivelli left a comment

Choose a reason for hiding this comment

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

LGTM

@kezhuw
Copy link
Member Author

kezhuw commented Aug 7, 2023

@tisonkun @eolivelli Shall we merge this ?

@eolivelli eolivelli merged commit e0890d0 into apache:master Sep 2, 2023
rahulrane50 pushed a commit to rahulrane50/zookeeper that referenced this pull request Sep 15, 2023
…apache#2001)

* ZOOKEEPER-4697: Add Builder to construct ZooKeeper and its derivations

Currently, there are 10 constructor variants for `ZooKeeper` and 4 for
`ZooKeeperAdmin`. It is enough for us to resort to a builder.

The `build` method throws `IOException` to make it a drop-in replacement
of existing constructors of `ZooKeeper`.

This pr also unify body of `ZooKeeper` constructor to one. Previously,
there are diverged to two. One has `sessionId` and `sessionPasswd`, and
another doesn't have. This pr uses `sessionId == 0` to differentiate the
two as it is used in server side to differentiate session create and
reconnect.

* Restrict Builder to only ZooKeeper and ZooKeeperAdmin
@kezhuw kezhuw deleted the ZOOKEEPER-4697-ZooKeeper-Builder branch September 24, 2023 15:08
AlphaCanisMajoris pushed a commit to AlphaCanisMajoris/zookeeper that referenced this pull request Mar 28, 2024
…apache#2001)

* ZOOKEEPER-4697: Add Builder to construct ZooKeeper and its derivations

Currently, there are 10 constructor variants for `ZooKeeper` and 4 for
`ZooKeeperAdmin`. It is enough for us to resort to a builder.

The `build` method throws `IOException` to make it a drop-in replacement
of existing constructors of `ZooKeeper`.

This pr also unify body of `ZooKeeper` constructor to one. Previously,
there are diverged to two. One has `sessionId` and `sessionPasswd`, and
another doesn't have. This pr uses `sessionId == 0` to differentiate the
two as it is used in server side to differentiate session create and
reconnect.

* Restrict Builder to only ZooKeeper and ZooKeeperAdmin
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.

4 participants