HEP 3 - Entry Cost Calculator SPI

General Proposal Information

HEP: 3
Title: Entry Cost Calculator SPI
HEP Shortname: Cost Calculator SPI
Author: Denis Sukhoroslov (dsukhoroslov_AT_gmail_DOT_com)
Sponsor: 
Signed-Of: Christoph Engelbert
Lead: Denis Sukhoroslov
Created: 2015/03/02
Status: Draft
Type: Feature
Component: Core
Discussion: https://gitter.im/hazelcast-incubator/entry-cost-calculator-spi
Specification: https://docs.google.com/document/d/1qKulL74Ah0Nl8eAE6yCIWtWHJc9lrw006t2hvogaZRs/edit?usp=sharing
Project: https://github.com/hazelcast-incubator/entry-cost-calculator-spi

Process Information

Start: 2015/Q1 
Depends:
Effort: S
Duration: S
Release: 3.6

Summary

In the currently available memory cost calculator implementation, memory cost is not calculated when In-Memory-Format is not BINARY, thus eviction policy based on memory consumption can't be used at all. It would be benefitial to allow users asign their own memory-cost-calculator for such case via Config interfaces and in xml configuration.

 

Goals

  • A possiblity to calculate entry memory cost when in-memory format is OBJECT

  • Unification of memory cost calculation via one common interface

  • Define the SPI
  • Create documentation of the SPI
  • Re-implement the current cost calculator as BinaryCostCalculator on top of the SPI

Non-Goals

  • Implementation of universal cost calculator which would introspect map entries and calculate its cost via reflection

Motivation

It will allow to use memory-consumption based eviction policy in case when in-memory-format is OBJECT

 Success Metrics

The system can properly calculate the memory consumption based on given map entries independent of the chosen In-Memory-Format. In case of In-Memory-Format OBJECT the user is able to supply a custom calculator.

No significant performance degradation will be observed.

Description

The API can be implemented using a simple interface:

public interface MemoryCostCalculator {
  int calculateCost(K key, V value);
  // or via this method, depends on how entries will be accessed from Hazelcast internals:
  int calculateCost(Map.Entry<K, V> entry)
}

The interface must be used in all entry formats: BINARY and OBJECT.

  • For BINARY format the system should use a standard BinaryCostCalculator (to be implemented, see goals) which will use existing logic to calculate entry costs.
  • For OBJECT format user should be able to assign custom calculator via configuration.


It will also be possible to implement/configure a standard ConstantCostCalculator which will just return a constant value provided to it in configuration which can simulate the current behavior of returning 0 as cost.

Testing

Default unittests to proof that calculation works properly. Memory-based eviction policies can be tested as well.

Risks

Implementation must be implemented in backward-compatible way. The major risk: this cost calculation SPI will hit performance, Javadoc with information on runtime behavior (O(1)) can help users to implement their own cost calculators.

Dependencies