You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ignite.apache.org by Mauricio Arroqui <ma...@gmail.com> on 2017/02/21 19:36:14 UTC

How to configure MongoDB Client? I'm getting NotSerializableException

Hi, 

I have the same problem mentioned in the post below, but I use mongoDb
instead of CacheJdbcBlobStore. 
http://apache-ignite-users.70518.x6.nabble.com/How-to-use-CacheJdbcBlobStore-Getting-NotSerializableException-td431.html

Here is an example with Mongo, but it use Embedded MongoDB which it's main
goal is for unit testing:
https://github.com/gridgain/gridgain-advanced-examples/blob/master/src/main/java/org/gridgain/examples/datagrid/store/CacheMongoStore.java

I read you solve the issue for CacheJdbcBlobStore
https://issues.apache.org/jira/browse/IGNITE-960

Are you planning to do the same with mongo? I'm losing something? 

Thanks in advance. 
Mauricio

Here's a my bean configuration:  
 <bean class="org.apache.ignite.configuration.CacheConfiguration">
                    <property name="name" value="local_simulation"/>
                    <property name="cacheMode" value="LOCAL"/>
                    <property name="atomicityMode" value="ATOMIC"/>
                    <property name="copyOnRead" value="true"/>
                     <property name="cacheStoreFactory">
                      <bean
class="javax.cache.configuration.FactoryBuilder$SingletonFactory">
                        <constructor-arg>
                          <bean
class="uniagro.simugant.db.grid.mongodb.SimulationCacheMongoStore">
                          </bean>
                        </constructor-arg>
                      </bean>
                  </property>
                  <property name="writeThrough" value="true"/>
                  <property name="writeBehindEnabled" value="true"/>
                </bean>

Abstract Cache store (SimulationCacheMongoStore extends this on):
public abstract class CacheMongoStore<K, V> extends CacheStoreAdapter<K, V>
implements Serializable, LifecycleAware {

    /**
     * MongoDB port.
     */
    private static final int MONGOD_PORT = 27017;

    /**
     * MongoDB executable for embedded MongoDB store.
     */
    private MongoClient mongoClient;

    /**
     * Mongo data store.
     */
    protected Datastore morphia;

    @Override
    public void start() throws IgniteException {

        mongoClient = new MongoClient();
        morphia = new Morphia().createDatastore(mongoClient, "test");
    }

    @Override
    public void stop() throws IgniteException {
        if (mongoClient != null) {
            mongoClient.close();
            
        }
    }

}








--
View this message in context: http://apache-ignite-users.70518.x6.nabble.com/How-to-configure-MongoDB-Client-I-m-getting-NotSerializableException-tp10767.html
Sent from the Apache Ignite Users mailing list archive at Nabble.com.

Re: How to configure MongoDB Client? I'm getting NotSerializableException

Posted by Mauricio Arroqui <ma...@gmail.com>.
Thanks Val.

On Tue, Feb 28, 2017 at 1:09 AM, vkulichenko [via Apache Ignite Users] <
ml-node+s70518n10931h89@n6.nabble.com> wrote:

> Mauricio,
>
> According to exception, you injected CacheStoreSession in a non-transient
> field. Making it transient might fix the issue, but generally I would not
> recommend this. The purpose of store factory is exactly to avoid
> serialization of the store and to create the instance on server side where
> it will be used. Store itself very rarely can be serialized.
>
> -Val
>
> ------------------------------
> If you reply to this email, your message will be added to the discussion
> below:
> http://apache-ignite-users.70518.x6.nabble.com/How-to-
> configure-MongoDB-Client-I-m-getting-NotSerializableException-
> tp10767p10931.html
> To unsubscribe from How to configure MongoDB Client? I'm getting
> NotSerializableException, click here
> <http://apache-ignite-users.70518.x6.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=10767&code=bWF1cmlhcnJvcXVpQGdtYWlsLmNvbXwxMDc2N3wtMTQxNjMzMjkxNg==>
> .
> NAML
> <http://apache-ignite-users.70518.x6.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>




