| 1 | package ShardedKV::Storage::ReplicatedRest; | ||
| 2 | |||
| 3 | 1 | ▶ | use ShardedKV::Storage::Rest; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use Moose; | ||
| 9 | |||
| 10 | with 'ShardedKV::Storage'; | ||
| 11 | |||
| 12 | has 'urls' => ( | ||
| 13 | is => 'ro', | ||
| 14 | isa => 'ArrayRef[Str]', | ||
| 15 | required => 1 | ||
| 16 | ); | ||
| 17 | |||
| 18 | has 'endpoints' => ( | ||
| 19 | is => 'ro', | ||
| 20 | isa => 'ArrayRef[ShardedKV::Storage::Rest]', | ||
| 21 | required => 0, | ||
| 22 | lazy => 1, | ||
| 23 | builder => '_build_endpoints' | ||
| 24 | ); | ||
| 25 | |||
| 26 | has 'max_failures' => ( | ||
| 27 | is => 'ro', | ||
| 28 | isa => 'Int', | ||
| 29 | required => 0, | ||
| 30 | default => 0 | ||
| 31 | ); | ||
| 32 | |||
| 33 | sub _build_endpoints { | ||
| 34 | my $self = shift; | ||
| 35 | $self->{endpoints} = []; | ||
| 36 | push @{$self->{endpoints}}, ShardedKV::Storage::Rest->new(url => $_) for @{$self->urls}; | ||
| 37 | return $self->{endpoints}; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub _maybe_use_endpoint { | ||
| 41 | my ($self, $endpoint) = @_; | ||
| 42 | |||
| 43 | my $state = $self->{endpoint_states}->{$endpoint}; | ||
| 44 | |||
| 45 | if (!defined($state) || !$state->{state}) { | ||
| 46 | $state->{state} = 'ok'; | ||
| 47 | $self->{endpoint_states}->{$endpoint} = $state; | ||
| 48 | return $endpoint; | ||
| 49 | } | ||
| 50 | |||
| 51 | if ($state->{state} eq 'ok' || $state->{state} eq 'warn') { | ||
| 52 | return $endpoint; | ||
| 53 | } | ||
| 54 | |||
| 55 | if ($state->{state} eq 'retry') { | ||
| 56 | # this state transition "shouldn't happen" | ||
| 57 | warn "Bad state transition maybe_use(retry)"; | ||
| 58 | return $endpoint; | ||
| 59 | } | ||
| 60 | |||
| 61 | if ($state->{state} eq 'fail') { | ||
| 62 | if ($state->{retry_after} < time) { | ||
| 63 | $state->{state} = 'retry'; | ||
| 64 | return $endpoint; | ||
| 65 | } | ||
| 66 | return undef; | ||
| 67 | } | ||
| 68 | |||
| 69 | warn "unknown state: " . $state->{state}; | ||
| 70 | |||
| 71 | } | ||
| 72 | |||
| 73 | sub _mark_endpoint_as_failed { | ||
| 74 | my ($self, $endpoint) = @_; | ||
| 75 | |||
| 76 | my $state = $self->{endpoint_states}->{$endpoint}; | ||
| 77 | |||
| 78 | if ($state->{state} eq 'ok') { | ||
| 79 | $state->{state} = 'warn'; | ||
| 80 | $state->{failure_count} = 1; | ||
| 81 | } elsif ($state->{state} eq 'warn') { | ||
| 82 | $state->{failure_count}++; | ||
| 83 | if ($state->{failure_count} == 5) { | ||
| 84 | $state->{state} = 'fail'; | ||
| 85 | $state->{retry_delay} = 1; | ||
| 86 | $state->{retry_after} = time + $state->{retry_delay}; | ||
| 87 | } | ||
| 88 | } elsif ($state->{state} eq 'fail') { | ||
| 89 | # this state transition "shouldn't happen" | ||
| 90 | warn "Bad state transition failed(fail)"; | ||
| 91 | } elsif ($state->{state} eq 'retry') { | ||
| 92 | $state->{state} = 'fail'; | ||
| 93 | $state->{retry_delay} *= 2; | ||
| 94 | if ($state->{retry_delay} > 60) { | ||
| 95 | $state->{retry_delay} = 60; | ||
| 96 | } | ||
| 97 | $state->{retry_after} = time + $state->{retry_delay}; | ||
| 98 | } else { | ||
| 99 | warn "unknown state: " . $state->{state}; | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | sub _mark_endpoint_as_success { | ||
| 104 | my ($self, $endpoint) = @_; | ||
| 105 | $self->{endpoint_states}->{$endpoint}->{state} = 'ok'; | ||
| 106 | } | ||
| 107 | |||
| 108 | sub get { | ||
| 109 | my ($self, $key) = @_; | ||
| 110 | |||
| 111 | for my $endpoint (@{$self->endpoints}) { | ||
| 112 | next unless $self->_maybe_use_endpoint($endpoint); | ||
| 113 | my $data; | ||
| 114 | my $caught_exception; | ||
| 115 | eval { | ||
| 116 | $data = $endpoint->get($key); | ||
| 117 | 1; | ||
| 118 | } or do { | ||
| 119 | my $err = $@ || "zombie error"; | ||
| 120 | $caught_exception = 1; | ||
| 121 | warn "caught exception during get($key): $err"; | ||
| 122 | }; | ||
| 123 | |||
| 124 | if ($caught_exception) { | ||
| 125 | $self->_mark_endpoint_as_failed($endpoint); | ||
| 126 | } else { | ||
| 127 | $self->_mark_endpoint_as_success($endpoint); | ||
| 128 | } | ||
| 129 | |||
| 130 | if (defined($data)) { | ||
| 131 | return $data; | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 | return undef; | ||
| 136 | } | ||
| 137 | |||
| 138 | sub set { | ||
| 139 | my ($self, $key, $value) = @_; | ||
| 140 | |||
| 141 | my $failures = 0; | ||
| 142 | |||
| 143 | for my $endpoint (@{$self->endpoints}) { | ||
| 144 | if (!$self->_maybe_use_endpoint($endpoint)) { | ||
| 145 | $failures++; | ||
| 146 | next; | ||
| 147 | } | ||
| 148 | my $r; | ||
| 149 | my $caught_exception; | ||
| 150 | eval { | ||
| 151 | $r = $endpoint->set($key, $value); | ||
| 152 | 1; | ||
| 153 | } or do { | ||
| 154 | # warn -- error setting key=$key | ||
| 155 | $caught_exception = 1; | ||
| 156 | $r = 0; | ||
| 157 | }; | ||
| 158 | |||
| 159 | if ($caught_exception) { | ||
| 160 | $self->_mark_endpoint_as_failed($endpoint); | ||
| 161 | } else { | ||
| 162 | $self->_mark_endpoint_as_success($endpoint); | ||
| 163 | } | ||
| 164 | |||
| 165 | if (!$r) { | ||
| 166 | $failures++; | ||
| 167 | } | ||
| 168 | } | ||
| 169 | |||
| 170 | return ($failures > $self->max_failures) ? 0 : 1; | ||
| 171 | } | ||
| 172 | |||
| 173 | sub delete { | ||
| 174 | my ($self, $key) = @_; | ||
| 175 | |||
| 176 | |||
| 177 | my $failures = 0; | ||
| 178 | |||
| 179 | for my $endpoint (@{$self->endpoints}) { | ||
| 180 | if (!$self->_maybe_use_endpoint($endpoint)) { | ||
| 181 | $failures++; | ||
| 182 | next; | ||
| 183 | } | ||
| 184 | my $r; | ||
| 185 | my $caught_exception; | ||
| 186 | eval { | ||
| 187 | $r = $endpoint->delete($key); | ||
| 188 | 1; | ||
| 189 | } or do { | ||
| 190 | # warn -- error deleting key=$key | ||
| 191 | $caught_exception = 1; | ||
| 192 | $r = 0; | ||
| 193 | }; | ||
| 194 | |||
| 195 | if ($caught_exception) { | ||
| 196 | $self->_mark_endpoint_as_failed($endpoint); | ||
| 197 | } else { | ||
| 198 | $self->_mark_endpoint_as_success($endpoint); | ||
| 199 | } | ||
| 200 | |||
| 201 | if (!$r) { | ||
| 202 | $failures++; | ||
| 203 | } | ||
| 204 | } | ||
| 205 | |||
| 206 | return ($failures > $self->max_failures) ? 0 : 1; | ||
| 207 | } | ||
| 208 | |||
| 209 | sub reset_connection { | ||
| 210 | my ($self, $key) = @_; | ||
| 211 | |||
| 212 | my $failures = 0; | ||
| 213 | |||
| 214 | for my $endpoint (@{$self->endpoints}) { | ||
| 215 | if (!$self->_maybe_use_endpoint($endpoint)) { | ||
| 216 | $failures++; | ||
| 217 | next; | ||
| 218 | } | ||
| 219 | my $r; | ||
| 220 | my $caught_exception; | ||
| 221 | eval { | ||
| 222 | $r = $endpoint->reset_connection($key); | ||
| 223 | 1; | ||
| 224 | } or do { | ||
| 225 | $r = 0; | ||
| 226 | $caught_exception = 1; | ||
| 227 | # warn -- error resetting connection for key=$key | ||
| 228 | }; | ||
| 229 | |||
| 230 | if ($caught_exception) { | ||
| 231 | $self->_mark_endpoint_as_failed($endpoint); | ||
| 232 | } else { | ||
| 233 | $self->_mark_endpoint_as_success($endpoint); | ||
| 234 | } | ||
| 235 | |||
| 236 | if (!$r) { | ||
| 237 | $failures++; | ||
| 238 | } | ||
| 239 | } | ||
| 240 | return ($failures > $self->max_failures) ? 0 : 1; | ||
| 241 | } | ||
| 242 | |||
| 243 | 1; | ||