-
Notifications
You must be signed in to change notification settings - Fork 0
/
coreadmin.go
424 lines (389 loc) · 14.5 KB
/
coreadmin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package solr
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"time"
)
// CoreAdmin Option & Action constants
const (
CoreAdminOptionIndexInfo = "indexInfo"
CoreAdminOptionName = "name"
CoreAdminOptionInstanceDir = "instanceDir"
CoreAdminOptionConfig = "config"
CoreAdminOptionSchema = "schema"
CoreAdminOptionDataDir = "dataDir"
CoreAdminOptionConfigSet = "configSet"
CoreAdminOptionCollection = "collection"
CoreAdminOptionShard = "shard"
CoreAdminOptionAsync = "async"
CoreAdminOptionCore = "core"
CoreAdminOptionOther = "other"
CoreAdminOptionAction = "action"
CoreAdminOptionDeleteIndex = "deleteIndex"
CoreAdminOptionDeleteDataDir = "deleteDataDir"
CoreAdminOptionDeleteInstanceDir = "deleteInstanceDir"
CoreAdminOptionIndexDir = "indexDir"
CoreAdminOptionSourceCore = "srcCore"
CoreAdminOptionPath = "path"
CoreAdminOptionTargetCore = "targetCore"
CoreAdminOptionRanges = "ranges"
CoreAdminOptionSplitKey = "split.key"
CoreAdminOptionRequestID = "requestid"
CoreAdminActionStatus = "STATUS"
CoreAdminActionCreate = "CREATE"
CoreAdminActionReload = "RELOAD"
CoreAdminActionRename = "RENAME"
CoreAdminActionSwap = "SWAP"
CoreAdminActionUnload = "UNLOAD"
CoreAdminActionMergeIndexes = "MERGEINDEXES"
CoreAdminActionSplit = "SPLIT"
CoreAdminActionRequestStatus = "REQUESTSTATUS"
CoreAdminActionRecover = "REQUESTRECOVERY"
)
// Errors that can be returned
var (
ErrMoreParamsPath = errors.New("only one of path, targetCore may be defined")
ErrMoreParamsRange = errors.New("only one of range, split.key may be defined")
)
// CoreCreateOpts are the optional properties that can
// be provided when creating a new core.
type CoreCreateOpts struct {
InstanceDir string
Config string
Schema string
DataDir string
ConfigSet string
Collection string
Shard string
AsyncID string
}
// CoreUnloadOpts are the optional properties that can
// be provided when unloading a core.
type CoreUnloadOpts struct {
DeleteIndex bool
DeleteDataDir bool
DeleteInstanceDir bool
AsyncID string
}
// CoreSplitOpts are the optional properties that can be
// provided when splitting a core. Path & TargetCore
// may not have a value simultaneously, the same
// goes for Ranges & SplitKey.
type CoreSplitOpts struct {
Path []string
TargetCore []string
Ranges string
SplitKey string
AsyncID string
}
// CoreMergeOpts are the optional properties that can be
// provided when merging a core.
type CoreMergeOpts struct {
IndexDir []string
SrcCore []string
AsyncID string
}
// CoreAdminResponse represents the response from the solr core admin API. It usually
// contains Header information, the response data or an error in case of erroneous
// response. Also it can contain the core's or a request's status, failures
// that might have happened during the initiation procedure as well as
// core information.
type CoreAdminResponse struct {
Header *ResponseHeader `json:"responseHeader"`
Error *ResponseError `json:"error"`
Status map[string]*CoreStatusResponse `json:"status"`
ReqStatus string `json:"STATUS"`
Response interface{} `json:"response"`
InitFailures interface{} `json:"initFailures"`
Core string `json:"core"`
}
// CoreStatusResponse contains information about a core and its status.
type CoreStatusResponse struct {
Name string `json:"name"`
InstanceDir string `json:"instanceDir"`
DataDir string `json:"dataDir"`
Config string `json:"config"`
Schema string `json:"schema"`
StartTime time.Time `json:"startTime"`
Uptime time.Duration `json:"uptime"`
Index *IndexData `json:"index"`
}
// IndexData contains information about a core's index.
type IndexData struct {
NumDocs int64 `json:"numDocs"`
MaxDoc int64 `json:"maxDoc"`
DeletedDocs int64 `json:"deletedDocs"`
IndexHeapUsageBytes int64 `json:"indexHeapUsageBytes"`
Version int64 `json:"version"`
SegmentCount int64 `json:"segmentCount"`
Current bool `json:"current"`
HasDeletions bool `json:"hasDeletions"`
Directory string `json:"directory"`
SegmentsFile string `json:"segmentsFile"`
SegmentsFileSizeInBytes int64 `json:"segmentsFileSizeInBytes"`
UserData *UserData `json:"userData"`
LastModified time.Time `json:"lastModified"`
SizeInBytes int64 `json:"sizeInBytes"`
Size string `json:"size"`
}
// UserData contains information about commits.
type UserData struct {
CommitCommandVersion string `json:"commitCommandVer"`
CommitTimeMSec string `json:"commitTimeMSec"`
}
// CoreAdmin contains a connectin to solr.
type CoreAdmin struct {
conn *Connection
Path string
}
// NewCoreAdmin returns a new core admin, creating a connection to solr using the provided
// http client and host, core info.
func NewCoreAdmin(ctx context.Context, host string, client *http.Client) (*CoreAdmin, error) {
if host == "" {
return nil, ErrInvalidConfig
}
_, err := url.ParseRequestURI(host)
if err != nil {
return nil, err
}
conn := &Connection{
Host: host,
Core: "",
httpClient: client,
}
path := fmt.Sprintf("%s/solr/admin/cores?", host)
return &CoreAdmin{conn: conn, Path: path}, nil
}
// SetBasicAuth sets the authentication credentials if needed.
func (a *CoreAdmin) SetBasicAuth(username, password string) {
a.conn.Username = username
a.conn.Password = password
}
func (a *CoreAdmin) request(ctx context.Context, method, url string) (*CoreAdminResponse, error) {
req, err := http.NewRequest(method, url, nil)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
if a.conn.Username != "" && a.conn.Password != "" {
req.SetBasicAuth(a.conn.Username, a.conn.Password)
}
res, err := a.conn.httpClient.Do(req.WithContext(ctx))
if err != nil {
return nil, err
}
var r CoreAdminResponse
defer res.Body.Close()
err = json.NewDecoder(res.Body).Decode(&r)
if err != nil {
return nil, err
}
if r.Error != nil {
return &r, r.Error
}
return &r, nil
}
// Status returns the status of all running Solr cores, or status for only the named core. If the
// noIndexInfo option is true information about the index will not be returned with a core.
// For more info:
// https://lucene.apache.org/solr/guide/8_5/coreadmin-api.html#coreadmin-status
func (a *CoreAdmin) Status(ctx context.Context, core string, noIndexInfo bool) (*CoreAdminResponse, error) {
params := url.Values{}
params.Set(CoreAdminOptionAction, CoreAdminActionStatus)
if core != "" {
params.Set(CoreAdminOptionCore, core)
}
if noIndexInfo {
params.Set(CoreAdminOptionIndexInfo, "false")
}
url := a.Path + params.Encode()
return a.request(ctx, http.MethodGet, url)
}
// Create creates a new core and registers it. For more info:
// https://lucene.apache.org/solr/guide/8_5/coreadmin-api.html#coreadmin-create
func (a *CoreAdmin) Create(ctx context.Context, name string, opts *CoreCreateOpts) (*CoreAdminResponse, error) {
params := url.Values{}
params.Set(CoreAdminOptionAction, CoreAdminActionCreate)
params.Set(CoreAdminOptionName, name)
if opts != nil {
if opts.AsyncID != "" {
params.Set(CoreAdminOptionAsync, opts.AsyncID)
}
if opts.DataDir != "" {
params.Set(CoreAdminOptionDataDir, opts.DataDir)
}
if opts.InstanceDir != "" {
params.Set(CoreAdminOptionInstanceDir, opts.InstanceDir)
}
if opts.Collection != "" {
params.Set(CoreAdminOptionCollection, opts.Collection)
}
if opts.ConfigSet != "" {
params.Set(CoreAdminOptionConfigSet, opts.ConfigSet)
}
if opts.Config != "" {
params.Set(CoreAdminOptionConfig, opts.Config)
}
if opts.Schema != "" {
params.Set(CoreAdminOptionSchema, opts.Schema)
}
if opts.Shard != "" {
params.Set(CoreAdminOptionShard, opts.Shard)
}
}
url := a.Path + params.Encode()
return a.request(ctx, http.MethodGet, url)
}
// Reload loads a new core from the configuration of an existing, registered Solr core. While the
// new core is initializing, the existing one will continue to handle requests. When the new
// Solr core is ready, it takes over and the old core is unloaded. For More info:
// https://lucene.apache.org/solr/guide/8_5/coreadmin-api.html#coreadmin-reload
func (a *CoreAdmin) Reload(ctx context.Context, core string) (*CoreAdminResponse, error) {
params := url.Values{}
params.Set(CoreAdminOptionAction, CoreAdminActionReload)
params.Set(CoreAdminOptionCore, core)
url := a.Path + params.Encode()
return a.request(ctx, http.MethodGet, url)
}
// Rename changes the name of a Solr core. An asyncID may be provided in order to track
// this action which will be processed asynchronously. For more info:
// https://lucene.apache.org/solr/guide/8_5/coreadmin-api.html#coreadmin-rename
func (a *CoreAdmin) Rename(ctx context.Context, core, other, asyncID string) (*CoreAdminResponse, error) {
params := url.Values{}
params.Set(CoreAdminOptionAction, CoreAdminActionRename)
params.Set(CoreAdminOptionCore, core)
params.Set(CoreAdminOptionOther, other)
if asyncID != "" {
params.Set(CoreAdminOptionAsync, asyncID)
}
url := a.Path + params.Encode()
return a.request(ctx, http.MethodGet, url)
}
// Swap atomically swaps the names used to access two existing Solr cores. This can be used
// to swap new content into production. For more info:
// https://lucene.apache.org/solr/guide/8_5/coreadmin-api.html#coreadmin-swap
func (a *CoreAdmin) Swap(ctx context.Context, core, other, asyncID string) (*CoreAdminResponse, error) {
params := url.Values{}
params.Set(CoreAdminOptionAction, CoreAdminActionSwap)
params.Set(CoreAdminOptionCore, core)
params.Set(CoreAdminOptionOther, other)
if asyncID != "" {
params.Set(CoreAdminOptionAsync, asyncID)
}
url := a.Path + params.Encode()
return a.request(ctx, http.MethodGet, url)
}
// Unload removes a core from Solr. Requires the name of the core to be unloaded.
// For more info:
// https://lucene.apache.org/solr/guide/8_5/coreadmin-api.html#coreadmin-unload
func (a *CoreAdmin) Unload(ctx context.Context, core string, opts *CoreUnloadOpts) (*CoreAdminResponse, error) {
params := url.Values{}
params.Set(CoreAdminOptionAction, CoreAdminActionUnload)
params.Set(CoreAdminOptionCore, core)
if opts != nil {
if opts.AsyncID != "" {
params.Set(CoreAdminOptionAsync, opts.AsyncID)
}
if opts.DeleteIndex {
params.Set(CoreAdminOptionDeleteIndex, "true")
}
if opts.DeleteDataDir {
params.Set(CoreAdminOptionDeleteDataDir, "true")
}
if opts.DeleteInstanceDir {
params.Set(CoreAdminOptionDeleteInstanceDir, "true")
}
}
url := a.Path + params.Encode()
return a.request(ctx, http.MethodGet, url)
}
// Merge merges one or more indexes to another index. The target core index must already exist
// and have a compatible schema with the one or more indexes that will be merged to it.
// Another commit on the target core should also be performed after the merge
// is complete. For more info:
// https://lucene.apache.org/solr/guide/8_5/coreadmin-api.html#coreadmin-mergeindexes
func (a *CoreAdmin) Merge(ctx context.Context, core string, opts *CoreMergeOpts) (*CoreAdminResponse, error) {
params := url.Values{}
params.Set(CoreAdminOptionAction, CoreAdminActionMergeIndexes)
params.Set(CoreAdminOptionCore, core)
if opts != nil {
if opts.AsyncID != "" {
params.Set(CoreAdminOptionAsync, opts.AsyncID)
}
if len(opts.IndexDir) > 0 {
for _, d := range opts.IndexDir {
params.Add(CoreAdminOptionIndexDir, d)
}
}
if len(opts.SrcCore) > 0 {
for _, c := range opts.SrcCore {
params.Add(CoreAdminOptionSourceCore, c)
}
}
}
url := a.Path + params.Encode()
return a.request(ctx, http.MethodGet, url)
}
// Split splits an index into two or more indexes. The index being split can continue to handle requests.
// The split pieces can be placed into a specified directory on the server’s filesystem or it can be
// merged into running Solr cores. For more info:
// https://lucene.apache.org/solr/guide/8_5/coreadmin-api.html#coreadmin-split
func (a *CoreAdmin) Split(ctx context.Context, core string, opts *CoreSplitOpts) (*CoreAdminResponse, error) {
params := url.Values{}
params.Set(CoreAdminOptionAction, CoreAdminActionSplit)
params.Set(CoreAdminOptionCore, core)
if opts != nil {
if len(opts.Path) > 0 && len(opts.TargetCore) > 0 {
return nil, ErrMoreParamsPath
}
if opts.Ranges != "" && opts.SplitKey != "" {
return nil, ErrMoreParamsRange
}
if opts.AsyncID != "" {
params.Set(CoreAdminOptionAsync, opts.AsyncID)
}
if len(opts.Path) > 0 {
for _, p := range opts.Path {
params.Add(CoreAdminOptionPath, p)
}
}
if opts.Ranges != "" {
params.Set(CoreAdminOptionRanges, opts.Ranges)
}
if len(opts.TargetCore) > 0 {
for _, t := range opts.TargetCore {
params.Add(CoreAdminOptionTargetCore, t)
}
}
if opts.SplitKey != "" {
params.Set(CoreAdminOptionSplitKey, opts.SplitKey)
}
}
url := a.Path + params.Encode()
return a.request(ctx, http.MethodGet, url)
}
// RequestStatus returns the status of an already submitted asynchronous CoreAdmin API call.
// For more info:
// https://lucene.apache.org/solr/guide/8_5/coreadmin-api.html#coreadmin-requeststatus
func (a *CoreAdmin) RequestStatus(ctx context.Context, id string) (*CoreAdminResponse, error) {
params := url.Values{}
params.Set(CoreAdminOptionAction, CoreAdminActionRequestStatus)
params.Set(CoreAdminOptionRequestID, id)
url := a.Path + params.Encode()
return a.request(ctx, http.MethodGet, url)
}
// Recover manually asks a core to recover by synching with the leader. This should be considered
// an "expert" level command and should be used in situations where the node (SorlCloud replica)
// is unable to become active automatically. For more info
// https://lucene.apache.org/solr/guide/8_5/coreadmin-api.html#coreadmin-requestrecovery
func (a *CoreAdmin) Recover(ctx context.Context, core string) (*CoreAdminResponse, error) {
params := url.Values{}
params.Set(CoreAdminOptionAction, CoreAdminActionRecover)
params.Set(CoreAdminOptionCore, core)
url := a.Path + params.Encode()
return a.request(ctx, http.MethodGet, url)
}