2020-12-30 14:38:00 +01:00
|
|
|
;; This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
;;
|
|
|
|
;; This Source Code Form is "Incompatible With Secondary Licenses", as
|
|
|
|
;; defined by the Mozilla Public License, v. 2.0.
|
|
|
|
;;
|
|
|
|
;; Copyright (c) 2020 UXBOX Labs SL
|
|
|
|
|
|
|
|
(ns app.storage
|
|
|
|
"File Storage abstraction layer."
|
|
|
|
(:require
|
|
|
|
[app.common.data :as d]
|
|
|
|
[app.common.exceptions :as ex]
|
|
|
|
[app.common.spec :as us]
|
|
|
|
[app.common.uuid :as uuid]
|
|
|
|
[app.config :as cfg]
|
|
|
|
[app.db :as db]
|
2021-01-04 18:41:05 +01:00
|
|
|
[app.storage.db :as sdb]
|
2020-12-30 14:38:00 +01:00
|
|
|
[app.storage.fs :as sfs]
|
|
|
|
[app.storage.impl :as impl]
|
|
|
|
[app.storage.s3 :as ss3]
|
|
|
|
[app.util.time :as dt]
|
2021-01-04 18:41:05 +01:00
|
|
|
[app.worker :as wrk]
|
2020-12-30 14:38:00 +01:00
|
|
|
[clojure.spec.alpha :as s]
|
|
|
|
[cuerdas.core :as str]
|
2021-01-04 18:41:05 +01:00
|
|
|
[integrant.core :as ig]
|
|
|
|
[lambdaisland.uri :as u]
|
2021-01-19 14:54:34 +01:00
|
|
|
[promesa.exec :as px])
|
|
|
|
(:import
|
|
|
|
java.io.InputStream))
|
|
|
|
|
2020-12-30 14:38:00 +01:00
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Storage Module State
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
(s/def ::backend ::us/keyword)
|
2021-01-25 15:22:39 +01:00
|
|
|
|
|
|
|
(s/def ::s3 ::ss3/backend)
|
|
|
|
(s/def ::fs ::sfs/backend)
|
|
|
|
(s/def ::db ::sdb/backend)
|
|
|
|
|
2020-12-30 14:38:00 +01:00
|
|
|
(s/def ::backends
|
2021-01-25 15:22:39 +01:00
|
|
|
(s/keys :opt-un [::s3 ::fs ::db]))
|
2020-12-30 14:38:00 +01:00
|
|
|
|
|
|
|
(defmethod ig/pre-init-spec ::storage [_]
|
2021-01-04 18:41:05 +01:00
|
|
|
(s/keys :req-un [::backend ::wrk/executor ::db/pool ::backends]))
|
2020-12-30 14:38:00 +01:00
|
|
|
|
|
|
|
(defmethod ig/prep-key ::storage
|
|
|
|
[_ {:keys [backends] :as cfg}]
|
2021-01-04 18:41:05 +01:00
|
|
|
(-> (d/without-nils cfg)
|
|
|
|
(assoc :backends (d/without-nils backends))))
|
2020-12-30 14:38:00 +01:00
|
|
|
|
|
|
|
(defmethod ig/init-key ::storage
|
2021-01-04 18:41:05 +01:00
|
|
|
[_ cfg]
|
|
|
|
cfg)
|
2020-12-30 14:38:00 +01:00
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Database Objects
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2021-01-25 15:22:39 +01:00
|
|
|
(defrecord StorageObject [id size created-at expired-at backend])
|
2020-12-30 14:38:00 +01:00
|
|
|
|
|
|
|
(def ^:private
|
|
|
|
sql:insert-storage-object
|
|
|
|
"insert into storage_object (id, size, backend, metadata)
|
|
|
|
values (?, ?, ?, ?::jsonb)
|
|
|
|
returning *")
|
|
|
|
|
2021-01-25 15:22:39 +01:00
|
|
|
(def ^:private
|
|
|
|
sql:insert-storage-object-with-expiration
|
|
|
|
"insert into storage_object (id, size, backend, metadata, deleted_at)
|
|
|
|
values (?, ?, ?, ?::jsonb, ?)
|
|
|
|
returning *")
|
|
|
|
|
|
|
|
(defn- insert-object
|
|
|
|
[conn id size backend mdata expiration]
|
|
|
|
(if expiration
|
|
|
|
(db/exec-one! conn [sql:insert-storage-object-with-expiration id size backend mdata expiration])
|
|
|
|
(db/exec-one! conn [sql:insert-storage-object id size backend mdata])))
|
|
|
|
|
2020-12-30 14:38:00 +01:00
|
|
|
(defn- create-database-object
|
2021-01-04 18:41:05 +01:00
|
|
|
[{:keys [conn backend]} {:keys [content] :as object}]
|
|
|
|
(if (instance? StorageObject object)
|
|
|
|
(let [id (uuid/random)
|
|
|
|
mdata (meta object)
|
2021-01-25 15:22:39 +01:00
|
|
|
result (insert-object conn
|
|
|
|
id
|
|
|
|
(:size object)
|
|
|
|
(name backend)
|
|
|
|
(db/tjson mdata)
|
|
|
|
(:expired-at object))]
|
2021-01-04 18:41:05 +01:00
|
|
|
(assoc object
|
|
|
|
:id (:id result)
|
2021-01-19 13:43:09 +01:00
|
|
|
:backend backend
|
2021-01-04 18:41:05 +01:00
|
|
|
:created-at (:created-at result)))
|
|
|
|
(let [id (uuid/random)
|
2021-01-25 15:22:39 +01:00
|
|
|
mdata (dissoc object :content :expired-at)
|
|
|
|
result (insert-object conn
|
|
|
|
id
|
|
|
|
(count content)
|
|
|
|
(name backend)
|
|
|
|
(db/tjson mdata)
|
|
|
|
(:expired-at object))]
|
2021-01-04 18:41:05 +01:00
|
|
|
(StorageObject. (:id result)
|
|
|
|
(:size result)
|
|
|
|
(:created-at result)
|
2021-01-25 15:22:39 +01:00
|
|
|
(:deleted-at result)
|
2021-01-04 18:41:05 +01:00
|
|
|
backend
|
|
|
|
mdata
|
|
|
|
nil))))
|
2020-12-30 14:38:00 +01:00
|
|
|
|
|
|
|
(def ^:private sql:retrieve-storage-object
|
2021-01-25 15:22:39 +01:00
|
|
|
"select * from storage_object where id = ? and (deleted_at is null or deleted_at > now())")
|
2020-12-30 14:38:00 +01:00
|
|
|
|
2021-01-19 15:04:28 +01:00
|
|
|
(defn row->storage-object [res]
|
|
|
|
(let [mdata (some-> (:metadata res) (db/decode-transit-pgobject))]
|
|
|
|
(StorageObject. (:id res)
|
|
|
|
(:size res)
|
|
|
|
(:created-at res)
|
2021-01-25 15:22:39 +01:00
|
|
|
(:deleted-at res)
|
2021-01-19 15:04:28 +01:00
|
|
|
(keyword (:backend res))
|
|
|
|
mdata
|
|
|
|
nil)))
|
|
|
|
|
2020-12-30 14:38:00 +01:00
|
|
|
(defn- retrieve-database-object
|
2021-01-04 18:41:05 +01:00
|
|
|
[{:keys [conn] :as storage} id]
|
2020-12-30 14:38:00 +01:00
|
|
|
(when-let [res (db/exec-one! conn [sql:retrieve-storage-object id])]
|
2021-01-19 15:04:28 +01:00
|
|
|
(row->storage-object res)))
|
2020-12-30 14:38:00 +01:00
|
|
|
|
|
|
|
(def sql:delete-storage-object
|
2021-01-25 15:22:39 +01:00
|
|
|
"update storage_object set deleted_at=now() where id=?")
|
2020-12-30 14:38:00 +01:00
|
|
|
|
|
|
|
(defn- delete-database-object
|
2021-01-04 18:41:05 +01:00
|
|
|
[{:keys [conn] :as storage} id]
|
2020-12-30 14:38:00 +01:00
|
|
|
(let [result (db/exec-one! conn [sql:delete-storage-object id])]
|
|
|
|
(pos? (:next.jdbc/update-count result))))
|
|
|
|
|
2021-01-04 18:41:05 +01:00
|
|
|
(defn- register-recheck
|
|
|
|
[{:keys [pool] :as storage} backend id]
|
|
|
|
(db/insert! pool :storage-pending {:id id :backend (name backend)}))
|
|
|
|
|
2020-12-30 14:38:00 +01:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; API
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
(declare resolve-backend)
|
|
|
|
|
2021-01-04 18:41:05 +01:00
|
|
|
(defn content
|
|
|
|
([data] (impl/content data nil))
|
|
|
|
([data size] (impl/content data size)))
|
2020-12-30 14:38:00 +01:00
|
|
|
|
|
|
|
(defn get-object
|
2021-01-04 18:41:05 +01:00
|
|
|
[{:keys [conn pool] :as storage} id]
|
|
|
|
(-> (assoc storage :conn (or conn pool))
|
|
|
|
(retrieve-database-object id)))
|
2020-12-30 14:38:00 +01:00
|
|
|
|
|
|
|
(defn put-object
|
2021-01-19 13:43:09 +01:00
|
|
|
[{:keys [pool conn backend executor] :as storage} {:keys [content] :as params}]
|
2021-01-04 18:41:05 +01:00
|
|
|
(us/assert impl/content? content)
|
|
|
|
(let [storage (assoc storage :conn (or conn pool))
|
2021-01-19 13:43:09 +01:00
|
|
|
object (create-database-object storage params)]
|
2021-01-04 18:41:05 +01:00
|
|
|
|
|
|
|
;; Schedule to execute in background; in an other transaction and
|
|
|
|
;; register the currently created storage object id for a later
|
|
|
|
;; recheck.
|
|
|
|
(px/run! executor #(register-recheck storage backend (:id object)))
|
|
|
|
|
|
|
|
;; Store the data finally on the underlying storage subsystem.
|
2020-12-30 14:38:00 +01:00
|
|
|
(-> (resolve-backend storage backend)
|
|
|
|
(impl/put-object object content))
|
2021-01-04 18:41:05 +01:00
|
|
|
|
2020-12-30 14:38:00 +01:00
|
|
|
object))
|
|
|
|
|
2021-01-04 18:41:05 +01:00
|
|
|
(defn clone-object
|
|
|
|
[{:keys [pool conn executor] :as storage} object]
|
|
|
|
(let [storage (assoc storage :conn (or conn pool))
|
|
|
|
object* (create-database-object storage object)]
|
2021-01-19 13:43:09 +01:00
|
|
|
(if (= (:backend object) (:backend storage))
|
|
|
|
;; if the source and destination backends are the same, we
|
|
|
|
;; proceed to use the fast path with specific copy
|
|
|
|
;; implementation on backend.
|
2021-01-04 18:41:05 +01:00
|
|
|
(-> (resolve-backend storage (:backend storage))
|
2021-01-19 13:43:09 +01:00
|
|
|
(impl/copy-object object object*))
|
|
|
|
|
|
|
|
;; if the source and destination backends are different, we just
|
|
|
|
;; need to obtain the streams and proceed full copy of the data
|
2021-01-19 14:54:34 +01:00
|
|
|
(with-open [^InputStream input
|
|
|
|
(-> (resolve-backend storage (:backend object))
|
|
|
|
(impl/get-object-data object))]
|
2021-01-19 13:43:09 +01:00
|
|
|
(-> (resolve-backend storage (:backend storage))
|
|
|
|
(impl/put-object object* (impl/content input (:size object))))))
|
2021-01-04 18:41:05 +01:00
|
|
|
|
2021-01-19 13:43:09 +01:00
|
|
|
object*))
|
2021-01-04 18:41:05 +01:00
|
|
|
|
2020-12-30 14:38:00 +01:00
|
|
|
(defn get-object-data
|
|
|
|
[{:keys [pool conn] :as storage} object]
|
2021-01-04 18:41:05 +01:00
|
|
|
(-> (assoc storage :conn (or conn pool))
|
|
|
|
(resolve-backend (:backend object))
|
|
|
|
(impl/get-object-data object)))
|
2020-12-30 14:38:00 +01:00
|
|
|
|
|
|
|
(defn get-object-url
|
|
|
|
([storage object]
|
|
|
|
(get-object-url storage object nil))
|
2021-01-04 18:41:05 +01:00
|
|
|
([{:keys [conn pool] :as storage} object options]
|
|
|
|
(-> (assoc storage :conn (or conn pool))
|
|
|
|
(resolve-backend (:backend object))
|
2020-12-30 14:38:00 +01:00
|
|
|
(impl/get-object-url object options))))
|
|
|
|
|
2021-01-25 15:22:39 +01:00
|
|
|
(defn object->path
|
|
|
|
[{:keys [id] :as obj}]
|
|
|
|
(impl/id->path id))
|
|
|
|
|
2020-12-30 14:38:00 +01:00
|
|
|
(defn del-object
|
2021-01-25 15:22:39 +01:00
|
|
|
[{:keys [conn pool] :as storage} id-or-obj]
|
2021-01-04 18:41:05 +01:00
|
|
|
(-> (assoc storage :conn (or conn pool))
|
2021-01-25 15:22:39 +01:00
|
|
|
(delete-database-object (if (uuid? id-or-obj) id-or-obj (:id id-or-obj)))))
|
|
|
|
|
|
|
|
(defn put-tmp-object
|
|
|
|
"A special function for create an object explicitly setting the TMP backend
|
|
|
|
and marking the object as deleted."
|
|
|
|
[storage params]
|
|
|
|
(let [storage (assoc storage :backend :fs)
|
|
|
|
params (assoc params
|
|
|
|
:expired-at (dt/in-future {:hours 2})
|
|
|
|
:temporal true)]
|
|
|
|
(put-object storage params)))
|
2020-12-30 14:38:00 +01:00
|
|
|
|
|
|
|
;; --- impl
|
|
|
|
|
2021-01-04 18:41:05 +01:00
|
|
|
(defn resolve-backend
|
2021-01-08 12:37:32 +01:00
|
|
|
[{:keys [conn pool] :as storage} backend-id]
|
2021-01-04 18:41:05 +01:00
|
|
|
(let [backend (get-in storage [:backends backend-id])]
|
|
|
|
(when-not backend
|
2020-12-30 14:38:00 +01:00
|
|
|
(ex/raise :type :internal
|
|
|
|
:code :backend-not-configured
|
2021-01-04 18:41:05 +01:00
|
|
|
:hint (str/fmt "backend '%s' not configured" backend-id)))
|
2021-01-08 12:37:32 +01:00
|
|
|
(assoc backend :conn (or conn pool))))
|
2020-12-30 14:38:00 +01:00
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Garbage Collection Task
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
;; A task responsible to permanently delete already marked as deleted
|
|
|
|
;; storage files.
|
|
|
|
|
|
|
|
(declare sql:retrieve-deleted-objects)
|
|
|
|
|
2021-01-25 15:22:39 +01:00
|
|
|
(s/def ::min-age ::dt/duration)
|
|
|
|
|
2020-12-30 14:38:00 +01:00
|
|
|
(defmethod ig/pre-init-spec ::gc-task [_]
|
2021-01-25 15:22:39 +01:00
|
|
|
(s/keys :req-un [::storage ::db/pool ::min-age]))
|
2020-12-30 14:38:00 +01:00
|
|
|
|
|
|
|
(defmethod ig/init-key ::gc-task
|
2021-01-25 15:22:39 +01:00
|
|
|
[_ {:keys [pool storage min-age] :as cfg}]
|
2020-12-30 14:38:00 +01:00
|
|
|
(letfn [(retrieve-deleted-objects [conn]
|
2021-01-25 15:22:39 +01:00
|
|
|
(let [min-age (db/interval min-age)
|
|
|
|
result (db/exec! conn [sql:retrieve-deleted-objects min-age])]
|
|
|
|
(when (seq result)
|
|
|
|
(as-> (group-by (comp keyword :backend) result) $
|
|
|
|
(reduce-kv #(assoc %1 %2 (map :id %3)) $ $)))))
|
2020-12-30 14:38:00 +01:00
|
|
|
|
|
|
|
(delete-in-bulk [conn backend ids]
|
|
|
|
(let [backend (resolve-backend storage backend)
|
|
|
|
backend (assoc backend :conn conn)]
|
|
|
|
(impl/del-objects-in-bulk backend ids)))]
|
|
|
|
|
|
|
|
(fn [task]
|
|
|
|
(db/with-atomic [conn pool]
|
|
|
|
(loop [groups (retrieve-deleted-objects conn)]
|
|
|
|
(when groups
|
|
|
|
(doseq [[sid objects] groups]
|
|
|
|
(delete-in-bulk conn sid objects))
|
|
|
|
(recur (retrieve-deleted-objects conn))))))))
|
|
|
|
|
|
|
|
(def sql:retrieve-deleted-objects
|
|
|
|
"with items_part as (
|
2021-01-25 15:22:39 +01:00
|
|
|
select s.id
|
|
|
|
from storage_object as s
|
2020-12-30 14:38:00 +01:00
|
|
|
where s.deleted_at is not null
|
2021-01-25 15:22:39 +01:00
|
|
|
and s.deleted_at < (now() - ?::interval)
|
2020-12-30 14:38:00 +01:00
|
|
|
order by s.deleted_at
|
|
|
|
limit 500
|
|
|
|
)
|
|
|
|
delete from storage_object
|
|
|
|
where id in (select id from items_part)
|
|
|
|
returning *;")
|
|
|
|
|
|
|
|
|
2021-01-04 18:41:05 +01:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Recheck Stalled Task
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
(declare sql:retrieve-pending)
|
|
|
|
(declare sql:exists-storage-object)
|
|
|
|
|
|
|
|
(defmethod ig/pre-init-spec ::recheck-task [_]
|
|
|
|
(s/keys :req-un [::storage ::db/pool]))
|
|
|
|
|
|
|
|
(defmethod ig/init-key ::recheck-task
|
|
|
|
[_ {:keys [pool storage] :as cfg}]
|
|
|
|
(letfn [(retrieve-pending [conn]
|
|
|
|
(->> (db/exec! conn [sql:retrieve-pending])
|
|
|
|
(map (fn [{:keys [backend] :as row}]
|
|
|
|
(assoc row :backend (keyword backend))))
|
|
|
|
(seq)))
|
|
|
|
|
|
|
|
(exists-on-database? [conn id]
|
|
|
|
(:exists (db/exec-one! conn [sql:exists-storage-object id])))
|
|
|
|
|
|
|
|
(recheck-item [conn {:keys [id backend]}]
|
|
|
|
(when-not (exists-on-database? conn id)
|
|
|
|
(let [backend (resolve-backend storage backend)
|
|
|
|
backend (assoc backend :conn conn)]
|
|
|
|
(impl/del-objects-in-bulk backend [id]))))]
|
|
|
|
|
|
|
|
(fn [task]
|
|
|
|
(db/with-atomic [conn pool]
|
|
|
|
(loop [items (retrieve-pending conn)]
|
|
|
|
(when items
|
|
|
|
(run! (partial recheck-item conn) items)
|
|
|
|
(recur (retrieve-pending conn))))))))
|
|
|
|
|
|
|
|
(def sql:retrieve-pending
|
|
|
|
"with items_part as (
|
|
|
|
select s.id from storage_pending as s
|
|
|
|
order by s.created_at
|
|
|
|
limit 100
|
|
|
|
)
|
|
|
|
delete from storage_pending
|
|
|
|
where id in (select id from items_part)
|
|
|
|
returning *;")
|
|
|
|
|
|
|
|
(def sql:exists-storage-object
|
|
|
|
"select exists (select id from storage_object where id = ?) as exists")
|