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

Support file sort in puller #477

Merged
merged 7 commits into from
Apr 24, 2020
Merged

Support file sort in puller #477

merged 7 commits into from
Apr 24, 2020

Conversation

amyangfei
Copy link
Contributor

@amyangfei amyangfei commented Apr 20, 2020

What problem does this PR solve?

This is the first part of pingcap/ticdc#466, add a file sort mechanism, a changefeed can use either memory sorter or file sorter

What is changed and how it works?

  • add two kinds of sort engine, memory and file
  • define a new interface EventSorter in puller, so we don't need to change original replication model.
  • file sorter has an input cache channel, it reads from input channel and flushes events to file until they are all mounted.
  • implement a simple double cache, one is used to sort with files, the other one is used to cache unsorted data to file.

Check List

Tests

  • Unit test
  • Integration test

@amyangfei
Copy link
Contributor Author

/run-integration-tests

@codecov-io
Copy link

Codecov Report

Merging #477 into master will decrease coverage by 0.6956%.
The diff coverage is 15.8778%.

@@               Coverage Diff                @@
##             master       #477        +/-   ##
================================================
- Coverage   29.0322%   28.3365%   -0.6957%     
================================================
  Files            58         59         +1     
  Lines          5673       6144       +471     
================================================
+ Hits           1647       1741        +94     
- Misses         3913       4287       +374     
- Partials        113        116         +3     

Ts uint64 `json:"t"`
RawKV *RawKVEntry `json:"-"`
Row *RowChangedEvent `json:"r"`
Finished chan struct{} `json:"-"`
Copy link
Contributor

Choose a reason for hiding this comment

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

why make Finished public? and why not store RowChangedEvent to the disk directly

Copy link
Contributor

Choose a reason for hiding this comment

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

by the way, i think json is not a suited serialization algorithm, it's advantages is readability but we don't need it here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch, Finished is set to public by mistake. And use gob encode to store PolymorphicEvent to file

@amyangfei
Copy link
Contributor Author

/run-integration-tests


func (fs *FileSorter) sortAndOutput(ctx context.Context) error {
bufferLen := 10
buffer := make([]*model.PolymorphicEvent, 0, bufferLen)
Copy link
Contributor

Choose a reason for hiding this comment

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

using bytes.Buffer to avoid frequent memory allocation

return 0, errors.Trace(err)
}
dataBuf.Reset()
err = gob.NewEncoder(dataBuf).Encode(entry)
Copy link
Contributor

Choose a reason for hiding this comment

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

how about create a new encoder with file buffer directly?

	f, err := os.OpenFile(fullpath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
	if err != nil {
		return 0, errors.Trace(err)
	}
	w := bufio.NewWriter(f)
        encoder:= gob.NewEncoder(w)

	for _, entry := range entries {
		encoder.Encode(entry)
	}


// clear and reset unsorted files, set cache sorting flag to prevent repeated sort
fs.cache.fileLock.Lock()
if atomic.LoadInt32(&fs.cache.sorting) == 1 {
Copy link
Contributor

Choose a reason for hiding this comment

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

it's needless that check the sorting is in process under lock protect
we can use CompareAndSwap to avoid using lock

return "", errors.New("unsorted file unexpected truncated")
}
ev := &model.PolymorphicEvent{}
err = gob.NewDecoder(bytes.NewReader(data[idx+8 : idx+8+dataLen])).Decode(ev)
Copy link
Contributor

Choose a reason for hiding this comment

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

can we avoid to create decode and reader continually?

Copy link
Contributor

@zier-one zier-one left a comment

Choose a reason for hiding this comment

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

LGTM

@zier-one zier-one added the LGT1 label Apr 22, 2020
@amyangfei
Copy link
Contributor Author

/run-all-tests

@amyangfei amyangfei merged commit 6e2c8e4 into pingcap:master Apr 24, 2020
@amyangfei amyangfei deleted the file-sort branch April 24, 2020 04:54
5kbpers pushed a commit to 5kbpers/ticdc that referenced this pull request Aug 24, 2020
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