Clojure の STM トランザクションが再試行されているかどうか、およびその頻度をポーリングする方法はありますか?
3 に答える
history count
競合があることを示す ref を観察できます。
user=> (def my-ref (ref 0 :min-history 1))
#'user/my-ref
user=> (ref-history-count my-ref)
0
user=> (dosync (alter my-ref inc))
1
user=> (ref-history-count my-ref)
1
履歴カウントは競合を直接表すものではありません。代わりに、同時読み取りを処理するために保持された過去の値の数を表します。
履歴のサイズは と の値によって制限されmin
ますmax
。デフォルトではそれぞれ0
と10
ですが、 を作成するときに変更できますref
(上記参照)。min-history
は0
デフォルトであるため、参照に競合がない限り、通常、ゼロref-history-count
以外の値が返されることはありません。
ここでさらに議論を参照してhistory count
ください: https://groups.google.com/forum/?fromgroups#!topic/clojure/n_MKCoa870o
clojure.core
が提供する、現時点で STM トランザクションのレートを観察する方法はないと思います。もちろん、@Chouser が履歴ストレス テストで行ったことと同様のことを行うことができます。
(dosync
(swap! try-count inc)
...)
つまり、トランザクション内でカウンターをインクリメントします。増分は、トランザクションが試行されるたびに発生します。try-count
より大きい場合、1
トランザクションは再試行されました。
この追加の実装はかなり単純です。
;; {thread-id retries-of-latest-tx}
(def tries (atom {}))
;; The max amount of tries any thread has performed
(def max-tries (atom 0))
(def ninc (fnil inc 0))
(def reporter (agent nil))
(defn report [_ tid]
(swap! max-tries #(max % (get @tries tid 0)))
(swap! tries update-in [tid] (constantly 0)))
(defmacro dosync [& body]
`(clojure.core/dosync
(swap! tries update-in [(.getId (Thread/currentThread))] ninc)
(commute commit-id inc)
(send reporter report (.getId (Thread/currentThread)))
~@body))