From 9aa663969e311bb623bc36006a3ee82cd17cba32 Mon Sep 17 00:00:00 2001 From: Alessandro Strada Date: Sat, 19 Nov 2016 14:22:51 +0100 Subject: [PATCH] Refactor metadata locking --- src/context.ml | 6 ++++++ src/drive.ml | 11 ++++------- src/gdfuse.ml | 1 + 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/context.ml b/src/context.ml index 11c9fde..297aab9 100644 --- a/src/context.ml +++ b/src/context.ml @@ -22,6 +22,8 @@ type t = { mountpoint_stats : Unix.LargeFile.stats; (* Current metadata *) metadata : Cache.Metadata.t option; + (* Metadata lock *) + metadata_lock : Mutex.t; (* Whether permanently delete files *) skip_trash : bool; (* Memory buffers *) @@ -62,6 +64,10 @@ let metadata = { GapiLens.get = (fun x -> x.metadata); GapiLens.set = (fun v x -> { x with metadata = v }) } +let metadata_lock = { + GapiLens.get = (fun x -> x.metadata_lock); + GapiLens.set = (fun v x -> { x with metadata_lock = v }) +} let skip_trash = { GapiLens.get = (fun x -> x.skip_trash); GapiLens.set = (fun v x -> { x with skip_trash = v }) diff --git a/src/drive.ml b/src/drive.ml index d346e03..5f074be 100644 --- a/src/drive.ml +++ b/src/drive.ml @@ -387,9 +387,6 @@ let get_root_resource trashed = inserted | Some resource -> resource -let metadata_mutex = Mutex.create () -let with_metadata_lock f = Utils.with_lock metadata_mutex f - let update_cache_size delta metadata cache = Utils.log_with_header "BEGIN: Updating cache size (delta=%Ld) in db\n%!" delta; @@ -415,7 +412,7 @@ let shrink_cache ?(file_size = 0L) () = let config = context |. Context.config_lens in let max_cache_size_mb = config.Config.max_cache_size_mb in let cache = context.Context.cache in - with_metadata_lock + Utils.with_lock context.Context.metadata_lock (fun () -> let max_cache_size = Int64.mul (Int64.of_int max_cache_size_mb) Utils.mb in @@ -486,14 +483,14 @@ let delete_cached_resources metadata cache resources = resources let update_cache_size_for_documents cache resource content_path op = - with_metadata_lock + let context = Context.get_ctx () in + Utils.with_lock context.Context.metadata_lock (fun () -> if resource.Cache.Resource.size = Some 0L && Sys.file_exists content_path then begin try let stats = Unix.LargeFile.stat content_path in let size = stats.Unix.LargeFile.st_size in - let context = Context.get_ctx () in let metadata = context |. Context.metadata_lens in let delta = op size in update_cache_size delta metadata cache @@ -740,7 +737,7 @@ let get_metadata () = db_metadata |> Cache.Metadata.cache_size ^= cache_size in - with_metadata_lock + Utils.with_lock context.Context.metadata_lock (fun () -> let metadata = let context = Context.get_ctx () in diff --git a/src/gdfuse.ml b/src/gdfuse.ml index 2f78147..ea3d4af 100644 --- a/src/gdfuse.ml +++ b/src/gdfuse.ml @@ -229,6 +229,7 @@ let setup_application params = curl_state; mountpoint_stats = Unix.LargeFile.stat mountpoint; metadata = None; + metadata_lock = Mutex.create (); skip_trash = params.skip_trash; memory_buffers = Buffering.MemoryBuffers.create config.Config.memory_buffer_size;