Skip to content

Adding object pooling abstraction layer to safeguard from wrong management #208

@eyalkoren

Description

@eyalkoren

The general idea is to create a small and simple abstraction layer in front of the ObjectPool that will guard us from getting that again, for example add:

    abstract class Poolable implements Recyclable {
        static final long  NON_USED = -1;
        
        volatile long usingThread = NON_USED;
        
        Poolable setUsed() {
            usingThread = Thread.currentThread().getId();
            return this;
        }
        
        boolean setUnused() {
            boolean stateChanged = false;
            if (Thread.currentThread().getId() == usingThread) {
                usingThread = NON_USED;
                stateChanged = true;
            }
            return stateChanged;
        }
    }

and make all poolable objects extend it and only allow getting and returning objects through one place. Currently such a role is played by ElasticApmTracer so, for example, it can be modified to something like:

    class ElasticApmTracer {
        ....
        public Transaction startTransaction(@Nullable String traceContextHeader, Sampler sampler, long nanoTime){
            ....
            transaction = transactionPool.createInstance().setUsed().start(traceContextHeader, nanoTime, sampler);
            ....
            return transaction;
        }

        public void recycle(Transaction transaction) {
            ....
            if (transaction.setUnused()) {
                transactionPool.recycle(transaction);
            }
            ....
        }
    }

Assuming the same thread won't try to return an object twice while trying to get an object in between, this is thread safe (since one thread cannot return an object it didn't take). If we are already at it, it would be useful to add to such abstraction layer an atomic counter to count all object currently in use so we can track leaks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    diagnosticsDiagnosability of the agent itself

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions