Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[query] [map] Hazelcast instance not injected in predicate #10620

Closed
mmedenjak opened this issue May 19, 2017 · 3 comments
Closed

[query] [map] Hazelcast instance not injected in predicate #10620

mmedenjak opened this issue May 19, 2017 · 3 comments

Comments

@mmedenjak
Copy link
Contributor

@mmedenjak mmedenjak commented May 19, 2017

Occurs when predicate is not serialized & deserialized because it was invoked on the partition owner.

Test :

@RunWith(Parameterized.class)
@Parameterized.UseParametersRunnerFactory(HazelcastParametersRunnerFactory.class)
public class PredicateHzAwareTest extends HazelcastTestSupport {
    @Parameterized.Parameters(name = "instanceCount:{0}")
    public static Collection<Object[]> parameters() {
        return Arrays.asList(new Object[][]{
                {1},
                {3}
        });
    }

    @Parameterized.Parameter(0)
    public int instanceCount;

    @Test
    public void testHzAware() throws Exception {
        final HazelcastInstance[] instances = createHazelcastInstanceFactory(instanceCount).newInstances();
        final IMap<String, Integer> m = instances[0].getMap("mappy");
        m.put("a", 1);
        m.put("b", 2);

        final Collection<Integer> result = m.project(new SimpleProjection(), new SimplePredicate());

        assertTrue(result.size() == 1);
        assertEquals(2, (int) result.iterator().next());
    }

    private static class SimpleProjection extends Projection<Map.Entry<String, Integer>, Integer>
            implements HazelcastInstanceAware, Serializable {

        private transient HazelcastInstance instance;

        @Override
        public Integer transform(Entry<String, Integer> input) {
            assertNotNull(instance);
            return input.getValue();
        }

        @Override
        public void setHazelcastInstance(HazelcastInstance hazelcastInstance) {
            this.instance = hazelcastInstance;
        }
    }

    private static class SimplePredicate implements Predicate<String, Integer>, Serializable, HazelcastInstanceAware {
        private transient HazelcastInstance instance;

        @Override
        public boolean apply(Entry<String, Integer> mapEntry) {
            assertNotNull(instance);
            return mapEntry.getValue() % 2 == 0;
        }

        @Override
        public void setHazelcastInstance(HazelcastInstance hazelcastInstance) {
            this.instance = hazelcastInstance;
        }
    }
}
@mmedenjak mmedenjak added this to the 3.9 milestone May 19, 2017
@tombujok
Copy link
Contributor

@tombujok tombujok commented May 19, 2017

good finding!

@pveentjer
Copy link
Member

@pveentjer pveentjer commented Jul 10, 2017

Perhaps it is best to force cloning the predicate. This will remove any unexpected shared state + would resolve this problem.

@tombujok
Copy link
Contributor

@tombujok tombujok commented Jul 10, 2017

@pveentjer I tried the cloning approach once - agree it would be good irrespective of this issue. But the PR got bigger and bigger, got a log more complex than initially though.

@mmedenjak mmedenjak changed the title Hazelcast instance not injected in predicate [query] [map] Hazelcast instance not injected in predicate Jul 11, 2017
@tombujok tombujok self-assigned this Sep 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

3 participants
You can’t perform that action at this time.