Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework resource allocation strategy #36

Open
goosedb opened this issue May 5, 2024 · 1 comment
Open

Rework resource allocation strategy #36

goosedb opened this issue May 5, 2024 · 1 comment

Comments

@goosedb
Copy link

goosedb commented May 5, 2024

I believe we should change the strategy for how resources are allocated across stripes.

The next code allocates 16 resources, while the maximum number specified in the config is nine.

main :: IO ()
main = do
  getNumCapabilities >>= print
  ref <- newIORef @Int 0
  pool <- newPool PoolConfig
    { createResource   = atomicModifyIORef ref (\a -> (a + 1, a)) >>= print
    , freeResource     = const $ atomicModifyIORef ref ((,()) . pred)
    , poolCacheTTL     = 0.5
    , poolMaxResources = 9
    , poolNumStripes   = Just 8
    } 
  hSetBuffering stdout LineBuffering
  forM_ @[] [1..200] \(_ :: Int) -> do
    forkIO $ withResource pool \() -> threadDelay 1000000
  threadDelay 2000000

This can play very badly when you for some reason reduce the number of available resources (for example, your database cannot accept so many connections), as in the example above from 16 to 9, but do not get the expected result.

I suggest adding an interface for manually distributing the number of resources across stripes and accepting not numStripes + maxResources, but something like [Int], where each element of the list is a stripe and the number is the amount of resources on this stripe

@arybczak
Copy link
Contributor

arybczak commented May 6, 2024

In hindsight looks like the decision of changing "resources per stripe" in the config to "resources across all stripes" was a mistake. I'm not sure how to back off of this now though 😕

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants