Search code examples
gemfiregeode

Running Gemfire Query during Transaction on field without Query Index


We are seeing below exception when querying a field in a region during GF transaction in a gemfire client.

java.lang.ClassCastException: com.gemstone.gemfire.internal.cache.EntrySnapshot cannot be cast to com.gemstone.gemfire.internal.cache.LocalRegion$NonTXEntry

at com.gemstone.gemfire.internal.cache.EntriesSet$EntriesIterator.moveNext(EntriesSet.java:183) 
at com.gemstone.gemfire.internal.cache.EntriesSet$EntriesIterator.<init>(EntriesSet.java:121) 
at com.gemstone.gemfire.internal.cache.EntriesSet.iterator(EntriesSet.java:85) 
at com.gemstone.gemfire.cache.query.internal.ResultsCollectionWrapper.iterator(ResultsCollectionWrapper.java:181) 
at com.gemstone.gemfire.cache.query.internal.QRegion.iterator(QRegion.java:225) 
at com.gemstone.gemfire.cache.query.internal.CompiledSelect.doNestedIterations(CompiledSelect.java:712) 
at com.gemstone.gemfire.cache.query.internal.CompiledSelect.doIterationEvaluate(CompiledSelect.java:577) 
at com.gemstone.gemfire.cache.query.internal.CompiledSelect.evaluate(CompiledSelect.java:413) 
at com.gemstone.gemfire.cache.query.internal.DefaultQuery.executeUsingContext(DefaultQuery.java:529) 
at com.gemstone.gemfire.cache.query.internal.DefaultQuery.execute(DefaultQuery.java:365)

From our trial and error, It only happens when it meets below criteria

  1. Transaction is running: i.e. called gemfireCache.getCacheTransactionManager().begin() and then execute the query

  2. Not create Functional index for the Query/Field: i.e. QueryService.createIndex(String, String, String) is not called on the specific field during init

3. where condition has a field that can be null in the region data: i.e. if run "SELECT * FROM /REGIONNAME WHERE fieldName = $1", if fieldName is null in some entry, it throws the above exception, otherwise, it is fine.

We are using compiled Query acquired from QueryService.newQuery("SELECT * FROM /REGIONNAME WHERE fieldName = $1"). and the queryservice is a local one if I am not wrong, not running query on the server.

We are using Gemfire 8.2.1

comment below if you need more information.

--- Update 05/12/2016 ---

Finally get some time to put together a simple test case to illustrate the problem:

I Launch the process using JUnit, just a personal habit. The first test case start a server with co-located locator on port 40001

The second test case start a client process and runs a query without index within transaction.

public class GemfireQueryInTXTest {

@Test
public void startServer() throws Exception {
    Properties props = new Properties();
    System.setProperty("gemfirePropertyFile", "query_in_tx/gfserver-query-in-tx.properties");
    String file = DistributedSystem.getPropertyFileURL().getFile();
    props.load(new FileReader(file));

    Cache cache = new CacheFactory(props).create();
    RegionFactory<String, ValueEntry> factory = cache
            .<String, ValueEntry>createRegionFactory("REPLICATE")
            .setKeyConstraint(String.class)
            .setValueConstraint(ValueEntry.class);

    Region<String, ValueEntry> valueEntryRegion = factory.create("VALUEENTRY");

    valueEntryRegion.put("first", new ValueEntry("firstEntry", "NotNull"));
    valueEntryRegion.put("second", new ValueEntry("secondEntry", null));

    CacheServer server = cache.addCacheServer();
    server.setPort(40000);

    server.start();

    Thread.sleep(1000000L);
}

@Test
public void testRunningQueryDuringTransactionOnNullableField() throws Exception {
    Properties props = new Properties();
    System.setProperty("gemfirePropertyFile", "query_in_tx/gemfire-query-in-tx.properties");
    String file = DistributedSystem.getPropertyFileURL().getFile();
    props.load(new FileReader(file));

    ClientCache cache = new ClientCacheFactory(props).create();
    ClientRegionFactory<String, ValueEntry> factory = cache
            .<String, ValueEntry>createClientRegionFactory("DEFAULT")
            .setKeyConstraint(String.class)
            .setValueConstraint(ValueEntry.class);

    Region<String, ValueEntry> valueEntryRegion = factory.create("VALUEENTRY");
    valueEntryRegion.registerInterest(".*", InterestResultPolicy.KEYS_VALUES);

    CacheTransactionManager cacheTransactionManager = cache.getCacheTransactionManager();

    QueryService localQueryService = cache.getLocalQueryService();
    Query query = localQueryService.newQuery("SELECT * from /VALUEENTRY WHERE nullable = $1");
    // No Exception will be thrown if create index for the field (uncomment below);
    // localQueryService.createIndex("IndexName", "nullable", "/VALUEENTRY");

    // ... Or run without transaction (comment below tx opening and closing)
    cacheTransactionManager.begin();
    System.out.println("Before Query Executed");
    query.execute(new Object[]{"1"});
    System.out.println("After Query Executed");
    cacheTransactionManager.commit();
}
}

Domain Object: ValueEntry.java

public class ValueEntry implements DataSerializable {
private String notNull;
private String nullable;

public ValueEntry() {
}

public ValueEntry(String notNull, String nullable) {
    this.notNull = notNull;
    this.nullable = nullable;
}

public String getNotNull() {
    return notNull;
}

public String getNullable() {
    return nullable;
}

@Override
public void toData(DataOutput dataOutput) throws IOException {
    DataSerializer.writeString(notNull, dataOutput);
    DataSerializer.writeString(nullable, dataOutput);
}

@Override
public void fromData(DataInput dataInput) throws IOException, ClassNotFoundException {
    this.notNull = DataSerializer.readString(dataInput);
    this.nullable = DataSerializer.readString(dataInput);
}
}

Server Proerties and xml:

cache-xml-file=query_in_tx\\cache-server.xml
start-locator=40001
locators=localhost[40001]
log-file=logs\\server.log
log-level=config
mcast-port=0
name=server

<cache>
<serialization-registration>
    <instantiator id="999">
        <class-name>com.testing.gemfire.domain.ValueEntry</class-name>
    </instantiator>
</serialization-registration>
</cache>

Client Proerties and xml:

cache-xml-file=query_in_tx\\cache-query-in-tx.xml
log-disk-space-limit=100
log-file-size-limit=20
log-file=logs\\cache.log
log-level=config
mcast-port=0
name=gemfire-playground

<client-cache>
    <pool name="Zero" subscription-enabled="true" read-timeout="3000"
          retry-attempts="5" socket-buffer-size="65536">
        <locator host="localhost" port="40001" />
    </pool>

    <region-attributes id="DEFAULT" refid="CACHING_PROXY" pool-name="Zero"/>
</client-cache>

Solution

  • After communicated with Pivotal team, this turns out to be a bug in Gemfire client. As mentioned in the question description, creating index will stop this exception from being thrown.

    I will update again if it is fixed in the future version.