script/myriad-migrate-rpc-streams.pl untested

filter: Statementstmt 0.0% 0 / 69 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 11 Podpod n/a totaltotal 0.0% 0 / 91 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/bin/env perl
2  
3 # Basic usage: myriad-migrate-rpc-streams.pl --uri <redis_uri> --service <service to migrate>
4 # This script will move messages from the old single rpc stream into multiple streams
5 # it won't delete the original stream.
6  
7 use strict;
8 use warnings;
9  
10 use Getopt::Long;
11  
12 use IO::Async::Loop;
13 use Future::AsyncAwait;
14  
15 use Net::Async::Redis;
16 use Net::Async::Redis::Cluster;
17  
18 use URI;
19 use Syntax::Keyword::Try;
20 use Log::Any qw($log);
21 use Log::Any::Adapter qw(Stderr), log_level => 'info';
22  
23 my $uri;
24 my $is_cluster = 0;
25 my $service_name;
26  
27 GetOptions(
28 'uri=s' => \$uri,
29 'service=s' => \$service_name,
30 'cluster' => \$is_cluster,
31 ) or die "usage $0 --uri <redis_uri> --service <service_name> [--cluster]";
32  
33 $uri = URI->new($uri);
34 $service_name =~ s/::/./;
35 $service_name = lc($service_name);
36  
37 my $loop = IO::Async::Loop->new();
38  
39 my $redis;
40  
41 if ($is_cluster) {
42 $loop->add(
43 $redis = Net::Async::Redis::Cluster->new
44 );
45 await $redis->bootstrap(
46 host => $uri->host,
47 port => $uri->port,
48 );
49 } else {
50 $loop->add(
51 $redis = Net::Async::Redis->new(uri => $uri)
52 );
53 }
54  
55 my $old_rpc_stream = "myriad.service.$service_name/rpc";
56 my $new_rpc_stream_prefix = "myriad.service.$service_name.rpc/";
57  
58 unless ( await $redis->exists($old_rpc_stream) ) {
59 $log->fatalf('Cannot find old rpc stream for service %s', $service_name);
60 exit 1;
61 }
62  
63 try {
64 await $redis->xgroup('CREATE', $old_rpc_stream, 'rpc_migration', 0);
65 } catch ($e) {
66 if ($e =~ /BUSYGROUP/) {
67 $log->fatalf('Got a busygroup for stream %s are you running another migration?', $old_rpc_stream);
68 } else {
69 $log->fatalf('Failed to create group for stream %s - %s', $old_rpc_stream, $e);
70 }
71 exit 1;
72 }
73  
74 try {
75 while (my ($batch) = await $redis->xreadgroup(
76 GROUP => 'rpc_migration', 'migrator',
77 COUNT => 50,
78 STREAMS => ($old_rpc_stream, '>'),
79 )) {
80 last unless $batch->@*;
81 my ($stream, $messages) = $batch->[0]->@*;
82 for my $message ($messages->@*) {
83 my ($id, $info) = $message->@*;
84 my %args = $info->@*;
85 my $stream = $new_rpc_stream_prefix . $args{rpc};
86  
87 await $redis->xadd(
88 $stream => '*',
89 %args
90 );
91 }
92 }
93 } catch ($e) {
94 $log->errorf('Error while migrating streams - %s', $e);
95 }
96  
97 await $redis->xgroup('destroy', $old_rpc_stream, 'rpc_migration');