-
Notifications
You must be signed in to change notification settings - Fork 195
/
Copy pathMapping.pm
687 lines (554 loc) · 20.1 KB
/
Mapping.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
package MetaCPAN::Script::Mapping;
use Moose;
use Cpanel::JSON::XS qw( decode_json );
use DateTime ();
use Log::Contextual qw( :log );
use MetaCPAN::Script::Mapping::CPAN::Author ();
use MetaCPAN::Script::Mapping::CPAN::Distribution ();
use MetaCPAN::Script::Mapping::CPAN::Favorite ();
use MetaCPAN::Script::Mapping::CPAN::File ();
use MetaCPAN::Script::Mapping::CPAN::Mirror ();
use MetaCPAN::Script::Mapping::CPAN::Permission ();
use MetaCPAN::Script::Mapping::CPAN::Package ();
use MetaCPAN::Script::Mapping::CPAN::Rating ();
use MetaCPAN::Script::Mapping::CPAN::Release ();
use MetaCPAN::Script::Mapping::DeployStatement ();
use MetaCPAN::Script::Mapping::User::Account ();
use MetaCPAN::Script::Mapping::User::Identity ();
use MetaCPAN::Script::Mapping::User::Session ();
use MetaCPAN::Script::Mapping::Contributor ();
use MetaCPAN::Script::Mapping::Cover ();
use MetaCPAN::Types::TypeTiny qw( Bool Str );
use constant {
EXPECTED => 1,
NOT_EXPECTED => 0,
};
with 'MetaCPAN::Role::Script', 'MooseX::Getopt';
has arg_deploy_mapping => (
init_arg => 'delete',
is => 'ro',
isa => Bool,
default => 0,
documentation => 'delete index if it exists already',
);
has arg_list_types => (
init_arg => 'list_types',
is => 'ro',
isa => Bool,
default => 0,
documentation => 'list available index type names',
);
has arg_cluster_info => (
init_arg => 'show_cluster_info',
is => 'ro',
isa => Bool,
default => 0,
documentation => 'show basic info about cluster, indices and aliases',
);
has arg_create_index => (
init_arg => 'create_index',
is => 'ro',
isa => Str,
default => "",
documentation => 'create a new empty index (copy mappings)',
);
has arg_update_index => (
init_arg => 'update_index',
is => 'ro',
isa => Str,
default => "",
documentation => 'update existing index (add mappings)',
);
has patch_mapping => (
is => 'ro',
isa => Str,
default => "{}",
documentation => 'type mapping patches',
);
has skip_existing_mapping => (
is => 'ro',
isa => Bool,
default => 0,
documentation => 'do NOT copy mappings other than patch_mapping',
);
has copy_to_index => (
is => 'ro',
isa => Str,
default => "",
documentation => 'index to copy type to',
);
has arg_copy_type => (
init_arg => 'copy_type',
is => 'ro',
isa => Str,
default => "",
documentation => 'type to copy',
);
has copy_query => (
is => 'ro',
isa => Str,
default => "",
documentation => 'match query (default: monthly time slices, '
. ' if provided must be a valid json query OR "match_all")',
);
has reindex => (
is => 'ro',
isa => Bool,
default => 0,
documentation => 'reindex data from source index for exact mapping types',
);
has arg_delete_index => (
init_arg => 'delete_index',
is => 'ro',
isa => Str,
default => "",
documentation => 'delete an existing index',
);
has delete_from_type => (
is => 'ro',
isa => Str,
default => "",
documentation => 'delete data from an existing type',
);
sub run {
my $self = shift;
if ( $self->await ) {
$self->delete_index if $self->arg_delete_index;
$self->update_index if $self->arg_update_index;
$self->copy_type if $self->copy_to_index;
$self->empty_type if $self->delete_from_type;
$self->list_types if $self->arg_list_types;
if ( $self->arg_deploy_mapping ) {
unless ( $self->deploy_mapping ) {
$self->print_error("Indices Re-creation has failed!");
$self->exit_code(1);
}
}
if ( $self->arg_cluster_info ) {
$self->check_health;
$self->show_info;
}
}
# The run() method is expected to communicate Success to the superior execution level
return ( $self->exit_code == 0 );
}
sub _check_index_exists {
my ( $self, $name, $expected ) = @_;
my $exists = $self->es->indices->exists( index => $name );
if ( $exists and !$expected ) {
log_error {"Index already exists: $name"};
exit 0;
}
if ( !$exists and $expected ) {
log_error {"Index doesn't exists: $name"};
exit 0;
}
}
sub delete_index {
my $self = shift;
my $name = $self->arg_delete_index;
$self->_check_index_exists( $name, EXPECTED );
$self->are_you_sure("Index $name will be deleted !!!");
$self->_delete_index($name);
}
sub _delete_index {
my ( $self, $name ) = @_;
log_info {"Deleting index: $name"};
$self->es->indices->delete( index => $name );
}
sub update_index {
my $self = shift;
my $name = $self->arg_update_index;
$self->_check_index_exists( $name, EXPECTED );
$self->are_you_sure("Index $name will be updated !!!");
die "update_index requires patch_mapping\n"
unless $self->patch_mapping;
my $patch_mapping = decode_json $self->patch_mapping;
my @patch_types = sort keys %{$patch_mapping};
my $dep = $self->index->deployment_statement;
my $existing_mapping = delete $dep->{mappings};
my $mapping = +{ map { $_ => $patch_mapping->{$_} } @patch_types };
log_info {"Updating mapping for index: $name"};
for my $type ( sort keys %{$mapping} ) {
log_info {"Adding mapping to index: $type"};
$self->es->indices->put_mapping(
index => $self->index->name,
type => $type,
body => { $type => $mapping->{$type} },
);
}
log_info {"Done."};
}
sub create_index {
my $self = shift;
my $dst_idx = $self->arg_create_index;
$self->_check_index_exists( $dst_idx, NOT_EXPECTED );
my $patch_mapping = decode_json $self->patch_mapping;
my @patch_types = sort keys %{$patch_mapping};
my $dep = $self->index->deployment_statement;
delete $dep->{mappings};
my $mapping = +{};
# create the new index with the copied settings
log_info {"Creating index: $dst_idx"};
$self->es->indices->create( index => $dst_idx, body => $dep );
# override with new type mapping
if ( $self->patch_mapping ) {
for my $type (@patch_types) {
log_info {"Patching mapping for type: $type"};
$mapping->{$type} = $patch_mapping->{$type};
}
}
# add the mappings to the index
for my $type ( sort keys %{$mapping} ) {
log_info {"Adding mapping to index: $type"};
$self->es->indices->put_mapping(
index => $dst_idx,
type => $type,
body => { $type => $mapping->{$type} },
);
}
# copy the data to the non-altered types
if ( $self->reindex ) {
for my $type (
grep { !exists $patch_mapping->{$_} }
sort keys %{$mapping}
)
{
log_info {"Re-indexing data to index $dst_idx from type: $type"};
$self->copy_type( $dst_idx, $type );
}
}
log_info {
"Done. you can now fill the data for the altered types: ("
. join( ',', @patch_types ) . ")"
}
if @patch_types;
}
sub copy_type {
my ( $self, $index, $type ) = @_;
$index //= $self->copy_to_index;
$self->_check_index_exists( $index, EXPECTED );
$type //= $self->arg_copy_type;
$type or die "can't copy without a type\n";
my $arg_query = $self->copy_query;
my $query
= $arg_query eq 'match_all'
? +{ match_all => {} }
: undef;
if ( $arg_query and !$query ) {
eval {
$query = decode_json $arg_query;
1;
} or do {
my $err = $@ || 'zombie error';
die $err;
};
}
return $self->_copy_slice( $query, $index, $type ) if $query;
# else ... do copy by monthly slices
my $dt = DateTime->new( year => 1994, month => 1 );
my $end_time = DateTime->now()->add( months => 1 );
while ( $dt < $end_time ) {
my $gte = $dt->strftime("%Y-%m");
$dt->add( months => 1 );
my $lt = $dt->strftime("%Y-%m");
my $q = +{ range => { date => { gte => $gte, lt => $lt } } };
log_info {"copying data for month: $gte"};
eval {
$self->_copy_slice( $q, $index, $type );
1;
} or do {
my $err = $@ || 'zombie error';
warn $err;
};
}
}
sub _copy_slice {
my ( $self, $query, $index, $type ) = @_;
my $scroll = $self->es->scroll_helper(
search_type => 'scan',
size => 250,
scroll => '10m',
index => $self->index->name,
type => $type,
body => {
query => {
filtered => {
query => $query
}
}
},
);
my $bulk = $self->es->bulk_helper(
index => $index,
type => $type,
max_count => 500,
);
while ( my $search = $scroll->next ) {
$bulk->create(
{
id => $search->{_id},
source => $search->{_source}
}
);
}
$bulk->flush;
}
sub empty_type {
my $self = shift;
my $type = $self->delete_from_type;
log_info {"Emptying type: $type"};
my $bulk = $self->es->bulk_helper(
index => $self->index->name,
type => $type,
max_count => 500,
);
my $scroll = $self->es->scroll_helper(
search_type => 'scan',
size => 250,
scroll => '10m',
index => $self->index->name,
type => $type,
body => { query => { match_all => {} } },
);
my @ids;
while ( my $search = $scroll->next ) {
push @ids => $search->{_id};
log_debug { "deleting id=" . $search->{_id} };
if ( @ids == 500 ) {
$bulk->delete_ids(@ids);
@ids = ();
}
}
$bulk->delete_ids(@ids);
$bulk->flush;
}
sub list_types {
my $self = shift;
print "$_\n" for sort keys %{ $self->index->types };
}
sub show_info {
my $self = $_[0];
my $info_rs = {
'cluster_info' => \%{ $self->cluster_info },
'indices_info' => \%{ $self->indices_info },
'aliases_info' => \%{ $self->aliases_info }
};
print JSON->new->utf8->pretty->encode($info_rs);
}
sub deploy_mapping {
my $self = shift;
my $cpan_index = 'cpan_v1_01';
my $imappingok = 0;
$self->are_you_sure(
'this will delete EVERYTHING and re-create the (empty) indexes');
my %mappings = (
$cpan_index => {
author =>
decode_json(MetaCPAN::Script::Mapping::CPAN::Author::mapping),
distribution =>
decode_json(MetaCPAN::Script::Mapping::CPAN::Distribution::mapping
),
favorite =>
decode_json(MetaCPAN::Script::Mapping::CPAN::Favorite::mapping
),
file =>
decode_json(MetaCPAN::Script::Mapping::CPAN::File::mapping),
mirror =>
decode_json(MetaCPAN::Script::Mapping::CPAN::Mirror::mapping),
permission =>
decode_json(MetaCPAN::Script::Mapping::CPAN::Permission::mapping
),
package =>
decode_json(MetaCPAN::Script::Mapping::CPAN::Package::mapping
),
rating =>
decode_json(MetaCPAN::Script::Mapping::CPAN::Rating::mapping),
release =>
decode_json(MetaCPAN::Script::Mapping::CPAN::Release::mapping
),
},
user => {
account =>
decode_json(MetaCPAN::Script::Mapping::User::Account::mapping
),
identity =>
decode_json(MetaCPAN::Script::Mapping::User::Identity::mapping
),
session =>
decode_json(MetaCPAN::Script::Mapping::User::Session::mapping
),
},
contributor => {
contributor =>
decode_json(MetaCPAN::Script::Mapping::Contributor::mapping),
},
cover => {
cover => decode_json(MetaCPAN::Script::Mapping::Cover::mapping),
},
);
my $deploy_statement
= decode_json(MetaCPAN::Script::Mapping::DeployStatement::mapping);
my $es = $self->es;
# recreate the indices and apply the mapping
for my $idx ( sort keys %mappings ) {
$self->_delete_index($idx) if $es->indices->exists( index => $idx );
log_info {"Creating index: $idx"};
$es->indices->create( index => $idx, body => $deploy_statement );
for my $type ( sort keys %{ $mappings{$idx} } ) {
log_info {"Adding mapping: $idx/$type"};
$es->indices->put_mapping(
index => $idx,
type => $type,
body => { $type => $mappings{$idx}{$type} },
);
}
}
# create aliases
my %aliases = ( 'cpan' => $cpan_index );
for my $alias ( sort keys %aliases ) {
log_info {"Creating alias: '$alias' -> '$aliases{$alias}'"};
$es->indices->put_alias(
index => $aliases{$alias},
name => $alias,
);
}
$self->check_health(1);
$imappingok = $self->verify_mapping( \%mappings, \%aliases );
# done
log_info {"Done."};
return $imappingok;
}
sub verify_mapping {
my ( $self, $rmappings, $raliases ) = @_;
my $ihealth = 0;
if ( defined $rmappings && ref $rmappings eq 'HASH' ) {
my $rhealth = undef;
$ihealth = 1;
for my $idx ( sort keys %$rmappings ) {
$rhealth = $self->indices_info->{$idx};
if ( defined $rhealth ) {
if ( $rhealth->{'health'} eq 'red' ) {
log_error {
"Broken index: $idx (state '"
. $rhealth->{'health'} . "')"
};
$ihealth = 0;
}
else {
log_info {
"Healthy index: $idx (state '"
. $rhealth->{'health'} . "')"
};
}
}
else {
log_error {"Missing index: $idx"};
$ihealth = 0;
}
}
}
if ( defined $raliases && ref $raliases eq 'HASH' ) {
my $ralias = undef;
for my $name ( sort keys %$raliases ) {
$ralias = $self->aliases_info->{$name};
if ( defined $ralias ) {
if ( $ralias->{'index'} eq $raliases->{$name} ) {
log_info {
"Correct alias: $name (index '"
. $ralias->{'index'} . "')"
};
}
else {
log_error {
"Broken alias: $name (index '"
. $ralias->{'index'} . "')"
};
$ihealth = 0;
}
}
else {
log_error {"Missing alias: $name"};
$ihealth = 0;
}
}
}
else {
$ihealth = 0 if ( scalar( keys %{ $self->aliases_info } ) == 0 );
}
return $ihealth;
}
__PACKAGE__->meta->make_immutable;
1;
__END__
=pod
=head1 NAME
MetaCPAN::Script::Mapping - Script to set the index and mapping the types
=head1 SYNOPSIS
# bin/metacpan mapping --show_cluster_info # show basic info about the cluster, indices and aliases
# bin/metacpan mapping --delete
# bin/metacpan mapping --list_types
# bin/metacpan mapping --delete_index xxx
# bin/metacpan mapping --create_index xxx --reindex
# bin/metacpan mapping --create_index xxx --reindex --patch_mapping '{"distribution":{"dynamic":"false","properties":{"name":{"index":"not_analyzed","ignore_above":2048,"type":"string"},"river":{"properties":{"total":{"type":"integer"},"immediate":{"type":"integer"},"bucket":{"type":"integer"}},"dynamic":"true"},"bugs":{"properties":{"rt":{"dynamic":"true","properties":{"rejected":{"type":"integer"},"closed":{"type":"integer"},"open":{"type":"integer"},"active":{"type":"integer"},"patched":{"type":"integer"},"source":{"type":"string","ignore_above":2048,"index":"not_analyzed"},"resolved":{"type":"integer"},"stalled":{"type":"integer"},"new":{"type":"integer"}}},"github":{"dynamic":"true","properties":{"active":{"type":"integer"},"open":{"type":"integer"},"closed":{"type":"integer"},"source":{"type":"string","index":"not_analyzed","ignore_above":2048}}}},"dynamic":"true"}}}}'
# bin/metacpan mapping --create_index xxx --patch_mapping '{...mapping...}' --skip_existing_mapping
# bin/metacpan mapping --update_index xxx --patch_mapping '{...mapping...}'
# bin/metacpan mapping --copy_to_index xxx --copy_type release
# bin/metacpan mapping --copy_to_index xxx --copy_type release --copy_query '{"range":{"date":{"gte":"2016-01","lt":"2017-01"}}}'
# bin/metacpan mapping --delete_from_type xxx # empty the type
=head1 DESCRIPTION
This is the index mapping handling script.
Used rarely, but carries the most important task of setting
the index and mapping the types.
=head1 OPTIONS
This Script accepts the following options
=over 4
=item Option C<--show_cluster_info>
This option makes the Script show basic information about the I<ElasticSearch> Cluster
and its indices and aliases.
This information has to be collected with the C<MetaCPAN::Role::Script::check_health()> Method.
On Script start-up it is empty.
bin/metacpan mapping --show_cluster_info
See L<Method C<MetaCPAN::Role::Script::check_health()>>
=item Option C<--delete>
This option makes the Script delete all indices configured in the project and re-create them emtpy.
It verifies the index integrity of the indices and aliases calling the methods
C<MetaCPAN::Role::Script::check_health()> and C<verify_mapping()>.
If the C<verify_mapping()> Method fails it exits the Script
with B<Exit Code> C< 1 >.
bin/metacpan mapping --delete
See L<Method C<deploy_mapping()>>
See L<Method C<verify_mapping()>>
See L<Method C<MetaCPAN::Role::Script::check_health()>>
=back
=head1 METHODS
This Package provides the following methods
=over 4
=item C<deploy_mapping()>
Deletes and re-creates the indices and aliases defined in the Project.
The user will be requested for manual confirmation on the command line before the elemination.
The integrity of the indices and aliases will be checked with the C<verify_mapping()> Method.
On successful creation it returns C< 1 >, otherwise it returns C< 0 >.
B<Returns:> It returns C< 1 > when the indices and aliases are created and verified as correct.
Otherwise it returns C< 0 >.
B<Exceptions:> It can throw exceptions when the connection to I<ElasticSearch> fails
or there is any issue in any I<ElasticSearch> Request run by the Script.
See L<Option C<--delete>>
See L<Method C<verify_mapping()>>
See L<Method C<MetaCPAN::Role::Script::check_health()>>
=item C<verify_mapping( \%indices, \%aliases )>
Checks the defined indices and aliases against the actually in the I<ElasticSearch> Cluster
existing indices and aliases which must have been requested with
the C<MetaCPAN::Role::Script::check_health()> Method.
B<Parameters:>
C<\%indices> - Reference to a hash that defines the indices required for the Project.
C<\%aliases> - Reference to a hash that defines the aliases required for the Project.
B<Returns:> It returns C< 1 > when the indices and aliases are created and verified as correct.
Otherwise it returns C< 0 >.
B<Exceptions:> It can throw exceptions when the connection to I<ElasticSearch> fails
or there is any issue in any I<ElasticSearch> Request run by the Script.
See L<Option C<--delete>>
See L<Method C<verify_mapping()>>
See L<Method C<MetaCPAN::Role::Script::check_health()>>
=back
=cut