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

[Issue 6170][functions] add custom property option to functions #6348

Merged

Conversation

nlu90
Copy link
Member

@nlu90 nlu90 commented Feb 17, 2020

Fixes #6170

Motivation

Allow users set custom system properties while submitting functions. This can be used to pass credentials via system property.

Modifications

  • pulsar-admin client tool to accept multiple --custom-property options while creating the function
  • function's java runtime to append these provided properties.

Verifying this change

  • Make sure that the change passes the CI checks.

(Please pick either of the following options)

This change is a trivial rework / code cleanup without any test coverage.

(or)

This change is already covered by existing tests, such as (please describe tests).

(or)

This change added tests and can be verified as follows:

(example:)

  • Added integration tests for end-to-end deployment with large payloads (10MB)
  • Extended integration test for recovery after broker failure

Does this pull request potentially affect one of the following parts:

If yes was chosen, please highlight the changes

  • Dependencies (does it add or upgrade a dependency): no
  • The public API: no
  • The schema: no
  • The default values of configurations: no
  • The wire protocol: no
  • The rest endpoints: no
  • The admin cli options: yes
  • Anything that affects deployment: no

Documentation

  • Does this pull request introduce a new feature? no
  • If yes, how is the feature documented? (not applicable / docs / JavaDocs / not documented)
  • If a feature is not applicable for documentation, explain why?
  • If a feature is not documented yet in this PR, please create a followup issue for adding the documentation

@nlu90
Copy link
Member Author

nlu90 commented Feb 17, 2020

Now we can submit a function with multiple custom properties as follows:

./bin/pulsar-admin functions create \
--custom-property prop1="hello_world" --custom-property success=1 \
--jar ./pulsar-functions/java-examples/target/pulsar-functions-api-examples.jar \
--classname org.apache.pulsar.functions.api.examples.ExclamationFunction \
--tenant public --namespace default --name exclamation \
--inputs persistent://public/default/my-topic \
--output persistent://public/default/exclamation

@nlu90
Copy link
Member Author

nlu90 commented Feb 18, 2020

After a discussion with @sijie , the way this options work is as follows now:

./bin/pulsar-admin functions create \
--custom-property prop1=PROP1_VALUE --custom-property prop2=PROP2_VALUE \
--jar ./pulsar-functions/java-examples/target/pulsar-functions-api-examples.jar \
--classname org.apache.pulsar.functions.api.examples.ExclamationFunction \
--tenant public --namespace default --name exclamation \
--inputs persistent://public/default/my-topic \
--output persistent://public/default/exclamation

both PROP1_VALUE and PROP2_VALUE would be replaced with the actual system property values(assume already set by user) when the function process is being started.

@KannarFr
Copy link
Contributor

@nlu90 Hi, thanks for the work you did here :).

  • So PROP2_VALUE must be a system env var? What if it's undefined? Does the system throws an exception on boot?

  • All these options are defined using pulsar-admin with arguments. Does the creation via YAML function configuration supports these features?

@nlu90
Copy link
Member Author

nlu90 commented Feb 18, 2020

@KannarFr Thanks for your feedback

  • I added check for whether the property is set in broker/worker. If it's not set, an IllegalArgumentException will be thrown and the function submission will be failed.

  • Yes, this change also works with YAML configuration. For my previously example submission command, you can also use the following YAML file as a configuration file to submit:

jar: ./pulsar-functions/java-examples/target/pulsar-functions-api-examples.jar
className: org.apache.pulsar.functions.api.examples.ExclamationFunction
tenant: public
namespace: default
name: exclamation
customProperties: 
  - prop1=PROP1_VALUE
  - prop2=PROP2_VALUE
inputs: 
  - persistent://public/default/my-topic
output: persistent://public/default/exclamation

and the submission command is:

./bin/pulsar-admin functions create --function-config-file ~/func-conf.yaml

@sijie sijie added this to the 2.6.0 milestone Feb 18, 2020
@sijie
Copy link
Member

sijie commented Feb 18, 2020

@nlu90 the change looks very good. although I think it might be worth thinking of the extensibility of this approach. We might be looking into some more generic variable binding approach that allows user dynamically generating the properties or values through variable bindings. Serverless provides a good example of how to load variable bindings through different formats (e.g. environment variables, cli options, and etc).

@nlu90
Copy link
Member Author

nlu90 commented Feb 18, 2020

@sijie We can create a new issue for the yaml file variable extension feature and add it in a separate PR.

@sijie
Copy link
Member

sijie commented Feb 18, 2020

@nlu90 sounds good. do you want to create an issue for that?

@sijie
Copy link
Member

sijie commented Feb 18, 2020

