]> git.arvados.org - arvados.git/blob - services/fuse/bin/arv-mount
3137: Change --stats to --crunchstat-interval as specified on the ticket.
[arvados.git] / services / fuse / bin / arv-mount
1 #!/usr/bin/env python
2
3 import argparse
4 import arvados
5 import daemon
6 import logging
7 import os
8 import signal
9 import subprocess
10 import time
11
12 import arvados.commands._util as arv_cmd
13 from arvados_fuse import *
14 from arvados.safeapi import ThreadSafeApiCache
15 import arvados.keep
16
17 logger = logging.getLogger('arvados.arv-mount')
18
19 class Stat(object):
20     def __init__(self, prefix, interval,
21                  egr_name, ing_name,
22                  egr_func, ing_func):
23         self.prefix = prefix
24         self.interval = interval
25         self.egr_name = egr_name
26         self.ing_name = ing_name
27         self.egress = egr_func
28         self.ingress = ing_func
29         self.egr = self.egress()
30         self.ing = self.ingress()
31
32     def update(self):
33         self.egr_prev = self.egr
34         self.ing_prev = self.ing
35         self.egr = self.egress()
36         self.ing = self.ingress()
37
38         delta = " -- interval %.4f seconds %d %s %d %s" % (self.interval,
39                                                            self.egr-self.egr_prev,
40                                                            self.egr_name,
41                                                            self.ing-self.ing_prev,
42                                                            self.ing_name)
43
44         sys.stderr.write("crunchstat: %s %d %s %d %s%s\n" % (self.prefix,
45                                                              self.egr,
46                                                              self.egr_name,
47                                                              self.ing,
48                                                              self.ing_name,
49                                                              delta))
50
51 def statlogger(interval, keep, ops):
52     calls = Stat("keepcalls", interval, "put", "get",
53                  keep.put_counter.get,
54                  keep.get_counter.get)
55     net = Stat("net:keep0", interval, "tx", "rx",
56                keep.upload_counter.get,
57                keep.download_counter.get)
58     cache = Stat("keepcache", interval, "hit", "miss",
59                keep.hits_counter.get,
60                keep.misses_counter.get)
61     fuseops = Stat("fuseops", interval,"write", "read",
62                    ops.write_ops_counter.get,
63                    ops.read_ops_counter.get)
64     blk = Stat("blkio:0:0", interval, "write", "read",
65                ops.write_counter.get,
66                ops.read_counter.get)
67
68     while True:
69         time.sleep(interval)
70         calls.update()
71         net.update()
72         cache.update()
73         fuseops.update()
74         blk.update()
75
76
77 if __name__ == '__main__':
78     # Handle command line parameters
79     parser = argparse.ArgumentParser(
80         parents=[arv_cmd.retry_opt],
81         description='''Mount Keep data under the local filesystem.  Default mode is --home''',
82         epilog="""
83 Note: When using the --exec feature, you must either specify the
84 mountpoint before --exec, or mark the end of your --exec arguments
85 with "--".
86 """)
87     parser.add_argument('mountpoint', type=str, help="""Mount point.""")
88     parser.add_argument('--allow-other', action='store_true',
89                         help="""Let other users read the mount""")
90
91     mount_mode = parser.add_mutually_exclusive_group()
92
93     mount_mode.add_argument('--all', action='store_true', help="""Mount a subdirectory for each mode: home, shared, by_tag, by_id (default).""")
94     mount_mode.add_argument('--home', action='store_true', help="""Mount only the user's home project.""")
95     mount_mode.add_argument('--shared', action='store_true', help="""Mount only list of projects shared with the user.""")
96     mount_mode.add_argument('--by-tag', action='store_true',
97                             help="""Mount subdirectories listed by tag.""")
98     mount_mode.add_argument('--by-id', action='store_true',
99                             help="""Mount subdirectories listed by portable data hash or uuid.""")
100     mount_mode.add_argument('--by-pdh', action='store_true',
101                             help="""Mount subdirectories listed by portable data hash.""")
102     mount_mode.add_argument('--project', type=str, help="""Mount a specific project.""")
103     mount_mode.add_argument('--collection', type=str, help="""Mount only the specified collection.""")
104
105     parser.add_argument('--debug', action='store_true', help="""Debug mode""")
106     parser.add_argument('--logfile', help="""Write debug logs and errors to the specified file (default stderr).""")
107     parser.add_argument('--foreground', action='store_true', help="""Run in foreground (default is to daemonize unless --exec specified)""", default=False)
108     parser.add_argument('--encoding', type=str, help="Character encoding to use for filesystem, default is utf-8 (see Python codec registry for list of available encodings)", default="utf-8")
109
110     parser.add_argument('--file-cache', type=int, help="File data cache size, in bytes (default 256MiB)", default=256*1024*1024)
111     parser.add_argument('--directory-cache', type=int, help="Directory data cache size, in bytes (default 128MiB)", default=128*1024*1024)
112
113     parser.add_argument('--read-only', action='store_false', help="Mount will be read only (default)", dest="enable_write", default=False)
114     parser.add_argument('--read-write', action='store_true', help="Mount will be read-write", dest="enable_write", default=False)
115
116     parser.add_argument('--crunchstat-interval', type=float, help="Write stats to stderr every N seconds (default disabled)", default=0)
117
118     parser.add_argument('--exec', type=str, nargs=argparse.REMAINDER,
119                         dest="exec_args", metavar=('command', 'args', '...', '--'),
120                         help="""Mount, run a command, then unmount and exit""")
121
122     args = parser.parse_args()
123     args.mountpoint = os.path.realpath(args.mountpoint)
124     if args.logfile:
125         args.logfile = os.path.realpath(args.logfile)
126
127     # Daemonize as early as possible, so we don't accidentally close
128     # file descriptors we're using.
129     if not (args.exec_args or args.foreground):
130         os.chdir(args.mountpoint)
131         daemon_ctx = daemon.DaemonContext(working_directory='.')
132         daemon_ctx.open()
133     else:
134         daemon_ctx = None
135
136     # Configure a log handler based on command-line switches.
137     if args.logfile:
138         log_handler = logging.FileHandler(args.logfile)
139     elif daemon_ctx:
140         log_handler = logging.NullHandler()
141     else:
142         log_handler = None
143
144     if log_handler is not None:
145         arvados.logger.removeHandler(arvados.log_handler)
146         arvados.logger.addHandler(log_handler)
147
148     if args.debug:
149         arvados.logger.setLevel(logging.DEBUG)
150         logger.debug("arv-mount debugging enabled")
151
152     logger.info("enable write is %s", args.enable_write)
153
154     try:
155         # Create the request handler
156         operations = Operations(os.getuid(),
157                                 os.getgid(),
158                                 encoding=args.encoding,
159                                 inode_cache=InodeCache(cap=args.directory_cache),
160                                 enable_write=args.enable_write)
161         api = ThreadSafeApiCache(apiconfig=arvados.config.settings(),
162                                  keep_params={"block_cache": arvados.keep.KeepBlockCache(args.file_cache)})
163
164         if args.crunchstat_interval:
165             statsthread = threading.Thread(target=statlogger, args=(args.crunchstat_interval, api.keep, operations))
166             statsthread.daemon = True
167             statsthread.start()
168
169         usr = api.users().current().execute(num_retries=args.retries)
170         now = time.time()
171         dir_class = None
172         dir_args = [llfuse.ROOT_INODE, operations.inodes, api, args.retries]
173         if args.by_id or args.by_pdh:
174             # Set up the request handler with the 'magic directory' at the root
175             dir_class = MagicDirectory
176             dir_args.append(args.by_pdh)
177         elif args.by_tag:
178             dir_class = TagsDirectory
179         elif args.shared:
180             dir_class = SharedDirectory
181             dir_args.append(usr)
182         elif args.home:
183             dir_class = ProjectDirectory
184             dir_args.append(usr)
185             dir_args.append(True)
186         elif args.collection is not None:
187             # Set up the request handler with the collection at the root
188             dir_class = CollectionDirectory
189             dir_args.append(args.collection)
190         elif args.project is not None:
191             dir_class = ProjectDirectory
192             dir_args.append(api.groups().get(uuid=args.project).execute(
193                     num_retries=args.retries))
194
195         if dir_class is not None:
196             operations.inodes.add_entry(dir_class(*dir_args))
197         else:
198             e = operations.inodes.add_entry(Directory(llfuse.ROOT_INODE, operations.inodes))
199             dir_args[0] = e.inode
200
201             e._entries['by_id'] = operations.inodes.add_entry(MagicDirectory(*dir_args))
202
203             e._entries['by_tag'] = operations.inodes.add_entry(TagsDirectory(*dir_args))
204
205             dir_args.append(usr)
206             dir_args.append(True)
207             e._entries['home'] = operations.inodes.add_entry(ProjectDirectory(*dir_args))
208             e._entries['shared'] = operations.inodes.add_entry(SharedDirectory(*dir_args))
209
210             text = '''
211 Welcome to Arvados!  This directory provides file system access to files and objects
212 available on the Arvados installation located at '{}'
213 using credentials for user '{}'.
214
215 From here, the following directories are available:
216
217   by_id/     Access to Keep collections by uuid or portable data hash (see by_id/README for details).
218   by_tag/    Access to Keep collections organized by tag.
219   home/      The contents of your home project.
220   shared/    Projects shared with you.
221 '''.format(arvados.config.get('ARVADOS_API_HOST'), usr['email'])
222
223             e._entries["README"] = operations.inodes.add_entry(StringFile(e.inode, text, now))
224
225
226     except Exception:
227         logger.exception("arv-mount: exception during API setup")
228         exit(1)
229
230     # FUSE options, see mount.fuse(8)
231     opts = [optname for optname in ['allow_other', 'debug']
232             if getattr(args, optname)]
233
234     # Increase default read/write size from 4KiB to 128KiB
235     opts += ["big_writes", "max_read=131072"]
236
237     if args.exec_args:
238         # Initialize the fuse connection
239         llfuse.init(operations, args.mountpoint, opts)
240
241         # Subscribe to change events from API server
242         if not args.by_pdh:
243             operations.listen_for_events(api)
244
245         t = threading.Thread(None, lambda: llfuse.main())
246         t.start()
247
248         # wait until the driver is finished initializing
249         operations.initlock.wait()
250
251         rc = 255
252         try:
253             sp = subprocess.Popen(args.exec_args, shell=False)
254
255             # forward signals to the process.
256             signal.signal(signal.SIGINT, lambda signum, frame: sp.send_signal(signum))
257             signal.signal(signal.SIGTERM, lambda signum, frame: sp.send_signal(signum))
258             signal.signal(signal.SIGQUIT, lambda signum, frame: sp.send_signal(signum))
259
260             # wait for process to complete.
261             rc = sp.wait()
262
263             # restore default signal handlers.
264             signal.signal(signal.SIGINT, signal.SIG_DFL)
265             signal.signal(signal.SIGTERM, signal.SIG_DFL)
266             signal.signal(signal.SIGQUIT, signal.SIG_DFL)
267         except Exception as e:
268             logger.exception('arv-mount: exception during exec %s',
269                              args.exec_args)
270             try:
271                 rc = e.errno
272             except AttributeError:
273                 pass
274         finally:
275             subprocess.call(["fusermount", "-u", "-z", args.mountpoint])
276             operations.destroy()
277
278         exit(rc)
279     else:
280         try:
281             llfuse.init(operations, args.mountpoint, opts)
282
283             # Subscribe to change events from API server
284             operations.listen_for_events(api)
285
286             llfuse.main()
287         except Exception as e:
288             logger.exception('arv-mount: exception during mount')
289             exit(getattr(e, 'errno', 1))
290         finally:
291             operations.destroy()