Topic
8 replies Latest Post - ‏2012-05-21T16:37:41Z by Chris.D.Johnson
SystemAdmin
SystemAdmin
1485 Posts
ACCEPTED ANSWER

Pinned topic Same Object Reference on Get

‏2012-05-15T19:55:39Z |
I have a use case in which I have to cache a very large object that will be read by many different clients. The clients will be read only. I was wondering if there's a way to get the same object reference from the cache when the client calls get(key). I have experimented with several different configurations using a NO_COPY setting on the client. I setup a server that uses a loader to preload a simple pojo into the object map (I'm not using the EntityManager api) on the server. I have a client that calls get on the same key a few times and compares the reference. For each get call on the map, a different reference to the object is passed to the client. Is this the expected behavior? Is there any configuration that can be used to always obtain the same reference? Also, is there anyway to tell if the values are coming from the client map rather than the server map?

Client ObjectGrid.xml configuration:

<?xml version="1.0" encoding="UTF-8"?>
<objectGridConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://ibm.com/ws/objectgrid/config ../objectGrid.xsd"
xmlns="http://ibm.com/ws/objectgrid/config">

<objectGrids>
<objectGrid name="Grid">
<backingMap name="Map1"
ttlEvictorType="CREATION_TIME"
timeToLive="120"
copyMode="NO_COPY"
numberOfBuckets="1023"
readOnly="true"/>
</objectGrid>
</objectGrids>
</objectGridConfig>

Server ObjectGrid.xml configuration:

<?xml version="1.0" encoding="UTF-8"?>
<objectGridConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://ibm.com/ws/objectgrid/config ../objectGrid.xsd"
xmlns="http://ibm.com/ws/objectgrid/config">

<objectGrids>
<objectGrid name="Grid">
<backingMap name="Map1"
lockStrategy="OPTIMISTIC"
numberOfBuckets="15"
ttlEvictorType="CREATION_TIME"
timeToLive="120"
pluginCollectionRef="map1PluginCollection"/>
</objectGrid>
</objectGrids>

<backingMapPluginCollections>
<backingMapPluginCollection id="map1PluginCollection">
<bean id="Loader" className="{spring}jpaLoader"/>
</backingMapPluginCollection>
</backingMapPluginCollections>
</objectGridConfig>
Server OjbectGridDeployment.xml configuration:

<?xml version="1.0" encoding="UTF-8"?>
<deploymentPolicy xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://ibm.com/ws/objectgrid/deploymentPolicy ../deploymentPolicy.xsd"
xmlns="http://ibm.com/ws/objectgrid/deploymentPolicy">

<objectgridDeployment objectgridName="Grid">
<mapSet
name="Grid_MapSet"
numberOfPartitions="2"
minSyncReplicas="0"
maxSyncReplicas="0"
maxAsyncReplicas="0"
replicaReadEnabled="false"
numInitialContainers="1"
autoReplaceLostShards="true"
developmentMode="true"
placementStrategy="FIXED_PARTITIONS">

