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

A simpler implement for snapshot custom retriever #27917

Closed
Tracked by #26952
lcwangchao opened this issue Sep 9, 2021 · 0 comments · Fixed by #27936
Closed
Tracked by #26952

A simpler implement for snapshot custom retriever #27917

lcwangchao opened this issue Sep 9, 2021 · 0 comments · Fixed by #27936
Assignees

Comments

@lcwangchao
Copy link
Collaborator

lcwangchao commented Sep 9, 2021

Considering comments in #27854 , we want some simpler implement for custom data retrieving in snapshot. The new implement have several considerations:

  • Make a simple code in snapshot to avoid potential bugs.
  • If possible, code in snapshot should be abstract enough for future extension.
  • We can suppose snapshot data is empty for custom retriever's ranges to reduce design complexity (avioding oneByOneIter)

A preliminary proposal:

Firstly introduce a new interface txn.SnapshotInterceptor to snapshot. The definition is:

type SnapshotInterceptor interface {
    OnGet(ctx context.Context, snapshot kv.Snapshot, key kv.Key) ([]byte, error)
    OnBatchGet(ctx context.Context, snapshot kv.Snapshot, keys []kv.Key) (map[string][]byte, error)
    OnIter(ctx context.Context, snapshot kv.Snapshot, k []kv.Key, upper []kv.Key) (kv.Iterator, error)
    OnIterReverse(ctx context.Context, snapshot kv.Snapshot, k []kv.Key) (kv.Iterator, error)
}

We can set a interceptor to snapshot like this:

snapshot.SetOption(kv.SnapshotInterceptor, interceptor)

Take Get as example,the inner tikvSnapshot code is:

func (s *tikvSnapshot) Get(ctx context.Context, k kv.Key) ([]byte, error) {
        if s.interceptor != nil {
            return s.interceptor.OnGet(ctx, NewSnapshot(s.KVSnapshot), k)
        }

	data, err := s.KVSnapshot.Get(ctx, k)
	return data, extractKeyErr(err)
}

It means that if an interceptor is set, snapshot will delegate its read to the interceptor passing a kv.Snapshot parameter to it. Then the interceptor can do any thing to read the data.

For temporary table case, we can create a interceptor called TemporaryTableSnapshotInteceptor and its OnGet looks like this:

func (i *TemporaryTableSnapshotInteceptor) OnGet(ctx context.Context, snapshot kv.Snapshot, key kv.Key) ([]byte, error) {
    if tblInfo, ok := i.getAccessTableInfoFromKey(key); ok {
        if tblInfo.TempTableType == model.TempTableGlobal {
            return nil, kv.ErrNotExist
        }

        if tblInfo.TempTableType == model.TempTableLocal {
            val, err := i.memBuffer.Get(key)
            if err == nil && len(val) == 0 {
                return nil, kv.ErrNotExist
            }
            return val, err
        }
    }

    // fallback to snapshot read if the key is not a temporary table key
    return snapshot.Get(ctx, key)
}

It checks if it is accessing a temporary table's key. If it is, get data from memory otherwise fallback to default snapshot

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 a pull request may close this issue.

1 participant