| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # client::elasticsearch::indices Brik | ||
| 5 | # | ||
| 6 | package Metabrik::Client::Elasticsearch::Indices; | ||
| 7 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | # | ||
| 11 | # DOC: Search::Elasticsearch::Client::6_0::Direct::Indices | ||
| 12 | # | ||
| 13 | |||
| 14 | 1 | ▶ | use base qw(Metabrik::Client::Elasticsearch); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | |||
| 16 | sub brik_properties { | ||
| 17 | return { | ||
| 18 | revision => '$Revision$', | ||
| 19 | tags => [ qw(unstable) ], | ||
| 20 | author => 'GomoR <GomoR[at]metabrik.org>', | ||
| 21 | license => 'http://opensource.org/licenses/BSD-3-Clause', | ||
| 22 | attributes => { | ||
| 23 | }, | ||
| 24 | attributes_default => { | ||
| 25 | }, | ||
| 26 | commands => { | ||
| 27 | show => [ qw(indices|OPTIONAL) ], | ||
| 28 | list => [ qw(indices|OPTIONAL) ], | ||
| 29 | get_settings => [ qw(indices|OPTIONAL) ], | ||
| 30 | put_settings => [ qw(settings indices|OPTIONAL) ], | ||
| 31 | set_indices_readonly => [ qw(indices) ], | ||
| 32 | clear_indices_readonly => [ qw(indices) ], | ||
| 33 | move_indices_to_node => [ qw(indices node) ], | ||
| 34 | reset_indices_node => [ qw(indices node) ], | ||
| 35 | move_indices_to_rack => [ qw(indices rack) ], | ||
| 36 | reset_indices_rack => [ qw(indices) ], | ||
| 37 | remove_indices_replicas => [ qw(indices) ], | ||
| 38 | forcemerge_indices => [ qw(indices) ], | ||
| 39 | check_forcemerge_indices => [ qw(indices) ], | ||
| 40 | shrink_index => [ qw(index size rack|OPTIONAL) ], | ||
| 41 | }, | ||
| 42 | }; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub brik_init { | ||
| 46 | my $self = shift; | ||
| 47 | |||
| 48 | $self->open or return 0; | ||
| 49 | |||
| 50 | return $self->SUPER::brik_init; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub show { | ||
| 54 | my $self = shift; | ||
| 55 | my ($indices) = @_; | ||
| 56 | |||
| 57 | return $self->SUPER::show_indices($indices); | ||
| 58 | } | ||
| 59 | |||
| 60 | sub list { | ||
| 61 | my $self = shift; | ||
| 62 | my ($indices) = @_; | ||
| 63 | |||
| 64 | return $self->SUPER::list_indices($indices); | ||
| 65 | } | ||
| 66 | |||
| 67 | sub get_settings { | ||
| 68 | my $self = shift; | ||
| 69 | my ($indices) = @_; | ||
| 70 | |||
| 71 | my %args = (); | ||
| 72 | if (defined($indices)) { | ||
| 73 | $args{index} = $indices; | ||
| 74 | } | ||
| 75 | |||
| 76 | return $self->_es->indices->get_settings(%args); | ||
| 77 | } | ||
| 78 | |||
| 79 | sub put_settings { | ||
| 80 | my $self = shift; | ||
| 81 | my ($settings, $indices) = @_; | ||
| 82 | |||
| 83 | $self->brik_help_run_undef_arg('put_settings', $settings) or return; | ||
| 84 | $self->brik_help_run_invalid_arg('put', $settings, 'HASH') or return; | ||
| 85 | |||
| 86 | return $self->_es->indices->put_settings(body => $settings); | ||
| 87 | } | ||
| 88 | |||
| 89 | sub set_indices_readonly { | ||
| 90 | my $self = shift; | ||
| 91 | my ($indices) = @_; | ||
| 92 | |||
| 93 | $self->brik_help_run_undef_arg('set_indices_readonly', $indices) or return; | ||
| 94 | |||
| 95 | my $settings = { | ||
| 96 | 'index.blocks.write' => 'true', | ||
| 97 | }; | ||
| 98 | |||
| 99 | my %args = ( | ||
| 100 | index => $indices, | ||
| 101 | body => $settings, | ||
| 102 | ); | ||
| 103 | |||
| 104 | return $self->_es->indices->put_settings(%args); | ||
| 105 | } | ||
| 106 | |||
| 107 | sub clear_indices_readonly { | ||
| 108 | my $self = shift; | ||
| 109 | my ($indices) = @_; | ||
| 110 | |||
| 111 | $self->brik_help_run_undef_arg('clear_indices_readonly', $indices) | ||
| 112 | or return; | ||
| 113 | |||
| 114 | my $settings = { | ||
| 115 | 'index.blocks.write' => undef, | ||
| 116 | }; | ||
| 117 | |||
| 118 | my %args = ( | ||
| 119 | index => $indices, | ||
| 120 | body => $settings, | ||
| 121 | ); | ||
| 122 | |||
| 123 | return $self->_es->indices->put_settings(%args); | ||
| 124 | } | ||
| 125 | |||
| 126 | sub move_indices_to_node { | ||
| 127 | my $self = shift; | ||
| 128 | my ($indices, $node) = @_; | ||
| 129 | |||
| 130 | $self->brik_help_run_undef_arg('move_indices_to_node', $indices) or return; | ||
| 131 | $self->brik_help_run_undef_arg('move_indices_to_node', $node) or return; | ||
| 132 | |||
| 133 | my $settings = { | ||
| 134 | 'index.routing.allocation.require._name' => $node, | ||
| 135 | }; | ||
| 136 | |||
| 137 | my %args = ( | ||
| 138 | index => $indices, | ||
| 139 | body => $settings, | ||
| 140 | ); | ||
| 141 | |||
| 142 | return $self->_es->indices->put_settings(%args); | ||
| 143 | } | ||
| 144 | |||
| 145 | sub reset_indices_node { | ||
| 146 | my $self = shift; | ||
| 147 | my ($indices) = @_; | ||
| 148 | |||
| 149 | $self->brik_help_run_undef_arg('reset_indices_node', $indices) or return; | ||
| 150 | |||
| 151 | my $settings = { | ||
| 152 | 'index.routing.allocation.require._name' => undef, | ||
| 153 | }; | ||
| 154 | |||
| 155 | my %args = ( | ||
| 156 | index => $indices, | ||
| 157 | body => $settings, | ||
| 158 | ); | ||
| 159 | |||
| 160 | return $self->_es->indices->put_settings(%args); | ||
| 161 | } | ||
| 162 | |||
| 163 | # | ||
| 164 | # POST /logs_2014-09-30/_settings | ||
| 165 | # { | ||
| 166 | # "index.routing.allocation.include.box_type" : "medium" | ||
| 167 | # } | ||
| 168 | # | ||
| 169 | sub move_indices_to_rack { | ||
| 170 | my $self = shift; | ||
| 171 | my ($indices, $rack) = @_; | ||
| 172 | |||
| 173 | $self->brik_help_run_undef_arg('move_indices_to_rack', $indices) or return; | ||
| 174 | $self->brik_help_run_undef_arg('move_indices_to_rack', $rack) or return; | ||
| 175 | |||
| 176 | my $settings = { | ||
| 177 | #'index.routing.allocation.include.node.attr.rack' => $rack, | ||
| 178 | 'index.routing.allocation.require.rack' => $rack, | ||
| 179 | }; | ||
| 180 | |||
| 181 | my %args = ( | ||
| 182 | index => $indices, | ||
| 183 | body => $settings, | ||
| 184 | ); | ||
| 185 | |||
| 186 | return $self->_es->indices->put_settings(%args); | ||
| 187 | } | ||
| 188 | |||
| 189 | sub reset_indices_rack { | ||
| 190 | my $self = shift; | ||
| 191 | my ($indices) = @_; | ||
| 192 | |||
| 193 | $self->brik_help_run_undef_arg('reset_indices_to_rack', $indices) or return; | ||
| 194 | |||
| 195 | my $settings = { | ||
| 196 | 'index.routing.allocation.include.node.attr.rack' => undef, | ||
| 197 | }; | ||
| 198 | |||
| 199 | my %args = ( | ||
| 200 | index => $indices, | ||
| 201 | body => $settings, | ||
| 202 | ); | ||
| 203 | |||
| 204 | return $self->_es->indices->put_settings(%args); | ||
| 205 | } | ||
| 206 | |||
| 207 | # | ||
| 208 | # set number of replicas to 0 | ||
| 209 | # | ||
| 210 | sub remove_indices_replicas { | ||
| 211 | my $self = shift; | ||
| 212 | my ($indices) = @_; | ||
| 213 | |||
| 214 | $self->brik_help_run_undef_arg('remove_indices_replicas', $indices) | ||
| 215 | or return; | ||
| 216 | |||
| 217 | my $settings = { | ||
| 218 | 'index.number_of_replicas' => 0, | ||
| 219 | }; | ||
| 220 | |||
| 221 | my %args = ( | ||
| 222 | index => $indices, | ||
| 223 | body => $settings, | ||
| 224 | ); | ||
| 225 | |||
| 226 | return $self->_es->indices->put_settings(%args); | ||
| 227 | } | ||
| 228 | |||
| 229 | # | ||
| 230 | # WARNING: do that only on read-only indices. | ||
| 231 | # | ||
| 232 | # http://www.elastic.co/guide/en/elasticsearch/reference/current/indices-forcemerge.html | ||
| 233 | # | ||
| 234 | sub forcemerge_indices { | ||
| 235 | my $self = shift; | ||
| 236 | my ($indices) = @_; | ||
| 237 | |||
| 238 | $self->brik_help_run_undef_arg('forcemerge_indices', $indices) | ||
| 239 | or return; | ||
| 240 | |||
| 241 | # Add optional argument: "index.codec": "best_compression" | ||
| 242 | |||
| 243 | my %args = ( | ||
| 244 | index => $indices, | ||
| 245 | max_num_segments => 1, | ||
| 246 | #wait_for_merge => 'false', # argument doesn't exist in ES. | ||
| 247 | ); | ||
| 248 | |||
| 249 | return $self->_es->indices->forcemerge(%args); | ||
| 250 | } | ||
| 251 | |||
| 252 | sub check_forcemerge_indices { | ||
| 253 | my $self = shift; | ||
| 254 | my ($indices) = @_; | ||
| 255 | |||
| 256 | $self->brik_help_run_undef_arg('forcemerge_indices', $indices) | ||
| 257 | or return; | ||
| 258 | |||
| 259 | my %args = ( | ||
| 260 | index => $indices, | ||
| 261 | max_num_segments => undef, | ||
| 262 | ); | ||
| 263 | |||
| 264 | return $self->_es->indices->forcemerge(%args); | ||
| 265 | } | ||
| 266 | |||
| 267 | # | ||
| 268 | # https://www.elastic.co/guide/en/elasticsearch/guide/current/retiring-data.html#optimize-indices | ||
| 269 | # sub optimize {} | ||
| 270 | # | ||
| 271 | # 1. Remove replicas | ||
| 272 | # 2. forcemerge to one segment | ||
| 273 | # 3. Put back replicas settings | ||
| 274 | # | ||
| 275 | |||
| 276 | sub shrink_index { | ||
| 277 | my $self = shift; | ||
| 278 | my ($index, $size, $rack) = @_; | ||
| 279 | |||
| 280 | $self->brik_help_run_undef_arg('shrink_index', $index) or return; | ||
| 281 | $self->brik_help_run_undef_arg('shrink_index', $size) or return; | ||
| 282 | |||
| 283 | my $target = "$index.shrink"; | ||
| 284 | |||
| 285 | my $body = { | ||
| 286 | settings => { | ||
| 287 | 'index.number_of_shards' => $size, | ||
| 288 | }, | ||
| 289 | #aliases => { | ||
| 290 | #}, | ||
| 291 | }; | ||
| 292 | |||
| 293 | if (defined($rack)) { | ||
| 294 | $body->{settings}{'index.routing.allocation.require.rack'} = $rack; | ||
| 295 | } | ||
| 296 | |||
| 297 | my %args = ( | ||
| 298 | index => $index, | ||
| 299 | target => $target, | ||
| 300 | body => $body, | ||
| 301 | #copy_settings => 'true', # Does not work yet. | ||
| 302 | ); | ||
| 303 | |||
| 304 | my $r = $self->_es->indices->shrink(%args); | ||
| 305 | |||
| 306 | #if (defined($rack)) { | ||
| 307 | #sleep(5); # Wait for Shrink to start, then move to rack if given | ||
| 308 | #$self->move_indices_to_rack($target, $rack); | ||
| 309 | #} | ||
| 310 | |||
| 311 | return $r; | ||
| 312 | } | ||
| 313 | |||
| 314 | 1; | ||
| 315 | |||
| 316 | __END__ | ||