6 Replies Latest reply: Apr 12, 2012 12:40 AM by LSV RSS

    How to implement Push replication and do a write/read through DB as well

    LSV
      Hi,

      We have push replication between two clusters which is configured as below

      <read-write-backing-map-scheme>
      <internal-cache-scheme>
      <local-scheme>
      </local-scheme>
      </internal-cache-scheme>
      <cachestore-scheme>
      <class-scheme>
      <class-name>com.oracle.coherence.patterns.pushreplication.PublishingCacheStore</class-name>
      <init-params>
      <init-param>
      <param-type>java.lang.String</param-type>
      <param-value>{cache-name}</param-value>
      </init-param>
      </init-params>
      </class-scheme>

      </cachestore-scheme>
      </read-write-backing-map-scheme>

      we also have another publisher which writes to DB (write through) when ever put operation is performed. This is acheived by adding

      xmlns:sync="class:com.example.test.CustomPushReplicationNamespaceContentHandler" in coherence-server.xml and

      <sync:publisher>
      <sync:publisher-name>Active2-JDBC-Publisher</sync:publisher-name>
      <sync:publisher-scheme>
      <sync:custom-publisher-scheme>
      </sync:custom-publisher-scheme>
      </sync:publisher-scheme>
      </sync:publisher>

      (followed this doc, https://blogs.oracle.com/cosmintudor/entry/coherence_-howto_develop_a_custom_push_replication_publisher)

      By this, we were able to perform DB update whenever put happens (either normal put or push replication put).

      We would now want to know, how to perform read through ?


      Note : we havent used CacheLoader interface to implement Write Through. We extended AbstractPublisherScheme to implement write through. There is no load method in this class AbstractPublisherScheme to perform read through.
        • 1. Re: How to implement Push replication and do a write/read through DB as well
          user738616
          Hi,

          In order to achieve it, you would need to implement your own cache-store "Store1" that extends PublishingCacheStore and implements BinaryEntryStore. In your configuration, use "Store1" as the cache-store and not PublishingCacheStore. In the "Store1" implementation, call super.load()/super.store() in your load/store methods if you want to push the entries fetched in your load() and store() method otherwise don't invoke super methods.

          Hope this helps!

          Cheers,
          NJ
          • 2. Re: How to implement Push replication and do a write/read through DB as well
            LSV
            Thank you so much NJ. It worked.

            public class TestCacheStore extends com.oracle.coherence.patterns.pushreplication.PublishingCacheStore
            {

            public TestCacheStore(String cacheName)
            {
            super(cacheName);
            }

            public void load(BinaryEntry binaryEntry)
            {
            super.load(binaryEntry);

            }

            public void store(BinaryEntry entry)
            {
            super.store(entry);
            }
            }


            Just want to clarify few more things.

            My actual requirement is,

            1. when ever put happens (store method is called) i need to do update Database. i have below code to do that

            public void store(BinaryEntry entry)
            {
            super.store(entry);
            //DB operation..
            }

            2. When ever get happens and if the data is not there in cache load method will be called, i need to get the data for that key from database and load it into cache. do i need to do a put to cache or is there already an api to achieve this ?

            public void load(BinaryEntry binaryEntry)
            {
            super.load(binaryEntry);
            //i will get the data from database here. do i need to put the data into cache or is there any api to do it much easily.
            }
            • 3. Re: How to implement Push replication and do a write/read through DB as well
              user738616
              Hi,
              public void load(BinaryEntry binaryEntry)
              {
              super.load(binaryEntry);
              //i will get the data from database here. do i need to put the data into cache or is there any api to do it much easily.
              }
              In order to save the value loaded from the DB into the cache you would need to do the following:

              binaryEntry.updateBinaryValue((Binary)(binaryEntry.getContext().getValueToInternalConverter().convert(valueObject)));

              Also, make sure you call the super.load(binaryEntry) after updating the binary value. Also, I don't think the load() method is implemented in the PublishingCacheStore (similar to store()) which you need to implement if you want to replicate the loaded values.

              Hope this helps!

              Cheers,
              NJ
              • 4. Re: How to implement Push replication and do a write/read through DB as well
                LSV
                Thanks NJ for your valuable suggestion.

                Are you asking to do something like below ? is this correct way ?


                public void load(BinaryEntry entry)
                {
                entry.updateBinaryValue((Binary)(binaryEntry.getContext().getValueToInternalConverter().convert(valueObject)));
                super.load(entry);

                Binary decoratedBinaryValue = decorateBinary(entry.getContext(),entry.getBinaryValue(), false);
                Binary undecoratedBinaryKey = undecorateBinary(entry.getBinaryKey());
                BackingMapBinaryEntry bmbe = (BackingMapBinaryEntry) entry;
                Binary originalBinaryValue = bmbe.getOriginalBinaryValue();

                if (originalBinaryValue == null)
                {
                publishEntryOperation(new EntryOperation(siteName, clusterName, cacheName, Operation.Insert,
                new PublishableEntry(undecoratedBinaryKey, decoratedBinaryValue, new Binary(), entry.getContext())));
                }
                else
                {
                publishEntryOperation(new EntryOperation(siteName, clusterName, cacheName, Operation.Update,
                new PublishableEntry(undecoratedBinaryKey, decoratedBinaryValue, originalBinaryValue, entry.getContext())));
                }

                }
                • 5. Re: How to implement Push replication and do a write/read through DB as well
                  user738616
                  Hi,

                  You need to modify the PublishingCacheStore.load() method included in the pushreplication jar or include the file in your source code
                  >
                  >
                  public void load(BinaryEntry entry)
                  {
                  Binary decoratedBinaryValue = decorateBinary(entry.getContext(),entry.getBinaryValue(), false);
                  Binary undecoratedBinaryKey = undecorateBinary(entry.getBinaryKey());
                  BackingMapBinaryEntry bmbe = (BackingMapBinaryEntry) entry;
                  Binary originalBinaryValue = bmbe.getOriginalBinaryValue();

                  if (originalBinaryValue == null)
                  {
                  publishEntryOperation(new EntryOperation(siteName, clusterName, cacheName, Operation.Insert,
                  new PublishableEntry(undecoratedBinaryKey, decoratedBinaryValue, new Binary(), entry.getContext())));
                  }
                  else
                  {
                  publishEntryOperation(new EntryOperation(siteName, clusterName, cacheName, Operation.Update,
                  new PublishableEntry(undecoratedBinaryKey, decoratedBinaryValue, originalBinaryValue, entry.getContext())));
                  }

                  }
                  Keep this code in your CacheStore implementation
                  entry.updateBinaryValue((Binary)(binaryEntry.getContext().getValueToInternalConverter().convert(valueObject)));
                  super.load(entry);
                  Hope this helps!

                  Cheers,
                  NJ

                  Edited by: user738616 on Apr 11, 2012 11:19 AM
                  • 6. Re: How to implement Push replication and do a write/read through DB as well
                    LSV
                    Thank You for your suggestion !!