--
View this message in context: http://apache-ignite-users.70518.x6.nabble.com/How-to-configure-MongoDB-Client-I-m-getting-NotSerializableException-tp10767p10975.html
Sent from the Apache Ignite Users mailing list archive at Nabble.com.

Re: How to configure MongoDB Client? I'm getting NotSerializableException

Posted by vkulichenko <va...@gmail.com>.
Mauricio,

According to exception, you injected CacheStoreSession in a non-transient
field. Making it transient might fix the issue, but generally I would not
recommend this. The purpose of store factory is exactly to avoid
serialization of the store and to create the instance on server side where
it will be used. Store itself very rarely can be serialized.

-Val



--
View this message in context: http://apache-ignite-users.70518.x6.nabble.com/How-to-configure-MongoDB-Client-I-m-getting-NotSerializableException-tp10767p10931.html
Sent from the Apache Ignite Users mailing list archive at Nabble.com.

Re: How to configure MongoDB Client? I'm getting NotSerializableException

Posted by Mauricio Arroqui <ma...@gmail.com>.
Val,

Thanks for you advice. I changed what you suggest and the exception
disappears, here's the piece of the final configuration:
...
<property name="cacheStoreFactory">
                      <bean
class="org.apache.ignite.configuration.IgniteReflectionFactory">
                        <property name="componentClass"
value="uniagro.simugant.db.grid.mongodb.SimulationCacheMongoStore"/>
                      </bean>
                  </property>
...

But I don't understand why? My first SimulationCacheMongoStore has the non
serialized property MongoClient, but the second hasn't, so.. why the
java.io.NotSerializableException? 

Thanks for your patience.
Mauricio 



--
View this message in context: http://apache-ignite-users.70518.x6.nabble.com/How-to-configure-MongoDB-Client-I-m-getting-NotSerializableException-tp10767p10914.html
Sent from the Apache Ignite Users mailing list archive at Nabble.com.

Re: How to configure MongoDB Client? I'm getting NotSerializableException

Posted by vkulichenko <va...@gmail.com>.
Mauricio,

You still use singleton factory for the cache store:

                  <property name="cacheStoreFactory">
                      <bean
class="javax.cache.configuration.FactoryBuilder$SingletonFactory">
                        <constructor-arg>
                          <bean
class="uniagro.simugant.db.grid.mongodb.SimulationCacheMongoStore">
                          </bean>
                        </constructor-arg>
                      </bean>
                  </property>

-Val



--
View this message in context: http://apache-ignite-users.70518.x6.nabble.com/How-to-configure-MongoDB-Client-I-m-getting-NotSerializableException-tp10767p10887.html
Sent from the Apache Ignite Users mailing list archive at Nabble.com.

Re: How to configure MongoDB Client? I'm getting NotSerializableException

Posted by Mauricio Arroqui <ma...@gmail.com>.
Thanks AG. 
I did what you suggest and now my code runs okey, but when I start a second
node on the same machine the first started node throws 
java.io.NotSerializableException:
org.apache.ignite.internal.processors.cache.store.GridCacheStoreManagerAdapter$ThreadLocalSession

My new xml configuration and code is: 

<bean class="org.apache.ignite.configuration.CacheConfiguration">
                    <property name="name" value="local_simulation"/>
                    <property name="cacheMode" value="LOCAL"/>
                    <property name="atomicityMode" value="ATOMIC"/>
                    <property name="copyOnRead" value="true"/>
                    <property name="cacheStoreSessionListenerFactories">
                      <bean
class="org.apache.ignite.configuration.IgniteReflectionFactory">
                        <property name="componentClass"  
value="uniagro.simugant.db.grid.mongodb.SimulationCacheMongoStoreSessionListener"/>
                      </bean>
                  </property>
                  
                  <property name="cacheStoreFactory">
                      <bean
class="javax.cache.configuration.FactoryBuilder$SingletonFactory">
                        <constructor-arg>
                          <bean
