1

I'm trying to do the following:

  1. Put some data into geode using the redis connector
  2. React to some key create / update events using a CacheListener
  3. Read that data using the Geode client and it's entrySet method on the Region.

I already had trouble accessing the redis data from my Geode client. I had to do the following:

region.get(Coder.stringToByteArrayWrapper("key"));

I also had alot of trouble making region.entrySet() work. First, it doesn't work at all with ClientRegionShortcut.PROXY and it seems that it works only 50% of the time with ClientRegionShortcut.CACHING_PROXY.

Here's the code i'm using to test this (note that i'm using lettuce as a redis client):

@Test
public void test_subscribe() throws InterruptedException, ExecutionException {
    ClientCache cache = new ClientCacheFactory()
        .addPoolLocator(HOST, LOCATOR_PORT)
        .create();

    @SuppressWarnings({ "rawtypes", "unchecked" })
    CacheListener<ByteArrayWrapper, ByteArrayWrapper> cl = new CacheListenerAdapter() {
        @Override
        public void afterCreate(EntryEvent event) {
            System.out.println("Created: " + event.getKey() + " = " + event.getNewValue());
        }

        @Override
        public void afterUpdate(EntryEvent event) {
            System.out.println("Updated: " + event.getKey() + " replacing " + event.getOldValue() + "with" + event.getNewValue());
        }
    };

    Region<ByteArrayWrapper, ByteArrayWrapper> region = cache
        .<ByteArrayWrapper, ByteArrayWrapper> createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY)
        .addCacheListener(cl)
        .setKeyConstraint(ByteArrayWrapper.class)
        .setValueConstraint(ByteArrayWrapper.class)
        .create(GeodeRedisServer.STRING_REGION);

    RedisClient client = RedisClient.create("redis://" + HOST);
    StatefulRedisConnection<String, String> connection = client.connect();
    RedisAsyncCommands<String, String> cmd = connection.async();

    cmd.set("1", "HelloGeodeRedis").get();
    cmd.set("2", "WorldGeodeRedis" + System.currentTimeMillis()).get();

    System.out.println("FromRedis: " + cmd.get("2").get());
    System.out.println("FromGeode: " + region.get(Coder.stringToByteArrayWrapper("2")));

    for (Map.Entry<?, ?> entry : region.entrySet()) {
        System.out.format("key = %s, value = %s\n", entry.getKey(), entry.getValue());
    }

    cache.close();
}

I'm wondering if the 50% thingy has to do with the servers i'm running:

gfsh>describe region --name=ReDiS_StRiNgS
..........................................................
Name            : ReDiS_StRiNgS
Data Policy     : persistent partition
Hosting Members : my-redis

Non-Default Attributes Shared By Hosting Members

 Type  |    Name     | Value
------ | ----------- | --------------------
Region | size        | 2
       | data-policy | PERSISTENT_PARTITION

gfsh>describe region --name my-region
..........................................................
Name            : my-region
Data Policy     : persistent replicate
Hosting Members : my-server
                  my-redis

Non-Default Attributes Shared By Hosting Members

 Type  |    Name     | Value
------ | ----------- | --------------------
Region | data-policy | PERSISTENT_REPLICATE
       | size        | 2
       | scope       | distributed-ack

gfsh>list members
      Name       | Id
---------------- | -----------------------------------------------------------
my-locator | 172.16.202.245(my-locator:21234:locator)<ec><v0>:1024
my-server  | 172.16.202.245(my-server:22154)<v1>:1025
my-redis   | 172.16.202.245(my-redis:24890)<v2>:1026

As you can see the region I created manually is hosted by both servers but the one created by redis is only hosted by the redis server.

The error i'm getting 50% of the time is the following:

