| 1 | package UR::Context::Transaction; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | require UR; | ||
| 7 | our $VERSION = "0.46"; # UR $VERSION; | ||
| 8 | |||
| 9 | use Carp qw(croak confess shortmess); | ||
| 10 | use constant TRANSACTION_STATE_OPEN => 'open'; | ||
| 11 | use constant TRANSACTION_STATE_COMMITTED => 'committed'; | ||
| 12 | |||
| 13 | use Exporter qw(import); | ||
| 14 | our @EXPORT_OK = qw(TRANSACTION_STATE_OPEN TRANSACTION_STATE_COMMITTED); | ||
| 15 | |||
| 16 | UR::Object::Type->define( | ||
| 17 | class_name => __PACKAGE__, | ||
| 18 | is => ['UR::Context'], | ||
| 19 | has => [ | ||
| 20 | begin_point => { is => 'Integer' }, | ||
| 21 | end_point => { is => 'Integer', is_optional => 1}, # FIXME is this ever used anywhere? | ||
| 22 | state => { is => 'Text', valid_values => [TRANSACTION_STATE_OPEN, TRANSACTION_STATE_COMMITTED] }, | ||
| 23 | commit_validator => { default_value => 'changes_can_be_saved', | ||
| 24 | doc => 'validation function used before commit() can succeed' }, | ||
| 25 | ], | ||
| 26 | is_transactional => 1, | ||
| 27 | ); | ||
| 28 | |||
| 29 | our $log_all_changes = 0; | ||
| 30 | our @change_log; | ||
| 31 | our @open_transaction_stack; | ||
| 32 | our $last_transaction_id = 0; | ||
| 33 | |||
| 34 | sub delete { | ||
| 35 | my $self = shift; | ||
| 36 | $self->rollback; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub begin { | ||
| 40 | my $class = shift; | ||
| 41 | my %params = @_; | ||
| 42 | |||
| 43 | delete @params{'begin_point', 'end_point', 'state'}; # These are set within this function | ||
| 44 | |||
| 45 | my $id = $last_transaction_id++; | ||
| 46 | |||
| 47 | my $begin_point = @change_log; | ||
| 48 | $log_all_changes = 1; | ||
| 49 | |||
| 50 | my $last_trans = $open_transaction_stack[-1]; | ||
| 51 | if ($last_trans and $last_trans != $UR::Context::current) { | ||
| 52 | die "Current transaction does not match the top of the transaction stack!?" | ||
| 53 | } | ||
| 54 | $last_trans ||= $UR::Context::current; | ||
| 55 | |||
| 56 | my $self = $class->create( | ||
| 57 | id => $id, | ||
| 58 | begin_point => $begin_point, | ||
| 59 | state => TRANSACTION_STATE_OPEN, | ||
| 60 | parent => $last_trans, | ||
| 61 | %params, | ||
| 62 | ); | ||
| 63 | |||
| 64 | unless ($self) { | ||
| 65 | Carp::confess("Failed to being transaction!"); | ||
| 66 | } | ||
| 67 | |||
| 68 | push @open_transaction_stack, $self; | ||
| 69 | |||
| 70 | $UR::Context::current = $self; | ||
| 71 | |||
| 72 | return $self; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub log_change { | ||
| 76 | my $this_class = shift; | ||
| 77 | my ($object, $class, $id, $aspect, $undo_data) = @_; | ||
| 78 | |||
| 79 | return if $class eq "UR::Change"; | ||
| 80 | |||
| 81 | # wrappers (create/delete/load/unload/define) signal change also | ||
| 82 | # and we undo the wrapper, thereby undoing these | ||
| 83 | # -> ignore any signal from a method which is wrapped by another signalling method which gets undone | ||
| 84 | return if ( $aspect eq "load" or | ||
| 85 | $aspect eq "load_external" | ||
| 86 | ); | ||
| 87 | |||
| 88 | if (!ref($object) or $class eq "UR::Object::Index") { | ||
| 89 | #print "skipping @_\n"; | ||
| 90 | return; | ||
| 91 | } | ||
| 92 | |||
| 93 | if ($aspect eq "delete") { | ||
| 94 | $undo_data = Data::Dumper::Dumper($object); | ||
| 95 | } | ||
| 96 | |||
| 97 | Carp::confess() if ref($class); | ||
| 98 | |||
| 99 | my $change = UR::Change->create( | ||
| 100 | id => scalar(@change_log)+1, | ||
| 101 | changed_class_name => $class, | ||
| 102 | changed_id => $id, | ||
| 103 | changed_aspect => $aspect, | ||
| 104 | undo_data => $undo_data, | ||
| 105 | ); | ||
| 106 | |||
| 107 | unless (ref($change)) { | ||
| 108 | #$DB::single = 1; | ||
| 109 | } | ||
| 110 | |||
| 111 | push @change_log, $change; | ||
| 112 | return $change; | ||
| 113 | } | ||
| 114 | |||
| 115 | sub has_changes { | ||
| 116 | my $self = shift; | ||
| 117 | my @changes = $self->get_changes(); | ||
| 118 | return (@changes > 1 ? 1 : ()); | ||
| 119 | } | ||
| 120 | |||
| 121 | sub get_changes { | ||
| 122 | my $self = shift; | ||
| 123 | my $begin_point = $self->begin_point; | ||
| 124 | my $end_point = $self->end_point || $#change_log; | ||
| 125 | my @changes = @change_log[$begin_point..$end_point]; | ||
| 126 | if (@_) { | ||
| 127 | @changes = UR::Change->get(id => \@changes, @_) | ||
| 128 | } | ||
| 129 | else { | ||
| 130 | return @changes; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | sub get_change_summary { | ||
| 135 | # TODO: This should compress multiple changes to the same object as much as possible | ||
| 136 | # Right now, it just omits the creation event for the transaction object itself. | ||
| 137 | # -> should the creation of the transaction be part of it? | ||
| 138 | # A: It should really be part of the prior transaction, and after commit/rollback | ||
| 139 | # the nesting collapses. The @change_log should be _inside the transaction object, | ||
| 140 | # or the change should contain a transaction id. The list can be destroyed on | ||
| 141 | # rollback, or summarized on commit. | ||
| 142 | my $self = shift; | ||
| 143 | my @changes = | ||
| 144 | grep { $_->changed_aspect !~ /^(load|define)$/ } | ||
| 145 | $self->get_changes; | ||
| 146 | shift @changes; # $self creation event | ||
| 147 | return @changes; | ||
| 148 | } | ||
| 149 | |||
| 150 | sub rollback { | ||
| 151 | my $self = shift; | ||
| 152 | |||
| 153 | # Support calling as a class method: UR::Context::Transaction->rollback rolls back the current trans | ||
| 154 | unless (ref($self)) { | ||
| 155 | $self = $open_transaction_stack[-1]; | ||
| 156 | unless ($self) { | ||
| 157 | Carp::confess("No open transaction!? Cannot rollback."); | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | if ($self->state ne TRANSACTION_STATE_OPEN) { | ||
| 162 | Carp::confess("Cannot rollback a transaction that is " . $self->state . ".") | ||
| 163 | } | ||
| 164 | |||
| 165 | $self->__signal_change__('prerollback'); | ||
| 166 | |||
| 167 | my $begin_point = $self->begin_point; | ||
| 168 | unless ($self eq $open_transaction_stack[-1]) { | ||
| 169 | # This is not the top transaction on the stack. | ||
| 170 | # Rollback internally nested transactions in order from the end. | ||
| 171 | my @transactions_with_begin_point = | ||
| 172 | map { [ $_->begin_point, $_ ] } | ||
| 173 | $self->class->get( | ||
| 174 | begin_point => { operator => ">", value => $begin_point } | ||
| 175 | ); | ||
| 176 | my @later_transactions = | ||
| 177 | map { $_->[1] } | ||
| 178 | sort { $b->[0] <=> $a->[0] } | ||
| 179 | @transactions_with_begin_point; | ||
| 180 | |||
| 181 | for my $later_transaction (@later_transactions) { | ||
| 182 | if ($later_transaction->isa("UR::DeletedRef")) { | ||
| 183 | #$DB::single = 1; | ||
| 184 | } | ||
| 185 | $later_transaction->rollback; | ||
| 186 | } | ||
| 187 | } | ||
| 188 | |||
| 189 | my $parent = $self->parent; | ||
| 190 | if ($open_transaction_stack[-2] and $open_transaction_stack[-2] != $parent) { | ||
| 191 | die "Parent transaction $parent is not below this one on the stack $open_transaction_stack[-2]?"; | ||
| 192 | } | ||
| 193 | |||
| 194 | { | ||
| 195 | # Reverse each change, starting from the most recent, and | ||
| 196 | # ending with the creation of the transaction object itself. | ||
| 197 | local $log_all_changes = 0; | ||
| 198 | |||
| 199 | |||
| 200 | $self->__signal_change__('rollback', 1); | ||
| 201 | my @changes_to_undo = reverse $self->get_changes(); | ||
| 202 | my $transaction_change = pop @changes_to_undo; | ||
| 203 | my $transaction = $transaction_change->changed_class_name->get($transaction_change->changed_id); | ||
| 204 | unless ($self == $transaction && $transaction_change->changed_aspect eq 'create') { | ||
| 205 | die "First change was not the creation of this transaction!"; | ||
| 206 | } | ||
| 207 | for my $change (@changes_to_undo) { | ||
| 208 | if ($change == $changes_to_undo[0]) { | ||
| 209 | # the transaction reverses itself in its own context, | ||
| 210 | # but the removal of the transaction itself happens in the parent context | ||
| 211 | $UR::Context::current = $parent; | ||
| 212 | } | ||
| 213 | |||
| 214 | $change->undo; | ||
| 215 | $change->delete; | ||
| 216 | } | ||
| 217 | |||
| 218 | for my $change (@changes_to_undo) { | ||
| 219 | unless($change->isa('UR::DeletedRef')) { | ||
| 220 | Carp::confess("Failed to undo a change during transaction rollback."); | ||
| 221 | } | ||
| 222 | } | ||
| 223 | |||
| 224 | $transaction_change->undo; | ||
| 225 | $transaction_change->delete; | ||
| 226 | } | ||
| 227 | |||
| 228 | $#change_log = $begin_point-1; | ||
| 229 | |||
| 230 | unless($self->isa("UR::DeletedRef")) { | ||
| 231 | #$DB::single = 1; | ||
| 232 | Carp::confess("Failed to remove transaction during rollback."); | ||
| 233 | } | ||
| 234 | |||
| 235 | pop @open_transaction_stack; | ||
| 236 | unless (@open_transaction_stack) { | ||
| 237 | $log_all_changes = 0; | ||
| 238 | } | ||
| 239 | $UR::Context::current = $parent; | ||
| 240 | |||
| 241 | return 1; | ||
| 242 | } | ||
| 243 | |||
| 244 | sub commit { | ||
| 245 | my $self = shift; | ||
| 246 | |||
| 247 | # Support calling as a class method: UR::Context::Transaction->commit commits the current transaction. | ||
| 248 | unless (ref($self)) { | ||
| 249 | $self = $open_transaction_stack[-1]; | ||
| 250 | unless ($self) { | ||
| 251 | Carp::confess("No open transaction!? Cannot commit."); | ||
| 252 | } | ||
| 253 | } | ||
| 254 | |||
| 255 | if ($self->state ne TRANSACTION_STATE_OPEN) { | ||
| 256 | Carp::confess("Cannot commit a transaction that is " . $self->state . ".") | ||
| 257 | } | ||
| 258 | |||
| 259 | unless ($open_transaction_stack[-1] == $self) { | ||
| 260 | # TODO: decide if this should work like rollback, and commit nested transactions automatically | ||
| 261 | Carp::confess("Cannot commit a transaction with open sub-transactions!"); | ||
| 262 | } | ||
| 263 | $self->__signal_change__('precommit'); | ||
| 264 | |||
| 265 | my $validator = $self->commit_validator; | ||
| 266 | unless ($self->$validator()) { | ||
| 267 | return; | ||
| 268 | } | ||
| 269 | |||
| 270 | $self->state(TRANSACTION_STATE_COMMITTED); | ||
| 271 | if ($self->state eq TRANSACTION_STATE_COMMITTED) { | ||
| 272 | $self->__signal_change__('commit',1); | ||
| 273 | } | ||
| 274 | else { | ||
| 275 | $self->__signal_change__('commit',0); | ||
| 276 | } | ||
| 277 | pop @open_transaction_stack; | ||
| 278 | unless (@open_transaction_stack) { | ||
| 279 | $log_all_changes = 0; | ||
| 280 | } | ||
| 281 | |||
| 282 | $UR::Context::current = $self->parent; | ||
| 283 | return 1; | ||
| 284 | } | ||
| 285 | |||
| 286 | sub changes_can_be_saved { | ||
| 287 | my $self = shift; | ||
| 288 | |||
| 289 | # This is very similar to behavior in UR::Context::_sync_databases. The only | ||
| 290 | # reason it isn't re-used from UR::Context is the desire to limit changed | ||
| 291 | # objects to those changed within the transaction. | ||
| 292 | # TODO: limit to objects that changed within transaction as to not duplicate | ||
| 293 | # error checking unnecessarily. | ||
| 294 | |||
| 295 | my @changed_objects = | ||
| 296 | grep { ! $_->isa('UR::DeletedRef') } | ||
| 297 | map { $_->changed_object() } $self->get_changes(); | ||
| 298 | |||
| 299 | # This is primarily to catch custom validity logic in class overrides. | ||
| 300 | my @invalid = grep { $_->__errors__ } @changed_objects; | ||
| 301 | if (@invalid) { | ||
| 302 | $self->display_invalid_data_for_save(\@invalid); | ||
| 303 | return; | ||
| 304 | } | ||
| 305 | |||
| 306 | return 1; | ||
| 307 | } | ||
| 308 | |||
| 309 | sub eval_or_do { | ||
| 310 | my $is_failure = shift; | ||
| 311 | my $block = shift; | ||
| 312 | |||
| 313 | my $class = __PACKAGE__; | ||
| 314 | if (@_) { | ||
| 315 | confess('%s::eval takes one argument', $class); | ||
| 316 | } | ||
| 317 | my $tx = $class->begin(); | ||
| 318 | my $result = CORE::eval { $block->() }; | ||
| 319 | my $eval_error = $@; | ||
| 320 | |||
| 321 | if ($is_failure->($result, $eval_error)) { | ||
| 322 | $class->debug_message(shortmess('Rolling back transaction')); | ||
| 323 | $class->debug_message($eval_error) if ($eval_error); | ||
| 324 | unless($tx->rollback()) { | ||
| 325 | Carp::croak 'failed to rollback transaction'; | ||
| 326 | } | ||
| 327 | } else { | ||
| 328 | unless($tx->commit()) { | ||
| 329 | Carp::croak 'failed to commit transaction'; | ||
| 330 | } | ||
| 331 | } | ||
| 332 | |||
| 333 | if (wantarray) { | ||
| 334 | return ($result, $eval_error); | ||
| 335 | } else { | ||
| 336 | return $result; | ||
| 337 | } | ||
| 338 | } | ||
| 339 | |||
| 340 | # eval function takes a block (&) sort of like CORE::eval | ||
| 341 | # eval will rollback on a caught die | ||
| 342 | sub eval(&) { | ||
| 343 | my $is_failure = sub { | ||
| 344 | my ($result, $eval_error) = @_; | ||
| 345 | return $eval_error; | ||
| 346 | }; | ||
| 347 | return eval_or_do($is_failure, @_); | ||
| 348 | } | ||
| 349 | |||
| 350 | # do function takes a block (&) sort of like CORE::do | ||
| 351 | # do will rollback on a false result as well as before re-throwing a caught die | ||
| 352 | sub do(&) { | ||
| 353 | my $is_failure = sub { | ||
| 354 | my ($result, $eval_error) = @_; | ||
| 355 | return !$result || $eval_error; | ||
| 356 | }; | ||
| 357 | my ($result, $eval_error) = eval_or_do($is_failure, @_); | ||
| 358 | if ($eval_error) { | ||
| 359 | croak $eval_error, "\t...propogated"; | ||
| 360 | } | ||
| 361 | return $result; | ||
| 362 | } | ||
| 363 | |||
| 364 | 1; | ||
| 365 | |||
| 366 | =pod | ||
| 367 | |||
| 368 | =head1 NAME | ||
| 369 | |||
| 370 | UR::Context::Transaction - API for software transactions | ||
| 371 | |||
| 372 | =head1 SYNOPSIS | ||
| 373 | |||
| 374 | my $o = Some::Obj->create(foo => 1); | ||
| 375 | print "o's foo is ",$o->foo,"\n"; # prints 1 | ||
| 376 | |||
| 377 | my $t = UR::Context::Transaction->begin(); | ||
| 378 | |||
| 379 | $o->foo(4); | ||
| 380 | |||
| 381 | print "In transaction, o's foo is ",$o->foo,"\n"; # prints 4 | ||
| 382 | |||
| 383 | if (&should_we_commit()) { | ||
| 384 | $t->commit(); | ||
| 385 | print "Transaction committed, o's foo is ",$o->foo,"\n"; # prints 4 | ||
| 386 | |||
| 387 | } else { | ||
| 388 | $t->rollback(); | ||
| 389 | print "Transaction rollback, o's foo is ",$o->foo,"\n"; # prints 1 | ||
| 390 | } | ||
| 391 | |||
| 392 | =head1 DESCRIPTION | ||
| 393 | |||
| 394 | UR::Context::Transaction instances represent in-memory transactions as a diff | ||
| 395 | of the contents of the object cache in the Process context. Transactions are | ||
| 396 | nestable. Their instances exist in the object cache and are subject to the | ||
| 397 | same scoping rules as other UR-based objects, meaning that they do not | ||
| 398 | disappear mearly because the lexical variable they're assigned to goes out of | ||
| 399 | scope. They must be explicitly disposed of via the commit or rollback methods. | ||
| 400 | |||
| 401 | =head1 INHERITANCE | ||
| 402 | |||
| 403 | UR::Context::Transaction is a subclass of UR::Context | ||
| 404 | |||
| 405 | =head1 CONSTRUCTOR | ||
| 406 | |||
| 407 | =over 4 | ||
| 408 | |||
| 409 | =item begin | ||
| 410 | |||
| 411 | $t = UR::Context::Transaction->begin(); | ||
| 412 | |||
| 413 | Creates a new software transaction context to track changes to UR-based | ||
| 414 | objects. As all activity to objects occurs in some kind of transaction | ||
| 415 | context, the newly created transaction exists within whatever context was | ||
| 416 | current before the call to begin(). | ||
| 417 | |||
| 418 | $t = UR::Context::Transaction->begin(commit_validator => sub { ... }); | ||
| 419 | |||
| 420 | A validation function may be assigned with the C<commit_validator> property. | ||
| 421 | When the transaction is committed, this function is called. The commit | ||
| 422 | proceeds if this function returns a true value. The default function, | ||
| 423 | C<changes_can_be_saved> requires that all objects changed within the | ||
| 424 | transaction be valid, ie. that C<$obj->__errors__()> returns an empty list. | ||
| 425 | The validation function is passed one argument: the transaction object | ||
| 426 | being committed. | ||
| 427 | |||
| 428 | =back | ||
| 429 | |||
| 430 | =head1 METHODS | ||
| 431 | |||
| 432 | =over 4 | ||
| 433 | |||
| 434 | =item commit | ||
| 435 | |||
| 436 | $t->commit(); | ||
| 437 | |||
| 438 | Causes all objects with changes to save those changes back to the underlying | ||
| 439 | context. | ||
| 440 | |||
| 441 | If the validation function (specified with the C<commit_validator> param when | ||
| 442 | the transaction was created with C<begin()>) returns false, the changes are | ||
| 443 | not committed to the encompassing context, C<commit()> returns false and this | ||
| 444 | transaction remains in effect. | ||
| 445 | |||
| 446 | Returns true if all the transaction's changes are committed to the encompassing | ||
| 447 | Context. This transaction object then becomes invalid, and its state will be | ||
| 448 | 'committed'. | ||
| 449 | |||
| 450 | =item rollback | ||
| 451 | |||
| 452 | $t->rollback(); | ||
| 453 | |||
| 454 | Causes all objects with changes to have those changes reverted to their | ||
| 455 | state when the transaction began. Classes with properties whose meta-property | ||
| 456 | is_transactional => 0 are not tracked within a transaction and will not be | ||
| 457 | reverted. | ||
| 458 | |||
| 459 | After C<rollback()>, this transaction becomes invalid, and the object will become | ||
| 460 | a L<UR::DeletedRef>. | ||
| 461 | |||
| 462 | =item delete | ||
| 463 | |||
| 464 | $t->delete(); | ||
| 465 | |||
| 466 | delete() is a synomym for rollback | ||
| 467 | |||
| 468 | =item has_changes | ||
| 469 | |||
| 470 | $bool = $t->has_changes(); | ||
| 471 | |||
| 472 | Returns true if any UR-based objects have changes within the transaction. | ||
| 473 | |||
| 474 | =item get_changes | ||
| 475 | |||
| 476 | @changes = $t->get_changes(); | ||
| 477 | |||
| 478 | Return a list or L<UR::Change> objects representing changes within the transaction. | ||
| 479 | |||
| 480 | =back | ||
| 481 | |||
| 482 | =head1 CLASS METHODS | ||
| 483 | |||
| 484 | =over 4 | ||
| 485 | |||
| 486 | =item eval | ||
| 487 | |||
| 488 | UR::Context::Transaction::eval BLOCK | ||
| 489 | |||
| 490 | Executes the BLOCK (with no arguments) wrapped by a software transaction and a | ||
| 491 | CORE::eval. If the BLOCK dies then the exception is caught and the software | ||
| 492 | transaction is rolled back. | ||
| 493 | |||
| 494 | =item do | ||
| 495 | |||
| 496 | UR::Context::Transaction::do BLOCK | ||
| 497 | |||
| 498 | Executes the BLOCK (with no arguments) wrapped by a software transaction and a | ||
| 499 | CORE::eval. If the BLOCK returns a true value and does not die then the | ||
| 500 | software transaction is committed. If the BLOCK returns false or dies then the | ||
| 501 | software transaction is rolled back. | ||
| 502 | |||
| 503 | If the BLOCK throws an exception, it will be caught, the software transaction | ||
| 504 | rolled back, and the exception will be re-thrown with die(). | ||
| 505 | |||
| 506 | =back | ||
| 507 | |||
| 508 | =head1 EXPORTS | ||
| 509 | |||
| 510 | This module can export constants that match the valid values of the C<state> | ||
| 511 | property: TRANSACTION_STATE_OPEN and TRANSACTION_STATE_COMMITTED | ||
| 512 | |||
| 513 | =head1 SEE ALSO | ||
| 514 | |||
| 515 | L<UR::Context> | ||
| 516 | |||
| 517 | =cut |