<map ref="Map1"/>
</mapSet>
</objectgridDeployment>
</deploymentPolicy>
Updated on 2012-05-21T16:37:41Z at 2012-05-21T16:37:41Z by Chris.D.Johnson
  • Chris.D.Johnson
    Chris.D.Johnson
    480 Posts
    ACCEPTED ANSWER

    Re: Same Object Reference on Get

    ‏2012-05-16T00:51:33Z  in response to SystemAdmin
    Yes, this should work with NO_COPY with the client cache (near cache) enabled as you do.

    It looks like the copy mode is not being overridden on the client configuration. To override it, you'll need to call ObjectMap.setCopyMode(CopyMode.NO_COPY, null) instead.

    Or you can specify the NO_COPY copy mode on the server-side objectgird xml
    • SystemAdmin
      SystemAdmin
      1485 Posts
      ACCEPTED ANSWER

      Re: Same Object Reference on Get

      ‏2012-05-17T13:13:57Z  in response to Chris.D.Johnson
      That worked. Thank you for the help.
    • SystemAdmin
      SystemAdmin
      1485 Posts
      ACCEPTED ANSWER

      Re: Same Object Reference on Get

      ‏2012-05-17T18:17:03Z  in response to Chris.D.Johnson
      I'm curious why this setting cannot be overriden in the client ObjectGrid.xml. In this case, we do not want NO_COPY on the server, and we would like to be able to override that in the ObjectGrid.xml file on the client. Is this a bug or expected behavior?
      • Chris.D.Johnson
        Chris.D.Johnson
        480 Posts
        ACCEPTED ANSWER

        Re: Same Object Reference on Get

        ‏2012-05-17T19:05:07Z  in response to SystemAdmin
        I think this was an oversight. However, we can't fix it otherwise we'd break existing customers who had inadvertently added the wrong copy mode to the file.
        • joshuawhite929
          joshuawhite929
          52 Posts
          ACCEPTED ANSWER

          Re: Same Object Reference on Get

          ‏2012-05-18T01:51:20Z  in response to Chris.D.Johnson
          Chris,

          Is there any other way we can get an object by reference from our near cache using a "readonly" client?

          -Joshua
          • Chris.D.Johnson
            Chris.D.Johnson
            480 Posts
            ACCEPTED ANSWER

            Re: Same Object Reference on Get

            ‏2012-05-18T04:48:02Z  in response to joshuawhite929
            The BackingMap has a readOnly="true" flag that disallows any modifications to the map. Only the Loader can insert data into the map via the preloadMap and get methods. This value is set on the server and affects all clients, and cannot be overridden.
            • joshuawhite929
              joshuawhite929
              52 Posts
              ACCEPTED ANSWER

              Re: Same Object Reference on Get

              ‏2012-05-21T14:01:20Z  in response to Chris.D.Johnson
              Chris,

              What we are trying to do here is obtain the object from the near cache by reference as opposed to by value. This specific object is large and contains read-only reference data.

              My understanding is that the readOnly flag just prevents clients from making modifications to the map, but doesn't offer any additional benefits. I looked into the setCopyMode method you mentioned earlier, but was disappointed to find that it works on a per session basis.

              Is there any other way to achieve obtaining a reference to an object in the near cache as opposed to a copy?

              Thanks,

              Joshua
              • Chris.D.Johnson
                Chris.D.Johnson
                480 Posts
                ACCEPTED ANSWER

                Re: Same Object Reference on Get

                ‏2012-05-21T16:37:41Z  in response to joshuawhite929
                The CopyMode is set on the ObjectMap, but yes, it is on a per-session basis, and it's the only way to retrieve the direct reference to the cached object value.

                You could simply provide a getSession() wrapper that automatically calls session.getMap("myreferencemap").setCopyMode(CopyMode.NO_COPY, null).

                Since the copy mode is never reset until the session is closed or manually reset using the ObjectMap.resetCopyMode() method, you could build a simple Session cache that returns a Session instance with the CopyMode already set, which means you won't have any performance hits.

                So:
                
                
                // Pseudo-code for brevity 
                
                class CustomGridAccessor 
                { 
                
                final 
                
                int numSessions = 100; 
                
                final List<Session> sessions = 
                
                new LinkedList<Session>(100); 
                
                final ObjectGrid grid = ...;   
                
                synchronized Session getSession() 
                { Session s = sessionList.remove(); 
                
                if(s == 
                
                null) 
                { s = grid.getSession(); s.getMap(
                "MyReferenceMap").setCopyMode(CopyMode.NO_COPY, 
                
                null); 
                } 
                
                return s; 
                }   
                
                synchronized 
                
                void returnSession(Session s) 
                { 
                
                if(sessions.size() < numSessions) 
                { sessions.add(s); 
                } 
                } 
                }