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

refreshable file data source #86

Merged
merged 6 commits into from Mar 18, 2020

Conversation

louyuting
Copy link
Collaborator

@louyuting louyuting commented Mar 10, 2020

Describe what this PR does / why we need it

refreshable file data source implementation.

  1. enhance the datasource framework:
    a) Add return value error for PropertyConverter function to indicate whether occur error when converting.
    b)Add return value error for datasource. ReadSource function to indicate whether occur error when reading source.

  2. refreshable file datasource implementation

  3. add plugin module.
    a) This module is design to encapsulate converter(default json) and updater related logic.

Does this pull request fix one issue?

Resolves #84

Describe how you did it

Describe how to verify it

Unit test

Special notes for reviews

@louyuting louyuting linked an issue Mar 10, 2020 that may be closed by this pull request
@louyuting louyuting force-pushed the datasource-ext-freshable_file branch from 6ab8eda to 6e51178 Compare March 10, 2020 14:14
@louyuting louyuting marked this pull request as ready for review March 11, 2020 07:58
@louyuting louyuting requested a review from sczyh30 March 11, 2020 07:58
@louyuting louyuting added area/data-source Issues or PRs related to data-source extension to-review PRs to review and removed to-review PRs to review labels Mar 11, 2020
@louyuting louyuting force-pushed the datasource-ext-freshable_file branch 2 times, most recently from c6b1fc1 to 5a69066 Compare March 15, 2020 13:20
@louyuting louyuting added the to-review PRs to review label Mar 15, 2020
}

func (s *RefreshableFileDataSource) Close() error {
return nil
Copy link
Member

Choose a reason for hiding this comment

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

The Close op should be supported.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Make sense.
The semantic of Close is to close the datasource and stop listen on dynamic datasource.

watcher *fsnotify.Watcher
}

func FileDataSourceStarter(sourceFilePath string, handlers ...datasource.PropertyHandler) (*RefreshableFileDataSource, error) {
Copy link
Member

Choose a reason for hiding this comment

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

The name starter might be confusing?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I will rename to NewFileDataSource


func (s *RefreshableFileDataSource) ReadSource() ([]byte, error) {
f, err := os.Open(s.sourceFilePath)
defer f.Close()
Copy link
Member

Choose a reason for hiding this comment

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

This should be put after the error checking?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Right

defer f.Close()

if err != nil {
return nil, errors.Errorf("The rules file is not existed, err: %+v.", errors.WithStack(err))
Copy link
Member

Choose a reason for hiding this comment

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

This may not just indicate non-exist, but also other circumstances like permission denied.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Make sense, I will refine the statement.

return nil
}

func (s *RefreshableFileDataSource) doUpdate() {
Copy link
Member

Choose a reason for hiding this comment

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

How about doReadAndUpdate?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Make sense.

for _, h := range s.Handlers() {
err := h.Handle(src)
if err != nil {
logger.Errorf("RefreshableFileDataSource fail to publish rules, handle: %+v; err: %+v.", h, err)
Copy link
Member

Choose a reason for hiding this comment

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

(1) Here we could use the word "data" instead of rule. (2) Printing the handler here might not be helpful. Maybe we need to print out the type?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

(1) Right;
(2) Is type the datasource type?

s.doUpdate()
w, err := fsnotify.NewWatcher()
if err != nil {
return errors.Errorf("Fail to new a w of fsnotify, err: %+v", err)
Copy link
Member

Choose a reason for hiding this comment

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

watcher?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes, it's caused by rename, I will rename here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, it's caused by mis rename. I will fix here.


func (s *RefreshableFileDataSource) Initialize() error {
if !s.isInitialized.CompareAndSet(false, true) {
return errors.New("RefreshableFileDataSource had been Initialized.")
Copy link
Member

Choose a reason for hiding this comment

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

Maybe just returning "nil" is enough?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Okay

@louyuting louyuting force-pushed the datasource-ext-freshable_file branch from fee5480 to 407c196 Compare March 16, 2020 14:10
ext/datasource/refreshable_file/file_datasource.go Outdated Show resolved Hide resolved

func (s *RefreshableFileDataSource) Close() error {
s.closeChan <- struct{}{}
logger.Info("The RefreshableFileDataSource had been closed.")
Copy link
Member

Choose a reason for hiding this comment

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

Also print the filePath here to make it clear to identify?

@louyuting louyuting force-pushed the datasource-ext-freshable_file branch from 407c196 to a853dac Compare March 17, 2020 09:05
@sczyh30
Copy link
Member

sczyh30 commented Mar 17, 2020

Maybe we could also add the case for file-deleted event?

@louyuting louyuting requested a review from sczyh30 March 18, 2020 12:28
Copy link
Member

@sczyh30 sczyh30 left a comment

Choose a reason for hiding this comment

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

LGTM

@sczyh30 sczyh30 merged commit f25ccc1 into alibaba:master Mar 18, 2020
@sczyh30
Copy link
Member

sczyh30 commented Mar 18, 2020

Nice work. Thanks!

@sczyh30 sczyh30 removed the to-review PRs to review label Mar 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/data-source Issues or PRs related to data-source extension
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FileRefreshable DataSource implementation
2 participants