Permalink
Browse files

refactoring to better share foreignreplica code

  • Loading branch information...
obra committed May 7, 2009
1 parent a70eb78 commit 1347cbdd10449f65f782596f725e99393eaeb5b9
Showing with 84 additions and 44 deletions.
  1. +50 −0 lib/App/SD/ForeignReplica.pm
  2. +17 −44 lib/App/SD/Replica/rt.pm
  3. +17 −0 lib/App/SD/Replica/trac.pm
@@ -33,6 +33,56 @@ sub integrate_change {
$recoder->integrate_change($change,$changeset);
}
+# XXX TODO docs
+
+sub record_pushed_transactions {
+ my $self = shift;
+ my %args = validate( @_,
+ { ticket => 1, changeset => { isa => 'Prophet::ChangeSet' }, start_time => 1} );
+
+
+ my $earliest_valid_txn_date;
+
+ # walk through every transaction on the ticket, starting with the latest
+
+ for my $txn ( $self->get_txn_list_by_date($args{ticket}) ) {
+
+ # walk backwards through all transactions on the ticket we just updated
+ # Skip any transaction where the remote user isn't me, this might include any transaction
+ # RT created with a scrip on your behalf
+
+ next unless $txn->{creator} eq $self->foreign_username;
+
+ # get the completion time _after_ we do our next round trip to rt to try to make sure
+ # a bit of lag doesn't skew us to the wrong side of a 1s boundary
+
+
+ if (!$earliest_valid_txn_date){
+ my $change_window = time() - $args{start_time};
+ # skip any transaction created more than 5 seconds before the push started.
+ # I can't think of any reason that number shouldn't be 1, but clocks are fickle
+ $earliest_valid_txn_date = $txn->{created} - ($change_window + 5);
+ }
+
+ last if $txn->{created} < $earliest_valid_txn_date;
+
+ # if the transaction id is older than the id of the last changeset
+ # we got from the original source of this changeset, we're done
+ last if $txn->{id} <= $self->upstream_last_txn();
+
+ # if the transaction from RT is more recent than the most recent
+ # transaction we got from the original source of the changeset
+ # then we should record that we sent that transaction upstream
+
+ $self->record_pushed_transaction(
+ transaction => $txn->{id},
+ changeset => $args{'changeset'},
+ record => $args{'ticket'}
+ );
+ }
+}
+
+
=head2 record_pushed_transaction $foreign_transaction_id, $changeset
Record that this replica was the original source of $foreign_transaction_id
View
@@ -48,58 +48,31 @@ sub BUILD {
$self->rt->login( username => $username, password => $password );
}
-sub record_pushed_transactions {
- my $self = shift;
- my %args = validate( @_,
- { ticket => 1, changeset => { isa => 'Prophet::ChangeSet' }, start_time => 1} );
-
-
- my $earliest_valid_txn_date;
- # walk through every transaction on the ticket, starting with the latest
- for my $txn ( sort {$b->{'Created'} <=> $a->{'Created'}} RT::Client::REST::Ticket->new(
- rt => $self->rt,
- id => $args{'ticket'})->transactions->get_iterator->()) {
+sub foreign_username { return shift->rt_username(@_)}
- # walk backwards through all transactions on the ticket we just updated
- # Skip any transaction where the remote user isn't me, this might include any transaction
- # RT created with a scrip on your behalf
- next unless $txn->creator eq $self->rt_username;
- # get the completion time _after_ we do our next round trip to rt to try to make sure
- # a bit of lag doesn't skew us to the wrong side of a 1s boundary
- my $txn_created_dt = App::SD::Util::string_to_datetime($txn->created);
- unless($txn_created_dt) {
- die "Couldn't parse '".$txn->created."' as a timestamp";
+sub get_txn_list_by_date {
+ my $self = shift;
+ my $ticket = shift;
+ my @txns = map {
+ my $txn_created_dt = App::SD::Util::string_to_datetime( $_->created );
+ unless ($txn_created_dt) {
+ die "Couldn't parse '" . $_->created . "' as a timestamp";
}
my $txn_created = $txn_created_dt->epoch;
- if (!$earliest_valid_txn_date){
- my $change_window = time() - $args{start_time};
- # skip any transaction created more than 5 seconds before the push started.
- # I can't think of any reason that number shouldn't be 1, but clocks are fickle
- $earliest_valid_txn_date = $txn_created - ($change_window + 5);
- }
-
- last if $txn_created < $earliest_valid_txn_date;
-
-
- # if the transaction id is older than the id of the last changeset
- # we got from the original source of this changeset, we're done
- last if $txn->id <= $self->upstream_last_txn();
-
-
- # if the transaction from RT is more recent than the most recent
- # transaction we got from the original source of the changeset
- # then we should record that we sent that transaction upstream
- $self->record_pushed_transaction(
- transaction => $txn->id,
- changeset => $args{'changeset'},
- record => $args{'ticket'}
- );
- }
+
+ return { id => $_->id, creator => $_->creator, created => $txn_created }
+ }
+
+ sort { $b->{'Created'} <=> $a->{'Created'} }
+ RT::Client::REST::Ticket->new( rt => $self->rt, id => $ticket )->transactions->get_iterator->();
+ return @txns;
}
+
+
sub upstream_last_txn {
my $self = shift;
return $self->fetch_local_metadata('last_txn_id');
View
@@ -17,6 +17,7 @@ use Prophet::ChangeSet;
has trac => ( isa => 'Net::Trac::Connection', is => 'rw');
has remote_url => ( isa => 'Str', is => 'rw');
+sub foreign_username { return shift->trac->user(@_) }
sub BUILD {
my $self = shift;
@@ -46,6 +47,22 @@ sub BUILD {
$self->trac->ensure_logged_in;
}
+
+
+sub get_txn_list_by_date {
+ my $self = shift;
+ my $ticket = shift;
+
+ my $ticket_obj = Net::Trac::Ticket->new( connection => $self->trac);
+ $ticket_obj->load($ticket);
+
+ my @txns = map { { id => $_->id, creator => $_->author, created => $_->date->epoch } }
+ sort {$b->date <=> $a->date } @{$ticket_obj->history->entries};
+ return @txns;
+}
+
+
+
sub record_pushed_transactions {
my $self = shift;
my %args = validate( @_,

0 comments on commit 1347cbd

Please sign in to comment.