|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2017 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | +// Author Ewout Prangsma |
| 21 | +// |
| 22 | + |
| 23 | +package driver |
| 24 | + |
| 25 | +import "context" |
| 26 | + |
| 27 | +// newCluster creates a new Cluster implementation. |
| 28 | +func newCluster(conn Connection) (Cluster, error) { |
| 29 | + if conn == nil { |
| 30 | + return nil, WithStack(InvalidArgumentError{Message: "conn is nil"}) |
| 31 | + } |
| 32 | + return &cluster{ |
| 33 | + conn: conn, |
| 34 | + }, nil |
| 35 | +} |
| 36 | + |
| 37 | +type cluster struct { |
| 38 | + conn Connection |
| 39 | +} |
| 40 | + |
| 41 | +// LoggerState returns the state of the replication logger |
| 42 | +func (c *cluster) Health(ctx context.Context) (ClusterHealth, error) { |
| 43 | + req, err := c.conn.NewRequest("GET", "_admin/cluster/health") |
| 44 | + if err != nil { |
| 45 | + return ClusterHealth{}, WithStack(err) |
| 46 | + } |
| 47 | + applyContextSettings(ctx, req) |
| 48 | + resp, err := c.conn.Do(ctx, req) |
| 49 | + if err != nil { |
| 50 | + return ClusterHealth{}, WithStack(err) |
| 51 | + } |
| 52 | + if err := resp.CheckStatus(200); err != nil { |
| 53 | + return ClusterHealth{}, WithStack(err) |
| 54 | + } |
| 55 | + var result ClusterHealth |
| 56 | + if err := resp.ParseBody("", &result); err != nil { |
| 57 | + return ClusterHealth{}, WithStack(err) |
| 58 | + } |
| 59 | + return result, nil |
| 60 | +} |
| 61 | + |
| 62 | +type moveShardRequest struct { |
| 63 | + Database string `json:"database"` |
| 64 | + Collection string `json:"collection"` |
| 65 | + Shard int `json:"shard"` |
| 66 | + FromServer ServerID `json:"fromServer"` |
| 67 | + ToServer ServerID `json:"toServer"` |
| 68 | +} |
| 69 | + |
| 70 | +// MoveShard moves a single shard of the given collection from server `fromServer` to |
| 71 | +// server `toServer`. |
| 72 | +func (c *cluster) MoveShard(ctx context.Context, col Collection, shard int, fromServer, toServer ServerID) error { |
| 73 | + req, err := c.conn.NewRequest("POST", "_admin/cluster/moveShard") |
| 74 | + if err != nil { |
| 75 | + return WithStack(err) |
| 76 | + } |
| 77 | + input := moveShardRequest{ |
| 78 | + Database: col.Database().Name(), |
| 79 | + Collection: col.Name(), |
| 80 | + Shard: shard, |
| 81 | + FromServer: fromServer, |
| 82 | + ToServer: toServer, |
| 83 | + } |
| 84 | + if _, err := req.SetBody(input); err != nil { |
| 85 | + return WithStack(err) |
| 86 | + } |
| 87 | + applyContextSettings(ctx, req) |
| 88 | + resp, err := c.conn.Do(ctx, req) |
| 89 | + if err != nil { |
| 90 | + return WithStack(err) |
| 91 | + } |
| 92 | + if err := resp.CheckStatus(200); err != nil { |
| 93 | + return WithStack(err) |
| 94 | + } |
| 95 | + if err := resp.ParseBody("", nil); err != nil { |
| 96 | + return WithStack(err) |
| 97 | + } |
| 98 | + return nil |
| 99 | +} |
0 commit comments