MT#63997 make underrun lock on requests optional

* a new ngcp_panel option $c->config->{api}{underrun_lock_on_request}
  is in place and if disabled - the underrun lock
  logic on GET contracts,customers,subscribers,subscriberpreferences,
  and POST subscribers is skipped.

Change-Id: I5ea337ce42b3c979fe55b3df3516a19548b64f90
mr14.1
Kirill Solomko 4 months ago
parent 31d07419f2
commit 6e2ca03a6e

@ -94,12 +94,19 @@ sub GET :Allow {
my $now = NGCP::Panel::Utils::DateTime::current_local;
my $contracts_rs = $self->item_rs($c,0,$now);
(my $total_count, $contracts_rs, my $contracts_rows) = $self->paginate_order_collection($c, $contracts_rs);
my $contracts = NGCP::Panel::Utils::Contract::acquire_contract_rowlocks(
c => $c,
rs => $contracts_rs,
contract_id_field => 'id',
skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1),
);
my $contracts;
if ($c->config->{api}{underrun_lock_on_request}) {
$contracts = NGCP::Panel::Utils::Contract::acquire_contract_rowlocks(
c => $c,
rs => $contracts_rs,
contract_id_field => 'id',
skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1),
);
} else {
$contracts = [$contracts_rs->all];
}
my (@embedded, @links);
my $form = $self->get_form($c);
$self->expand_prepare_collection($c);
@ -112,7 +119,11 @@ sub GET :Allow {
);
}
$self->expand_collection_fields($c, \@embedded);
$self->delay_commit($c,$guard); #potential db write ops in hal_from
if ($c->config->{api}{underrun_lock_on_request}) {
$self->delay_commit($c,$guard); #potential db write ops in hal_from
}
push @links,
Data::HAL::Link->new(
relation => 'curies',

@ -167,12 +167,19 @@ sub GET :Allow {
my $now = NGCP::Panel::Utils::DateTime::current_local;
my $customers_rs = $self->item_rs($c,$now);
(my $total_count, $customers_rs, my $customers_rows) = $self->paginate_order_collection($c, $customers_rs);
my $customers = NGCP::Panel::Utils::Contract::acquire_contract_rowlocks(
c => $c,
rs => $customers_rs,
contract_id_field => 'id',
skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1),
);
my $customers;
if ($c->config->{api}{underrun_lock_on_request}) {
$customers = NGCP::Panel::Utils::Contract::acquire_contract_rowlocks(
c => $c,
rs => $customers_rs,
contract_id_field => 'id',
skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1),
);
} else {
$customers = [$customers_rs->all];
}
my (@embedded, @links);
my $form = $self->get_form($c);
$self->expand_prepare_collection($c);
@ -184,7 +191,12 @@ sub GET :Allow {
);
}
$self->expand_collection_fields($c, \@embedded);
$self->delay_commit($c,$guard); #potential db write ops in hal_from
if ($c->config->{api}{underrun_lock_on_request}) {
$self->delay_commit($c,$guard); #potential db write ops in hal_from
}
push @links,
Data::HAL::Link->new(
relation => 'curies',

@ -95,22 +95,32 @@ sub GET :Allow {
{
my $subscribers_rs = $self->item_rs($c, "subscribers");
(my $total_count, $subscribers_rs, my $subscribers_rows) = $self->paginate_order_collection($c, $subscribers_rs);
my $subscribers = NGCP::Panel::Utils::Contract::acquire_contract_rowlocks(
c => $c,
rs => $subscribers_rs,
contract_id_field => 'contract_id',
skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1),
);
my $subscribers;
if ($c->config->{api}{underrun_lock_on_request}) {
$subscribers = NGCP::Panel::Utils::Contract::acquire_contract_rowlocks(
c => $c,
rs => $subscribers_rs,
contract_id_field => 'contract_id',
skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1),
);
} else {
$subscribers = [$subscribers_rs->all];
}
my $now = NGCP::Panel::Utils::DateTime::current_local;
my (@embedded, @links, %contract_map);
$self->expand_prepare_collection($c);
for my $subscriber (@$subscribers) {
next unless($subscriber->provisioning_voip_subscriber);
my $contract = $subscriber->contract;
my $balance; $balance = NGCP::Panel::Utils::ProfilePackages::get_contract_balance(c => $c,
contract => $contract,
now => $now) if !exists $contract_map{$contract->id}; #apply underrun lock level
$contract_map{$contract->id} = 1;
if ($c->config->{api}{underrun_lock_on_request}) {
my $contract = $subscriber->contract;
my $balance; $balance = NGCP::Panel::Utils::ProfilePackages::get_contract_balance(c => $c,
contract => $contract,
now => $now) if !exists $contract_map{$contract->id}; #apply underrun lock level
$contract_map{$contract->id} = 1;
}
push @embedded, $self->hal_from_item($c, $subscriber, "subscribers");
push @links, Data::HAL::Link->new(
relation => 'ngcp:'.$self->resource_name,
@ -118,7 +128,11 @@ sub GET :Allow {
);
}
$self->expand_collection_fields($c, \@embedded);
$self->delay_commit($c,$guard);
if ($c->config->{api}{underrun_lock_on_request}) {
$self->delay_commit($c,$guard);
}
push @links,
Data::HAL::Link->new(
relation => 'curies',

@ -59,12 +59,18 @@ sub GET :Allow {
my $subscriber = $self->item_by_id($c, $id, "subscribers");
last unless $self->resource_exists($c, subscriberpreference => $subscriber);
my $balance = NGCP::Panel::Utils::ProfilePackages::get_contract_balance(c => $c,
contract => $subscriber->contract,
skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1),
); #apply underrun lock level
if ($c->config->{api}{underrun_lock_on_request}) {
my $balance = NGCP::Panel::Utils::ProfilePackages::get_contract_balance(c => $c,
contract => $subscriber->contract,
skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1),
); #apply underrun lock level
}
my $hal = $self->hal_from_item($c, $subscriber, "subscribers");
$guard->commit; #potential db write ops in hal_from
if ($c->config->{api}{underrun_lock_on_request}) {
$guard->commit; #potential db write ops in hal_from
}
my $response = HTTP::Response->new(HTTP_OK, undef, HTTP::Headers->new(
(map { # XXX Data::HAL must be able to generate links with multiple relations
@ -109,10 +115,13 @@ sub PATCH :Allow {
my $subscriber = $self->item_by_id($c, $id, "subscribers");
last unless $self->resource_exists($c, subscriberpreferences => $subscriber);
my $balance = NGCP::Panel::Utils::ProfilePackages::get_contract_balance(c => $c,
contract => $subscriber->contract,
skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1),
); #apply underrun lock level
if ($c->config->{api}{underrun_lock_on_request}) {
my $balance = NGCP::Panel::Utils::ProfilePackages::get_contract_balance(c => $c,
contract => $subscriber->contract,
skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1),
); #apply underrun lock level
}
my $old_resource = $self->get_resource($c, $subscriber, "subscribers");
my $resource = $self->apply_patch($c, $old_resource, $json);
@ -167,10 +176,14 @@ sub PUT :Allow {
my $subscriber = $self->item_by_id($c, $id, "subscribers");
last unless $self->resource_exists($c, systemcontact => $subscriber);
my $balance = NGCP::Panel::Utils::ProfilePackages::get_contract_balance(c => $c,
contract => $subscriber->contract,
skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1),
); #apply underrun lock level
if ($c->config->{api}{underrun_lock_on_request}) {
my $balance = NGCP::Panel::Utils::ProfilePackages::get_contract_balance(c => $c,
contract => $subscriber->contract,
skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1),
); #apply underrun lock level
}
my $resource = $self->get_valid_put_data(
c => $c,
id => $id,

@ -322,22 +322,31 @@ sub GET :Allow {
{
my $subscribers_rs = $self->item_rs($c);
(my $total_count, $subscribers_rs, my $subscribers_rows) = $self->paginate_order_collection($c, $subscribers_rs);
my $subscribers = NGCP::Panel::Utils::Contract::acquire_contract_rowlocks(
c => $c,
rs => $subscribers_rs,
contract_id_field => 'contract_id',
skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1),
);
my $subscribers;
if ($c->config->{api}{underrun_lock_on_request}) {
$subscribers = NGCP::Panel::Utils::Contract::acquire_contract_rowlocks(
c => $c,
rs => $subscribers_rs,
contract_id_field => 'contract_id',
skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1),
);
} else {
$subscribers = [$subscribers_rs->all];
}
my $now = NGCP::Panel::Utils::DateTime::current_local;
my (@embedded, @links, %contract_map);
my ($form) = $self->get_form($c);
$self->expand_prepare_collection($c);
for my $subscriber (@$subscribers) {
my $contract = $subscriber->contract;
NGCP::Panel::Utils::ProfilePackages::get_contract_balance(c => $c,
contract => $contract,
now => $now) if !exists $contract_map{$contract->id}; #apply underrun lock level
$contract_map{$contract->id} = 1;
if ($c->config->{api}{underrun_lock_on_request}) {
my $contract = $subscriber->contract;
NGCP::Panel::Utils::ProfilePackages::get_contract_balance(c => $c,
contract => $contract,
now => $now) if !exists $contract_map{$contract->id}; #apply underrun lock level
$contract_map{$contract->id} = 1;
}
my $resource = $self->resource_from_item($c, $subscriber, $form);
push @embedded, $self->hal_from_item($c, $subscriber, $resource, $form);
push @links, Data::HAL::Link->new(
@ -346,7 +355,11 @@ sub GET :Allow {
);
}
$self->expand_collection_fields($c, \@embedded);
$self->delay_commit($c,$guard);
if ($c->config->{api}{underrun_lock_on_request}) {
$self->delay_commit($c,$guard);
}
push @links,
Data::HAL::Link->new(
relation => 'curies',

@ -48,15 +48,20 @@ sub GET :Allow {
my $subscriber = $self->item_by_id($c, $id);
last unless $self->resource_exists($c, subscriber => $subscriber);
my $balance = NGCP::Panel::Utils::ProfilePackages::get_contract_balance(c => $c,
contract => $subscriber->contract,
skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1),
); #apply underrun lock level
if ($c->config->{api}{underrun_lock_on_request}) {
my $balance = NGCP::Panel::Utils::ProfilePackages::get_contract_balance(c => $c,
contract => $subscriber->contract,
skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1),
); #apply underrun lock level
}
my ($form) = $self->get_form($c);
my $resource = $self->resource_from_item($c, $subscriber, $form);
my $hal = $self->hal_from_item($c, $subscriber, $resource, $form);
$guard->commit; #potential db write ops in hal_from
if ($c->config->{api}{underrun_lock_on_request}) {
$guard->commit; #potential db write ops in hal_from
}
my $response = HTTP::Response->new(HTTP_OK, undef, HTTP::Headers->new(
(map { # XXX Data::HAL must be able to generate links with multiple relations
@ -97,10 +102,14 @@ sub PUT :Allow {
my $subscriber = $self->item_by_id($c, $id);
last unless $self->resource_exists($c, subscriber => $subscriber);
my $balance = NGCP::Panel::Utils::ProfilePackages::get_contract_balance(c => $c,
contract => $subscriber->contract,
skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1),
); #apply underrun lock level
if ($c->config->{api}{underrun_lock_on_request}) {
my $balance = NGCP::Panel::Utils::ProfilePackages::get_contract_balance(c => $c,
contract => $subscriber->contract,
skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1),
); #apply underrun lock level
}
$c->stash->{subscriber} = $subscriber; # password validation
my $resource = $self->get_valid_put_data(
c => $c,
@ -122,6 +131,7 @@ sub PUT :Allow {
last unless $self->add_update_journal_item_hal($c,$hal);
$guard->commit;
$self->return_representation($c, 'hal' => $hal, 'preference' => $preference );
}
} catch($e) {
@ -153,10 +163,14 @@ sub PATCH :Allow {
my $subscriber = $self->item_by_id($c, $id);
last unless $self->resource_exists($c, subscriber => $subscriber);
my $balance = NGCP::Panel::Utils::ProfilePackages::get_contract_balance(c => $c,
contract => $subscriber->contract,
skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1),
); #apply underrun lock level
if ($c->config->{api}{underrun_lock_on_request}) {
my $balance = NGCP::Panel::Utils::ProfilePackages::get_contract_balance(c => $c,
contract => $subscriber->contract,
skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1),
); #apply underrun lock level
}
$c->stash->{subscriber} = $subscriber; # password validation
my $json = $self->get_valid_patch_data(
c => $c,
@ -186,6 +200,7 @@ sub PATCH :Allow {
last unless $self->add_update_journal_item_hal($c,$hal);
$guard->commit;
$self->return_representation($c, 'hal' => $hal, 'preference' => $preference );
}
} catch($e) {

@ -395,12 +395,16 @@ sub prepare_resource {
getcustomer_code => sub {
my ($cid) = @_;
my $contract = $self->get_customer($c, $cid);
NGCP::Panel::Utils::Contract::acquire_contract_rowlocks(
c => $c,
schema => $c->model('DB'),
contract_id => $contract->id,
skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1),
) if $contract;
if ($c->config->{api}{underrun_lock_on_request}) {
NGCP::Panel::Utils::Contract::acquire_contract_rowlocks(
c => $c,
schema => $c->model('DB'),
contract_id => $contract->id,
skip_locked => ($c->request->header('X-Delay-Commit') ? 0 : 1),
) if $contract;
}
return $contract;
},
);

Loading…
Cancel
Save