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

     1  // Copyright (c) 2019 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 fs
    22  
    23  import (
    24  	"fmt"
    25  
    26  	"github.com/m3db/m3/src/dbnode/digest"
    27  	"github.com/m3db/m3/src/x/mmap"
    28  )
    29  
    30  func validateAndMmap(
    31  	fdWithDigest digest.FdWithDigestReader,
    32  	expectedDigest uint32,
    33  	forceMmapMemory bool,
    34  	reporterOptions mmap.ReporterOptions,
    35  ) (mmap.Descriptor, error) {
    36  	if forceMmapMemory {
    37  		return validateAndMmapMemory(fdWithDigest, expectedDigest, reporterOptions)
    38  	}
    39  
    40  	return validateAndMmapFile(fdWithDigest, expectedDigest, reporterOptions)
    41  }
    42  
    43  func validateAndMmapMemory(
    44  	fdWithDigest digest.FdWithDigestReader,
    45  	expectedDigest uint32,
    46  	reporterOptions mmap.ReporterOptions,
    47  ) (mmap.Descriptor, error) {
    48  	fd := fdWithDigest.Fd()
    49  	stat, err := fd.Stat()
    50  	if err != nil {
    51  		return mmap.Descriptor{}, err
    52  	}
    53  	numBytes := stat.Size()
    54  
    55  	// Request an anonymous (non-file-backed) mmap region. Note that we're going
    56  	// to use the mmap'd region to store the read-only summaries data, but the mmap
    57  	// region itself needs to be writable so we can copy the bytes from disk
    58  	// into it.
    59  	mmapDescriptor, err := mmap.Bytes(numBytes, mmap.Options{Read: true, Write: true, ReporterOptions: reporterOptions})
    60  	if err != nil {
    61  		return mmap.Descriptor{}, err
    62  	}
    63  
    64  	// Validate the bytes on disk using the digest, and read them into
    65  	// the mmap'd region
    66  	_, err = fdWithDigest.ReadAllAndValidate(mmapDescriptor.Bytes, expectedDigest)
    67  	if err != nil {
    68  		mmap.Munmap(mmapDescriptor)
    69  		return mmap.Descriptor{}, err
    70  	}
    71  
    72  	return mmapDescriptor, nil
    73  }
    74  
    75  func validateAndMmapFile(
    76  	fdWithDigest digest.FdWithDigestReader,
    77  	expectedDigest uint32,
    78  	reporterOptions mmap.ReporterOptions,
    79  ) (mmap.Descriptor, error) {
    80  	fd := fdWithDigest.Fd()
    81  	mmapDescriptor, err := mmap.File(fd, mmap.Options{Read: true, Write: false, ReporterOptions: reporterOptions})
    82  	if err != nil {
    83  		return mmap.Descriptor{}, err
    84  	}
    85  
    86  	if calculatedDigest := digest.Checksum(mmapDescriptor.Bytes); calculatedDigest != expectedDigest {
    87  		mmap.Munmap(mmapDescriptor)
    88  		return mmap.Descriptor{}, fmt.Errorf("expected %s file digest was: %d, but got: %d",
    89  			fd.Name(), expectedDigest, calculatedDigest)
    90  	}
    91  
    92  	return mmapDescriptor, nil
    93  }