Skip to content
This repository was archived by the owner on Apr 15, 2025. It is now read-only.

Commit 7dd653d

Browse files
authored
Merge pull request #100 from github/uyumazhakan/allocate-stale-primary-shard
Allocate stale primary shard functionality is added
2 parents e471a00 + d2dba4b commit 7dd653d

2 files changed

Lines changed: 71 additions & 2 deletions

File tree

es.go

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,11 @@ type ClusterSettings struct {
227227
// A setting name and value with the setting name to be a "collapsed" version of
228228
// the setting. A setting of:
229229
//
230-
// { "indices": { "recovery" : { "max_bytes_per_sec": "10mb" } } }
230+
// { "indices": { "recovery" : { "max_bytes_per_sec": "10mb" } } }
231231
//
232232
// would be represented by:
233233
//
234-
// ClusterSetting{ Setting: "indices.recovery.max_bytes_per_sec", Value: "10mb" }
234+
// ClusterSetting{ Setting: "indices.recovery.max_bytes_per_sec", Value: "10mb" }
235235
type Setting struct {
236236
Setting string
237237
Value string
@@ -1606,3 +1606,55 @@ func (c *Client) ClusterAllocationExplain(req *ClusterAllocationExplainRequest,
16061606

16071607
return string(body), nil
16081608
}
1609+
1610+
type RerouteRequest struct {
1611+
// The commands to perform (move, cancel, allocate, etc)
1612+
Commands []RerouteCommand `json:"commands,omitempty"`
1613+
}
1614+
1615+
type RerouteCommand struct {
1616+
AllocateStalePrimary AllocateStalePrimary `json:"allocate_stale_primary,omitempty"`
1617+
}
1618+
1619+
type AllocateStalePrimary struct {
1620+
// The node ID or node name of the node to assign the shard to.
1621+
Node string `json:"node,omitempty"`
1622+
1623+
// The name of the index containing the shard to be assigned.
1624+
Index string `json:"index,omitempty"`
1625+
1626+
// The shard ID of the shard to be assigned.
1627+
Shard *int `json:"shard,omitempty"`
1628+
1629+
// If a node which has the good copy of the data rejoins the cluster later on, that data will be deleted or overwritten with the data of the stale copy that was forcefully allocated with this command.
1630+
AcceptDataLoss bool `json:"accept_data_loss,omitempty"`
1631+
}
1632+
1633+
// AllocateStalePrimary allows to manually allocate a stale primary shard to a specific node
1634+
func (c *Client) AllocateStalePrimaryShard(node, index string, shard int) error {
1635+
var urlBuilder strings.Builder
1636+
urlBuilder.WriteString("_cluster/reroute")
1637+
1638+
agent := c.buildPostRequest(urlBuilder.String())
1639+
1640+
req := RerouteRequest{
1641+
Commands: []RerouteCommand{
1642+
{
1643+
AllocateStalePrimary: AllocateStalePrimary{
1644+
Node: node,
1645+
Index: index,
1646+
Shard: &shard,
1647+
AcceptDataLoss: true,
1648+
},
1649+
},
1650+
},
1651+
}
1652+
agent.Set("Content-Type", "application/json").Send(req)
1653+
1654+
_, err := handleErrWithBytes(agent)
1655+
if err != nil {
1656+
return err
1657+
}
1658+
1659+
return nil
1660+
}

es_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2179,3 +2179,20 @@ func TestClusterAllocationExplain(t *testing.T) {
21792179
})
21802180
}
21812181
}
2182+
2183+
func TestAllocateStalePrimaryShard(t *testing.T) {
2184+
testSetup := &ServerSetup{
2185+
Method: "POST",
2186+
Path: "/_cluster/reroute",
2187+
Body: `{"commands":[{"allocate_stale_primary":{"accept_data_loss":true,"index":"test-index","node":"test-node","shard":0}}]}`,
2188+
}
2189+
2190+
host, port, ts := setupTestServers(t, []*ServerSetup{testSetup})
2191+
defer ts.Close()
2192+
client := NewClient(host, port)
2193+
2194+
err := client.AllocateStalePrimaryShard("test-node", "test-index", 0)
2195+
if err != nil {
2196+
t.Fatalf("Unexpected error. expected nil, got %s", err)
2197+
}
2198+
}

0 commit comments

Comments
 (0)