[Bps-public-commit] SD branch, master, updated. 1347cbdd10449f65f782596f725e99393eaeb5b9

jesse jesse at bestpractical.com
Wed May 6 23:36:10 EDT 2009


The branch, master has been updated
       via  1347cbdd10449f65f782596f725e99393eaeb5b9 (commit)
      from  a70eb78982a99bd484d565bbbebb3de3cd757f38 (commit)

Summary of changes:
 lib/App/SD/ForeignReplica.pm |   50 ++++++++++++++++++++++++++++++++++
 lib/App/SD/Replica/rt.pm     |   61 +++++++++++------------------------------
 lib/App/SD/Replica/trac.pm   |   17 +++++++++++
 3 files changed, 84 insertions(+), 44 deletions(-)

- Log -----------------------------------------------------------------
commit 1347cbdd10449f65f782596f725e99393eaeb5b9
Author: Jesse Vincent <jesse at bestpractical.com>
Date:   Wed May 6 23:35:53 2009 -0400

    refactoring to better share foreignreplica code

diff --git a/lib/App/SD/ForeignReplica.pm b/lib/App/SD/ForeignReplica.pm
index 2ba121e..47e7563 100644
--- a/lib/App/SD/ForeignReplica.pm
+++ b/lib/App/SD/ForeignReplica.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 
diff --git a/lib/App/SD/Replica/rt.pm b/lib/App/SD/Replica/rt.pm
index f51e74c..feda76e 100644
--- a/lib/App/SD/Replica/rt.pm
+++ b/lib/App/SD/Replica/rt.pm
@@ -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');
diff --git a/lib/App/SD/Replica/trac.pm b/lib/App/SD/Replica/trac.pm
index 7bdea56..aa31880 100644
--- a/lib/App/SD/Replica/trac.pm
+++ b/lib/App/SD/Replica/trac.pm
@@ -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( @_,

-----------------------------------------------------------------------



More information about the Bps-public-commit mailing list