Cannot serialize predicate

344 views
Skip to first unread message

Yury Kudryashov

unread,
Nov 25, 2013, 11:42:58 AM11/25/13
to haze...@googlegroups.com
Hi,

I've run into the following issue in Hazelcast 3.1: I have a cluster and a client node. On a client node I try to register the entry listener for the IMap:
map.addEntryListener(new ListenerAdapter<>(listener), new PredicateAdapter<>(predicate), true);

Both ListenerAdapter and PredicateAdapter are Java Serializables as well as all their fields. But I get the following from Hazelcast:
com.hazelcast.nio.serialization.HazelcastSerializationException: java.lang.ClassCastException: ....PredicateAdapter cannot be cast to com.hazelcast.nio.serialization.Portable
        at com.hazelcast.nio.serialization.SerializationServiceImpl.handleException(SerializationServiceImpl.java:290)
        at com.hazelcast.nio.serialization.SerializationServiceImpl.toData(SerializationServiceImpl.java:192)
        at com.hazelcast.nio.serialization.SerializationServiceImpl.toData(SerializationServiceImpl.java:155)
        at com.hazelcast.client.spi.impl.ClientClusterServiceImpl._sendAndHandle(ClientClusterServiceImpl.java:270)
        at com.hazelcast.client.spi.impl.ClientClusterServiceImpl.sendAndHandle(ClientClusterServiceImpl.java:257)
        at com.hazelcast.client.spi.impl.ClientInvocationServiceImpl.invokeOnRandomTarget(ClientInvocationServiceImpl.java:60)
        at com.hazelcast.client.spi.ListenerSupport$1.run(ListenerSupport.java:60)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
        at java.util.concurrent.FutureTask.run(FutureTask.java:166)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:722)
        at com.hazelcast.util.executor.PoolExecutorThreadFactory$ManagedThread.run(PoolExecutorThreadFactory.java:59)
Caused by: java.lang.ClassCastException: ....PredicateAdapter cannot be cast to com.hazelcast.nio.serialization.Portable
        at com.hazelcast.map.client.MapAddEntryListenerRequest.writePortable(MapAddEntryListenerRequest.java:139)
        at com.hazelcast.nio.serialization.PortableSerializer.write(PortableSerializer.java:57)
        at com.hazelcast.nio.serialization.PortableSerializer.write(PortableSerializer.java:29)
        at com.hazelcast.nio.serialization.StreamSerializerAdapter.write(StreamSerializerAdapter.java:48)


The reason seems to be in com.hazelcast.nio.serialization.SerializationServiceImpl.toData - it registers a portableSerializerAdapter for any instance of Portable, and MapAddEntryListenerRequest requires Predicate to be Portable (see MapAddEntryListenerRequest.writePortable). This is a very limiting restriction provided I'm not going to use Hazelcast's Portable serialization and not going to tie my predicate implementation to any of Hazelcast-specific interfaces.

Is there a way to work around this without using Portables? 

-- Yury

Peter Veentjer

unread,
Nov 25, 2013, 11:50:33 AM11/25/13
to haze...@googlegroups.com
Can you provide a test which reproduces this problem? 


--
You received this message because you are subscribed to the Google Groups "Hazelcast" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hazelcast+...@googlegroups.com.
To post to this group, send email to haze...@googlegroups.com.
Visit this group at http://groups.google.com/group/hazelcast.
For more options, visit https://groups.google.com/groups/opt_out.

Yury Kudryashov

unread,
Nov 25, 2013, 12:44:24 PM11/25/13
to haze...@googlegroups.com
Here is the simplest test:

I have two classes, Client and Server.

Server:
public class Server  {
    public static void main(String[] args) throws IOException {
        HazelcastInstance hz = Hazelcast.newHazelcastInstance();
        System.out.println("Ready");

        System.in.read();
        hz.shutdown();
    }
}

Client:
public class Client {

    public static void main(String[] args) throws InterruptedException {
        HazelcastInstance client = HazelcastClient.newHazelcastClient();
        System.out.println("Ready");
        final IMap<Object,Object> a = client.getMap("A");
        a.addEntryListener(new EntryAdapter<Object, Object>() {
            @Override
            public void entryAdded(EntryEvent<Object, Object> event) {
                System.out.println(event.getKey());
            }
        }, new TestPredicate(), true);
        Thread.sleep(1000000);
        client.shutdown();
    }

    private static class TestPredicate implements Predicate<Object, Object> {
        @Override
        public boolean apply(Map.Entry<Object, Object> objectObjectEntry) {
            return true;
        }
    }
}

If you run server first, wait for "Ready" message and start "Client", you will see endless "INFO: Closing connection -> Connection" messages in Client's console. Debugging traces it to SerializationServiceImpl and HazelcastSerializationException (Hazelcast 3.1) is used. If I use a listener without predicate, everything runs smoothly.


понедельник, 25 ноября 2013 г., 20:50:33 UTC+4 пользователь peter veentjer написал:

Peter Veentjer

unread,
Nov 25, 2013, 1:18:35 PM11/25/13
to haze...@googlegroups.com
Thanks. 

With 3.2-SNAPSHOT it doesn't cause any problems. 

Let me try 3.1 maintenance branch

Peter Veentjer

unread,
Nov 25, 2013, 1:29:41 PM11/25/13
to haze...@googlegroups.com
With 3.1.2-SNAPSHOT there is no problem either.

With 3.1 I do get the error you are describing:

Nov 25, 2013 8:25:39 PM com.hazelcast.client.connection.ClientConnectionManager
INFO: Closing connection -> Connection [Address[192.168.1.103]:5701 -> /192.168.1.103:50678]
.... repeating

You could try with 3.1.1. Perhaps the issue has been solved in that release. Unfortunately somebody
forgot to make a git tag for 3.1.1, so I can't check that one out and try it.

Otherwise use 3.1.2-SNAPSHOT for the time being, till we have released 3.1.2.

Peter.

Peter Veentjer

unread,
Nov 25, 2013, 2:01:12 PM11/25/13
to haze...@googlegroups.com
My bad.

I forgot to update my local repo. 

Can you try with Hazelcast 3.1.2? 

On my machine it doesn't give your problem.

Yury Kudryashov

unread,
Nov 25, 2013, 3:02:47 PM11/25/13
to haze...@googlegroups.com
Hi Peter, thank you very much. Indeed upgrading to 3.1.2 helps, and I see the issue fixed in the code.

понедельник, 25 ноября 2013 г., 23:01:12 UTC+4 пользователь peter veentjer написал:
Reply all
Reply to author
Forward
0 new messages