github.com/m3db/m3@v1.5.0/src/dbnode/persist/fs/migration/migration.go (about)

     1  // Copyright (c) 2020 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package migration
    22  
    23  import (
    24  	"github.com/m3db/m3/src/dbnode/namespace"
    25  	"github.com/m3db/m3/src/dbnode/persist"
    26  	"github.com/m3db/m3/src/dbnode/persist/fs"
    27  	xtime "github.com/m3db/m3/src/x/time"
    28  )
    29  
    30  // Task interface is implemented by tasks that wish to perform a data migration
    31  // on a fileset. This typically involves updating files in a fileset that were created by
    32  // a previous version of the database client.
    33  type Task interface {
    34  	// Run is the set of steps to successfully complete a migration. Returns the potentially
    35  	// updated ReadInfoFileResult or an error.
    36  	Run() (fs.ReadInfoFileResult, error)
    37  }
    38  
    39  // NewTaskFn is a function that can create a new migration task.
    40  type NewTaskFn func(opts TaskOptions) (Task, error)
    41  
    42  // toVersion1_1Task is an object responsible for migrating a fileset to version 1.1.
    43  type toVersion1_1Task struct {
    44  	opts TaskOptions
    45  }
    46  
    47  // MigrationTask returns true or false if a fileset should be migrated. If true, also returns
    48  // a function that can be used to create a new migration task.
    49  func MigrationTask(info fs.ReadInfoFileResult) (NewTaskFn, bool) {
    50  	if info.Info.MajorVersion == 1 && info.Info.MinorVersion == 0 {
    51  		return NewToVersion1_1Task, true
    52  	}
    53  	return nil, false
    54  }
    55  
    56  // NewToVersion1_1Task creates a task for migrating a fileset to version 1.1.
    57  func NewToVersion1_1Task(opts TaskOptions) (Task, error) {
    58  	if err := opts.Validate(); err != nil {
    59  		return nil, err
    60  	}
    61  	return &toVersion1_1Task{
    62  		opts: opts,
    63  	}, nil
    64  }
    65  
    66  // Run executes the steps to bring a fileset to Version 1.1.
    67  func (v *toVersion1_1Task) Run() (fs.ReadInfoFileResult, error) {
    68  	var (
    69  		sOpts          = v.opts.StorageOptions()
    70  		fsOpts         = v.opts.FilesystemOptions()
    71  		newMergerFn    = v.opts.NewMergerFn()
    72  		nsMd           = v.opts.NamespaceMetadata()
    73  		infoFileResult = v.opts.InfoFileResult()
    74  		shard          = v.opts.Shard()
    75  		persistManager = v.opts.PersistManager()
    76  	)
    77  	reader, err := fs.NewReader(sOpts.BytesPool(), fsOpts)
    78  	if err != nil {
    79  		return infoFileResult, err
    80  	}
    81  
    82  	merger := newMergerFn(reader, sOpts.DatabaseBlockOptions().DatabaseBlockAllocSize(),
    83  		sOpts.SegmentReaderPool(), sOpts.MultiReaderIteratorPool(),
    84  		sOpts.IdentifierPool(), sOpts.EncoderPool(), sOpts.ContextPool(),
    85  		fsOpts.FilePathPrefix(), nsMd.Options())
    86  
    87  	volIndex := infoFileResult.Info.VolumeIndex
    88  	fsID := fs.FileSetFileIdentifier{
    89  		Namespace:   nsMd.ID(),
    90  		Shard:       shard,
    91  		BlockStart:  xtime.UnixNano(infoFileResult.Info.BlockStart),
    92  		VolumeIndex: volIndex,
    93  	}
    94  
    95  	nsCtx := namespace.NewContextFrom(nsMd)
    96  
    97  	flushPersist, err := persistManager.StartFlushPersist()
    98  	if err != nil {
    99  		return infoFileResult, err
   100  	}
   101  
   102  	// Intentionally use a noop merger here as we simply want to rewrite the same files with the current encoder which
   103  	// will generate index files with the entry level checksums.
   104  	newIndex := volIndex + 1
   105  	if err = merger.MergeAndCleanup(fsID, fs.NewNoopMergeWith(), newIndex, flushPersist, nsCtx,
   106  		&persist.NoOpColdFlushNamespace{}, false); err != nil {
   107  		return infoFileResult, err
   108  	}
   109  
   110  	if err = flushPersist.DoneFlush(); err != nil {
   111  		return infoFileResult, err
   112  	}
   113  
   114  	infoFileResult.Info.VolumeIndex = newIndex
   115  	infoFileResult.Info.MinorVersion = 1
   116  
   117  	return infoFileResult, nil
   118  }