Skip to content

Filters

This page describes how filters allow you to specify which records will be shipped by XDR. You use Aerospike Expressions to build these filters. Expressions provide a rich interface for expressing complex logic. You can select records based on the metadata of the record as well as the content of the bins. These filters are evaluated when a record is about to be shipped to the destination DC.

A few use cases for these filters:

  • GDPR - If a record needs to be shipped based on the user’s profile.
  • Conserve network bandwidth - Fine-grained control on which records get shipped.
  • Change notification - XDR is also used to notify external systems. Filters can send notifications only if the record meets specific criteria. For example, only if the account balance falls below a threshold.

Remember that expressions only decide if a record will be shipped or not. They do not determine which bins get shipped. The bin-policy still determines which bins get shipped.

Writing the filter

Aerospike Expressions are used to build XDR shipping filters. Get yourself familiar with the Expressions API. Minimally, look at the options available under

The following example builds a filter that selects all records with a bin named age where the bin type is integer and the value is >= 21. Each language builds the expression and prints its base64 representation, which you pass to the xdr-set-filter info command.

Expression filter = Exp.build(
Exp.ge(Exp.intBin("age"), Exp.val(21)));
System.out.println(filter.getBase64());

Dealing with deletes

If a namespace is configured to ship using XDR, a command which deletes a record leaves a tombstone in place of the original record until XDR successfully ships the record. Filters apply to these tombstones as well. Tombstones have metadata but no storage data (bin-level data). Storage expressions (bin-level) evaluate to false for a tombstone. Metadata checks apply as usual. If the overall expression evaluates to false, the record is not shipped.

In the above example where we check age >= 21, it does not ship record delete operations because the age bin is no longer present and the expression evaluates to false. To ship record deletes as well as records with age >= 21, modify the filter to also check for tombstones:

Expression filter = Exp.build(
Exp.or(
Exp.isTombstone(),
Exp.ge(Exp.intBin("age"), Exp.val(21))));
System.out.println(filter.getBase64());

Setting the filter

XDR filters use Aerospike’s SMD infrastructure. The filter needs to be set using an info command on a single node in the cluster and the SMD layer distributes it across the rest of the cluster. Any new node that joins the cluster automatically gets the existing filters using SMD.

Setting the filter using info commands

Filters in XDR should be set for each namespace in a DC using the xdr-set-filter info command. This command expects the filter in base64 representation. The above examples illustrate how to convert the expression into the corresponding base64 representation.

Use the command below to set the filter:

Terminal window
asinfo -v "xdr-set-filter:dc=DC1;namespace=test;exp=kxGRSJMEk1ECo2FnZRU="

Use exp=null to remove an existing filter:

Terminal window
asinfo -v "xdr-set-filter:dc=DC1;namespace=test;exp=null"

Setting the filter using asadm

You can set and view XDR filters from within asadm using the asinfo command in privileged mode. This runs the info command across the cluster.

Terminal window
Admin+> asinfo -v "xdr-set-filter:dc=DC1;namespace=test;exp=kxGRSJMEk1ECo2FnZRU="

To remove an existing filter:

Terminal window
Admin+> asinfo -v "xdr-set-filter:dc=DC1;namespace=test;exp=null"

To view the filters currently set, use show config xdr filter:

Terminal window
Admin> show config xdr filter
~~~~~~~~~~~~~~~~~~~~~~~XDR Filters (2023-02-16 22:55:02 UTC)~~~~~~~~~~~~~~~~~~~~~~~
Namespace|Datacenter| Base64 Expression| Expression
bar |dc2 |null |null
test |dc2 |kxGRSJMEk1ECo2FnZRU|or(is_tombstone(), ge(bin_int("age"), 21))
Number of rows: 2

Setting the filter using client code

Aerospike client libraries provide an API to set the filter directly without generating the base64 output and calling the xdr-set-filter info command manually.

AerospikeClient.setXDRFilter sets the filter in one call. Pass null as the filter to remove it.

import com.aerospike.client.AerospikeClient;
import com.aerospike.client.exp.Exp;
import com.aerospike.client.exp.Expression;
AerospikeClient client = new AerospikeClient("127.0.0.1", 3000);
Expression filter = Exp.build(
Exp.or(
Exp.isTombstone(),
Exp.ge(Exp.intBin("age"), Exp.val(21))));
client.setXDRFilter(null, "DC1", "test", filter);
client.close();

Getting the filter

Use the command below to see which filter is set:

Terminal window
asinfo -v "xdr-get-filter:dc=DC1;namespace=test"
output: namespace=test:exp=or(is_tombstone(), ge(bin_int("age"), 21))

Note: In version 5.3.0, the above command used to display the filter in base-64 format.

In version 5.4.0 and above, use the command below to get the filter in base-64 format:

Terminal window
asinfo -v "xdr-get-filter:dc=DC1;namespace=test;b64=true"
output: namespace=test:exp=kxGRSJMEk1ECo2FnZRU=

Examples of filters

Ship records from specific sets

Ship records from sets shipped_set1 and shipped_set2. This filter also ships tombstones from these sets because metadata checks (including set name) apply as usual for tombstones.

Expression filter = Exp.build(
Exp.or(
Exp.eq(Exp.setName(), Exp.val("shipped_set1")),
Exp.eq(Exp.setName(), Exp.val("shipped_set2"))));
System.out.println(filter.getBase64());

Ship records by bin type

Ship records only if bin1 is an integer bin.

import com.aerospike.client.command.ParticleType;
Expression filter = Exp.build(
Exp.eq(
Exp.binType("bin1"),
Exp.val(ParticleType.INTEGER)));
System.out.println(filter.getBase64());

Ship records by bin value

Ship records only if the string bin country equals NL.

Expression filter = Exp.build(
Exp.eq(Exp.stringBin("country"), Exp.val("NL")));
System.out.println(filter.getBase64());
Feedback

Was this page helpful?

What type of feedback are you giving?

What would you like us to know?

+Capture screenshot

Can we reach out to you?