class="uniagro.simugant.db.grid.mongodb.SimulationCacheMongoStore">
                          </bean>
                        </constructor-arg>
                      </bean>
                  </property>
                  <property name="writeThrough" value="true"/>
                  <property name="writeBehindEnabled" value="true"/>
        </bean>

public class SimulationCacheMongoStoreSessionListener implements
LifecycleAware, CacheStoreSessionListener {

    public SimulationCacheMongoStoreSessionListener() {

    }

    private MongoClient mongoClient;

    @Override
    public void start() throws IgniteException {
        mongoClient = new MongoClient();
    }

    @Override
    public void stop() throws IgniteException {
        if (mongoClient != null) {
            mongoClient.close();

        }
    }

    @Override
    public void onSessionStart(CacheStoreSession ses) {
        if (ses.attachment() == null) {
            ses.attach(mongoClient);
        }

    }

    @Override
    public void onSessionEnd(CacheStoreSession ses, boolean commit) {
        ses.attach(null);
    }

public class SimulationCacheMongoStore extends CacheStoreAdapter<Long,
SimulationCache> implements Serializable{

    /**
     * Logger.
     */
    @LoggerResource
    private IgniteLogger log;

    @CacheStoreSessionResource
    private CacheStoreSession ses;

    public SimulationCacheMongoStore() {
        
    }
    
 
    @Override
    public SimulationCache load(Long k) throws CacheLoaderException {
        //NO-OP
    }

    @Override
    public void write(Cache.Entry<? extends Long, ? extends SimulationCache>
entry) throws CacheWriterException {
        MongoClient mongoClient = ses.attachment();
        Datastore morphia = new Morphia().createDatastore(mongoClient,
"test");
        morphia.save(entry.getValue());
        if (log.isDebugEnabled()) {
            log.debug("Stored Simulation: " + entry.getValue());
        }

    }

    @Override
    public void delete(Object o) throws CacheWriterException {
       NO-OP
    }

As I said, my code is running okey, but I don't know what are the back
effects that exception could have. 
I leave here the complete exception:

at
org.apache.ignite.marshaller.jdk.JdkMarshaller.marshal0(JdkMarshaller.java:84)
	at
org.apache.ignite.marshaller.AbstractNodeNameAwareMarshaller.marshal(AbstractNodeNameAwareMarshaller.java:70)
	at
org.apache.ignite.marshaller.jdk.JdkMarshaller.marshal0(JdkMarshaller.java:98)
	at
org.apache.ignite.marshaller.AbstractNodeNameAwareMarshaller.marshal(AbstractNodeNameAwareMarshaller.java:58)
	at
org.apache.ignite.internal.util.IgniteUtils.marshal(IgniteUtils.java:9861)
	at
org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi.collectExchangeData(TcpDiscoverySpi.java:1685)
	at
org.apache.ignite.spi.discovery.tcp.ServerImpl$RingMessageWorker.processNodeAddedMessage(ServerImpl.java:4092)
	at
org.apache.ignite.spi.discovery.tcp.ServerImpl$RingMessageWorker.processJoinRequestMessage(ServerImpl.java:3740)
	at
org.apache.ignite.spi.discovery.tcp.ServerImpl$RingMessageWorker.processMessage(ServerImpl.java:2532)
	at
org.apache.ignite.spi.discovery.tcp.ServerImpl$RingMessageWorker.processMessage(ServerImpl.java:2362)
	at
org.apache.ignite.spi.discovery.tcp.ServerImpl$MessageWorkerAdapter.body(ServerImpl.java:6436)
	at
org.apache.ignite.spi.discovery.tcp.ServerImpl$RingMessageWorker.body(ServerImpl.java:2445)
	at org.apache.ignite.spi.IgniteSpiThread.run(IgniteSpiThread.java:62)
Caused by: java.io.NotSerializableException:
org.apache.ignite.internal.processors.cache.store.GridCacheStoreManagerAdapter$ThreadLocalSession
	at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
	at
java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
	at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
	at
java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
	at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
	at
java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
	at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
	at
java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
	at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
	at
java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
	at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
	at
java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
	at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
	at
java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
	at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
	at
java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
	at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
	at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
	at java.util.ArrayList.writeObject(ArrayList.java:762)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:988)
	at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1496)
	at
