Skip to content

Use semaphore to access thread pools #1

@fg-cdd

Description

@fg-cdd

Description
Some pool classes create a limited amount of new threads. When the maximum number of threads is reached, the handling class is actively waiting in while loop that another thread terminates. This active wait should be replaced by a semaphore (java.util.concurrent.Semaphore).

Concerned classes
PoolUpload, PoolDowload, PoolReplicate and PoolDelete.

Example
An example of implementation can be seen in

  • Clone of this project: wassong-iphc/GRIDA
  • Branch: semaphore
  • File GRIDA/grida-server/src/main/java/fr/insalyon/creatis/grida/server/execution/PoolUpload.java
  • Fullpath to raw file: https://raw.githubusercontent.com/wassong-iphc/GRIDA/semaphores/grida-server/src/main/java/fr/insalyon/creatis/grida/server/execution/PoolUpload.java

Testing
Testing the nominal case is quite easy. It is possible to create the new jar and use it in a test server, like sbgsol.

Testing the stress situation, when the limit is reached, is harder. A solution is to use JUnit tests for that. But the code has to be refactored, to allow injection of all dependencies, so that they can be replaced by mock objets. This can be done like that, in PoolUpload:

    public synchronized static PoolUpload getInstance() {
        if (instance == null) {
            instance = new PoolUpload(
                Configuration.getInstance().getMaxSimultaneousUploads(),
                Configuration.getInstance().getMaxRetryCount(),
                new DAOFactory.getDAOFactory().getPoolDAO(), // PoolDAO must become an interface
                new DefaultOperationBusinessFactory(), // must be created
                new DefaultPoolBusinessFactory(),  // must be created
                new DefaultFileUtils());  // FileUtils must become an interface
            instance.start();
        }
        return instance;
    }

    // package private constructor, so it can be used by tests.
    PoolUpload(
        int maxSimultaneousUploads,
        int maxRetryCount,
        PoolDAO poolDAO,
        OperationBusinessFactory obFact,
        PoolBusinessFactory pbFact,
        FileUtils fileUtils) {
        …
    }

Some new interfaces and classes must be defined, so that a PoolUpload class suitable for tests can be instantiated with test dependencies. This has not been implemented in the cited fork.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions