2018-01-05 19:32:47 +01:00
|
|
|
// Copyright (c) 2018 Couchbase, Inc.
|
2017-12-01 21:42:50 +01:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-09-29 18:42:37 +02:00
|
|
|
package scorch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2017-12-07 00:33:47 +01:00
|
|
|
"fmt"
|
2018-02-28 20:36:35 +01:00
|
|
|
"io/ioutil"
|
2017-12-07 00:33:47 +01:00
|
|
|
"os"
|
2017-09-29 18:42:37 +02:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/RoaringBitmap/roaring"
|
|
|
|
"github.com/blevesearch/bleve/analysis"
|
|
|
|
"github.com/blevesearch/bleve/document"
|
|
|
|
"github.com/blevesearch/bleve/index"
|
|
|
|
"github.com/blevesearch/bleve/index/scorch/segment"
|
2018-01-18 03:46:57 +01:00
|
|
|
"github.com/blevesearch/bleve/index/scorch/segment/zap"
|
2017-09-29 18:42:37 +02:00
|
|
|
"github.com/blevesearch/bleve/index/store"
|
|
|
|
"github.com/blevesearch/bleve/registry"
|
2017-12-07 00:33:47 +01:00
|
|
|
"github.com/boltdb/bolt"
|
2017-09-29 18:42:37 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const Name = "scorch"
|
|
|
|
|
|
|
|
const Version uint8 = 1
|
|
|
|
|
2018-03-02 23:30:17 +01:00
|
|
|
var ErrClosed = fmt.Errorf("scorch closed")
|
|
|
|
|
2017-09-29 18:42:37 +02:00
|
|
|
type Scorch struct {
|
attempt to fix core reference counting issues
Observed problem:
Persisted index state (in root bolt) would contain index snapshots which
pointed to index files that did not exist.
Debugging this uncovered two main problems:
1. At the end of persisting a snapshot, the persister creates a new index
snapshot with the SAME epoch as the current root, only it replaces in-memory
segments with the new disk based ones. This is problematic because reference
counting an index segment triggers "eligible for deletion". And eligible for
deletion is keyed by epoch. So having two separate instances going by the same
epoch is problematic. Specifically, one of them gets to 0 before the other,
and we wrongly conclude it's eligible for deletion, when in fact the "other"
instance with same epoch is actually still in use.
To address this problem, we have modified the behavior of the persister. Now,
upon completion of persistence, ONLY if new files were actually created do we
proceed to introduce a new snapshot. AND, this new snapshot now gets it's own
brand new epoch. BOTH of these are important because since the persister now
also introduces a new epoch, it will see this epoch again in the future AND be
expected to persist it. That is OK (mostly harmless), but we cannot allow it
to form a loop. Checking that new files were actually introduced is what
short-circuits the potential loop. The new epoch introduced by the persister,
if seen again will not have any new segments that actually need persisting to
disk, and the cycle is stopped.
2. The implementation of NumSnapshotsToKeep, and related code to deleted old
snapshots from the root bolt also contains problems. Specifically, the
determination of which snapshots to keep vs delete did not consider which ones
were actually persisted. So, lets say you had set NumSnapshotsToKeep to 3, if
the introducer gets 3 snapshots ahead of the persister, what can happen is that
the three snapshots we choose to keep are all in memory. We now wrongly delete
all of the snapshots from the root bolt. But it gets worse, in this instant of
time, we now have files on disk that nothing in the root bolt points to, so we
also go ahead and delete those files. Those files were still being referenced
by the in-memory snapshots. But, now even if they get persisted to disk, they
simply have references to non-existent files. Opening up one of these indexes
results in lost data (often everything).
To address this problem, we made large change to the way this section of code
operates. First, we now start with a list of all epochs actually persisted in
the root bolt. Second, we set aside NumSnapshotsToKeep of these snapshots to
keep. Third, anything else in the eligibleForRemoval list will be deleted. I
suspect this code is slower and less elegant, but I think it is more correct.
Also, previously NumSnapshotsToKeep defaulted to 0, I have now defaulted it to
1, which feels like saner out-of-the-box behavior (though it's debatable if the
original intent was perhaps instead for "extra" snapshots to keep, but with the
variable named as it is, 1 makes more sense to me)
Other minor changes included in this change:
- Location of 'nextSnapshotEpoch', 'eligibleForRemoval', and
'ineligibleForRemoval' members of Scorch struct were moved into the
paragraph with 'rootLock' to clarify that you must hold the lock to access it.
- TestBatchRaceBug260 was updated to properly Close() the index, which leads to
occasional test failures.
2018-01-02 22:09:55 +01:00
|
|
|
readOnly bool
|
|
|
|
version uint8
|
|
|
|
config map[string]interface{}
|
|
|
|
analysisQueue *index.AnalysisQueue
|
2018-02-28 20:36:35 +01:00
|
|
|
stats Stats
|
attempt to fix core reference counting issues
Observed problem:
Persisted index state (in root bolt) would contain index snapshots which
pointed to index files that did not exist.
Debugging this uncovered two main problems:
1. At the end of persisting a snapshot, the persister creates a new index
snapshot with the SAME epoch as the current root, only it replaces in-memory
segments with the new disk based ones. This is problematic because reference
counting an index segment triggers "eligible for deletion". And eligible for
deletion is keyed by epoch. So having two separate instances going by the same
epoch is problematic. Specifically, one of them gets to 0 before the other,
and we wrongly conclude it's eligible for deletion, when in fact the "other"
instance with same epoch is actually still in use.
To address this problem, we have modified the behavior of the persister. Now,
upon completion of persistence, ONLY if new files were actually created do we
proceed to introduce a new snapshot. AND, this new snapshot now gets it's own
brand new epoch. BOTH of these are important because since the persister now
also introduces a new epoch, it will see this epoch again in the future AND be
expected to persist it. That is OK (mostly harmless), but we cannot allow it
to form a loop. Checking that new files were actually introduced is what
short-circuits the potential loop. The new epoch introduced by the persister,
if seen again will not have any new segments that actually need persisting to
disk, and the cycle is stopped.
2. The implementation of NumSnapshotsToKeep, and related code to deleted old
snapshots from the root bolt also contains problems. Specifically, the
determination of which snapshots to keep vs delete did not consider which ones
were actually persisted. So, lets say you had set NumSnapshotsToKeep to 3, if
the introducer gets 3 snapshots ahead of the persister, what can happen is that
the three snapshots we choose to keep are all in memory. We now wrongly delete
all of the snapshots from the root bolt. But it gets worse, in this instant of
time, we now have files on disk that nothing in the root bolt points to, so we
also go ahead and delete those files. Those files were still being referenced
by the in-memory snapshots. But, now even if they get persisted to disk, they
simply have references to non-existent files. Opening up one of these indexes
results in lost data (often everything).
To address this problem, we made large change to the way this section of code
operates. First, we now start with a list of all epochs actually persisted in
the root bolt. Second, we set aside NumSnapshotsToKeep of these snapshots to
keep. Third, anything else in the eligibleForRemoval list will be deleted. I
suspect this code is slower and less elegant, but I think it is more correct.
Also, previously NumSnapshotsToKeep defaulted to 0, I have now defaulted it to
1, which feels like saner out-of-the-box behavior (though it's debatable if the
original intent was perhaps instead for "extra" snapshots to keep, but with the
variable named as it is, 1 makes more sense to me)
Other minor changes included in this change:
- Location of 'nextSnapshotEpoch', 'eligibleForRemoval', and
'ineligibleForRemoval' members of Scorch struct were moved into the
paragraph with 'rootLock' to clarify that you must hold the lock to access it.
- TestBatchRaceBug260 was updated to properly Close() the index, which leads to
occasional test failures.
2018-01-02 22:09:55 +01:00
|
|
|
nextSegmentID uint64
|
|
|
|
path string
|
2017-12-07 00:33:47 +01:00
|
|
|
|
|
|
|
unsafeBatch bool
|
2017-09-29 18:42:37 +02:00
|
|
|
|
attempt to fix core reference counting issues
Observed problem:
Persisted index state (in root bolt) would contain index snapshots which
pointed to index files that did not exist.
Debugging this uncovered two main problems:
1. At the end of persisting a snapshot, the persister creates a new index
snapshot with the SAME epoch as the current root, only it replaces in-memory
segments with the new disk based ones. This is problematic because reference
counting an index segment triggers "eligible for deletion". And eligible for
deletion is keyed by epoch. So having two separate instances going by the same
epoch is problematic. Specifically, one of them gets to 0 before the other,
and we wrongly conclude it's eligible for deletion, when in fact the "other"
instance with same epoch is actually still in use.
To address this problem, we have modified the behavior of the persister. Now,
upon completion of persistence, ONLY if new files were actually created do we
proceed to introduce a new snapshot. AND, this new snapshot now gets it's own
brand new epoch. BOTH of these are important because since the persister now
also introduces a new epoch, it will see this epoch again in the future AND be
expected to persist it. That is OK (mostly harmless), but we cannot allow it
to form a loop. Checking that new files were actually introduced is what
short-circuits the potential loop. The new epoch introduced by the persister,
if seen again will not have any new segments that actually need persisting to
disk, and the cycle is stopped.
2. The implementation of NumSnapshotsToKeep, and related code to deleted old
snapshots from the root bolt also contains problems. Specifically, the
determination of which snapshots to keep vs delete did not consider which ones
were actually persisted. So, lets say you had set NumSnapshotsToKeep to 3, if
the introducer gets 3 snapshots ahead of the persister, what can happen is that
the three snapshots we choose to keep are all in memory. We now wrongly delete
all of the snapshots from the root bolt. But it gets worse, in this instant of
time, we now have files on disk that nothing in the root bolt points to, so we
also go ahead and delete those files. Those files were still being referenced
by the in-memory snapshots. But, now even if they get persisted to disk, they
simply have references to non-existent files. Opening up one of these indexes
results in lost data (often everything).
To address this problem, we made large change to the way this section of code
operates. First, we now start with a list of all epochs actually persisted in
the root bolt. Second, we set aside NumSnapshotsToKeep of these snapshots to
keep. Third, anything else in the eligibleForRemoval list will be deleted. I
suspect this code is slower and less elegant, but I think it is more correct.
Also, previously NumSnapshotsToKeep defaulted to 0, I have now defaulted it to
1, which feels like saner out-of-the-box behavior (though it's debatable if the
original intent was perhaps instead for "extra" snapshots to keep, but with the
variable named as it is, 1 makes more sense to me)
Other minor changes included in this change:
- Location of 'nextSnapshotEpoch', 'eligibleForRemoval', and
'ineligibleForRemoval' members of Scorch struct were moved into the
paragraph with 'rootLock' to clarify that you must hold the lock to access it.
- TestBatchRaceBug260 was updated to properly Close() the index, which leads to
occasional test failures.
2018-01-02 22:09:55 +01:00
|
|
|
rootLock sync.RWMutex
|
|
|
|
root *IndexSnapshot // holds 1 ref-count on the root
|
|
|
|
rootPersisted []chan error // closed when root is persisted
|
|
|
|
nextSnapshotEpoch uint64
|
|
|
|
eligibleForRemoval []uint64 // Index snapshot epochs that are safe to GC.
|
|
|
|
ineligibleForRemoval map[string]bool // Filenames that should not be GC'ed yet.
|
2018-03-07 08:42:06 +01:00
|
|
|
numSnapshotsToKeep int
|
2017-09-29 18:42:37 +02:00
|
|
|
|
2017-12-07 00:33:47 +01:00
|
|
|
closeCh chan struct{}
|
|
|
|
introductions chan *segmentIntroduction
|
2018-03-02 23:30:17 +01:00
|
|
|
persists chan *persistIntroduction
|
2017-12-13 19:41:03 +01:00
|
|
|
merges chan *segmentMerge
|
2017-12-17 17:23:00 +01:00
|
|
|
introducerNotifier chan *epochWatcher
|
2017-12-15 23:49:33 +01:00
|
|
|
revertToSnapshots chan *snapshotReversion
|
2018-02-05 12:07:43 +01:00
|
|
|
persisterNotifier chan *epochWatcher
|
2017-12-07 00:33:47 +01:00
|
|
|
rootBolt *bolt.DB
|
|
|
|
asyncTasks sync.WaitGroup
|
2017-12-13 23:54:58 +01:00
|
|
|
|
2018-01-05 21:29:01 +01:00
|
|
|
onEvent func(event Event)
|
|
|
|
onAsyncError func(err error)
|
2017-12-27 03:11:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewScorch(storeName string,
|
|
|
|
config map[string]interface{},
|
|
|
|
analysisQueue *index.AnalysisQueue) (index.Index, error) {
|
2017-09-29 18:42:37 +02:00
|
|
|
rv := &Scorch{
|
2017-12-14 19:49:33 +01:00
|
|
|
version: Version,
|
|
|
|
config: config,
|
|
|
|
analysisQueue: analysisQueue,
|
|
|
|
nextSnapshotEpoch: 1,
|
|
|
|
closeCh: make(chan struct{}),
|
|
|
|
ineligibleForRemoval: map[string]bool{},
|
2017-09-29 18:42:37 +02:00
|
|
|
}
|
2017-12-13 23:54:58 +01:00
|
|
|
rv.root = &IndexSnapshot{parent: rv, refs: 1}
|
2017-12-11 15:07:01 +01:00
|
|
|
ro, ok := config["read_only"].(bool)
|
|
|
|
if ok {
|
|
|
|
rv.readOnly = ro
|
|
|
|
}
|
2017-12-20 19:06:50 +01:00
|
|
|
ub, ok := config["unsafe_batch"].(bool)
|
|
|
|
if ok {
|
|
|
|
rv.unsafeBatch = ub
|
2017-12-14 23:19:50 +01:00
|
|
|
}
|
2018-01-05 19:32:47 +01:00
|
|
|
ecbName, ok := config["eventCallbackName"].(string)
|
|
|
|
if ok {
|
|
|
|
rv.onEvent = RegistryEventCallbacks[ecbName]
|
|
|
|
}
|
2018-01-05 21:29:01 +01:00
|
|
|
aecbName, ok := config["asyncErrorCallbackName"].(string)
|
|
|
|
if ok {
|
|
|
|
rv.onAsyncError = RegistryAsyncErrorCallbacks[aecbName]
|
|
|
|
}
|
2017-09-29 18:42:37 +02:00
|
|
|
return rv, nil
|
|
|
|
}
|
|
|
|
|
2017-12-27 03:11:14 +01:00
|
|
|
func (s *Scorch) fireEvent(kind EventKind, dur time.Duration) {
|
|
|
|
if s.onEvent != nil {
|
|
|
|
s.onEvent(Event{Kind: kind, Scorch: s, Duration: dur})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-05 21:29:01 +01:00
|
|
|
func (s *Scorch) fireAsyncError(err error) {
|
|
|
|
if s.onAsyncError != nil {
|
|
|
|
s.onAsyncError(err)
|
|
|
|
}
|
2018-02-28 11:01:55 +01:00
|
|
|
atomic.AddUint64(&s.stats.TotOnErrors, 1)
|
2018-01-05 21:29:01 +01:00
|
|
|
}
|
|
|
|
|
2017-09-29 18:42:37 +02:00
|
|
|
func (s *Scorch) Open() error {
|
2018-02-05 21:12:12 +01:00
|
|
|
err := s.openBolt()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.asyncTasks.Add(1)
|
|
|
|
go s.mainLoop()
|
|
|
|
|
|
|
|
if !s.readOnly && s.path != "" {
|
|
|
|
s.asyncTasks.Add(1)
|
|
|
|
go s.persisterLoop()
|
|
|
|
s.asyncTasks.Add(1)
|
|
|
|
go s.mergerLoop()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scorch) openBolt() error {
|
2017-12-07 00:33:47 +01:00
|
|
|
var ok bool
|
|
|
|
s.path, ok = s.config["path"].(string)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("must specify path")
|
|
|
|
}
|
|
|
|
if s.path == "" {
|
2017-12-11 15:07:01 +01:00
|
|
|
s.unsafeBatch = true
|
2017-12-07 00:33:47 +01:00
|
|
|
}
|
|
|
|
|
2017-12-11 15:07:01 +01:00
|
|
|
var rootBoltOpt *bolt.Options
|
|
|
|
if s.readOnly {
|
|
|
|
rootBoltOpt = &bolt.Options{
|
|
|
|
ReadOnly: true,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if s.path != "" {
|
|
|
|
err := os.MkdirAll(s.path, 0700)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-12-07 00:33:47 +01:00
|
|
|
}
|
2018-02-05 21:12:12 +01:00
|
|
|
|
2017-12-07 00:33:47 +01:00
|
|
|
rootBoltPath := s.path + string(os.PathSeparator) + "root.bolt"
|
2017-12-11 15:07:01 +01:00
|
|
|
var err error
|
|
|
|
if s.path != "" {
|
|
|
|
s.rootBolt, err = bolt.Open(rootBoltPath, 0600, rootBoltOpt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-12-07 00:33:47 +01:00
|
|
|
|
2017-12-11 15:07:01 +01:00
|
|
|
// now see if there is any existing state to load
|
|
|
|
err = s.loadFromBolt()
|
|
|
|
if err != nil {
|
2017-12-14 19:49:33 +01:00
|
|
|
_ = s.Close()
|
2017-12-11 15:07:01 +01:00
|
|
|
return err
|
|
|
|
}
|
2017-12-07 00:33:47 +01:00
|
|
|
}
|
|
|
|
|
2017-09-29 18:42:37 +02:00
|
|
|
s.introductions = make(chan *segmentIntroduction)
|
2018-03-02 23:30:17 +01:00
|
|
|
s.persists = make(chan *persistIntroduction)
|
2017-12-13 19:41:03 +01:00
|
|
|
s.merges = make(chan *segmentMerge)
|
2017-12-17 17:23:00 +01:00
|
|
|
s.introducerNotifier = make(chan *epochWatcher, 1)
|
2017-12-15 23:49:33 +01:00
|
|
|
s.revertToSnapshots = make(chan *snapshotReversion)
|
2018-02-05 12:07:43 +01:00
|
|
|
s.persisterNotifier = make(chan *epochWatcher, 1)
|
2017-12-07 00:33:47 +01:00
|
|
|
|
2017-12-14 19:49:33 +01:00
|
|
|
if !s.readOnly && s.path != "" {
|
|
|
|
err := s.removeOldZapFiles() // Before persister or merger create any new files.
|
|
|
|
if err != nil {
|
|
|
|
_ = s.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-07 08:42:06 +01:00
|
|
|
s.numSnapshotsToKeep = NumSnapshotsToKeep
|
|
|
|
if v, ok := s.config["numSnapshotsToKeep"]; ok {
|
|
|
|
var t int
|
|
|
|
if t, err = parseToInteger(v); err != nil {
|
|
|
|
return fmt.Errorf("numSnapshotsToKeep parse err: %v", err)
|
|
|
|
}
|
|
|
|
if t > 0 {
|
|
|
|
s.numSnapshotsToKeep = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-29 18:42:37 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-07 00:33:47 +01:00
|
|
|
func (s *Scorch) Close() (err error) {
|
2017-12-27 03:11:14 +01:00
|
|
|
startTime := time.Now()
|
|
|
|
defer func() {
|
|
|
|
s.fireEvent(EventKindClose, time.Since(startTime))
|
|
|
|
}()
|
|
|
|
|
|
|
|
s.fireEvent(EventKindCloseStart, 0)
|
|
|
|
|
2017-12-07 00:33:47 +01:00
|
|
|
// signal to async tasks we want to close
|
2017-09-29 18:42:37 +02:00
|
|
|
close(s.closeCh)
|
2017-12-07 00:33:47 +01:00
|
|
|
// wait for them to close
|
|
|
|
s.asyncTasks.Wait()
|
|
|
|
// now close the root bolt
|
2017-12-11 15:07:01 +01:00
|
|
|
if s.rootBolt != nil {
|
|
|
|
err = s.rootBolt.Close()
|
|
|
|
s.rootLock.Lock()
|
2017-12-14 19:49:33 +01:00
|
|
|
if s.root != nil {
|
|
|
|
_ = s.root.DecRef()
|
|
|
|
}
|
2017-12-13 22:10:44 +01:00
|
|
|
s.root = nil
|
|
|
|
s.rootLock.Unlock()
|
2017-12-07 00:33:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2017-09-29 18:42:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scorch) Update(doc *document.Document) error {
|
|
|
|
b := index.NewBatch()
|
|
|
|
b.Update(doc)
|
|
|
|
return s.Batch(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scorch) Delete(id string) error {
|
|
|
|
b := index.NewBatch()
|
|
|
|
b.Delete(id)
|
|
|
|
return s.Batch(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Batch applices a batch of changes to the index atomically
|
2018-01-18 03:46:57 +01:00
|
|
|
func (s *Scorch) Batch(batch *index.Batch) (err error) {
|
2017-12-27 03:11:14 +01:00
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
s.fireEvent(EventKindBatchIntroduction, time.Since(start))
|
|
|
|
}()
|
2017-09-29 18:42:37 +02:00
|
|
|
|
|
|
|
resultChan := make(chan *index.AnalysisResult, len(batch.IndexOps))
|
|
|
|
|
|
|
|
var numUpdates uint64
|
2017-12-21 02:25:21 +01:00
|
|
|
var numDeletes uint64
|
2017-09-29 18:42:37 +02:00
|
|
|
var numPlainTextBytes uint64
|
|
|
|
var ids []string
|
|
|
|
for docID, doc := range batch.IndexOps {
|
|
|
|
if doc != nil {
|
|
|
|
// insert _id field
|
|
|
|
doc.AddField(document.NewTextFieldCustom("_id", nil, []byte(doc.ID), document.IndexField|document.StoreField, nil))
|
|
|
|
numUpdates++
|
|
|
|
numPlainTextBytes += doc.NumPlainTextBytes()
|
2017-12-21 02:25:21 +01:00
|
|
|
} else {
|
|
|
|
numDeletes++
|
2017-09-29 18:42:37 +02:00
|
|
|
}
|
|
|
|
ids = append(ids, docID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME could sort ids list concurrent with analysis?
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for _, doc := range batch.IndexOps {
|
|
|
|
if doc != nil {
|
|
|
|
aw := index.NewAnalysisWork(s, doc, resultChan)
|
|
|
|
// put the work on the queue
|
|
|
|
s.analysisQueue.Queue(aw)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// wait for analysis result
|
|
|
|
analysisResults := make([]*index.AnalysisResult, int(numUpdates))
|
|
|
|
var itemsDeQueued uint64
|
|
|
|
for itemsDeQueued < numUpdates {
|
|
|
|
result := <-resultChan
|
|
|
|
analysisResults[itemsDeQueued] = result
|
|
|
|
itemsDeQueued++
|
|
|
|
}
|
|
|
|
close(resultChan)
|
|
|
|
|
2018-02-28 11:01:55 +01:00
|
|
|
atomic.AddUint64(&s.stats.TotAnalysisTime, uint64(time.Since(start)))
|
2017-12-27 03:11:14 +01:00
|
|
|
|
2018-03-02 20:07:37 +01:00
|
|
|
indexStart := time.Now()
|
|
|
|
|
2017-12-27 03:11:14 +01:00
|
|
|
// notify handlers that we're about to introduce a segment
|
|
|
|
s.fireEvent(EventKindBatchIntroductionStart, 0)
|
2017-09-29 18:42:37 +02:00
|
|
|
|
|
|
|
var newSegment segment.Segment
|
|
|
|
if len(analysisResults) > 0 {
|
2018-03-09 09:16:28 +01:00
|
|
|
newSegment, err = zap.AnalysisResultsToSegmentBase(analysisResults, DefaultChunkFactor)
|
2018-01-18 03:46:57 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-02-28 11:01:55 +01:00
|
|
|
} else {
|
2018-02-28 20:36:35 +01:00
|
|
|
atomic.AddUint64(&s.stats.TotBatchesEmpty, 1)
|
2017-09-29 18:42:37 +02:00
|
|
|
}
|
2017-12-13 22:10:44 +01:00
|
|
|
|
2018-01-18 03:46:57 +01:00
|
|
|
err = s.prepareSegment(newSegment, ids, batch.InternalOps)
|
2017-12-21 02:25:21 +01:00
|
|
|
if err != nil {
|
|
|
|
if newSegment != nil {
|
|
|
|
_ = newSegment.Close()
|
|
|
|
}
|
2018-02-28 11:01:55 +01:00
|
|
|
atomic.AddUint64(&s.stats.TotOnErrors, 1)
|
2017-12-21 02:25:21 +01:00
|
|
|
} else {
|
2018-02-28 11:01:55 +01:00
|
|
|
atomic.AddUint64(&s.stats.TotUpdates, numUpdates)
|
|
|
|
atomic.AddUint64(&s.stats.TotDeletes, numDeletes)
|
|
|
|
atomic.AddUint64(&s.stats.TotBatches, 1)
|
|
|
|
atomic.AddUint64(&s.stats.TotIndexedPlainTextBytes, numPlainTextBytes)
|
2017-12-13 22:10:44 +01:00
|
|
|
}
|
2018-03-02 20:07:37 +01:00
|
|
|
|
|
|
|
atomic.AddUint64(&s.stats.TotIndexTime, uint64(time.Since(indexStart)))
|
|
|
|
|
2017-12-13 22:10:44 +01:00
|
|
|
return err
|
2017-09-29 18:42:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scorch) prepareSegment(newSegment segment.Segment, ids []string,
|
|
|
|
internalOps map[string][]byte) error {
|
|
|
|
|
|
|
|
// new introduction
|
|
|
|
introduction := &segmentIntroduction{
|
|
|
|
id: atomic.AddUint64(&s.nextSegmentID, 1),
|
|
|
|
data: newSegment,
|
|
|
|
ids: ids,
|
|
|
|
obsoletes: make(map[uint64]*roaring.Bitmap),
|
|
|
|
internal: internalOps,
|
2017-12-07 00:33:47 +01:00
|
|
|
applied: make(chan error),
|
|
|
|
}
|
|
|
|
|
|
|
|
if !s.unsafeBatch {
|
2017-12-17 17:23:00 +01:00
|
|
|
introduction.persisted = make(chan error, 1)
|
2017-09-29 18:42:37 +02:00
|
|
|
}
|
|
|
|
|
2018-02-02 01:59:59 +01:00
|
|
|
// optimistically prepare obsoletes outside of rootLock
|
2017-09-29 18:42:37 +02:00
|
|
|
s.rootLock.RLock()
|
2018-02-02 01:59:59 +01:00
|
|
|
root := s.root
|
|
|
|
root.AddRef()
|
|
|
|
s.rootLock.RUnlock()
|
|
|
|
|
|
|
|
for _, seg := range root.segment {
|
2017-12-13 22:10:44 +01:00
|
|
|
delta, err := seg.segment.DocNumbers(ids)
|
2017-12-05 00:06:06 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-12-13 22:10:44 +01:00
|
|
|
introduction.obsoletes[seg.id] = delta
|
2017-09-29 18:42:37 +02:00
|
|
|
}
|
2018-02-02 01:59:59 +01:00
|
|
|
|
|
|
|
_ = root.DecRef()
|
2017-09-29 18:42:37 +02:00
|
|
|
|
2018-03-02 20:17:16 +01:00
|
|
|
introStartTime := time.Now()
|
|
|
|
|
2017-09-29 18:42:37 +02:00
|
|
|
s.introductions <- introduction
|
|
|
|
|
|
|
|
// block until this segment is applied
|
2017-12-07 00:33:47 +01:00
|
|
|
err := <-introduction.applied
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-09-29 18:42:37 +02:00
|
|
|
|
2017-12-17 06:21:43 +01:00
|
|
|
if introduction.persisted != nil {
|
2017-12-07 00:33:47 +01:00
|
|
|
err = <-introduction.persisted
|
|
|
|
}
|
|
|
|
|
2018-03-02 20:17:16 +01:00
|
|
|
introTime := uint64(time.Since(introStartTime))
|
|
|
|
atomic.AddUint64(&s.stats.TotBatchIntroTime, introTime)
|
|
|
|
if atomic.LoadUint64(&s.stats.MaxBatchIntroTime) < introTime {
|
2018-03-04 04:32:39 +01:00
|
|
|
atomic.StoreUint64(&s.stats.MaxBatchIntroTime, introTime)
|
2018-03-02 20:17:16 +01:00
|
|
|
}
|
|
|
|
|
2017-12-07 00:33:47 +01:00
|
|
|
return err
|
2017-09-29 18:42:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scorch) SetInternal(key, val []byte) error {
|
|
|
|
b := index.NewBatch()
|
|
|
|
b.SetInternal(key, val)
|
|
|
|
return s.Batch(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scorch) DeleteInternal(key []byte) error {
|
|
|
|
b := index.NewBatch()
|
|
|
|
b.DeleteInternal(key)
|
|
|
|
return s.Batch(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reader returns a low-level accessor on the index data. Close it to
|
|
|
|
// release associated resources.
|
|
|
|
func (s *Scorch) Reader() (index.IndexReader, error) {
|
2018-03-15 21:31:04 +01:00
|
|
|
return s.currentSnapshot(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scorch) currentSnapshot() *IndexSnapshot {
|
2017-09-29 18:42:37 +02:00
|
|
|
s.rootLock.RLock()
|
2018-03-02 23:03:54 +01:00
|
|
|
rv := s.root
|
|
|
|
rv.AddRef()
|
2017-12-13 22:10:44 +01:00
|
|
|
s.rootLock.RUnlock()
|
2018-03-15 21:31:04 +01:00
|
|
|
return rv
|
2017-09-29 18:42:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scorch) Stats() json.Marshaler {
|
2018-02-28 20:36:35 +01:00
|
|
|
return &s.stats
|
2017-09-29 18:42:37 +02:00
|
|
|
}
|
|
|
|
func (s *Scorch) StatsMap() map[string]interface{} {
|
2018-02-28 20:36:35 +01:00
|
|
|
m := s.stats.ToMap()
|
|
|
|
|
|
|
|
if s.path != "" {
|
|
|
|
finfos, err := ioutil.ReadDir(s.path)
|
|
|
|
if err == nil {
|
|
|
|
var numFilesOnDisk, numBytesUsedDisk uint64
|
|
|
|
for _, finfo := range finfos {
|
|
|
|
if !finfo.IsDir() {
|
|
|
|
numBytesUsedDisk += uint64(finfo.Size())
|
|
|
|
numFilesOnDisk++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-01 23:13:36 +01:00
|
|
|
m["CurOnDiskBytes"] = numBytesUsedDisk
|
|
|
|
m["CurOnDiskFiles"] = numFilesOnDisk
|
2018-02-28 20:36:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: consider one day removing these backwards compatible
|
|
|
|
// names for apps using the old names
|
|
|
|
m["updates"] = m["TotUpdates"]
|
|
|
|
m["deletes"] = m["TotDeletes"]
|
|
|
|
m["batches"] = m["TotBatches"]
|
|
|
|
m["errors"] = m["TotOnErrors"]
|
|
|
|
m["analysis_time"] = m["TotAnalysisTime"]
|
|
|
|
m["index_time"] = m["TotIndexTime"]
|
|
|
|
m["term_searchers_started"] = m["TotTermSearchersStarted"]
|
|
|
|
m["term_searchers_finished"] = m["TotTermSearchersFinished"]
|
|
|
|
m["num_plain_text_bytes_indexed"] = m["TotIndexedPlainTextBytes"]
|
|
|
|
m["num_items_introduced"] = m["TotIntroducedItems"]
|
|
|
|
m["num_items_persisted"] = m["TotPersistedItems"]
|
2018-03-01 23:13:36 +01:00
|
|
|
m["num_bytes_used_disk"] = m["CurOnDiskBytes"]
|
|
|
|
m["num_files_on_disk"] = m["CurOnDiskFiles"]
|
2018-03-07 10:07:33 +01:00
|
|
|
m["total_compaction_written_bytes"] = m["TotFileMergeWrittenBytes"]
|
2018-02-28 20:36:35 +01:00
|
|
|
|
2018-01-24 23:10:14 +01:00
|
|
|
return m
|
2017-09-29 18:42:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scorch) Analyze(d *document.Document) *index.AnalysisResult {
|
|
|
|
rv := &index.AnalysisResult{
|
|
|
|
Document: d,
|
|
|
|
Analyzed: make([]analysis.TokenFrequencies, len(d.Fields)+len(d.CompositeFields)),
|
|
|
|
Length: make([]int, len(d.Fields)+len(d.CompositeFields)),
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, field := range d.Fields {
|
|
|
|
if field.Options().IsIndexed() {
|
|
|
|
fieldLength, tokenFreqs := field.Analyze()
|
|
|
|
rv.Analyzed[i] = tokenFreqs
|
|
|
|
rv.Length[i] = fieldLength
|
|
|
|
|
|
|
|
if len(d.CompositeFields) > 0 {
|
|
|
|
// see if any of the composite fields need this
|
|
|
|
for _, compositeField := range d.CompositeFields {
|
|
|
|
compositeField.Compose(field.Name(), fieldLength, tokenFreqs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scorch) Advanced() (store.KVStore, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2017-12-13 23:54:58 +01:00
|
|
|
func (s *Scorch) AddEligibleForRemoval(epoch uint64) {
|
|
|
|
s.rootLock.Lock()
|
|
|
|
if s.root == nil || s.root.epoch != epoch {
|
|
|
|
s.eligibleForRemoval = append(s.eligibleForRemoval, epoch)
|
|
|
|
}
|
|
|
|
s.rootLock.Unlock()
|
|
|
|
}
|
|
|
|
|
2017-12-29 02:48:38 +01:00
|
|
|
func (s *Scorch) MemoryUsed() uint64 {
|
2018-03-15 21:31:04 +01:00
|
|
|
indexSnapshot := s.currentSnapshot()
|
|
|
|
defer func() {
|
|
|
|
_ = indexSnapshot.Close()
|
|
|
|
}()
|
2018-03-16 16:31:47 +01:00
|
|
|
return uint64(indexSnapshot.Size())
|
2017-12-29 02:48:38 +01:00
|
|
|
}
|
|
|
|
|
2017-12-14 19:49:33 +01:00
|
|
|
func (s *Scorch) markIneligibleForRemoval(filename string) {
|
|
|
|
s.rootLock.Lock()
|
|
|
|
s.ineligibleForRemoval[filename] = true
|
|
|
|
s.rootLock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scorch) unmarkIneligibleForRemoval(filename string) {
|
|
|
|
s.rootLock.Lock()
|
|
|
|
delete(s.ineligibleForRemoval, filename)
|
|
|
|
s.rootLock.Unlock()
|
|
|
|
}
|
|
|
|
|
2017-09-29 18:42:37 +02:00
|
|
|
func init() {
|
|
|
|
registry.RegisterIndexType(Name, NewScorch)
|
|
|
|
}
|
2018-03-07 08:42:06 +01:00
|
|
|
|
2018-03-09 06:49:39 +01:00
|
|
|
func parseToInteger(i interface{}) (int, error) {
|
|
|
|
switch v := i.(type) {
|
2018-03-07 08:42:06 +01:00
|
|
|
case float64:
|
2018-03-09 06:49:39 +01:00
|
|
|
return int(v), nil
|
2018-03-07 08:42:06 +01:00
|
|
|
case int:
|
2018-03-09 06:49:39 +01:00
|
|
|
return v, nil
|
|
|
|
|
2018-03-07 08:42:06 +01:00
|
|
|
default:
|
2018-03-09 06:49:39 +01:00
|
|
|
return 0, fmt.Errorf("expects int or float64 value")
|
2018-03-07 08:42:06 +01:00
|
|
|
}
|
|
|
|
}
|