<commit_before>(ns fault.patterns
  (:require [fault.core :as core])
  (:import (fault ServiceExecutor)))

(set! *warn-on-reflection* true)

(defn- next-idx [last-idx current]
  (if (<= last-idx current)
    0
    (inc current)))

(defn load-balancer [key->service]
  (let [next-fn (partial next-idx (dec (count key->service)))
        state (atom -1)
        key-service-tuples (vec key->service)]
    (fn []
      (let [start-idx (swap! state next-fn)]
        (map #(nth key-service-tuples %) (take (count key->service)
                                               (iterate next-fn start-idx)))))))

(defn submit-load-balanced-action [load-balancer key->fn timeout-millis]
  (let [[key {:keys [service]}] (load-balancer)]
    (core/submit-action ^ServiceExecutor service
                        (get key->fn key)
                        timeout-millis)))

(defn perform-load-balanced-action [load-balancer key->fn]
  (let [[key {:keys [service]}] (load-balancer)]
    (core/perform-action service (get key->fn key))))

(defn shotgun [key->service action-count]
  (let [key-service-tuples (vec key->service)
        service-count (count key->service)
        rand-fn (fn [] (rand-int service-count))]
    (assert (>= service-count action-count))
    (if (= service-count action-count)
      (fn []
        key-service-tuples)
      (fn []
        (map #(nth key-service-tuples %)
             (reduce (fn [acc i]
                       (let [acc1 (conj! acc i)]
                         (if (= action-count (count acc1))
                           (reduced (persistent! acc1))
                           acc1)))
                     (transient #{})
                     (repeatedly rand-fn)))))))<commit_msg>WIP: The load balancer will continue until one request goes through or all services exhausted<commit_after>(ns fault.patterns
  (:require [fault.core :as core])
  (:import (fault ServiceExecutor RejectedActionException)))

(set! *warn-on-reflection* true)

(defn- next-idx [last-idx current]
  (if (<= last-idx current)
    0
    (inc current)))

(defn load-balancer [key->service]
  (let [next-fn (partial next-idx (dec (count key->service)))
        state (atom -1)
        key-service-tuples (vec key->service)]
    (fn []
      (let [start-idx (swap! state next-fn)]
        (map #(nth key-service-tuples %) (take (count key->service)
                                               (iterate next-fn start-idx)))))))

(defn submit-load-balanced-action [load-balancer key->fn timeout-millis]
  (some (fn [[key {:keys [service]}]]
          (try (core/submit-action ^ServiceExecutor service
                                   (get key->fn key)
                                   timeout-millis)
               (catch RejectedActionException _ nil)))
        (load-balancer)))

(defn perform-load-balanced-action [load-balancer key->fn]
  (some (fn [[key {:keys [service]}]]
          (try (core/perform-action service (get key->fn key))
               (catch RejectedActionException _ nil)))
        (load-balancer)))

(defn shotgun [key->service action-count]
  (let [key-service-tuples (vec key->service)
        service-count (count key->service)
        rand-fn (fn [] (rand-int service-count))]
    (assert (>= service-count action-count))
    (if (= service-count action-count)
      (fn []
        key-service-tuples)
      (fn []
        (map #(nth key-service-tuples %)
             (reduce (fn [acc i]
                       (let [acc1 (conj! acc i)]
                         (if (= action-count (count acc1))
                           (reduced (persistent! acc1))
                           acc1)))
                     (transient #{})
                     (repeatedly rand-fn)))))))

(defn submit-shotgun-actions [shotgun key->fn timeout-millis]
  )
{'fraction_non_alphanumeric': 0.14728476821192052, 'fraction_numerical': 0.0031788079470198675, 'mean_word_length': 1.9523064894448787, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1185399', 'n_tokens_mistral': 1154, 'n_tokens_neox': 1108, 'n_words': 291}