github.com/m3db/m3@v1.5.0/src/m3ninx/doc/types.go (about)

     1  // Copyright (c) 2017 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 doc
    22  
    23  import (
    24  	"github.com/m3db/m3/src/x/resource"
    25  	xtime "github.com/m3db/m3/src/x/time"
    26  )
    27  
    28  // MetadataIterator provides an iterator over a collection of document metadata. It is NOT
    29  // safe for multiple goroutines to invoke methods on an MetadataIterator simultaneously.
    30  type MetadataIterator interface {
    31  	// Next returns a bool indicating if the iterator has any more metadata
    32  	// to return.
    33  	Next() bool
    34  
    35  	// Current returns the current metadata. It is only safe to call Current immediately
    36  	// after a call to Next confirms there are more elements remaining. The Metadata
    37  	// returned from Current is only valid until the following call to Next(). Callers
    38  	// should copy the Metadata if they need it live longer.
    39  	Current() Metadata
    40  
    41  	// Err returns any errors encountered during iteration.
    42  	Err() error
    43  
    44  	// Close releases any internal resources used by the iterator.
    45  	Close() error
    46  }
    47  
    48  // Iterator provides an iterator over a collection of documents. It is NOT
    49  // safe for multiple goroutines to invoke methods on an Iterator simultaneously.
    50  type Iterator interface {
    51  
    52  	// Next returns a bool indicating if the iterator has any more documents
    53  	// to return.
    54  	Next() bool
    55  
    56  	// Current returns the current document. It is only safe to call Current immediately
    57  	// after a call to Next confirms there are more elements remaining. The Document
    58  	// returned from Current is only valid until the following call to Next(). Callers
    59  	// should copy the Document if they need it live longer.
    60  	Current() Document
    61  
    62  	// Err returns any errors encountered during iteration.
    63  	Err() error
    64  
    65  	// Close releases any internal resources used by the iterator.
    66  	Close() error
    67  }
    68  
    69  // QueryDocIterator is an Iterator for all documents returned for a query. See Iterator for more details.
    70  type QueryDocIterator interface {
    71  	Iterator
    72  
    73  	// Done returns true if iterator is done and Next will return false on the next call. On the first call this will
    74  	// always return false and Next may still return false for an empty iterator. Callers still need to check for an
    75  	// Err after Done returns true.
    76  	// This is used by the index query path to check if there are more docs to process before waiting for an index
    77  	// worker.
    78  	Done() bool
    79  }
    80  
    81  // OnIndexSeries provides a set of callback hooks to allow the reverse index
    82  // to do lifecycle management of any resources retained during indexing.
    83  type OnIndexSeries interface {
    84  	// StringID returns the index series ID, as a string.
    85  	StringID() string
    86  
    87  	// OnIndexSuccess is executed when an entry is successfully indexed. The
    88  	// provided value for `blockStart` is the blockStart for which the write
    89  	// was indexed.
    90  	OnIndexSuccess(blockStart xtime.UnixNano)
    91  
    92  	// OnIndexFinalize is executed when the index no longer holds any references
    93  	// to the provided resources. It can be used to cleanup any resources held
    94  	// during the course of indexing. `blockStart` is the startTime of the index
    95  	// block for which the write was attempted.
    96  	OnIndexFinalize(blockStart xtime.UnixNano)
    97  
    98  	// OnIndexPrepare prepares the Entry to be handed off to the indexing sub-system.
    99  	// NB(prateek): we retain the ref count on the entry while the indexing is pending,
   100  	// the callback executed on the entry once the indexing is completed releases this
   101  	// reference.
   102  	OnIndexPrepare(blockStart xtime.UnixNano)
   103  
   104  	// NeedsIndexUpdate returns a bool to indicate if the Entry needs to be indexed
   105  	// for the provided blockStart. It only allows a single index attempt at a time
   106  	// for a single entry.
   107  	// NB(prateek): NeedsIndexUpdate is a CAS, i.e. when this method returns true, it
   108  	// also sets state on the entry to indicate that a write for the given blockStart
   109  	// is going to be sent to the index, and other go routines should not attempt the
   110  	// same write. Callers are expected to ensure they follow this guideline.
   111  	// Further, every call to NeedsIndexUpdate which returns true needs to have a corresponding
   112  	// OnIndexFinalze() call. This is required for correct lifecycle maintenance.
   113  	NeedsIndexUpdate(indexBlockStartForWrite xtime.UnixNano) bool
   114  
   115  	// IfAlreadyIndexedMarkIndexSuccessAndFinalize checks if the blockStart has been indexed.
   116  	// If indexed, it will be marked as such and finalized, and then return true. Otherwise false.
   117  	IfAlreadyIndexedMarkIndexSuccessAndFinalize(
   118  		blockStart xtime.UnixNano,
   119  	) bool
   120  
   121  	// TryMarkIndexGarbageCollected checks if the entry is eligible to be garbage collected
   122  	// from the index. If so, it marks the entry as GCed and returns true. Otherwise returns false.
   123  	TryMarkIndexGarbageCollected() bool
   124  
   125  	// NeedsIndexGarbageCollected returns if the entry is eligible to be garbage collected
   126  	// from the index.
   127  	NeedsIndexGarbageCollected() bool
   128  
   129  	// IndexedForBlockStart returns true if the blockStart has been indexed.
   130  	IndexedForBlockStart(blockStart xtime.UnixNano) bool
   131  
   132  	// IndexedRange returns minimum and maximum blockStart values covered by index entry.
   133  	// The range is inclusive. Note that there may be uncovered gaps within the range.
   134  	// Returns (0, 0) for an empty range.
   135  	IndexedRange() (xtime.UnixNano, xtime.UnixNano)
   136  
   137  	// ReconciledOnIndexSeries attempts to retrieve the most recent index entry from the
   138  	// shard if the entry this method was called on was never inserted there. If there
   139  	// is an error during retrieval, simply returns the current entry. Additionally,
   140  	// returns a cleanup function to run once finished using the reconciled entry and
   141  	// a boolean value indicating whether the result came from reconciliation or not.
   142  	// Cleanup function must be called once done with the reconciled entry so that
   143  	// reader and writer counts are accurately updated.
   144  	ReconciledOnIndexSeries() (OnIndexSeries, resource.SimpleCloser, bool)
   145  
   146  	// MergeEntryIndexBlockStates merges the given states into the current
   147  	// indexed entry.
   148  	MergeEntryIndexBlockStates(states EntryIndexBlockStates)
   149  
   150  	// TryReconcileDuplicates attempts to reconcile the index states of this entry.
   151  	TryReconcileDuplicates()
   152  }
   153  
   154  // EntryIndexBlockStates captures the indexing for a single shard entry, across
   155  // all block starts.
   156  type EntryIndexBlockStates map[xtime.UnixNano]EntryIndexBlockState
   157  
   158  // EntryIndexBlockState is used to capture the state of indexing for a single shard
   159  // entry for a given index block start. It's used to prevent attempts at double indexing
   160  // for the same block start.
   161  type EntryIndexBlockState struct {
   162  	// Attempt indicates that indexing has been attempted.
   163  	Attempt bool
   164  	// Success indicates that indexing has succeeded.
   165  	Success bool
   166  }