github.com/pingcap/br@v5.3.0-alpha.0.20220125034240-ec59c7b6ce30+incompatible/pkg/lightning/backend/noop/noop.go (about)

     1  // Copyright 2020 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package noop
    15  
    16  import (
    17  	"context"
    18  	"time"
    19  
    20  	"github.com/google/uuid"
    21  	"github.com/pingcap/parser/model"
    22  	"github.com/pingcap/tidb/table"
    23  	"github.com/pingcap/tidb/types"
    24  
    25  	"github.com/pingcap/br/pkg/lightning/backend"
    26  	"github.com/pingcap/br/pkg/lightning/backend/kv"
    27  	"github.com/pingcap/br/pkg/lightning/log"
    28  	"github.com/pingcap/br/pkg/lightning/verification"
    29  )
    30  
    31  func NewNoopBackend() backend.Backend {
    32  	return backend.MakeBackend(noopBackend{})
    33  }
    34  
    35  type noopBackend struct{}
    36  
    37  type noopRows struct{}
    38  
    39  func (r noopRows) SplitIntoChunks(int) []kv.Rows {
    40  	return []kv.Rows{r}
    41  }
    42  
    43  // Clear returns a new collection with empty content. It may share the
    44  // capacity with the current instance. The typical usage is `x = x.Clear()`.
    45  func (r noopRows) Clear() kv.Rows {
    46  	return r
    47  }
    48  
    49  // Close the connection to the backend.
    50  func (b noopBackend) Close() {}
    51  
    52  // MakeEmptyRows creates an empty collection of encoded rows.
    53  func (b noopBackend) MakeEmptyRows() kv.Rows {
    54  	return noopRows{}
    55  }
    56  
    57  // RetryImportDelay returns the duration to sleep when retrying an import
    58  func (b noopBackend) RetryImportDelay() time.Duration {
    59  	return 0
    60  }
    61  
    62  // ShouldPostProcess returns whether KV-specific post-processing should be
    63  // performed for this backend. Post-processing includes checksum and analyze.
    64  func (b noopBackend) ShouldPostProcess() bool {
    65  	return false
    66  }
    67  
    68  // NewEncoder creates an encoder of a TiDB table.
    69  func (b noopBackend) NewEncoder(tbl table.Table, options *kv.SessionOptions) (kv.Encoder, error) {
    70  	return noopEncoder{}, nil
    71  }
    72  
    73  func (b noopBackend) OpenEngine(context.Context, *backend.EngineConfig, uuid.UUID) error {
    74  	return nil
    75  }
    76  
    77  func (b noopBackend) CloseEngine(ctx context.Context, cfg *backend.EngineConfig, engineUUID uuid.UUID) error {
    78  	return nil
    79  }
    80  
    81  func (b noopBackend) ImportEngine(ctx context.Context, engineUUID uuid.UUID) error {
    82  	return nil
    83  }
    84  
    85  func (b noopBackend) CleanupEngine(ctx context.Context, engineUUID uuid.UUID) error {
    86  	return nil
    87  }
    88  
    89  // CheckRequirements performs the check whether the backend satisfies the
    90  // version requirements
    91  func (b noopBackend) CheckRequirements(context.Context, *backend.CheckCtx) error {
    92  	return nil
    93  }
    94  
    95  // FetchRemoteTableModels obtains the models of all tables given the schema
    96  // name. The returned table info does not need to be precise if the encoder,
    97  // is not requiring them, but must at least fill in the following fields for
    98  // TablesFromMeta to succeed:
    99  //  - Name
   100  //  - State (must be model.StatePublic)
   101  //  - ID
   102  //  - Columns
   103  //     * Name
   104  //     * State (must be model.StatePublic)
   105  //     * Offset (must be 0, 1, 2, ...)
   106  //  - PKIsHandle (true = do not generate _tidb_rowid)
   107  func (b noopBackend) FetchRemoteTableModels(ctx context.Context, schemaName string) ([]*model.TableInfo, error) {
   108  	return nil, nil
   109  }
   110  
   111  // FlushEngine ensures all KV pairs written to an open engine has been
   112  // synchronized, such that kill-9'ing Lightning afterwards and resuming from
   113  // checkpoint can recover the exact same content.
   114  //
   115  // This method is only relevant for local backend, and is no-op for all
   116  // other backends.
   117  func (b noopBackend) FlushEngine(ctx context.Context, engineUUID uuid.UUID) error {
   118  	return nil
   119  }
   120  
   121  // FlushAllEngines performs FlushEngine on all opened engines. This is a
   122  // very expensive operation and should only be used in some rare situation
   123  // (e.g. preparing to resolve a disk quota violation).
   124  func (b noopBackend) FlushAllEngines(ctx context.Context) error {
   125  	return nil
   126  }
   127  
   128  // EngineFileSizes obtains the size occupied locally of all engines managed
   129  // by this backend. This method is used to compute disk quota.
   130  // It can return nil if the content are all stored remotely.
   131  func (b noopBackend) EngineFileSizes() []backend.EngineFileSize {
   132  	return nil
   133  }
   134  
   135  // ResetEngine clears all written KV pairs in this opened engine.
   136  func (b noopBackend) ResetEngine(ctx context.Context, engineUUID uuid.UUID) error {
   137  	return nil
   138  }
   139  
   140  // LocalWriter obtains a thread-local EngineWriter for writing rows into the given engine.
   141  func (b noopBackend) LocalWriter(context.Context, *backend.LocalWriterConfig, uuid.UUID) (backend.EngineWriter, error) {
   142  	return noopWriter{}, nil
   143  }
   144  
   145  func (b noopBackend) CollectLocalDuplicateRows(ctx context.Context, tbl table.Table) error {
   146  	panic("Unsupported Operation")
   147  }
   148  
   149  func (b noopBackend) CollectRemoteDuplicateRows(ctx context.Context, tbl table.Table) error {
   150  	panic("Unsupported Operation")
   151  }
   152  
   153  type noopEncoder struct{}
   154  
   155  // Close the encoder.
   156  func (e noopEncoder) Close() {}
   157  
   158  // Encode encodes a row of SQL values into a backend-friendly format.
   159  func (e noopEncoder) Encode(log.Logger, []types.Datum, int64, []int, int64) (kv.Row, error) {
   160  	return noopRow{}, nil
   161  }
   162  
   163  type noopRow struct{}
   164  
   165  func (r noopRow) Size() uint64 {
   166  	return 0
   167  }
   168  
   169  func (r noopRow) ClassifyAndAppend(*kv.Rows, *verification.KVChecksum, *kv.Rows, *verification.KVChecksum) {
   170  }
   171  
   172  type noopWriter struct{}
   173  
   174  func (w noopWriter) AppendRows(context.Context, string, []string, kv.Rows) error {
   175  	return nil
   176  }
   177  
   178  func (w noopWriter) IsSynced() bool {
   179  	return true
   180  }
   181  
   182  func (w noopWriter) Close(context.Context) (backend.ChunkFlushStatus, error) {
   183  	return nil, nil
   184  }