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