org.apache.geode.cache.client.ServerOperationException: remote server on My-Computer(4352:loner):64103:58d54999: While performing a remote get
    at org.apache.geode.cache.client.internal.AbstractOp.processObjResponse(AbstractOp.java:285)
    at org.apache.geode.cache.client.internal.GetOp$GetOpImpl.processResponse(GetOp.java:143)
    at org.apache.geode.cache.client.internal.AbstractOp.attemptReadResponse(AbstractOp.java:171)
    at org.apache.geode.cache.client.internal.AbstractOp.attempt(AbstractOp.java:382)
    at org.apache.geode.cache.client.internal.ConnectionImpl.execute(ConnectionImpl.java:275)
    at org.apache.geode.cache.client.internal.pooling.PooledConnection.execute(PooledConnection.java:332)
    at org.apache.geode.cache.client.internal.OpExecutorImpl.executeWithPossibleReAuthentication(OpExecutorImpl.java:900)
    at org.apache.geode.cache.client.internal.OpExecutorImpl.execute(OpExecutorImpl.java:158)
    at org.apache.geode.cache.client.internal.OpExecutorImpl.execute(OpExecutorImpl.java:115)
    at org.apache.geode.cache.client.internal.PoolImpl.execute(PoolImpl.java:763)
    at org.apache.geode.cache.client.internal.GetOp.execute(GetOp.java:91)
    at org.apache.geode.cache.client.internal.ServerRegionProxy.get(ServerRegionProxy.java:116)
    at org.apache.geode.internal.cache.LocalRegion.findObjectInSystem(LocalRegion.java:2776)
    at org.apache.geode.internal.cache.LocalRegion.nonTxnFindObject(LocalRegion.java:1488)
    at org.apache.geode.internal.cache.LocalRegionDataView.findObject(LocalRegionDataView.java:175)
    at org.apache.geode.internal.cache.LocalRegion.get(LocalRegion.java:1377)
    at org.apache.geode.internal.cache.LocalRegion.get(LocalRegion.java:1310)
    at org.apache.geode.internal.cache.LocalRegion.get(LocalRegion.java:1295)
    at org.apache.geode.internal.cache.AbstractRegion.get(AbstractRegion.java:320)
    at trial.GeodeTest.test_subscribe(GeodeTest.java:112)
    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:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.apache.geode.cache.RegionDestroyedException: Server connection from [identity(192.168.64.106(4352:loner):64103:58d54999,connection=1; port=64103]: Region named /ReDiS_StRiNgS/ReDiS_StRiNgS was not found during get request
    at org.apache.geode.internal.cache.tier.sockets.BaseCommand.writeRegionDestroyedEx(BaseCommand.java:615)
    at org.apache.geode.internal.cache.tier.sockets.command.Get70.cmdExecute(Get70.java:126)
    at org.apache.geode.internal.cache.tier.sockets.BaseCommand.execute(BaseCommand.java:165)
    at org.apache.geode.internal.cache.tier.sockets.ServerConnection.doNormalMsg(ServerConnection.java:780)
    at org.apache.geode.internal.cache.tier.sockets.ServerConnection.doOneMessage(ServerConnection.java:911)
    at org.apache.geode.internal.cache.tier.sockets.ServerConnection.run(ServerConnection.java:1166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.geode.internal.cache.tier.sockets.AcceptorImpl$1$1.run(AcceptorImpl.java:523)
    at java.lang.Thread.run(Thread.java:745)

So you have the full disclaimer, the reason i'm testing this is that I want to put data from a kafka topic into geode using kafka-connect-redis to avoid having to code a geode kafka connector myself.

EDIT: The 50% issue has been fixed thanks to @Swapnil but now i'm back to having trouble making entrySet and event notification work. It seems that unless I forcefully get the key I put using the redis client, I'm not getting any EntryEvent notification.

Crystark
  • 3,693
  • 5
  • 40
  • 61

2 Answers2

2

The problem I had with not receiving the events was because I didn't register an interest to the keys I was interested in.

Adding region.registerInterestRegex(".*"); allows to mark an interest to every key. It requires to set up the ClientCache with .setPoolSubscriptionEnabled(true).

    ClientCache cache = new ClientCacheFactory()
        .addPoolLocator(HOST, LOCATOR_PORT)
        .setPoolSubscriptionEnabled(true)
        .create();

    // ...

    Region<ByteArrayWrapper, ByteArrayWrapper> region = cache
        .<ByteArrayWrapper, ByteArrayWrapper> createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY)
        .addCacheListener(cl)
        .setKeyConstraint(ByteArrayWrapper.class)
        .setValueConstraint(ByteArrayWrapper.class)
        .create(GeodeRedisServer.STRING_REGION);

    region.registerInterestRegex(".*");

    // ...
Crystark
  • 3,693
  • 5
  • 40
  • 61
  • I had the [same problem](https://stackoverflow.com/questions/45286249/apache-geode-cachelistener-not-executing) – rupweb Aug 05 '17 at 18:26
1

I suspect it is way you start your Geode servers. I had no problems when I setup my Geode cluster like so:

gfsh>start locator --name=loc1  
gfsh>start server --name=serv1 --redis-port=1111 --redis-bind-address=localhost  
gfsh>start server --name=serv2 --server-port=40405 --redis-port=2222 --redis-bind-address=localhost

describe regions shows me that RedisStrings was created on both servers:

gfsh>describe region --name=/ReDiS_StRiNgS
..........................................................
Name            : ReDiS_StRiNgS
Data Policy     : partition
Hosting Members : serv2
                  serv1

Non-Default Attributes Shared By Hosting Members

 Type  |    Name     | Value
------ | ----------- | ---------
Region | size        | 0
       | data-policy | PARTITION

Then From my test program, I inserted data using through Redis (Jedis client) and then read back the data successfully from Geode:

public void putRedis() {
  Jedis jedis = new Jedis("localhost", 1111);
  for (int i=0; i<10; i++) {
    jedis.set("foo"+i, "bar"+i);
  }  
}  

public void readGeode() {
  ClientCacheFactory clientCacheFactory = new ClientCacheFactory();
  clientCacheFactory.addPoolLocator("localhost", 10334);
  ClientCache client = clientCacheFactory.create();
  Region redisStrings = client.createClientRegionFactory(ClientRegionShortcut.PROXY).create("ReDiS_StRiNgS");  
  for (int i=0; i<10; i++) {
    System.out.println(redisStrings.get(Coder.stringToByteArrayWrapper("foo"+i)));
  }
}
Swapnil
  • 1,191
  • 7
  • 8
  • Thanks it indeed had to do with that. I stopped the other server and no more errors. I guess that means that each server much host a redis adapter. – Crystark Aug 01 '17 at 07:50
  • It seems that the 50% issue has been fixed but now i'm facing an other preoblem: unless I forcefully get the keys I put using the geode client, i'm getting no event notification and no data in my `entrySet`. – Crystark Aug 01 '17 at 08:11