Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

samples: add samples #37

Merged
merged 23 commits into from Nov 9, 2020
Merged

samples: add samples #37

merged 23 commits into from Nov 9, 2020

Conversation

anguillanneuf
Copy link
Collaborator

@anguillanneuf anguillanneuf commented Oct 3, 2020

Testing:

Samples are tested as part of the required test pubsublite-build. They can be also tested using the following commands locally:

export INSTALL_LIBRARY_FROM_SOURCE=true
cd samples/snippets
python -m nox
# Optionally (choose a session): python -m nox -s lint py-3.6

Summary:

This PR includes the following samples and all their tests are in ./quickstart_test.py:

.
├── create_lite_subscription_example.py
├── create_lite_topic_example.py
├── delete_lite_subscription_example.py
├── delete_lite_topic_example.py
├── get_lite_subscription_example.py
├── get_lite_topic_example.py
├── list_lite_subscriptions_in_project_example.py
├── list_lite_subscriptions_in_topic_example.py
├── list_lite_topics_example.py
├── publish_with_batch_settings_example.py
├── publish_with_custom_attributes_example.py
├── publish_with_ordering_key_example.py
├── publisher_example.py
├── subscriber_example.py
├── update_lite_subscription_example.py
└── update_lite_topic_example.py

Note to reviewer:

Please expect this PR to have the do not merge label as we work to polish the library. Below are some issues I have identified the library. Your reviews can be used for support and/or to open additional issues. Update: These issues have been addressed.

  1. Admin, publisher, and subscriber client import and creation are atypical. issue: atypical client creation  #45
  2. Publisher futures do not return outside a context manager. issue: publish future hangs when a context manager is not used #46
  3. make_subscriber() does not return a subscriber client but a streaming pull future. issue: make_subscriber() doesn't make a subscriber #47
  4. Admin client expects a string but publisher and subscriber clients expect typed objects for resource names. issue: make clients accept strings and typed objects for resource names #48
  5. FlowControlSettings in the subscriber example should ideally be created as a type: from google.cloud.pubsublite.types import FlowControlSettings. Refactor FlowControlSettings under google.cloud.pubsublite.types #49
  6. Consider bringing the following under google.cloud.pubsublite.types: a) Classes in google.cloud.pubsublite.location b) Classes in google.cloud.pubsublite.paths c) Classes in google.cloud.pubsublite.endpoints. Refactor paths/location/endpoints under google.cloud.pubsublite.types #50

add create topic

set project number

lint
@google-cla google-cla bot added the cla: yes This human has signed the Contributor License Agreement. label Oct 3, 2020
@anguillanneuf anguillanneuf changed the title samples: add admin samples samples: add samples Oct 9, 2020
@anguillanneuf anguillanneuf requested a review from a team October 16, 2020 01:13
@anguillanneuf anguillanneuf marked this pull request as ready for review October 16, 2020 01:14
@anguillanneuf anguillanneuf added the do not merge Indicates a pull request not ready for merge, due to either quality or timing. label Oct 16, 2020
samples/snippets/create_lite_subscription_example.py Outdated Show resolved Hide resolved
samples/snippets/create_lite_subscription_example.py Outdated Show resolved Hide resolved
samples/snippets/create_lite_topic_example.py Outdated Show resolved Hide resolved
assert "created successfully." in out


def test_update_lite_topic_example(topic_path, capsys):
Copy link

Choose a reason for hiding this comment

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

A small concern that this test depends on the execution order. Probably not a big deal.

samples/snippets/quickstart_test.py Outdated Show resolved Hide resolved
anguillanneuf and others added 3 commits October 19, 2020 15:05
* fix: Move types to common directory

Also move internal-only files to internal directory.

* fix: Fix test imports
@anguillanneuf anguillanneuf requested a review from a team as a code owner October 19, 2020 22:36
subscription_path = SubscriptionPath(project_number, location, subscription_id)

subscription = Subscription(
name=str(subscription_path),
Copy link

Choose a reason for hiding this comment

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

re: str(), it might be a library usability issue, but isn't it better if we can just pass the SubscriptionPath as the argument? Is it possible to do it in the constructor? Feel free to ignore, or file a new issue and fix later.

Copy link
Collaborator

Choose a reason for hiding this comment

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

This type is a proto, so unfortunately not unless we want to wrap the proto.

Copy link

Choose a reason for hiding this comment

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

Ok, so nevermind.

Copy link
Contributor

Choose a reason for hiding this comment

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

https://github.com/googleapis/python-pubsublite/blob/master/google/cloud/pubsublite/types/paths.py looks like a handwritten file to me.

The generator usually produces methods to construct and decompose paths (see examples in AutoML). https://aip.dev/client-libraries/4231

Did that not happen for pubsublite?

Copy link
Collaborator

Choose a reason for hiding this comment

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

It produces methods but not types. These methods (like

def subscription_path(project: str, location: str, subscription: str,) -> str:
) produce strings as their output: meaning there is no way to type check arguments of path type. If these produced wrapper objects I would have used them.

Copy link
Collaborator

Choose a reason for hiding this comment

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

They also don't type check the current restriction (that we're trying to remove in the backend) that only project numbers be used in the path.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Those methods did get generated for pubsublite but they aren't used.

Topic paths are created by a publisher client in Pub/Sub but by an admin client in Pub/Sub Lite.

# This looks cumbersome.
client = AdminClient(cloud_region)
topic_path = client.topic_path(project_number, location, topic_id)
with PublisherClient() as publisher_client:
    api_future = publisher_client.publish(topic_path, data.encode("utf-8"))

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah okay, thanks for the explanation. 👍

Was there a decision made that pubsublite is not going to support project IDs? https://google.aip.dev/cloud/2510 I haven't been looking at all these PRs so apologies if this is digging up an old conversation.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Its the difference between "must" and "should". Its nontrivial work to convert between the two and it has not been addressed until this quarter. I've sent you the internal link from the elysium team that clarifies this guidance.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The discussion was in internal b/162623801. Java currently accepts both. Python/Go will wait for this change on the server side.

data = f"{message}"
api_future = publisher_client.publish(topic_path, data.encode("utf-8"))
# result() blocks. To resolve API futures asynchronously, use add_done_callback().
message_id = api_future.result()
Copy link

Choose a reason for hiding this comment

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

re: result(), is there a hard deadline, or will it block forever?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

result() can take an optional timeout argument, by default, it blocks forever unless it errors out first.

Copy link

Choose a reason for hiding this comment

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

Can we have timeout then? I think we should avoid infinite wait if possible.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's rare for result() to fail. In the case of failure, the client library will retry publish if no timeout is provided (as is the case for Pub/Sub too). I think not adding timeout is what most people want.

Copy link

Choose a reason for hiding this comment

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

Ok, I can live with that, but keep in mind if a rare case happens, the code will indefinitely wait.

@tmatsuo tmatsuo added the snippet-bot:force-run Force snippet-bot runs its logic label Nov 5, 2020
@snippet-bot snippet-bot bot removed the snippet-bot:force-run Force snippet-bot runs its logic label Nov 5, 2020
@anguillanneuf anguillanneuf removed the do not merge Indicates a pull request not ready for merge, due to either quality or timing. label Nov 5, 2020
Copy link
Collaborator

@dpcollins-google dpcollins-google left a comment

Choose a reason for hiding this comment

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

LGTM once two comments are addressed.

samples/snippets/create_lite_topic_example.py Outdated Show resolved Hide resolved
@anguillanneuf anguillanneuf merged commit 7fae567 into master Nov 9, 2020
@anguillanneuf anguillanneuf deleted the samples-admin branch November 9, 2020 17:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants