Thursday, August 23, 2012

Coherence Client using TCP Extend


There are multiple ways a client can connect to a coherence cluster. TCP Extend is one of them.

Server side Setup:

It is recommended to have a separate coherence instance for a TCP Proxy that doesn’t hold data. We can have a separate Coherence instance by setting this parameter tangosol.coherence.distributed.localstorage=false.
You can make a copy of the existing coherence-cache-config.xml and add the lines marked in Red 
 
proxy-coherence-cache-config.xml
<?xml version="1.0" ?>
<cache-config xmlns:xsi=
http://www.w3.org/2001/XMLSchema-instance             xmlns=http://xmlns.oracle.com/coherence/coherence-cache-config”             xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-cache-config coherence-cache-config.xsd">  <scope-name>TestCacheScope</scope-name>
  <defaults>
    <serializer>pof</serializer>
  </defaults>
  <caching-scheme-mapping>
    <cache-mapping>
      <cache-name>distributed-test</cache-name>
      <scheme-name>distributed-default</scheme-name>
      <init-params>
        <init-param>
          <param-name>size-limit</param-name>
          <param-value>50000</param-value>
        </init-param>
      </init-params>
    </cache-mapping>
  </caching-scheme-mapping>
  <caching-schemes>
    <distributed-scheme>
      <scheme-name>distributed-default</scheme-name>
      <service-name>DistributedCache</service-name>
      <thread-count>5</thread-count>
      <backup-count>1</backup-count>
      <backup-storage>
        <type>file-mapped</type>
        <initial-size>1M</initial-size>
        <maximum-size>5G</maximum-size>
        <directory>/coherence/backup</directory>
      </backup-storage>
      <backing-map-scheme>
        <overflow-scheme>
          <scheme-name>LocalMemoryWithDiskOverflow</scheme-name>
          <front-scheme>
            <local-scheme>
             <high-units>300000</high-units>
            </local-scheme>
          </front-scheme>
          <back-scheme>
            <external-scheme>
              <scheme-name>DiskScheme</scheme-name>
              <nio-file-manager>
                <initial-size>1MB</initial-size>
                <maximum-size>1024MB</maximum-size>
                <directory>/coherence/backup</directory>
              </nio-file-manager>
            </external-scheme>
          </back-scheme>
        </overflow-scheme>
      </backing-map-scheme>
      <autostart>true</autostart>
    </distributed-scheme>
    <proxy-scheme>
      <service-name>ExtendTcpProxyService</service-name>
      <thread-count>5</thread-count>
      <acceptor-config>
        <tcp-acceptor>
          <local-address>
            <address>localhost</address>
            <port>9099</port>
          </local-address>
        </tcp-acceptor>
        <!-- Add the below if you use POF -->
        <serializer>
          <instance>
            <class-name>com.tangosol.io.pof.ConfigurablePofContext</class-name>
            <init-params>
              <init-param>
                <param-type>String</param-type>
                <param-value>pof-config.xml</param-value>
              </init-param>
            </init-params>
          </instance>
        </serializer>
      </acceptor-config>
      <autostart>true</autostart>
    </proxy-scheme>
  </caching-schemes>
</cache-config>


Optionally make a copy of your existing tangosol coherence override xml and change tangosol.coherence.role to ProxyServer as illustrated below. If you don’t change the override.xml when starting the proxy use the original tangosol coherence override .xml as defined for the data caches

proxy-tangasol-coherence-override.xml
<?xml version="1.0" ?>
<coherence xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
           xmlns="http://xmlns.oracle.com/coherence/coherence-operational-config"
           xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-operational-config coherence-operational-config.xsd">  <cluster-config>
    <member-identity>
      <cluster-name system-property="tangosol.coherence.cluster">OperDataCluster</cluster-name>
      <machine-name system-property="tangosol.coherence.machine">coherence1</machine-name>
      <process-name system-property="tangosol.coherence.process">CoherenceProxyServer</process-name>
      <role-name system-property="tangosol.coherence.role">ProxyServer</role-name>
    </member-identity>
    <unicast-listener>
      <address system-property="tangosol.coherence.localhost">localhost</address>
      <port system-property="tangosol.coherence.localport">8088</port>
      <port-auto-adjust system-property="tangosol.coherence.localport.adjust">true</port-auto-adjust>
    </unicast-listener>
    <multicast-listener>
      <address system-property="tangosol.coherence.clusteraddress">224.4.0.255</address>
      <port system-property="tangosol.coherence.clusterport">3060</port>
      <time-to-live system-property="tangosol.coherence.ttl">3</time-to-live>
      <join-timeout-milliseconds>30000</join-timeout-milliseconds>
      <multicast-threshold-percent>25</multicast-threshold-percent>
    </multicast-listener>
  </cluster-config>
  <logging-config>
    <destination system-property="tangosol.coherence.log">stdout</destination>
    <severity-level system-property="tangosol.coherence.log.level">9</severity-level>
  </logging-config>
</coherence>



Starting the proxy 

java -server -Dtangosol.coherence.override=proxy-tangasol-coherence-override.xml -Dtangosol.coherence.distributed.localstorage=false -Dtangosol.coherence.cacheconfig=proxy-coherence-cache-config.xml -cp "$COHERENCE_HOME/lib/coherence.jar com.tangosol.net.DefaultCacheServer

Client Side Setup:

On the client side you need a tangosol.coherence.cacheconfig where you define TCP Extend proxy and invocation service
client-tangasol-cache-config.xml
<?xml version="1.0"?>
<cache-config xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
              xmlns="http://xmlns.oracle.com/coherence/coherence-cache-config"
              xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-cache-config          coherence-cache-config.xsd">
  <scope-name>TestCacheScope</scope-name>
  <caching-scheme-mapping>
    <cache-mapping>
      <cache-name>distributed-*</cache-name>
      <scheme-name>extend-near</scheme-name>
    </cache-mapping>
  </caching-scheme-mapping>
  <caching-schemes>
    <near-scheme>
      <scheme-name>extend-near</scheme-name>
      <front-scheme>
        <local-scheme>
          <scheme-name>localCache</scheme-name>
          <high-units>1000</high-units>
        </local-scheme>
      </front-scheme>
      <back-scheme>
        <remote-cache-scheme>
          <scheme-ref>extend-dist</scheme-ref>
        </remote-cache-scheme>
      </back-scheme>
      <invalidation-strategy>all</invalidation-strategy>
    </near-scheme>
    <remote-cache-scheme>
      <scheme-name>extend-dist</scheme-name>
      <service-name>ExtendTcpCacheService</service-name>
      <initiator-config>
        <tcp-initiator>
          <remote-addresses>
            <socket-address>
              <address>SERVERS IP</address>
              <port>9099</port>
            </socket-address>
          </remote-addresses>
          <connect-timeout>10s</connect-timeout>
        </tcp-initiator>
        <outgoing-message-handler>
          <request-timeout>60s</request-timeout>
        </outgoing-message-handler>
      </initiator-config>
    </remote-cache-scheme>
    <remote-invocation-scheme>
      <scheme-name>extend-invocation</scheme-name>
      <service-name>ExtendTcpInvocationService</service-name>
      <initiator-config>
        <tcp-initiator>
          <remote-addresses>
            <socket-address>
              <address>SERVERS IP</address>
              <port>9099</port>
            </socket-address>
          </remote-addresses>
          <connect-timeout>10s</connect-timeout>
        </tcp-initiator>
        <outgoing-message-handler>
          <request-timeout>5s</request-timeout>
        </outgoing-message-handler>
      </initiator-config>
    </remote-invocation-scheme>
  </caching-schemes>
</cache-config>

 
Package this in the client WAR or EAR along with coherence.jar  when you run the code