| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | # | 
| 2 |  |  |  |  |  |  | # $Id$ | 
| 3 |  |  |  |  |  |  | # | 
| 4 |  |  |  |  |  |  | # client::elasticsearch::indices Brik | 
| 5 |  |  |  |  |  |  | # | 
| 6 |  |  |  |  |  |  | package Metabrik::Client::Elasticsearch::Indices; | 
| 7 | 1 |  |  | 1 |  | 872 | use strict; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 29 |  | 
| 8 | 1 |  |  | 1 |  | 5 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 31 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | # | 
| 11 |  |  |  |  |  |  | # DOC: Search::Elasticsearch::Client::6_0::Direct::Indices | 
| 12 |  |  |  |  |  |  | # | 
| 13 |  |  |  |  |  |  |  | 
| 14 | 1 |  |  | 1 |  | 5 | use base qw(Metabrik::Client::Elasticsearch); | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 1303 |  | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | sub brik_properties { | 
| 17 |  |  |  |  |  |  | return { | 
| 18 | 0 |  |  | 0 | 1 |  | revision => '$Revision$', | 
| 19 |  |  |  |  |  |  | tags => [ qw(unstable) ], | 
| 20 |  |  |  |  |  |  | author => 'GomoR ', | 
| 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 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 47 |  |  |  |  |  |  |  | 
| 48 | 0 | 0 |  |  |  |  | $self->open or return 0; | 
| 49 |  |  |  |  |  |  |  | 
| 50 | 0 |  |  |  |  |  | return $self->SUPER::brik_init; | 
| 51 |  |  |  |  |  |  | } | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | sub show { | 
| 54 | 0 |  |  | 0 | 0 |  | my $self = shift; | 
| 55 | 0 |  |  |  |  |  | my ($indices) = @_; | 
| 56 |  |  |  |  |  |  |  | 
| 57 | 0 |  |  |  |  |  | return $self->SUPER::show_indices($indices); | 
| 58 |  |  |  |  |  |  | } | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | sub list { | 
| 61 | 0 |  |  | 0 | 0 |  | my $self = shift; | 
| 62 | 0 |  |  |  |  |  | my ($indices) = @_; | 
| 63 |  |  |  |  |  |  |  | 
| 64 | 0 |  |  |  |  |  | return $self->SUPER::list_indices($indices); | 
| 65 |  |  |  |  |  |  | } | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | sub get_settings { | 
| 68 | 0 |  |  | 0 | 0 |  | my $self = shift; | 
| 69 | 0 |  |  |  |  |  | my ($indices) = @_; | 
| 70 |  |  |  |  |  |  |  | 
| 71 | 0 |  |  |  |  |  | my %args = (); | 
| 72 | 0 | 0 |  |  |  |  | if (defined($indices)) { | 
| 73 | 0 |  |  |  |  |  | $args{index} = $indices; | 
| 74 |  |  |  |  |  |  | } | 
| 75 |  |  |  |  |  |  |  | 
| 76 | 0 |  |  |  |  |  | return $self->_es->indices->get_settings(%args); | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | sub put_settings { | 
| 80 | 0 |  |  | 0 | 0 |  | my $self = shift; | 
| 81 | 0 |  |  |  |  |  | my ($settings, $indices) = @_; | 
| 82 |  |  |  |  |  |  |  | 
| 83 | 0 | 0 |  |  |  |  | $self->brik_help_run_undef_arg('put_settings', $settings) or return; | 
| 84 | 0 | 0 |  |  |  |  | $self->brik_help_run_invalid_arg('put', $settings, 'HASH') or return; | 
| 85 |  |  |  |  |  |  |  | 
| 86 | 0 |  |  |  |  |  | return $self->_es->indices->put_settings(body => $settings); | 
| 87 |  |  |  |  |  |  | } | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | sub set_indices_readonly { | 
| 90 | 0 |  |  | 0 | 0 |  | my $self = shift; | 
| 91 | 0 |  |  |  |  |  | my ($indices) = @_; | 
| 92 |  |  |  |  |  |  |  | 
| 93 | 0 | 0 |  |  |  |  | $self->brik_help_run_undef_arg('set_indices_readonly', $indices) or return; | 
| 94 |  |  |  |  |  |  |  | 
| 95 | 0 |  |  |  |  |  | my $settings = { | 
| 96 |  |  |  |  |  |  | 'index.blocks.write' => 'true', | 
| 97 |  |  |  |  |  |  | }; | 
| 98 |  |  |  |  |  |  |  | 
| 99 | 0 |  |  |  |  |  | my %args = ( | 
| 100 |  |  |  |  |  |  | index => $indices, | 
| 101 |  |  |  |  |  |  | body => $settings, | 
| 102 |  |  |  |  |  |  | ); | 
| 103 |  |  |  |  |  |  |  | 
| 104 | 0 |  |  |  |  |  | return $self->_es->indices->put_settings(%args); | 
| 105 |  |  |  |  |  |  | } | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | sub clear_indices_readonly { | 
| 108 | 0 |  |  | 0 | 0 |  | my $self = shift; | 
| 109 | 0 |  |  |  |  |  | my ($indices) = @_; | 
| 110 |  |  |  |  |  |  |  | 
| 111 | 0 | 0 |  |  |  |  | $self->brik_help_run_undef_arg('clear_indices_readonly', $indices) | 
| 112 |  |  |  |  |  |  | or return; | 
| 113 |  |  |  |  |  |  |  | 
| 114 | 0 |  |  |  |  |  | my $settings = { | 
| 115 |  |  |  |  |  |  | 'index.blocks.write' => undef, | 
| 116 |  |  |  |  |  |  | }; | 
| 117 |  |  |  |  |  |  |  | 
| 118 | 0 |  |  |  |  |  | my %args = ( | 
| 119 |  |  |  |  |  |  | index => $indices, | 
| 120 |  |  |  |  |  |  | body => $settings, | 
| 121 |  |  |  |  |  |  | ); | 
| 122 |  |  |  |  |  |  |  | 
| 123 | 0 |  |  |  |  |  | return $self->_es->indices->put_settings(%args); | 
| 124 |  |  |  |  |  |  | } | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  | sub move_indices_to_node { | 
| 127 | 0 |  |  | 0 | 0 |  | my $self = shift; | 
| 128 | 0 |  |  |  |  |  | my ($indices, $node) = @_; | 
| 129 |  |  |  |  |  |  |  | 
| 130 | 0 | 0 |  |  |  |  | $self->brik_help_run_undef_arg('move_indices_to_node', $indices) or return; | 
| 131 | 0 | 0 |  |  |  |  | $self->brik_help_run_undef_arg('move_indices_to_node', $node) or return; | 
| 132 |  |  |  |  |  |  |  | 
| 133 | 0 |  |  |  |  |  | my $settings = { | 
| 134 |  |  |  |  |  |  | 'index.routing.allocation.require._name' => $node, | 
| 135 |  |  |  |  |  |  | }; | 
| 136 |  |  |  |  |  |  |  | 
| 137 | 0 |  |  |  |  |  | my %args = ( | 
| 138 |  |  |  |  |  |  | index => $indices, | 
| 139 |  |  |  |  |  |  | body => $settings, | 
| 140 |  |  |  |  |  |  | ); | 
| 141 |  |  |  |  |  |  |  | 
| 142 | 0 |  |  |  |  |  | return $self->_es->indices->put_settings(%args); | 
| 143 |  |  |  |  |  |  | } | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | sub reset_indices_node { | 
| 146 | 0 |  |  | 0 | 0 |  | my $self = shift; | 
| 147 | 0 |  |  |  |  |  | my ($indices) = @_; | 
| 148 |  |  |  |  |  |  |  | 
| 149 | 0 | 0 |  |  |  |  | $self->brik_help_run_undef_arg('reset_indices_node', $indices) or return; | 
| 150 |  |  |  |  |  |  |  | 
| 151 | 0 |  |  |  |  |  | my $settings = { | 
| 152 |  |  |  |  |  |  | 'index.routing.allocation.require._name' => undef, | 
| 153 |  |  |  |  |  |  | }; | 
| 154 |  |  |  |  |  |  |  | 
| 155 | 0 |  |  |  |  |  | my %args = ( | 
| 156 |  |  |  |  |  |  | index => $indices, | 
| 157 |  |  |  |  |  |  | body => $settings, | 
| 158 |  |  |  |  |  |  | ); | 
| 159 |  |  |  |  |  |  |  | 
| 160 | 0 |  |  |  |  |  | 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 | 0 |  |  | 0 | 0 |  | my $self = shift; | 
| 171 | 0 |  |  |  |  |  | my ($indices, $rack) = @_; | 
| 172 |  |  |  |  |  |  |  | 
| 173 | 0 | 0 |  |  |  |  | $self->brik_help_run_undef_arg('move_indices_to_rack', $indices) or return; | 
| 174 | 0 | 0 |  |  |  |  | $self->brik_help_run_undef_arg('move_indices_to_rack', $rack) or return; | 
| 175 |  |  |  |  |  |  |  | 
| 176 | 0 |  |  |  |  |  | my $settings = { | 
| 177 |  |  |  |  |  |  | #'index.routing.allocation.include.node.attr.rack' => $rack, | 
| 178 |  |  |  |  |  |  | 'index.routing.allocation.require.rack' => $rack, | 
| 179 |  |  |  |  |  |  | }; | 
| 180 |  |  |  |  |  |  |  | 
| 181 | 0 |  |  |  |  |  | my %args = ( | 
| 182 |  |  |  |  |  |  | index => $indices, | 
| 183 |  |  |  |  |  |  | body => $settings, | 
| 184 |  |  |  |  |  |  | ); | 
| 185 |  |  |  |  |  |  |  | 
| 186 | 0 |  |  |  |  |  | return $self->_es->indices->put_settings(%args); | 
| 187 |  |  |  |  |  |  | } | 
| 188 |  |  |  |  |  |  |  | 
| 189 |  |  |  |  |  |  | sub reset_indices_rack { | 
| 190 | 0 |  |  | 0 | 0 |  | my $self = shift; | 
| 191 | 0 |  |  |  |  |  | my ($indices) = @_; | 
| 192 |  |  |  |  |  |  |  | 
| 193 | 0 | 0 |  |  |  |  | $self->brik_help_run_undef_arg('reset_indices_to_rack', $indices) or return; | 
| 194 |  |  |  |  |  |  |  | 
| 195 | 0 |  |  |  |  |  | my $settings = { | 
| 196 |  |  |  |  |  |  | 'index.routing.allocation.include.node.attr.rack' => undef, | 
| 197 |  |  |  |  |  |  | }; | 
| 198 |  |  |  |  |  |  |  | 
| 199 | 0 |  |  |  |  |  | my %args = ( | 
| 200 |  |  |  |  |  |  | index => $indices, | 
| 201 |  |  |  |  |  |  | body => $settings, | 
| 202 |  |  |  |  |  |  | ); | 
| 203 |  |  |  |  |  |  |  | 
| 204 | 0 |  |  |  |  |  | return $self->_es->indices->put_settings(%args); | 
| 205 |  |  |  |  |  |  | } | 
| 206 |  |  |  |  |  |  |  | 
| 207 |  |  |  |  |  |  | # | 
| 208 |  |  |  |  |  |  | # set number of replicas to 0 | 
| 209 |  |  |  |  |  |  | # | 
| 210 |  |  |  |  |  |  | sub remove_indices_replicas { | 
| 211 | 0 |  |  | 0 | 0 |  | my $self = shift; | 
| 212 | 0 |  |  |  |  |  | my ($indices) = @_; | 
| 213 |  |  |  |  |  |  |  | 
| 214 | 0 | 0 |  |  |  |  | $self->brik_help_run_undef_arg('remove_indices_replicas', $indices) | 
| 215 |  |  |  |  |  |  | or return; | 
| 216 |  |  |  |  |  |  |  | 
| 217 | 0 |  |  |  |  |  | my $settings = { | 
| 218 |  |  |  |  |  |  | 'index.number_of_replicas' => 0, | 
| 219 |  |  |  |  |  |  | }; | 
| 220 |  |  |  |  |  |  |  | 
| 221 | 0 |  |  |  |  |  | my %args = ( | 
| 222 |  |  |  |  |  |  | index => $indices, | 
| 223 |  |  |  |  |  |  | body => $settings, | 
| 224 |  |  |  |  |  |  | ); | 
| 225 |  |  |  |  |  |  |  | 
| 226 | 0 |  |  |  |  |  | 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 | 0 |  |  | 0 | 0 |  | my $self = shift; | 
| 236 | 0 |  |  |  |  |  | my ($indices) = @_; | 
| 237 |  |  |  |  |  |  |  | 
| 238 | 0 | 0 |  |  |  |  | $self->brik_help_run_undef_arg('forcemerge_indices', $indices) | 
| 239 |  |  |  |  |  |  | or return; | 
| 240 |  |  |  |  |  |  |  | 
| 241 |  |  |  |  |  |  | # Add optional argument: "index.codec": "best_compression" | 
| 242 |  |  |  |  |  |  |  | 
| 243 | 0 |  |  |  |  |  | my %args = ( | 
| 244 |  |  |  |  |  |  | index => $indices, | 
| 245 |  |  |  |  |  |  | max_num_segments => 1, | 
| 246 |  |  |  |  |  |  | #wait_for_merge => 'false', # argument doesn't exist in ES. | 
| 247 |  |  |  |  |  |  | ); | 
| 248 |  |  |  |  |  |  |  | 
| 249 | 0 |  |  |  |  |  | return $self->_es->indices->forcemerge(%args); | 
| 250 |  |  |  |  |  |  | } | 
| 251 |  |  |  |  |  |  |  | 
| 252 |  |  |  |  |  |  | sub check_forcemerge_indices { | 
| 253 | 0 |  |  | 0 | 0 |  | my $self = shift; | 
| 254 | 0 |  |  |  |  |  | my ($indices) = @_; | 
| 255 |  |  |  |  |  |  |  | 
| 256 | 0 | 0 |  |  |  |  | $self->brik_help_run_undef_arg('forcemerge_indices', $indices) | 
| 257 |  |  |  |  |  |  | or return; | 
| 258 |  |  |  |  |  |  |  | 
| 259 | 0 |  |  |  |  |  | my %args = ( | 
| 260 |  |  |  |  |  |  | index => $indices, | 
| 261 |  |  |  |  |  |  | max_num_segments => undef, | 
| 262 |  |  |  |  |  |  | ); | 
| 263 |  |  |  |  |  |  |  | 
| 264 | 0 |  |  |  |  |  | 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 | 0 |  |  | 0 | 0 |  | my $self = shift; | 
| 278 | 0 |  |  |  |  |  | my ($index, $size, $rack) = @_; | 
| 279 |  |  |  |  |  |  |  | 
| 280 | 0 | 0 |  |  |  |  | $self->brik_help_run_undef_arg('shrink_index', $index) or return; | 
| 281 | 0 | 0 |  |  |  |  | $self->brik_help_run_undef_arg('shrink_index', $size) or return; | 
| 282 |  |  |  |  |  |  |  | 
| 283 | 0 |  |  |  |  |  | my $target = "$index.shrink"; | 
| 284 |  |  |  |  |  |  |  | 
| 285 | 0 |  |  |  |  |  | my $body = { | 
| 286 |  |  |  |  |  |  | settings => { | 
| 287 |  |  |  |  |  |  | 'index.number_of_shards' => $size, | 
| 288 |  |  |  |  |  |  | }, | 
| 289 |  |  |  |  |  |  | #aliases => { | 
| 290 |  |  |  |  |  |  | #}, | 
| 291 |  |  |  |  |  |  | }; | 
| 292 |  |  |  |  |  |  |  | 
| 293 | 0 | 0 |  |  |  |  | if (defined($rack)) { | 
| 294 | 0 |  |  |  |  |  | $body->{settings}{'index.routing.allocation.require.rack'} = $rack; | 
| 295 |  |  |  |  |  |  | } | 
| 296 |  |  |  |  |  |  |  | 
| 297 | 0 |  |  |  |  |  | my %args = ( | 
| 298 |  |  |  |  |  |  | index => $index, | 
| 299 |  |  |  |  |  |  | target => $target, | 
| 300 |  |  |  |  |  |  | body => $body, | 
| 301 |  |  |  |  |  |  | #copy_settings => 'true',  # Does not work yet. | 
| 302 |  |  |  |  |  |  | ); | 
| 303 |  |  |  |  |  |  |  | 
| 304 | 0 |  |  |  |  |  | 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 | 0 |  |  |  |  |  | return $r; | 
| 312 |  |  |  |  |  |  | } | 
| 313 |  |  |  |  |  |  |  | 
| 314 |  |  |  |  |  |  | 1; | 
| 315 |  |  |  |  |  |  |  | 
| 316 |  |  |  |  |  |  | __END__ |