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

CaffeineCache doesn't track entry and cache size correctly when unit factor is used #75

Closed
aseovic opened this issue Jul 29, 2022 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@aseovic
Copy link
Member

aseovic commented Jul 29, 2022

Describe the bug

CaffeineCache calculates the "weight" of individual entries at insertion time, using weigh function:

    private int weigh(Object oKey, Object oValue)
        {
        int cUnits = m_unitCalculator.calculateUnits(oKey, oValue);
        if (cUnits < 0)
            {
            throw new IllegalStateException(String.format(
                    "Negative unit (%s) for %s=%s", cUnits, oKey, oValue));
            }
        return (cUnits / m_nUnitFactor);
        }

This is problematic, because the method above returns 0 if a unit factor is larger than the number of units for a give entry, which is often the case when unit factor is used (typical values for unit factor are 2^10 and 2^20, in order to track and limit cache size in KBs or MBs). Even if the factor is smaller than the entry size, the loss of precision due to integer arithmetic at the single entry level is significant.

To Reproduce

  1. Configure unit factor that is larger than the average entry size
  2. Configure BINARY unit calculator
  3. Add some entries to the cache
  4. Observe that the getUnits method returns zero, even though there are entries in the cache

Expected behavior
getUnits methods should return the actual number of bytes consumed by all entries, divided by the unit factor.

In other words, unit factor should only be applied during final calculation for the backing map as a whole, not at the individual entry level.

Environment (please complete the following information):

  • Coherence CE 22.06.1
  • Java 11
  • OS: any
  • OS Version: n/a
@aseovic aseovic added the bug Something isn't working label Jul 29, 2022
@aseovic aseovic self-assigned this Jul 29, 2022
ben-manes added a commit to ben-manes/coherence that referenced this issue Jul 30, 2022
ben-manes added a commit to ben-manes/coherence that referenced this issue Aug 2, 2022
ben-manes added a commit to ben-manes/coherence that referenced this issue Aug 2, 2022
ben-manes added a commit to ben-manes/coherence that referenced this issue Aug 2, 2022
ben-manes added a commit to ben-manes/coherence that referenced this issue Aug 2, 2022
@rlubke
Copy link
Member

rlubke commented Aug 18, 2022

Change has been merged

@rlubke rlubke closed this as completed Aug 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants