github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/common/elasticsearch/esVersionManager.go (about) 1 // Copyright 2015 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 package elasticsearch 15 16 import ( 17 "fmt" 18 "github.com/golang/glog" 19 "github.com/pborman/uuid" 20 "golang.org/x/net/context" 21 elastic2 "gopkg.in/olivere/elastic.v3" 22 elastic5 "gopkg.in/olivere/elastic.v5" 23 "time" 24 ) 25 26 type UnsupportedVersion struct{} 27 28 func (UnsupportedVersion) Error() string { 29 return "Unsupported ElasticSearch Client Version" 30 } 31 32 type esClient struct { 33 version int 34 clientV2 *elastic2.Client 35 clientV5 *elastic5.Client 36 bulkProcessorV2 *elastic2.BulkProcessor 37 bulkProcessorV5 *elastic5.BulkProcessor 38 pipeline string 39 } 40 41 func NewMockClient() *esClient { 42 return &esClient{} 43 } 44 func newEsClientV5(startupFns []elastic5.ClientOptionFunc, bulkWorkers int, pipeline string) (*esClient, error) { 45 client, err := elastic5.NewClient(startupFns...) 46 if err != nil { 47 return nil, fmt.Errorf("Failed to an ElasticSearch Client: %v", err) 48 } 49 bps, err := client.BulkProcessor(). 50 Name("ElasticSearchWorker"). 51 Workers(bulkWorkers). 52 After(bulkAfterCB). 53 BulkActions(1000). // commit if # requests >= 1000 54 BulkSize(2 << 20). // commit if size of requests >= 2 MB 55 FlushInterval(10 * time.Second). // commit every 10s 56 Do(context.Background()) 57 if err != nil { 58 return nil, fmt.Errorf("Failed to an ElasticSearch Bulk Processor: %v", err) 59 } 60 return &esClient{version: 5, clientV5: client, bulkProcessorV5: bps, pipeline: pipeline}, nil 61 } 62 func newEsClientV2(startupFns []elastic2.ClientOptionFunc, bulkWorkers int) (*esClient, error) { 63 client, err := elastic2.NewClient(startupFns...) 64 if err != nil { 65 return nil, fmt.Errorf("Failed to an ElasticSearch Client: %v", err) 66 } 67 bps, err := client.BulkProcessor(). 68 Name("ElasticSearchWorker"). 69 Workers(bulkWorkers). 70 After(bulkAfterCB_v2). 71 BulkActions(1000). // commit if # requests >= 1000 72 BulkSize(2 << 20). // commit if size of requests >= 2 MB 73 FlushInterval(10 * time.Second). // commit every 10s 74 Do() 75 if err != nil { 76 return nil, fmt.Errorf("Failed to an ElasticSearch Bulk Processor: %v", err) 77 } 78 return &esClient{version: 2, clientV2: client, bulkProcessorV2: bps}, nil 79 } 80 81 func (es *esClient) IndexExists(indices ...string) (bool, error) { 82 switch es.version { 83 case 2: 84 return es.clientV2.IndexExists(indices...).Do() 85 case 5: 86 return es.clientV5.IndexExists(indices...).Do(context.Background()) 87 default: 88 return false, UnsupportedVersion{} 89 } 90 } 91 92 func (es *esClient) CreateIndex(name string, mapping string) (interface{}, error) { 93 switch es.version { 94 case 2: 95 return es.clientV2.CreateIndex(name).BodyString(mapping).Do() 96 case 5: 97 return es.clientV5.CreateIndex(name).BodyString(mapping).Do(context.Background()) 98 default: 99 return nil, UnsupportedVersion{} 100 } 101 } 102 103 func (es *esClient) GetAliases(index string) (interface{}, error) { 104 switch es.version { 105 case 2: 106 return es.clientV2.Aliases().Index(index).Do() 107 case 5: 108 return es.clientV5.Aliases().Index(index).Do(context.Background()) 109 default: 110 return nil, UnsupportedVersion{} 111 } 112 } 113 114 func (es *esClient) AddAlias(index string, alias string) (interface{}, error) { 115 switch es.version { 116 case 2: 117 return es.clientV2.Alias().Add(index, alias).Do() 118 case 5: 119 return es.clientV5.Alias().Add(index, alias).Do(context.Background()) 120 default: 121 return nil, UnsupportedVersion{} 122 } 123 } 124 125 func (es *esClient) AddBulkReq(index, typeName string, data interface{}) error { 126 switch es.version { 127 case 2: 128 es.bulkProcessorV2.Add(elastic2.NewBulkIndexRequest(). 129 Index(index). 130 Type(typeName). 131 Id(uuid.NewUUID().String()). 132 Doc(data)) 133 return nil 134 case 5: 135 req := elastic5.NewBulkIndexRequest(). 136 Index(index). 137 Type(typeName). 138 Id(uuid.NewUUID().String()). 139 Doc(data) 140 if es.pipeline != "" { 141 req.Pipeline(es.pipeline) 142 } 143 144 es.bulkProcessorV5.Add(req) 145 return nil 146 default: 147 return UnsupportedVersion{} 148 } 149 } 150 151 func (es *esClient) FlushBulk() error { 152 switch es.version { 153 case 2: 154 return es.bulkProcessorV2.Flush() 155 case 5: 156 return es.bulkProcessorV5.Flush() 157 default: 158 return UnsupportedVersion{} 159 } 160 } 161 162 func bulkAfterCB_v2(_ int64, _ []elastic2.BulkableRequest, response *elastic2.BulkResponse, err error) { 163 if err != nil { 164 glog.Warningf("Failed to execute bulk operation to ElasticSearch: %v", err) 165 } 166 167 if response.Errors { 168 for _, list := range response.Items { 169 for name, itm := range list { 170 if itm.Error != nil { 171 glog.V(3).Infof("Failed to execute bulk operation to ElasticSearch on %s: %v", name, itm.Error) 172 } 173 } 174 } 175 } 176 } 177 func bulkAfterCB(_ int64, _ []elastic5.BulkableRequest, response *elastic5.BulkResponse, err error) { 178 if err != nil { 179 glog.Warningf("Failed to execute bulk operation to ElasticSearch: %v", err) 180 } 181 182 if response.Errors { 183 for _, list := range response.Items { 184 for name, itm := range list { 185 if itm.Error != nil { 186 glog.V(3).Infof("Failed to execute bulk operation to ElasticSearch on %s: %v", name, itm.Error) 187 } 188 } 189 } 190 } 191 }