go.temporal.io/server@v1.23.0/common/persistence/visibility/store/elasticsearch/client/bulk_processor_v7.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Copyright (c) 2020 Uber Technologies, Inc.
     6  //
     7  // Permission is hereby granted, free of charge, to any person obtaining a copy
     8  // of this software and associated documentation files (the "Software"), to deal
     9  // in the Software without restriction, including without limitation the rights
    10  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11  // copies of the Software, and to permit persons to whom the Software is
    12  // furnished to do so, subject to the following conditions:
    13  //
    14  // The above copyright notice and this permission notice shall be included in
    15  // all copies or substantial portions of the Software.
    16  //
    17  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    20  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  // THE SOFTWARE.
    24  
    25  package client
    26  
    27  import (
    28  	"errors"
    29  	"time"
    30  
    31  	"github.com/olivere/elastic/v7"
    32  )
    33  
    34  type (
    35  	bulkProcessorImpl struct {
    36  		esBulkProcessor *elastic.BulkProcessor
    37  	}
    38  )
    39  
    40  func newBulkProcessor(esBulkProcessor *elastic.BulkProcessor) *bulkProcessorImpl {
    41  	if esBulkProcessor == nil {
    42  		return nil
    43  	}
    44  	return &bulkProcessorImpl{
    45  		esBulkProcessor: esBulkProcessor,
    46  	}
    47  }
    48  
    49  func (p *bulkProcessorImpl) Stop() error {
    50  	// Flush can block indefinitely if we can't reach ES. Call it with a timeout to avoid
    51  	// blocking server shutdown. Default fx app shutdown timeout is 15s, so use 5s.
    52  	errC := make(chan error)
    53  	go func() {
    54  		errF := p.esBulkProcessor.Flush()
    55  		errS := p.esBulkProcessor.Stop()
    56  		if errF != nil {
    57  			errC <- errF
    58  		} else {
    59  			errC <- errS
    60  		}
    61  	}()
    62  	timer := time.NewTimer(5 * time.Second)
    63  	defer timer.Stop()
    64  	select {
    65  	case err := <-errC:
    66  		return err
    67  	case <-timer.C:
    68  		return errors.New("esBulkProcessor Flush/Stop timed out")
    69  	}
    70  }
    71  
    72  func (p *bulkProcessorImpl) Add(request *BulkableRequest) {
    73  	switch request.RequestType {
    74  	case BulkableRequestTypeIndex:
    75  		bulkIndexRequest := elastic.NewBulkIndexRequest().
    76  			Index(request.Index).
    77  			Id(request.ID).
    78  			VersionType(versionTypeExternal).
    79  			Version(request.Version).
    80  			Doc(request.Doc)
    81  		p.esBulkProcessor.Add(bulkIndexRequest)
    82  	case BulkableRequestTypeDelete:
    83  		bulkDeleteRequest := elastic.NewBulkDeleteRequest().
    84  			Index(request.Index).
    85  			Id(request.ID).
    86  			VersionType(versionTypeExternal).
    87  			Version(request.Version)
    88  		p.esBulkProcessor.Add(bulkDeleteRequest)
    89  	}
    90  }