Filtering Data

In some cases, there can be data that should not be replicated between the sites but should still be replicated locally between the primary and the backup/mirror service. In this case using the replicate class level or object level decoration is irrelevant as there is a need to control the replication behavior only to the remote site. Since a replication channel to a gateway is like any other replication channel, therefore a custom Replication Filter at the source space can be used to filter the relevant data from being sent to the target gateway.

WAN-replicationfilter.jpg

This filtering should be based on the replication target name in order to identify the correct replication filter is called for the outgoing replication to the gateway.

The output-filter can be used also to modify the replicated data before it is arriving the target site. When using a SpaceDocument the modified field must be a predefined field described with the document schema (fixed field).

Using the Filter

Filtering Objects

With the example below a replication filter is used with the source space (output-filter). The New-York space is configured not to replicate the Stock object type to London site. This Stock object type still being replicated to all other location replication targets (backup/mirror) and also to the remote Hong Kong gateway. The filtering can be determined also based on the content of the replicated object or any other custom business logic.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:os-core="http://www.openspaces.org/schema/core"
    xmlns:os-events="http://www.openspaces.org/schema/events"
    xmlns:os-remoting="http://www.openspaces.org/schema/remoting"
    xmlns:os-sla="http://www.openspaces.org/schema/sla"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://www.openspaces.org/schema/core http://www.openspaces.org/schema/14.0/core/openspaces-core.xsd
       http://www.openspaces.org/schema/events http://www.openspaces.org/schema/14.0/events/openspaces-events.xsd
       http://www.openspaces.org/schema/remoting http://www.openspaces.org/schema/14.0/remoting/openspaces-remoting.xsd
       http://www.openspaces.org/schema/sla http://www.openspaces.org/schema/14.0/sla/openspaces-sla.xsd
       http://www.openspaces.org/schema/core/gateway http://www.openspaces.org/schema/14.0/core/gateway/openspaces-gateway.xsd">

        <bean id="londonFilter" class="com.gigaspaces.examples.gateway.LondonReplicationFilter"/>

        <os-core:embedded-space id="space" space-name="myNYSpace" gateway-targets="gatewayTargets">
          <os-core:space-replication-filter>
            <os-core:output-filter ref="londonFilter"/>
          </os-core:space-replication-filter>
        </os-core:embedded-space>

        <os-gateway:targets id="gatewayTargets" local-gateway-name="NEWYORK">
          <os-gateway:target name="LONDON"/>
          <os-gateway:target name="HONGKONG"/>
        </os-gateway:targets>

</beans>
public class LondonReplicationFilter implements IReplicationFilter {

  public void init(IJSpace space, String paramUrl, ReplicationPolicy replicationPolicy) {
  }

  public void process(int direction, IReplicationFilterEntry replicationFilterEntry, String replicationTargetName) {
      if (replicationTargetName.equals("gateway:LONDON")) {
          if (replicationFilterEntry.getClassName().equals(Stock.class.getName())) {
              replicationFilterEntry.discard();
          }
      }
  }

  public void close() {
  }
}

Filtering Space Events

You can choose which Space events (Take, Lease Expiration etc) are replicated. In the example below we define a filter that prevents the DISCARD and LEASE_EXPIRATION events to be replicated:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:os-core="http://www.openspaces.org/schema/core" xmlns:os-events="http://www.openspaces.org/schema/events"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:os-remoting="http://www.openspaces.org/schema/remoting"
    xmlns:os-gateway="http://www.openspaces.org/schema/core/gateway"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
       http://www.openspaces.org/schema/core http://www.openspaces.org/schema/14.0/core/openspaces-core.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
       http://www.openspaces.org/schema/events http://www.openspaces.org/schema/events/openspaces-events.xsd
       http://www.openspaces.org/schema/core/gateway http://www.openspaces.org/schema/14.0/core/gateway/openspaces-gateway.xsd
       http://www.openspaces.org/schema/remoting http://www.openspaces.org/schema/remoting/openspaces-remoting.xsd">

    <context:component-scan base-package="com.gigaspaces.demo" />

    <!-- Enables the usage of @GigaSpaceContext annotation based injection. -->
    <os-core:giga-space-context />

    <context:annotation-config />
    <!--<tx:annotation-driven transaction-manager="transactionManager" />-->

    <os-core:distributed-tx-manager id="transactionManager" />

    <!-- Enable support for @Polling annotation -->
    <os-events:annotation-support />
   
    <!-- Bean to filter the take operations -->
    <bean id="XPIFilter" class="com.gigaspaces.demo.XPIReplicationFilter"/>

    <os-core:embedded-space id="space" space-name="wanSpaceAgency" gateway-targets="gatewayTargets">    
        <os-core:space-replication-filter>
            <os-core:output-filter ref="XPIFilter"/>
        </os-core:space-replication-filter>
    </os-core:space>
 
    <os-core:giga-space id="gigaSpace" space="space" tx-manager="transactionManager" />

    <os-gateway:targets id="gatewayTargets" local-gateway-name="XPI">
        <os-gateway:target name="IE" />
    </os-gateway:targets>
</beans>
package com.gigaspaces.demo;

import com.j_spaces.core.cluster.*;
import com.j_spaces.core.*;

public class XPIReplicationFilter implements IReplicationFilter {
    
    public void init(IJSpace space, String paramUrl, ReplicationPolicy replicationPolicy) {
    }
    
    public void process(int direction, IReplicationFilterEntry replicationFilterEntry, String replicationTargetName) {
      
        switch ( replicationFilterEntry.getOperationType() )
        {
            case TAKE: replicationFilterEntry.discard(); break;
            case DISCARD: replicationFilterEntry.discard(); break;
            case LEASE_EXPIRATION: replicationFilterEntry.discard(); break;
        }
    }
    
    public void close() {
    }
}