Skip to content

New API to create an ephemeral node that is session aware#630

Closed
huizhilu wants to merge 1 commit intoapache:masterfrom
huizhilu:createSessionAwareEphemeralV2
Closed

New API to create an ephemeral node that is session aware#630
huizhilu wants to merge 1 commit intoapache:masterfrom
huizhilu:createSessionAwareEphemeralV2

Conversation

@huizhilu
Copy link
Contributor

@huizhilu huizhilu commented Nov 22, 2019

  • Creates an ephemeral node that is session aware.
  • If the ephemeral node is created successfully, returns true.
  • If session is expired, returns false.
  • If connection is timed out or interrupted, exception is thrown.

I will complete the descriptions once the code itself is agreed. We will focus on the code logic in this draft.

@huizhilu huizhilu force-pushed the createSessionAwareEphemeralV2 branch from f6feb71 to c345714 Compare November 22, 2019 17:52
}

/**
* Creates a session aware ephemeral node.
Copy link
Contributor

Choose a reason for hiding this comment

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

Please explain what "session aware" means here as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, will do.

Copy link
Contributor

@kaisun2000 kaisun2000 Nov 22, 2019

Choose a reason for hiding this comment

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

"Creates an ephemeral node that is session aware.
If the ephemeral node is created successfully, returns true.
If session is expired, returns false.
If connection is timed out or interrupted, exception is thrown."

The description is not very accurate. The gist is that given a session id, we need to make sure the created ephemeral node must be in this session. Otherwise, the creation should fail.
ZkClient has a auto-reconnect feature. This feature makes this creating ephemeral node in this session operation not that easy.
We need handle session expired, disconnected state change. In additional close().

So all in all the following semantic should be true.

"
given a session ID, the api should return true it can create an ephemeral node in this session.
If met irrecoverable condition, the API should return false.
If met recoverable condition, the API should retry and create the ephemeral node if possible.
"

Here, irrecoverable condition can be: session expired, new session established, zkclient closed, interrrupted, keeper exceptions such as invalid parameters etc.

Recoverable condition can be: session disconnect. (Note, session can still be reconnected with the same session id potentially later.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, thanks for the meaningful description. This is what I am planning to complete. Once the code itself is agreed by us, I will complete the description.

}
// Call zookeeper to create an ephemeral node.
getConnection()
.create(path, serializedData, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we get SessionExpiredException here, the exception would be caught in retryUntilConnected(), and waitForRetry() will be run. And in the next connection in retryUntilConnected(), we get that getSessionId() != sessionId and so return false. It means, we don't return false right after catching SessionExpiredException, but waitForRetry and return false in the next connection. So false would be returned a bit delay.

* @throws RuntimeException
* if any other exception occurs
*/
public boolean createEphemeral(final String path, final Object data, final long sessionId)
Copy link
Contributor Author

@huizhilu huizhilu Nov 22, 2019

Choose a reason for hiding this comment

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

@lei-xia's comment:
lei-xia 2 minutes ago Many of the code in this method is duplicated with the one in create() (line 522). Another option is to extend that method to take a sessionID (also wrap the content of that method as a new private method to avoid expose it to be public API)?

My reply: I tried to reduce the duplicate code. There are 2 things to consider:

  1. Because the input data is an object, we need to serialize it. If so, we need to change the api parameters like:
    create(final String path, byte[] data, final List acl, final CreateMode mode, Callable callable);
    And we will need to change create() implementation, whether or not we want to do?
  2. Object data needs to be serialized and passed to the final create().
    I will show you the change for the above idea and see if we could accept it.

@huizhilu
Copy link
Contributor Author

Lei's comments: huizhilu@f6feb71

@lei-xia
Copy link
Member

lei-xia commented Nov 22, 2019

Can we have only 1 PR and keep all comments in one place. It is a bit confusing here.

}

/**
* Creates a session aware ephemeral node.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@lei-xia 's comment:
Give a more clear description what does this extra sessionId in parameters mean, such as, this method allows you to specify an expected sessionId, if the current sessionId of underlying ZK connection does not match the given sessionId, the creation will fail ....

*
* @param path path of the ephemeral node being created
* @param data data of the ephemeral node being created
* @param sessionId session id of the zookeeper client
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Lei @lei-xia
This is should be the expected session Id of the ZK connection, not necessarily the sessionID of current ZK Connection, rephrase it to make it less confusing. And also make it clear that if the sessionID of current ZK connection does not match the given (expected) sessionId, the operation will fail.

Copy link
Contributor

@junkaixue junkaixue left a comment

Choose a reason for hiding this comment

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

If we think this is fundamental change, why not we have another implementation of retryUntilConnect instead of moving code to createEmphemeNode?

@huizhilu
Copy link
Contributor Author

huizhilu commented Nov 22, 2019 via email

@huizhilu huizhilu closed this Nov 26, 2019
@huizhilu huizhilu deleted the createSessionAwareEphemeralV2 branch February 11, 2020 08:08
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.

5 participants