Commit 0ed611a3 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent a384567c
...@@ -38,7 +38,8 @@ import logging as log ...@@ -38,7 +38,8 @@ import logging as log
from os.path import dirname from os.path import dirname
from errno import ENOENT, EEXIST from errno import ENOENT, EEXIST
from golang import go, chan, select, default from golang import chan, select, default
from golang import sync, context
from golang.gcompat import qq from golang.gcompat import qq
from ZODB.FileStorage import FileStorage from ZODB.FileStorage import FileStorage
...@@ -51,11 +52,16 @@ class Conn(object): ...@@ -51,11 +52,16 @@ class Conn(object):
# .mountpoint path to wcfs mountpoint # .mountpoint path to wcfs mountpoint
# ._fwcfs /.wcfs/zurl opened to keep the server from going away (at least cleanly) # ._fwcfs /.wcfs/zurl opened to keep the server from going away (at least cleanly)
def __init__(self, mountpoint, fwcfs): # XXX 4testing only?
# ._proc wcfs process if it was opened by this conn | None
def __init__(self, mountpoint, fwcfs, proc):
self.mountpoint = mountpoint self.mountpoint = mountpoint
self._fwcfs = fwcfs self._fwcfs = fwcfs
self._proc = proc
def close(self): def close(self):
# XXX unmount wcfs as well?
self._fwcfs.close() self._fwcfs.close()
# open creates wcfs file handle, which can be mmaped to give data of ZBigFile. # open creates wcfs file handle, which can be mmaped to give data of ZBigFile.
...@@ -171,7 +177,7 @@ def join(zurl, autostart=_default_autostart(), shared=False): # -> Conn ...@@ -171,7 +177,7 @@ def join(zurl, autostart=_default_autostart(), shared=False): # -> Conn
raise raise
else: else:
# already have it # already have it
return Conn(mntpt, f) return Conn(mntpt, f, None)
if not autostart: if not autostart:
raise RuntimeError("wcfs: join %s: server not started" % zurl) raise RuntimeError("wcfs: join %s: server not started" % zurl)
...@@ -190,51 +196,40 @@ def _start(zurl, *optv): # -> Conn ...@@ -190,51 +196,40 @@ def _start(zurl, *optv): # -> Conn
mntpt = _mntpt_4zurl(zurl) mntpt = _mntpt_4zurl(zurl)
log.info("wcfs: starting for %s ...", zurl) log.info("wcfs: starting for %s ...", zurl)
cancel = chan() # cancels wcfs server running (and waitmounted in initialization phase) # XXX errctx "wcfs: start"
startedok = chan() # indicates to wcfs server that whole startup was ok
# spawn spawns and monitors wcfs server. it is running until either wcfs # spawn wcfs and wait till filesystem-level access to it is ready
# server terminates, or cancel or startedok are ready. conn = Conn(mntpt, None, None)
ewcfs = chan(1) # err | None wg = sync.WorkGroup(context.background())
def spawn(): fsready = chan()
err = None def _(ctx):
try: # XXX errctx "spawn"
argv = [_wcfs_exe()] + list(optv) + [zurl, mntpt] argv = [_wcfs_exe()] + list(optv) + [zurl, mntpt]
p = subprocess.Popen(argv, close_fds=True) proc = subprocess.Popen(argv, close_fds=True)
while 1: while 1:
ret = p.poll() ret = proc.poll()
if ret is not None: if ret is not None:
err = "spawn: exited with %s" % ret raise "exited with %s" % ret
break
_, _rx = select( _, _rx = select(
cancel.recv, # 0 ctx.done().recv, # 0
startedok.recv, # 1 fsready.recv, # 1
default, # 2 default, # 2
) )
if _ == 0: if _ == 0:
p.terminate() proc.terminate()
break raise ctx.err()
if _ == 1: if _ == 1:
# startup was ok - don't monitor spawned wcfs any longer # startup was ok - don't monitor spawned wcfs any longer
break conn._proc = proc
return
time.sleep(0.1) time.sleep(0.1)
wg.go(_)
except Exception as e: def _(ctx):
log.exception("wcfs server") # XXX errctx "waitmount"
err = "spawn: %s" % e # XXX wrap with errctx
ewcfs.send(err)
# waitmounted waits till wcfs mount is ready.
mounted = chan(1) # file | err
def waitmounted():
res = None
# XXX errctx
try:
while 1: while 1:
try: try:
f = open("%s/.wcfs/zurl" % mntpt) f = open("%s/.wcfs/zurl" % mntpt)
...@@ -242,61 +237,26 @@ def _start(zurl, *optv): # -> Conn ...@@ -242,61 +237,26 @@ def _start(zurl, *optv): # -> Conn
if e.errno != ENOENT: if e.errno != ENOENT:
raise raise
else: else:
res = f
dotwcfs = f.read() dotwcfs = f.read()
if dotwcfs != zurl: if dotwcfs != zurl:
raise RuntimeError(".wcfs/zurl != zurl (%s != %s)" % (qq(dotwcfs), qq(zurl))) raise RuntimeError(".wcfs/zurl != zurl (%s != %s)" % (qq(dotwcfs), qq(zurl)))
break
conn._fwcfs = f
fsready.close()
return
_, _rx = select( _, _rx = select(
cancel.recv, # 0 ctx.done().recv, # 0
default, # 1 default, # 1
) )
if _ == 0: if _ == 0:
res = "waitmounted: cancel" raise ctx.err()
break
time.sleep(0.1) time.sleep(0.1)
wg.go(_)
except Exception as e: wg.wait()
res = "waitmounted: %s" % e # XXX errctx return conn
mounted.send(res)
# spawn wcfs and wait till it is mounted.
go(spawn)
go(waitmounted)
_, _rx = select(
ewcfs.recv, # 0
mounted.recv, # 1
)
if _ == 0:
# wcfs error
err = _rx
if _ == 1:
if isinstance(_rx, file):
# mounted ok - return Conn object.
#
# NOTE: we tell `spawn` thread to exit and stop monitoring spawned
# wcfs, because we want spawned wcfs to potentially overlive our
# process and to serve other processes. For the same reason we do
# not preserve cancel channel in returned Conn.
f = _rx
startedok.close()
return Conn(mntpt, f)
# waitmounted error
err = _rx
cancel.close()
raise RuntimeError("wcfs: start: %s" % err) # XXX errctx
# _wcfs_exe returns path to wcfs executable. # _wcfs_exe returns path to wcfs executable.
......
...@@ -78,7 +78,7 @@ def setup_function(f): ...@@ -78,7 +78,7 @@ def setup_function(f):
# make sure we unmount wcfs after every test. # make sure we unmount wcfs after every test.
def teardown_function(f): def teardown_function(f):
mounted = not subprocess.call(["mountpoint", "-q", testmntpt]) mounted = (0 == subprocess.call(["mountpoint", "-q", testmntpt]))
if mounted: if mounted:
subprocess.check_call(["fusermount", "-u", testmntpt]) subprocess.check_call(["fusermount", "-u", testmntpt])
if os.path.exists(testmntpt): if os.path.exists(testmntpt):
...@@ -188,7 +188,10 @@ class DFile: ...@@ -188,7 +188,10 @@ class DFile:
class tDB: class tDB:
def __init__(t): def __init__(t):
t.root = testdb.dbopen() t.root = testdb.dbopen()
assert not os.path.exists(testmntpt)
t.wc = wcfs.join(testzurl, autostart=True) t.wc = wcfs.join(testzurl, autostart=True)
assert os.path.exists(testmntpt)
assert 0 == subprocess.call(["mountpoint", "-q", testmntpt])
# ZBigFile(s) scheduled for commit # ZBigFile(s) scheduled for commit
t._changed = {} # ZBigFile -> {} blk -> data t._changed = {} # ZBigFile -> {} blk -> data
...@@ -227,6 +230,27 @@ class tDB: ...@@ -227,6 +230,27 @@ class tDB:
# it also prints change history to help developer overview current testcase. # it also prints change history to help developer overview current testcase.
@func @func
def close(t): def close(t):
# unmount and wait for wcfs to exit
def _():
assert 0 == subprocess.call(["mountpoint", "-q", testmntpt])
subprocess.check_call(["fusermount", "-u", testmntpt])
wg = sync.WorkGroup(timeout())
def _(ctx):
while 1:
if ready(ctx.done()):
raise ctx.err()
ret = t.wc._proc.poll()
if ret is not None:
return
tdelay()
wg.go(_)
wg.wait()
assert 0 != subprocess.call(["mountpoint", "-q", testmntpt])
os.rmdir(testmntpt)
defer(_)
defer(t.dump_history) defer(t.dump_history)
for tf in t._files.copy(): for tf in t._files.copy():
tf.close() tf.close()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment