Rev 4309: Lock repositories created by BzrDirFormat.initialize_on_transport_ex. in http://people.ubuntu.com/~robertc/baz2.0/pending/push.roundtrips

Robert Collins robertc at robertcollins.net
Tue Apr 28 04:55:59 BST 2009


At http://people.ubuntu.com/~robertc/baz2.0/pending/push.roundtrips

------------------------------------------------------------
revno: 4309
revision-id: robertc at robertcollins.net-20090428035556-z6ucu9363itcb9qx
parent: robertc at robertcollins.net-20090428015132-a9n7dqmbm03w5nlh
committer: Robert Collins <robertc at robertcollins.net>
branch nick: push.roundtrips
timestamp: Tue 2009-04-28 13:55:56 +1000
message:
  Lock repositories created by BzrDirFormat.initialize_on_transport_ex.
=== modified file 'bzrlib/branch.py'
--- a/bzrlib/branch.py	2009-04-27 03:47:55 +0000
+++ b/bzrlib/branch.py	2009-04-28 03:55:56 +0000
@@ -1397,7 +1397,14 @@
         control_files = lockable_files.LockableFiles(branch_transport,
             lock_name, lock_class)
         control_files.create_lock()
-        control_files.lock_write()
+        try:
+            control_files.lock_write()
+        except errors.LockContention:
+            if lock_type != 'branch4':
+                raise
+            lock_taken = False
+        else:
+            lock_taken = True
         if set_format:
             utf8_files += [('format', self.get_format_string())]
         try:
@@ -1406,7 +1413,8 @@
                     filename, content,
                     mode=a_bzrdir._get_file_mode())
         finally:
-            control_files.unlock()
+            if lock_taken:
+                control_files.unlock()
         return self.open(a_bzrdir, _found=True)
 
     def initialize(self, a_bzrdir):

=== modified file 'bzrlib/bzrdir.py'
--- a/bzrlib/bzrdir.py	2009-04-28 01:51:32 +0000
+++ b/bzrlib/bzrdir.py	2009-04-28 03:55:56 +0000
@@ -246,22 +246,26 @@
             repo_format_name=repo_format_name,
             make_working_trees=make_working_trees, shared_repo=want_shared)
         if repo_format_name:
-            # If the result repository is in the same place as the resulting
-            # bzr dir, it will have no content, further if the result is not stacked
-            # then we know all content should be copied, and finally if we are
-            # copying up to a specific revision_id then we can use the
-            # pending-ancestry-result which does not require traversing all of
-            # history to describe it.
-            if (result_repo.bzrdir.root_transport.base ==
-                result.root_transport.base and not require_stacking and
-                revision_id is not None):
-                fetch_spec = graph.PendingAncestryResult(
-                    [revision_id], local_repo)
-                result_repo.fetch(local_repo, fetch_spec=fetch_spec)
-            else:
-                result_repo.fetch(local_repo, revision_id=revision_id)
+            try:
+                # If the result repository is in the same place as the
+                # resulting bzr dir, it will have no content, further if the
+                # result is not stacked then we know all content should be
+                # copied, and finally if we are copying up to a specific
+                # revision_id then we can use the pending-ancestry-result which
+                # does not require traversing all of history to describe it.
+                if (result_repo.bzrdir.root_transport.base ==
+                    result.root_transport.base and not require_stacking and
+                    revision_id is not None):
+                    fetch_spec = graph.PendingAncestryResult(
+                        [revision_id], local_repo)
+                    result_repo.fetch(local_repo, fetch_spec=fetch_spec)
+                else:
+                    result_repo.fetch(local_repo, revision_id=revision_id)
+            finally:
+                result_repo.unlock()
         else:
-            result_repo = None
+            if result_repo is not None:
+                raise AssertionError('result_repo not None(%r)' % result_repo)
         # 1 if there is a branch present
         #   make sure its content is available in the target repository
         #   clone it.
@@ -1946,6 +1950,7 @@
             if not require_stacking and repository_policy._require_stacking:
                 require_stacking = True
                 result._format.require_stacking()
+            result_repo.lock_write()
         else:
             result_repo = None
             repository_policy = None
@@ -3164,6 +3169,12 @@
             final_stack = response[8] or None
             final_stack_pwd = response[9] or None
             remote_repo = remote.RemoteRepository(repo_bzr, repo_format)
+            if len(response) > 10:
+                # Updated server verb that locks remotely.
+                repo_lock_token = response[10] or None
+                remote_repo.lock_write(repo_lock_token, _skip_rpc=True)
+            else:
+                remore_repo.lock_write()
             policy = UseExistingRepository(remote_repo, final_stack,
                 final_stack_pwd, require_stacking)
             policy.acquire_repository()

=== modified file 'bzrlib/smart/bzrdir.py'
--- a/bzrlib/smart/bzrdir.py	2009-04-24 05:08:51 +0000
+++ b/bzrlib/smart/bzrdir.py	2009-04-28 03:55:56 +0000
@@ -356,7 +356,11 @@
         make_working_trees, shared_repo):
         """Initialize a bzrdir at path as per BzrDirFormat.initialize_ex
 
-        :return: SmartServerResponse()
+        :return: return SuccessfulSmartServerResponse((repo_path, rich_root,
+            tree_ref, external_lookup, repo_network_name,
+            repo_bzrdir_network_name, bzrdir_format_network_name,
+            NoneTrueFalse(stacking), final_stack, final_stack_pwd,
+            repo_lock_token))
         """
         target_transport = self.transport_from_client_path(path)
         format = network_format_registry.get(bzrdir_network_name)
