Skip to content

Commit

Permalink
Add move replica method
Browse files Browse the repository at this point in the history
Signed-off-by: pritamdas99 <[email protected]>
  • Loading branch information
pritamdas99 committed Sep 17, 2024
1 parent 07ec5f5 commit 774c3bb
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions solr/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,5 @@ type SLClient interface {
GetClient() *resty.Client
GetLog() logr.Logger
DecodeBackupResponse(data map[string]interface{}, collection string) ([]byte, error)
MoveReplica(target string, replica string, collection string) (*Response, error)
}
10 changes: 10 additions & 0 deletions solr/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,13 @@ type CreateParams struct {
NumShards int `json:"numShards,omitempty" yaml:"numShards,omitempty"`
ReplicationFactor int `json:"replicationFactor,omitempty" yaml:"replicationFactor,omitempty"`
}

type MoveReplicaInfo struct {
Replica string `json:"replica,omitempty" yaml:"replica,omitempty"`
TargetNode string `json:"targetNode,omitempty" yaml:"targetNode,omitempty"`
Async string `json:"async,omitempty" yaml:"async,omitempty"`
}

type MoveReplicaParams struct {
MoveReplica MoveReplicaInfo `json:"move-replica,omitempty" yaml:"move-replica,omitempty"`
}
26 changes: 26 additions & 0 deletions solr/solrv8.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,29 @@ func (sc *SLClientV8) DecodeBackupResponse(data map[string]interface{}, collecti
return b, nil

}

func (sc *SLClientV8) MoveReplica(target string, replica string, collection string) (*Response, error) {
sc.Config.log.V(5).Info(fmt.Sprintf("Move replica %v of collection %v to target node %v", replica, collection, target))
req := sc.Client.R().SetDoNotParseResponse(true)
req.SetHeader("Content-Type", "application/json")
moveReplica := &MoveReplicaParams{
MoveReplica: MoveReplicaInfo{
TargetNode: target,
Replica: replica,
Async: fmt.Sprintf("%s-%s-%s", collection, replica, target),
},
}
req.SetBody(moveReplica)
res, err := req.Post(fmt.Sprintf("/api/c/%s", collection))
if err != nil {
sc.Config.log.Error(err, "Failed to send http request to move replica")
return nil, err
}

moveReplicaResponse := &Response{
Code: res.StatusCode(),
header: res.Header(),
body: res.RawBody(),
}
return moveReplicaResponse, nil
}
26 changes: 26 additions & 0 deletions solr/solrv9.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,29 @@ func (sc *SLClientV9) DecodeBackupResponse(data map[string]interface{}, collecti
klog.Info(fmt.Sprintf("Response for collection %s\n%v", collection, string(b)))
return b, nil
}

func (sc *SLClientV9) MoveReplica(target string, replica string, collection string) (*Response, error) {
sc.Config.log.V(5).Info(fmt.Sprintf("Move replica %v of collection %v to target node %v", replica, collection, target))
req := sc.Client.R().SetDoNotParseResponse(true)
req.SetHeader("Content-Type", "application/json")
moveReplica := &MoveReplicaParams{
MoveReplica: MoveReplicaInfo{
TargetNode: target,
Replica: replica,
Async: fmt.Sprintf("%s-%s-%s", collection, replica, target),
},
}
req.SetBody(moveReplica)
res, err := req.Post(fmt.Sprintf("/api/collections/%s", collection))
if err != nil {
sc.Config.log.Error(err, "Failed to send http request to move replica")
return nil, err
}

moveReplicaResponse := &Response{
Code: res.StatusCode(),
header: res.Header(),
body: res.RawBody(),
}
return moveReplicaResponse, nil
}

0 comments on commit 774c3bb

Please sign in to comment.