|
1 | 1 | [[allocation-awareness]]
|
2 |
| -=== Shard Allocation Awareness |
| 2 | +=== Shard allocation awareness |
| 3 | + |
| 4 | +You can use custom node attributes as _awareness attributes_ to enable {es} |
| 5 | +to take your physical hardware configuration into account when allocating shards. |
| 6 | +If {es} knows which nodes are on the same physical server, in the same rack, or |
| 7 | +in the same zone, it can distribute the primary shard and its replica shards to |
| 8 | +minimise the risk of losing all shard copies in the event of a failure. |
| 9 | + |
| 10 | +When shard allocation awareness is enabled with the |
| 11 | +`cluster.routing.allocation.awareness.attributes` setting, shards are only |
| 12 | +allocated to nodes that have values set for the specified awareness |
| 13 | +attributes. If you use multiple awareness attributes, {es} considers |
| 14 | +each attribute separately when allocating shards. |
| 15 | + |
| 16 | +The allocation awareness settings can be configured in |
| 17 | +`elasticsearch.yml` and updated dynamically with the |
| 18 | +<<cluster-update-settings,cluster-update-settings>> API. |
3 | 19 |
|
4 |
| -When running nodes on multiple VMs on the same physical server, on multiple |
5 |
| -racks, or across multiple zones or domains, it is more likely that two nodes on |
6 |
| -the same physical server, in the same rack, or in the same zone or domain will |
7 |
| -crash at the same time, rather than two unrelated nodes crashing |
8 |
| -simultaneously. |
| 20 | +{es} prefers using shards in the same location (with the same |
| 21 | +awareness attribute values) to process search or GET requests. Using local |
| 22 | +shards is usually faster than crossing rack or zone boundaries. |
9 | 23 |
|
10 |
| -If Elasticsearch is _aware_ of the physical configuration of your hardware, it |
11 |
| -can ensure that the primary shard and its replica shards are spread across |
12 |
| -different physical servers, racks, or zones, to minimise the risk of losing |
13 |
| -all shard copies at the same time. |
| 24 | +NOTE: The number of attribute values determines how many shard copies are |
| 25 | +allocated in each location. If the number of nodes in each location is |
| 26 | +unbalanced and there are a lot of replicas, replica shards might be left |
| 27 | +unassigned. |
14 | 28 |
|
15 |
| -The shard allocation awareness settings allow you to tell Elasticsearch about |
16 |
| -your hardware configuration. |
| 29 | +[float] |
| 30 | +[[enabling-awareness]] |
| 31 | +==== Enabling shard allocation awareness |
17 | 32 |
|
18 |
| -As an example, let's assume we have several racks. When we start a node, we |
19 |
| -can tell it which rack it is in by assigning it an arbitrary metadata |
20 |
| -attribute called `rack_id` -- we could use any attribute name. For example: |
| 33 | +To enable shard allocation awareness: |
21 | 34 |
|
| 35 | +. Specify the location of each node with a custom node attribute. For example, |
| 36 | +if you want Elasticsearch to distribute shards across different racks, you might |
| 37 | +set an awareness attribute called `rack_id` in each node's `elasticsearch.yml` |
| 38 | +config file. |
| 39 | ++ |
| 40 | +[source,yaml] |
| 41 | +-------------------------------------------------------- |
| 42 | +node.attr.rack_id: rack_one |
| 43 | +-------------------------------------------------------- |
| 44 | ++ |
| 45 | +You can also set custom attributes when you start a node: |
| 46 | ++ |
22 | 47 | [source,sh]
|
23 |
| ----------------------- |
24 |
| -./bin/elasticsearch -Enode.attr.rack_id=rack_one <1> |
25 |
| ----------------------- |
26 |
| -<1> This setting could also be specified in the `elasticsearch.yml` config file. |
27 |
| - |
28 |
| -Now, we need to set up _shard allocation awareness_ by telling Elasticsearch |
29 |
| -which attributes to use. This can be configured in the `elasticsearch.yml` |
30 |
| -file on *all* master-eligible nodes, or it can be set (and changed) with the |
31 |
| -<<cluster-update-settings,cluster-update-settings>> API. |
32 |
| - |
33 |
| -For our example, we'll set the value in the config file: |
| 48 | +-------------------------------------------------------- |
| 49 | +`./bin/elasticsearch -Enode.attr.rack_id=rack_one` |
| 50 | +-------------------------------------------------------- |
34 | 51 |
|
| 52 | +. Tell {es} to take one or more awareness attributes into account when |
| 53 | +allocating shards by setting |
| 54 | +`cluster.routing.allocation.awareness.attributes` in *every* master-eligible |
| 55 | +node's `elasticsearch.yml` config file. |
| 56 | ++ |
| 57 | +-- |
35 | 58 | [source,yaml]
|
36 | 59 | --------------------------------------------------------
|
37 |
| -cluster.routing.allocation.awareness.attributes: rack_id |
| 60 | +cluster.routing.allocation.awareness.attributes: rack_id <1> |
38 | 61 | --------------------------------------------------------
|
39 |
| - |
40 |
| -With this config in place, let's say we start two nodes with |
41 |
| -`node.attr.rack_id` set to `rack_one`, and we create an index with 5 primary |
42 |
| -shards and 1 replica of each primary. All primaries and replicas are |
| 62 | +<1> Specify multiple attributes as a comma-separated list. |
| 63 | +-- |
| 64 | ++ |
| 65 | +You can also use the |
| 66 | +<<cluster-update-settings,cluster-update-settings>> API to set or update |
| 67 | +a cluster's awareness attributes. |
| 68 | + |
| 69 | +With this example configuration, if you start two nodes with |
| 70 | +`node.attr.rack_id` set to `rack_one` and create an index with 5 primary |
| 71 | +shards and 1 replica of each primary, all primaries and replicas are |
43 | 72 | allocated across the two nodes.
|
44 | 73 |
|
45 |
| -Now, if we start two more nodes with `node.attr.rack_id` set to `rack_two`, |
46 |
| -Elasticsearch will move shards across to the new nodes, ensuring (if possible) |
47 |
| -that no two copies of the same shard will be in the same rack. However if |
48 |
| -`rack_two` were to fail, taking down both of its nodes, Elasticsearch will |
49 |
| -still allocate the lost shard copies to nodes in `rack_one`. |
50 |
| - |
51 |
| -.Prefer local shards |
52 |
| -********************************************* |
53 |
| -
|
54 |
| -When executing search or GET requests, with shard awareness enabled, |
55 |
| -Elasticsearch will prefer using local shards -- shards in the same awareness |
56 |
| -group -- to execute the request. This is usually faster than crossing between |
57 |
| -racks or across zone boundaries. |
| 74 | +If you add two nodes with `node.attr.rack_id` set to `rack_two`, |
| 75 | +{es} moves shards to the new nodes, ensuring (if possible) |
| 76 | +that no two copies of the same shard are in the same rack. |
58 | 77 |
|
59 |
| -********************************************* |
60 |
| - |
61 |
| -Multiple awareness attributes can be specified, in which case each attribute |
62 |
| -is considered separately when deciding where to allocate the shards. |
63 |
| - |
64 |
| -[source,yaml] |
65 |
| -------------------------------------------------------------- |
66 |
| -cluster.routing.allocation.awareness.attributes: rack_id,zone |
67 |
| -------------------------------------------------------------- |
68 |
| - |
69 |
| -NOTE: When using awareness attributes, shards will not be allocated to nodes |
70 |
| -that don't have values set for those attributes. |
71 |
| - |
72 |
| -NOTE: Number of primary/replica of a shard allocated on a specific group of |
73 |
| -nodes with the same awareness attribute value is determined by the number of |
74 |
| -attribute values. When the number of nodes in groups is unbalanced and there |
75 |
| -are many replicas, replica shards may be left unassigned. |
| 78 | +If `rack_two` fails and takes down both its nodes, by default {es} |
| 79 | +allocates the lost shard copies to nodes in `rack_one`. To prevent multiple |
| 80 | +copies of a particular shard from being allocated in the same location, you can |
| 81 | +enable forced awareness. |
76 | 82 |
|
77 | 83 | [float]
|
78 | 84 | [[forced-awareness]]
|
79 |
| -=== Forced Awareness |
80 |
| - |
81 |
| -Imagine that you have two zones and enough hardware across the two zones to |
82 |
| -host all of your primary and replica shards. But perhaps the hardware in a |
83 |
| -single zone, while sufficient to host half the shards, would be unable to host |
84 |
| -*ALL* the shards. |
| 85 | +==== Forced awareness |
85 | 86 |
|
86 |
| -With ordinary awareness, if one zone lost contact with the other zone, |
87 |
| -Elasticsearch would assign all of the missing replica shards to a single zone. |
88 |
| -But in this example, this sudden extra load would cause the hardware in the |
89 |
| -remaining zone to be overloaded. |
| 87 | +By default, if one location fails, Elasticsearch assigns all of the missing |
| 88 | +replica shards to the remaining locations. While you might have sufficient |
| 89 | +resources across all locations to host your primary and replica shards, a single |
| 90 | +location might be unable to host *ALL* of the shards. |
90 | 91 |
|
91 |
| -Forced awareness solves this problem by *NEVER* allowing copies of the same |
92 |
| -shard to be allocated to the same zone. |
| 92 | +To prevent a single location from being overloaded in the event of a failure, |
| 93 | +you can set `cluster.routing.allocation.awareness.force` so no replicas are |
| 94 | +allocated until nodes are available in another location. |
93 | 95 |
|
94 |
| -For example, lets say we have an awareness attribute called `zone`, and we |
95 |
| -know we are going to have two zones, `zone1` and `zone2`. Here is how we can |
96 |
| -force awareness on a node: |
| 96 | +For example, if you have an awareness attribute called `zone` and configure nodes |
| 97 | +in `zone1` and `zone2`, you can use forced awareness to prevent Elasticsearch |
| 98 | +from allocating replicas if only one zone is available: |
97 | 99 |
|
98 | 100 | [source,yaml]
|
99 | 101 | -------------------------------------------------------------------
|
100 |
| -cluster.routing.allocation.awareness.force.zone.values: zone1,zone2 <1> |
101 | 102 | cluster.routing.allocation.awareness.attributes: zone
|
| 103 | +cluster.routing.allocation.awareness.force.zone.values: zone1,zone2 <1> |
102 | 104 | -------------------------------------------------------------------
|
103 |
| -<1> We must list all possible values that the `zone` attribute can have. |
104 |
| - |
105 |
| -Now, if we start 2 nodes with `node.attr.zone` set to `zone1` and create an |
106 |
| -index with 5 shards and 1 replica. The index will be created, but only the 5 |
107 |
| -primary shards will be allocated (with no replicas). Only when we start more |
108 |
| -nodes with `node.attr.zone` set to `zone2` will the replicas be allocated. |
109 |
| - |
110 |
| -The `cluster.routing.allocation.awareness.*` settings can all be updated |
111 |
| -dynamically on a live cluster with the |
112 |
| -<<cluster-update-settings,cluster-update-settings>> API. |
113 |
| - |
| 105 | +<1> Specify all possible values for the awareness attribute. |
114 | 106 |
|
| 107 | +With this example configuration, if you start two nodes with `node.attr.zone` set |
| 108 | +to `zone1` and create an index with 5 shards and 1 replica, Elasticsearch creates |
| 109 | +the index and allocates the 5 primary shards but no replicas. Replicas are |
| 110 | +only allocated once nodes with `node.attr.zone` set to `zone2` are available. |
0 commit comments