Merge lp:~cmiller/desktopcouch/dbus-refs-explicit-and-rm-resolve-on-disappear into lp:desktopcouch

Proposed by Chad Miller
Status: Merged
Approved by: Chad Miller
Approved revision: 81
Merged at revision: not available
Proposed branch: lp:~cmiller/desktopcouch/dbus-refs-explicit-and-rm-resolve-on-disappear
Merge into: lp:desktopcouch
Diff against target: 239 lines
2 files modified
desktopcouch/pair/couchdb_pairing/dbus_io.py (+35/-48)
desktopcouch/replication.py (+9/-11)
To merge this branch: bzr merge lp:~cmiller/desktopcouch/dbus-refs-explicit-and-rm-resolve-on-disappear
Reviewer Review Type Date Requested Status
Eric Casteleijn (community) Approve
Review via email: mp+12747@code.launchpad.net

Commit message

Be explicit in references, as evidently there is a way for the name/reference to disappear. (LP: #440072)

Do not attempt to resolve messages for nearby machines when they disappear.

Fix a few unreported bugs in error handling.

To post a comment you must log in.
Revision history for this message
Eric Casteleijn (thisfred) wrote :

in replication.py, the following imports are unused:

import threading
import logging.handlers
import desktopcouch

this is unconventionally indented and uses an undefined variable 'sn' (occurs twice):

        log.error("The service %r is unknown. It is not a "
                "module in the %s package ." % (sn, container))
        return ""

this has an undefined variable 'remote_identifier'

                        couchdb_io.expunge_pairing(remote_identifier)

and there are several long lines.

dbus_io

args is undefined in:

             logging.error("annc doesn't look like one of ours. %r", args)

also, this scares me:

class LocationAdvertisement(Advertisement):
    """An advertised couchdb location. See Advertisement class."""
    def __init__(self, *args, **kwargs):
        if "stype" in kwargs:
            kwargs.pop(stype)
        super(LocationAdvertisement, self).__init__(
                stype=location_discovery_service_type, *args, **kwargs)

class PairAdvertisement(Advertisement):
    """An advertised couchdb pairing opportunity. See Advertisement class."""
    def __init__(self, *args, **kwargs):
        if "stype" in kwargs:
            kwargs.pop(stype)
        super(PairAdvertisement, self).__init__(
                stype=invitations_discovery_service_type, *args, **kwargs)

Isn't it possible to make it a little more explicit? I think I would prefer:

kwargs.pop(kwargs["stype"])

review: Needs Fixing
80. By Chad Miller

Clean up Eric's complaints. This fixes three bugs.

Revision history for this message
Eric Casteleijn (thisfred) wrote :

Looks great, tests pass!

review: Approve
81. By Chad Miller

Eric is pleasantly persistent. More cleanups, more bug fixes.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'desktopcouch/pair/couchdb_pairing/dbus_io.py'
--- desktopcouch/pair/couchdb_pairing/dbus_io.py 2009-09-30 15:52:57 +0000
+++ desktopcouch/pair/couchdb_pairing/dbus_io.py 2009-10-01 21:11:09 +0000
@@ -103,18 +103,14 @@
103class LocationAdvertisement(Advertisement):103class LocationAdvertisement(Advertisement):
104 """An advertised couchdb location. See Advertisement class."""104 """An advertised couchdb location. See Advertisement class."""
105 def __init__(self, *args, **kwargs):105 def __init__(self, *args, **kwargs):
106 if "stype" in kwargs:106 kwargs["stype"] = location_discovery_service_type
107 kwargs.pop(stype)107 super(LocationAdvertisement, self).__init__(*args, **kwargs)
108 super(LocationAdvertisement, self).__init__(
109 stype=location_discovery_service_type, *args, **kwargs)
110108
111class PairAdvertisement(Advertisement):109class PairAdvertisement(Advertisement):
112 """An advertised couchdb pairing opportunity. See Advertisement class."""110 """An advertised couchdb pairing opportunity. See Advertisement class."""
113 def __init__(self, *args, **kwargs):111 def __init__(self, *args, **kwargs):
114 if "stype" in kwargs:112 kwargs["stype"] = invitations_discovery_service_type
115 kwargs.pop(stype)113 super(PairAdvertisement, self).__init__(*args, **kwargs)
116 super(PairAdvertisement, self).__init__(
117 stype=invitations_discovery_service_type, *args, **kwargs)
118114
119def avahitext_to_dict(avahitext):115def avahitext_to_dict(avahitext):
120 text = {}116 text = {}
@@ -155,51 +151,39 @@
155def maintain_discovered_servers(add_cb=cb_found_desktopcouch_server, 151def maintain_discovered_servers(add_cb=cb_found_desktopcouch_server,
156 del_cb=cb_lost_desktopcouch_server):152 del_cb=cb_lost_desktopcouch_server):
157153
158 def remove_item_handler(interface, protocol, name, stype, domain, flags):154 def remove_item_handler(cb, interface, protocol, name, stype, domain,
155 flags):
159 """A service disappeared."""156 """A service disappeared."""
160157
161 def handle_error(*args):158 if name.startswith("desktopcouch "):
162 """An error in resolving a new service."""159 hostid = name[13:]
163 logging.error("zeroconf ItemNew error for services, %s", args)160 logging.debug("lost sight of %r", hostid)
164161 cb(hostid)
165 def handle_resolved(*args):162 else:
166 """Successfully resolved a new service, which we decode and send163 logging.error("annc doesn't look like one of ours. %r", name)
167 back to our calling environment with the callback function."""164
168165 def new_item_handler(cb, interface, protocol, name, stype, domain, flags):
169 name, host, port = args[2], args[5], args[8]
170 if name.startswith("desktopcouch "):
171 hostid = name[13:]
172 logging.debug("lost sight of %r", hostid)
173 del_cb(hostid)
174 else:
175 logging.error("no UUID in zeroconf message, %r", args)
176
177 server.ResolveService(interface, protocol, name, stype,
178 domain, avahi.PROTO_UNSPEC, dbus.UInt32(0),
179 reply_handler=handle_resolved, error_handler=handle_error)
180
181 def new_item_handler(interface, protocol, name, stype, domain, flags):
182 """A service appeared."""166 """A service appeared."""
183167
184 def handle_error(*args):168 def handle_error(*args):
185 """An error in resolving a new service."""169 """An error in resolving a new service."""
186 logging.error("zeroconf ItemNew error for services, %s", args)170 logging.error("zeroconf ItemNew error for services, %s", args)
187171
188 def handle_resolved(*args):172 def handle_resolved(cb, *args):
189 """Successfully resolved a new service, which we decode and send173 """Successfully resolved a new service, which we decode and send
190 back to our calling environment with the callback function."""174 back to our calling environment with the callback function."""
191175
192 name, host, port = args[2], args[5], args[8]176 name, host, port = args[2], args[5], args[8]
193 # FIXME strip off "desktopcouch "
194 if name.startswith("desktopcouch "):177 if name.startswith("desktopcouch "):
195 add_cb(name[13:], host, port)178 cb(name[13:], host, port)
196 else:179 else:
197 logging.error("no UUID in zeroconf message, %r", name)180 logging.error("annc doesn't look like one of ours. %r", name)
198 return True181 return True
199182
200 server.ResolveService(interface, protocol, name, stype, 183 server.ResolveService(interface, protocol, name, stype,
201 domain, avahi.PROTO_UNSPEC, dbus.UInt32(0), 184 domain, avahi.PROTO_UNSPEC, dbus.UInt32(0),
202 reply_handler=handle_resolved, error_handler=handle_error)185 reply_handler=lambda *a: handle_resolved(cb, *a),
186 error_handler=handle_error)
203187
204 bus, server = get_dbus_bus_server()188 bus, server = get_dbus_bus_server()
205 domain_name = get_local_hostname()[1]189 domain_name = get_local_hostname()[1]
@@ -209,8 +193,10 @@
209193
210 sbrowser = dbus.Interface(browser_name,194 sbrowser = dbus.Interface(browser_name,
211 avahi.DBUS_INTERFACE_SERVICE_BROWSER)195 avahi.DBUS_INTERFACE_SERVICE_BROWSER)
212 sbrowser.connect_to_signal("ItemNew", new_item_handler)196 sbrowser.connect_to_signal("ItemNew",
213 sbrowser.connect_to_signal("ItemRemove", remove_item_handler)197 lambda *a: new_item_handler(add_cb, *a))
198 sbrowser.connect_to_signal("ItemRemove",
199 lambda *a: remove_item_handler(del_cb, *a))
214 sbrowser.connect_to_signal("Failure", 200 sbrowser.connect_to_signal("Failure",
215 lambda *a: logging.error("avahi error %r", a))201 lambda *a: logging.error("avahi error %r", a))
216202
@@ -220,27 +206,26 @@
220 """Start looking for services. Use two callbacks to handle seeing206 """Start looking for services. Use two callbacks to handle seeing
221 new services and seeing services disappear."""207 new services and seeing services disappear."""
222208
223 def remove_item_handler(interface, protocol, name, stype, domain, flags):209 def remove_item_handler(cb, interface, protocol, name, stype, domain, flags):
224 """A service disappeared."""210 """A service disappeared."""
225211
226 if not show_local and flags & avahi.LOOKUP_RESULT_LOCAL:212 if not show_local and flags & avahi.LOOKUP_RESULT_LOCAL:
227 return213 return
228214 cb(name)
229 del_commport_name_cb(name)215
230216 def new_item_handler(cb, interface, protocol, name, stype, domain, flags):
231 def new_item_handler(interface, protocol, name, stype, domain, flags):
232 """A service appeared."""217 """A service appeared."""
233218
234 def handle_error(*args):219 def handle_error(*args):
235 """An error in resolving a new service."""220 """An error in resolving a new service."""
236 logging.error("zeroconf ItemNew error for services, %s", args)221 logging.error("zeroconf ItemNew error for services, %s", args)
237222
238 def handle_resolved(*args):223 def handle_resolved(cb, *args):
239 """Successfully resolved a new service, which we decode and send224 """Successfully resolved a new service, which we decode and send
240 back to our calling environment with the callback function."""225 back to our calling environment with the callback function."""
241 text = avahitext_to_dict(args[9])226 text = avahitext_to_dict(args[9])
242 name, host, port = args[2], args[5], args[8]227 name, host, port = args[2], args[5], args[8]
243 add_commport_name_cb(name, text.get("description", "?"),228 cb(name, text.get("description", "?"),
244 host, port, text.get("version", None))229 host, port, text.get("version", None))
245230
246 if not show_local and flags & avahi.LOOKUP_RESULT_LOCAL:231 if not show_local and flags & avahi.LOOKUP_RESULT_LOCAL:
@@ -248,8 +233,8 @@
248233
249 server.ResolveService(interface, protocol, name, stype, 234 server.ResolveService(interface, protocol, name, stype,
250 domain, avahi.PROTO_UNSPEC, dbus.UInt32(0), 235 domain, avahi.PROTO_UNSPEC, dbus.UInt32(0),
251 reply_handler=handle_resolved, error_handler=handle_error)236 reply_handler=lambda *a: handle_resolved(cb, *a),
252237 error_handler=handle_error)
253238
254 bus, server = get_dbus_bus_server()239 bus, server = get_dbus_bus_server()
255 domain_name = get_local_hostname()[1]240 domain_name = get_local_hostname()[1]
@@ -260,7 +245,9 @@
260245
261 sbrowser = dbus.Interface(browser_name,246 sbrowser = dbus.Interface(browser_name,
262 avahi.DBUS_INTERFACE_SERVICE_BROWSER)247 avahi.DBUS_INTERFACE_SERVICE_BROWSER)
263 sbrowser.connect_to_signal("ItemNew", new_item_handler)248 sbrowser.connect_to_signal("ItemNew",
264 sbrowser.connect_to_signal("ItemRemove", remove_item_handler)249 lambda *a: new_item_handler(add_commport_name_cb, *a))
250 sbrowser.connect_to_signal("ItemRemove",
251 lambda *a: remove_item_handler(del_commport_name_cb, *a))
265 sbrowser.connect_to_signal("Failure",252 sbrowser.connect_to_signal("Failure",
266 lambda *a: logging.error("avahi error %r", a))253 lambda *a: logging.error("avahi error %r", a))
267254
=== modified file 'desktopcouch/replication.py'
--- desktopcouch/replication.py 2009-09-30 19:30:37 +0000
+++ desktopcouch/replication.py 2009-10-01 21:11:09 +0000
@@ -16,14 +16,11 @@
16#16#
17# Authors: Chad Miller <chad.miller@canonical.com>17# Authors: Chad Miller <chad.miller@canonical.com>
1818
19import threading
20import logging19import logging
21import logging.handlers
22log = logging.getLogger("replication")20log = logging.getLogger("replication")
2321
24import dbus.exceptions22import dbus.exceptions
2523
26import desktopcouch
27from desktopcouch.pair.couchdb_pairing import couchdb_io24from desktopcouch.pair.couchdb_pairing import couchdb_io
28from desktopcouch.pair.couchdb_pairing import dbus_io25from desktopcouch.pair.couchdb_pairing import dbus_io
29from desktopcouch import replication_services26from desktopcouch import replication_services
@@ -52,7 +49,7 @@
52 return getattr(mod, service_name).db_name_prefix49 return getattr(mod, service_name).db_name_prefix
53 except ImportError, e:50 except ImportError, e:
54 log.error("The service %r is unknown. It is not a "51 log.error("The service %r is unknown. It is not a "
55 "module in the %s package ." % (sn, container))52 "module in the %s package ." % (service_name, container))
56 return ""53 return ""
57 except Exception, e:54 except Exception, e:
58 log.exception("Not changing remote db name.")55 log.exception("Not changing remote db name.")
@@ -68,7 +65,7 @@
68 return getattr(mod, service_name).get_oauth_data()65 return getattr(mod, service_name).get_oauth_data()
69 except ImportError, e:66 except ImportError, e:
70 log.error("The service %r is unknown. It is not a "67 log.error("The service %r is unknown. It is not a "
71 "module in the %s package ." % (sn, container))68 "module in the %s package ." % (service_name, container))
72 return None69 return None
7370
74def do_all_replication(local_port):71def do_all_replication(local_port):
@@ -106,11 +103,12 @@
106 # push caught exception back...103 # push caught exception back...
107 except:104 except:
108 # ... so that we log it here.105 # ... so that we log it here.
109 logging.exception("failed to unpair from other end.")106 logging.exception(
107 "failed to unpair from other end.")
110 continue108 continue
111 else:109 else:
112 # Finally, find your inner peace...110 # Finally, find your inner peace...
113 couchdb_io.expunge_pairing(remote_identifier)111 couchdb_io.expunge_pairing(remote_hostid)
114 # ...and move on.112 # ...and move on.
115 continue113 continue
116114
@@ -184,10 +182,12 @@
184 log.error("skipping %r on %s. %s", db_name, sn, e)182 log.error("skipping %r on %s. %s", db_name, sn, e)
185 continue183 continue
186184
187 db_name = remote_db_name[1+len(str(remote_db_name_prefix)):]185 prefix_len = len(str(remote_db_name_prefix))
186 db_name = remote_db_name[1+prefix_len:]
188 if db_name.strip("/") == "management":187 if db_name.strip("/") == "management":
189 continue # be paranoid about what we accept.188 continue # be paranoid about what we accept.
190 log.debug("want to replipull %r from static host %r @ %s",189 log.debug(
190 "want to replipull %r from static host %r @ %s",
191 db_name, remote_hostid, addr)191 db_name, remote_hostid, addr)
192 couchdb_io.replicate(remote_db_name, db_name,192 couchdb_io.replicate(remote_db_name, db_name,
193 source_host=addr, source_port=port, 193 source_host=addr, source_port=port,
@@ -224,8 +224,6 @@
224 "our zeroconf advert. %s", e)224 "our zeroconf advert. %s", e)
225 return None225 return None
226226
227 dbus_io.discover_services(None, None, True)
228
229 dbus_io.maintain_discovered_servers()227 dbus_io.maintain_discovered_servers()
230228
231 t = task.LoopingCall(replicate_local_databases_to_paired_hosts, port)229 t = task.LoopingCall(replicate_local_databases_to_paired_hosts, port)

Subscribers

People subscribed via source and target branches