java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
	at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
	at
java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
	at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
	at
java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
	at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
	at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
	at
org.apache.ignite.marshaller.jdk.JdkMarshaller.marshal0(JdkMarshaller.java:79)
	... 12 more

Regards,
Mauricio



--
View this message in context: http://apache-ignite-users.70518.x6.nabble.com/How-to-configure-MongoDB-Client-I-m-getting-NotSerializableException-tp10767p10875.html
Sent from the Apache Ignite Users mailing list archive at Nabble.com.

Re: How to configure MongoDB Client? I'm getting NotSerializableException

Posted by Alexey Goncharuk <al...@gmail.com>.
Hi Mauricio,

You encounter this exception because SingletonFactory actually stores a
reference to the factory instance which is later is serialized. Instead of
SingletonFactory, you can
use org.apache.ignite.configuration.IgniteReflectionFactory which does not
store this reference and can be successfully serialized. Please refer to
the IgniteReflectionFactory javadoc.

Hope this helps,
AG

2017-02-21 22:36 GMT+03:00 Mauricio Arroqui <ma...@gmail.com>:

> Hi,
>
> I have the same problem mentioned in the post below, but I use mongoDb
> instead of CacheJdbcBlobStore.
> http://apache-ignite-users.70518.x6.nabble.com/How-to-
> use-CacheJdbcBlobStore-Getting-NotSerializableException-td431.html
>
> Here is an example with Mongo, but it use Embedded MongoDB which it's main
> goal is for unit testing:
> https://github.com/gridgain/gridgain-advanced-examples/
> blob/master/src/main/java/org/gridgain/examples/datagrid/
> store/CacheMongoStore.java
>
> I read you solve the issue for CacheJdbcBlobStore
> https://issues.apache.org/jira/browse/IGNITE-960
>
> Are you planning to do the same with mongo? I'm losing something?
>
> Thanks in advance.
> Mauricio
>
> Here's a my bean configuration:
>  <bean class="org.apache.ignite.configuration.CacheConfiguration">
>                     <property name="name" value="local_simulation"/>
>                     <property name="cacheMode" value="LOCAL"/>
>                     <property name="atomicityMode" value="ATOMIC"/>
>                     <property name="copyOnRead" value="true"/>
>                      <property name="cacheStoreFactory">
>                       <bean
> class="javax.cache.configuration.FactoryBuilder$SingletonFactory">
>                         <constructor-arg>
>                           <bean
> class="uniagro.simugant.db.grid.mongodb.SimulationCacheMongoStore">
>                           </bean>
>                         </constructor-arg>
>                       </bean>
>                   </property>
>                   <property name="writeThrough" value="true"/>
>                   <property name="writeBehindEnabled" value="true"/>
>                 </bean>
>
> Abstract Cache store (SimulationCacheMongoStore extends this on):
> public abstract class CacheMongoStore<K, V> extends CacheStoreAdapter<K, V>
> implements Serializable, LifecycleAware {
>
>     /**
>      * MongoDB port.
>      */
>     private static final int MONGOD_PORT = 27017;
>
>     /**
>      * MongoDB executable for embedded MongoDB store.
>      */
>     private MongoClient mongoClient;
>
>     /**
>      * Mongo data store.
>      */
>     protected Datastore morphia;
>
>     @Override
>     public void start() throws IgniteException {
>
>         mongoClient = new MongoClient();
>         morphia = new Morphia().createDatastore(mongoClient, "test");
>     }
>
>     @Override
>     public void stop() throws IgniteException {
>         if (mongoClient != null) {
>             mongoClient.close();
>
>         }
>     }
>
> }
>
>
>
>
>
>
>
>
> --
> View this message in context: http://apache-ignite-users.
> 70518.x6.nabble.com/How-to-configure-MongoDB-Client-I-m-getting-
> NotSerializableException-tp10767.html
> Sent from the Apache Ignite Users mailing list archive at Nabble.com.
>