Transformations #
While replication maintains eventual consistency between a primary and replica attribute family of the same attribute, a transformation derives updates to different attributes (on the same or another entity) whenever a source attribute is updated.
A classic use case is maintaining a secondary index or projected view: for example, whenever a new event.data record arrives, automatically projecting it into user.event.<stamp> so a user’s recent activity can be queried in O(1) via random access.
Configuring Transformations #
Transformations are declared in the top-level transformations section of your HOCON configuration:
transformations {
event-to-user-history {
entity: event
attributes: [ "data" ]
using: cz.o2.proxima.example.EventDataToUserHistory
# Optional: filter which source elements trigger the transformation
filter: cz.o2.proxima.example.EventHistoryFilter
# Optional: disable transformation without removing its definition
# disabled: true
# Optional: append a suffix to the ReplicationController consumer name
# transformer.consumer.name.suffix: "-v2"
# Any additional keys are passed into the `cfg` map in setup(...)
max-history-age-ms: 86400000
}
}
Configuration Properties #
| Key | Required | Description |
|---|---|---|
entity | Yes | Source entity name. |
attributes | Yes | List of source attribute names (or ["*"]). |
using | Yes | Fully qualified class name implementing ElementWiseTransformation or ContextualTransformation. |
filter | No | Optional StorageFilter implementation (defaults to PassthroughFilter). |
disabled | No | Set true to skip loading and executing this transformation. |
transformer.consumer.name.suffix | No | Optional suffix appended to the consumer group name used by ReplicationController. |
| (custom keys) | No | Passed verbatim into the Map<String, Object> cfg argument of setup(...). |
Implementing a Transformation #
Proxima supports two transformation interfaces depending on whether the logic is pure/stateless or needs access to a DataOperator.
1. Stateless Element-Wise Transformation (ElementWiseTransformation)
#
ElementWiseTransformation is operator-agnostic (DataOperatorAgnostic) and transforms a single input StreamElement into zero, one, or many output StreamElements via a
Collector:
package cz.o2.proxima.example;
import cz.o2.proxima.core.repository.AttributeDescriptor;
import cz.o2.proxima.core.repository.EntityDescriptor;
import cz.o2.proxima.core.repository.Repository;
import cz.o2.proxima.core.storage.StreamElement;
import cz.o2.proxima.core.transform.ElementWiseTransformation;
import cz.o2.proxima.example.event.Event;
import java.util.Map;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class EventDataToUserHistory implements ElementWiseTransformation {
private EntityDescriptor user;
private AttributeDescriptor<Event.BaseEvent> event;
private String prefix;
@SuppressWarnings("unchecked")
@Override
public void setup(Repository repo, Map<String, Object> cfg) {
user = repo.getEntity("user");
event = (AttributeDescriptor<Event.BaseEvent>) user.getAttribute("event.*");
prefix = event.toAttributePrefix();
}
@Override
public int apply(StreamElement input, Collector<StreamElement> collector) {
if (!input.isDelete()) {
Optional<Event.BaseEvent> data = input.getParsed();
if (data.isPresent()) {
collector.collect(
StreamElement.upsert(
user,
event,
input.getUuid(),
data.get().getUserName(),
prefix + input.getStamp(),
input.getStamp(),
input.getValue()));
return 1;
}
} else {
log.warn("Ignored delete in transformed event {}", input);
}
return 0;
}
}
Return Value Contract #
apply(input, collector) returns an int specifying how many elements will be emitted to collector (synchronously or asynchronously):
- Return
0when the input produces no output (the input offset is confirmed immediately). - Return
N > 0whenNoutput elements will be passed tocollector.collect(...).ReplicationControllerwaits until allNemitted elements have been confirmed by their target primary writers before confirming the input element on the source commit log!
2. Operator-Aware Contextual Transformation (DirectElementWiseTransform)
#
When a transformation needs to query existing state (e.g., look up another entity via RandomAccessReader or CachedView while enriching an update), implement
DirectElementWiseTransform (which extends ContextualTransformation<DirectDataOperator>):
public class EnrichEventTransform implements DirectElementWiseTransform {
private RandomAccessReader productReader;
private EntityDescriptor enrichedEntity;
private AttributeDescriptor<EnrichedEvent> targetAttr;
@Override
public void setup(Repository repo, DirectDataOperator direct, Map<String, Object> cfg) {
EntityDescriptor product = repo.getEntity("product");
productReader = direct.getRandomAccess(product.getAttribute("details")).orElseThrow();
enrichedEntity = repo.getEntity("enriched_event");
targetAttr = enrichedEntity.getAttribute("data");
}
@Override
public int apply(StreamElement input, Collector<StreamElement> collector) {
// Perform random-access lookup and emit enriched StreamElement
// ...
return 1;
}
@Override
public void onRestart() {
// Optional hook invoked on initial startup and error recovery
}
}
Runtime Execution & Best Practices #
Transformations are executed automatically by
ReplicationController using a dedicated commit-log consumer per transformation:
- Target writes go to the target’s primary family: Each element passed to
collector.collect(...)is written throughDirectDataOperator.getWriter(...)to the target attribute’s primary storage, from which it is subsequently replicated to all of the target attribute’s replicas. - Transactional interoperability: If a transformation reads from or writes to
transactional attributes,
TransformationObserverhandles input/output transaction coordination automatically (including the built-inTransactionCommitTransformationthat powers ACID commit propagation). - Keep transformations idempotent: Because commit-log delivery is at-least-once, design output keys, suffixes, and timestamps deterministically from the input
StreamElement(for example, reusinginput.getUuid()andinput.getStamp()). - Avoid cycles: Never configure a chain of transformations whose outputs feed back into their own inputs unless explicitly guarded by a terminating
filter. - Handle deletes explicitly: Always check
input.isDelete()andinput.isDeleteWildcard()before callinginput.getParsed().