@nlu90 actually looked into the description of #6170, I think what @KannarFr is looking for might be similar to a variable binding approach. Maybe let's ask @KannarFr for some clarifications.

@KannarFr
Copy link
Contributor

KannarFr commented Feb 18, 2020 via email

@nlu90
Copy link
Member Author

nlu90 commented Feb 18, 2020

Hi @KannarFr, Just want to confirm if the following solution meet your needs (Notice the last line of customRuntimeOptions):

tenant: "test"
namespace: "test-namespace"
name: "example"
className: "org.apache.pulsar.functions.api.examples.ExclamationFunction"
inputs: ["test_src"]
userConfig:
  "PublishTopic": "test_result"
output: "test_result"
autoAck: true
parallelism: 1
customRuntimeOptions: "-DmyProp=${MY_PROP} -Dother_option=other_value"

and when you create function with this yaml file, the variable ${MY_PROP} will be automatically replaced with the actual system property value.

Let me know your thoughts.

@KannarFr
Copy link
Contributor

KannarFr commented Feb 18, 2020 via email

@nlu90
Copy link
Member Author

nlu90 commented Feb 19, 2020

@sijie @KannarFr Now you can submit a function with the following yaml file. And ${MY_PROP1} and ${MY_PROP2} will be replaced with the predefined environment variables.

jar: ./pulsar-functions/java-examples/target/pulsar-functions-api-examples.jar
className: org.apache.pulsar.functions.api.examples.ExclamationFunction
tenant: public
namespace: default
name: exclamation
inputs:
  - persistent://public/default/my-topic
output: persistent://public/default/exclamation
customRuntimeOptions: "-Dprop1=${MY_PROP1} -Dprop2=${MY_PROP2}"

One more question is currently if we fetch function info with the cli, it will show the actual value of ${MY_PROP1} and ${MY_PROP2}, is this expected? Or should we hide the actual value in the cli result?

@KannarFr
Copy link
Contributor

@nlu90 Well considering logs are often exported to external services, I think we must hide them from logs. Thats being said, we can log the env var name like: injecting $PROP as prop foreach prop. WDYT?

@nlu90
Copy link
Member Author

nlu90 commented Feb 19, 2020

@KannarFr Now no actual variable value will be logged into files.

@KannarFr
Copy link
Contributor

@nlu90 Perfect! Thanks for all the work you did here :).

@KannarFr
Copy link
Contributor

@sijie can we merge this?

@KannarFr
Copy link
Contributor

@nlu90 oh, there few tests issues, can you check them?

@sijie sijie added the doc-required Your PR changes impact docs and you will update later. label Feb 24, 2020
@sijie
Copy link
Member

sijie commented Feb 24, 2020

/pulsarbot run-failure-checks

@KannarFr
Copy link
Contributor

What is the status of this PR?

@nlu90
Copy link
Member Author

nlu90 commented Mar 24, 2020

@KannarFr Sorry for the late reply, I was busy with some other projects.

After a discussion with @sijie , now the changes enable you put the env variable in userConfig and get the actual value via context.getUserConfigValue

an example yaml would be:

jar: ./pulsar-functions/java-examples/target/pulsar-functions-api-examples.jar
className: org.apache.pulsar.functions.api.examples.ExclamationFunction
tenant: public
namespace: default
name: exclamation
inputs:
  - persistent://public/default/my-topic
output: persistent://public/default/exclamation
userConfig:
  "prop1": "$MY_PROP"

and you can access the actual value for prop1 in your function code like this:
context.getUserConfigValue("prop1")

@KannarFr
Copy link
Contributor

@nlu90 Thanks for your answer.

So

  "prop1": "$MY_PROP"

reads $MY_PROP from system env vars? Right?

@nlu90
Copy link
Member Author

nlu90 commented Mar 24, 2020

@nlu90 Thanks for your answer.

So

  "prop1": "$MY_PROP"

reads $MY_PROP from system env vars? Right?

Yes.

@KannarFr
Copy link
Contributor

Awesome!

@sijie sijie merged commit 00463ad into apache:master Mar 26, 2020
@sijie sijie deleted the neng/add-custom-property-to-functions branch March 26, 2020 07:02
@Anonymitaet Anonymitaet removed the doc-required Your PR changes impact docs and you will update later. label Jun 29, 2020
@Anonymitaet
Copy link
Member

Docs have been added here.
Will copy to Pulsar later.

huangdx0726 pushed a commit to huangdx0726/pulsar that referenced this pull request Aug 24, 2020
…he#6348)

Fixes apache#6170

### Motivation
Allow users set custom system properties while submitting functions. This can be used to pass credentials via system property.

### Modifications

- pulsar-admin client tool to accept multiple `--custom-property` options while creating the function
- function's java runtime to append these provided properties.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support system env var as functions parameters
4 participants