@@ -383,6 +387,7 @@
             repo_bzrdir_name = ''
             final_stack = None
             final_stack_pwd = None
+            repo_lock_token = ''
         else:
             repo_path = self._repo_relpath(bzrdir.root_transport, repo)
             if repo_path == '':
@@ -393,13 +398,20 @@
             repo_bzrdir_name = repo.bzrdir._format.network_name()
             final_stack = repository_policy._stack_on
             final_stack_pwd = repository_policy._stack_on_pwd
+            # It is returned locked, but we need to do the lock to get the lock
+            # token.
+            repo.unlock()
+            repo_lock_token = repo.lock_write() or ''
+            if repo_lock_token:
+                repo.leave_lock_in_place()
+            repo.unlock()
         final_stack = final_stack or ''
         final_stack_pwd = final_stack_pwd or ''
         return SuccessfulSmartServerResponse((repo_path, rich_root, tree_ref,
             external_lookup, repo_name, repo_bzrdir_name,
             bzrdir._format.network_name(),
             self._serialize_NoneTrueFalse(stacking), final_stack,
-            final_stack_pwd))
+            final_stack_pwd, repo_lock_token))
 
 
 class SmartServerRequestOpenBranch(SmartServerRequestBzrDir):

=== modified file 'bzrlib/tests/blackbox/test_push.py'
--- a/bzrlib/tests/blackbox/test_push.py	2009-04-28 01:51:32 +0000
+++ b/bzrlib/tests/blackbox/test_push.py	2009-04-28 03:55:56 +0000
@@ -201,7 +201,7 @@
         # being too low. If rpc_count increases, more network roundtrips have
         # become necessary for this use case. Please do not adjust this number
         # upwards without agreement from bzr's network support maintainers.
-        self.assertLength(10, self.hpss_calls)
+        self.assertLength(9, self.hpss_calls)
 
     def test_push_smart_stacked_streaming_acceptance(self):
         self.setup_smart_server_with_call_log()
@@ -217,7 +217,7 @@
         # being too low. If rpc_count increases, more network roundtrips have
         # become necessary for this use case. Please do not adjust this number
         # upwards without agreement from bzr's network support maintainers.
-        self.assertLength(17, self.hpss_calls)
+        self.assertLength(16, self.hpss_calls)
         remote = Branch.open('public')
         self.assertEndsWith(remote.get_stacked_on_url(), '/parent')
 

=== modified file 'bzrlib/tests/bzrdir_implementations/test_bzrdir.py'
--- a/bzrlib/tests/bzrdir_implementations/test_bzrdir.py	2009-04-27 03:34:12 +0000
+++ b/bzrlib/tests/bzrdir_implementations/test_bzrdir.py	2009-04-28 03:55:56 +0000
@@ -1207,6 +1207,9 @@
             return
         self.assertNotEqual(repo.bzrdir.root_transport.base,
             made_repo.bzrdir.root_transport.base)
+        # New repositories are write locked.
+        self.assertTrue(made_repo.is_write_locked())
+        made_repo.unlock()
 
     def test_format_initialize_on_transport_ex_force_new_repo_False(self):
         t = self.get_transport('repo')
@@ -1239,6 +1242,9 @@
             # uninitialisable format
             return
         self.assertLength(1, repo._fallback_repositories)
+        # New repositories are write locked.
+        self.assertTrue(repo.is_write_locked())
+        repo.unlock()
 
     def test_format_initialize_on_transport_ex_repo_fmt_name_None(self):
         t = self.get_transport('dir')
@@ -1259,6 +1265,9 @@
             # must stay with the all-in-one-format.
             repo_name = self.bzrdir_format.network_name()
         self.assertEqual(repo_name, repo._format.network_name())
+        # New repositories are write locked.
+        self.assertTrue(repo.is_write_locked())
+        repo.unlock()
 
     def assertInitializeEx(self, t, need_meta=False, **kwargs):
         """Execute initialize_on_transport_ex and check it succeeded correctly.

=== modified file 'bzrlib/tests/test_smart.py'
--- a/bzrlib/tests/test_smart.py	2009-04-27 03:34:12 +0000
+++ b/bzrlib/tests/test_smart.py	2009-04-28 03:55:56 +0000
@@ -362,7 +362,7 @@
         name = self.make_bzrdir('reference')._format.network_name()
         request = smart.bzrdir.SmartServerRequestBzrDirInitializeEx(backing)
         self.assertEqual(SmartServerResponse(('', '', '', '', '', '', name,
-            'False', '', '')),
+            'False', '', '', '')),
             request.execute(name, '', 'True', 'False', 'False', '', '', '', '',
             'False'))
         made_dir = bzrdir.BzrDir.open_from_transport(backing)




More information about the bazaar-commits mailing list