Colin Werner created IGNITE-29380:
-------------------------------------

             Summary: Java thin client inverts the sort order of QueryIndex 
fields when it creates a cache
                 Key: IGNITE-29380
                 URL: https://issues.apache.org/jira/browse/IGNITE-29380
             Project: Ignite
          Issue Type: Bug
          Components: thin client
    Affects Versions: 2.18
            Reporter: Colin Werner


Also filed as https://github.com/apache/ignite/issues/13905.

Ignite 2.18.0; the same code is on master (447e554a).

h3. Summary

A cache created through the Java thin client 
({{IgniteClient.createCache(ClientCacheConfiguration)}}) with a {{QueryEntity}} 
index gets every index field in the opposite order to the one requested: fields 
marked ascending ({{true}}) are built descending, and the other way round. The 
same {{QueryEntity}} created through the server API gets the requested order.

Reading the configuration back through the thin client shows the requested 
order, so the inversion is only visible in {{SYS.INDEXES}} or in query plans.

h3. Cause

The client writes each field's flag as "ascending"; the server reads it as 
"descending".

* Thin client, {{ClientUtils}}: writes {{w.writeBoolean(f.getValue())}} and 
reads {{reader.readBoolean()}} back unchanged.
* Server, {{ClientCacheConfigurationSerializer}}: reads indexes with 
{{PlatformConfigurationUtils.readQueryIndex}} ({{fields.put(in.readString(), 
!in.readBoolean())}}) and writes them with {{writeQueryIndex}} 
({{writer.writeBoolean(!field.getValue())}}).

{{PlatformConfigurationUtils}} follows the platform protocol, where the flag 
means "descending", which is how the .NET client sends it. The Java thin client 
sends "ascending" without inverting it, so the server inverts it once on the 
way in. On the way back the server inverts it again and the client does not, so 
a round trip looks correct.

h3. Reproducer

One server node with {{ignite-indexing}} on the classpath, and a thin client on 
127.0.0.1:10800:

{code:java}
LinkedHashMap<String, String> fields = new LinkedHashMap<>();
fields.put("id", Integer.class.getName());
fields.put("a", Integer.class.getName());
fields.put("b", Integer.class.getName());

LinkedHashMap<String, Boolean> idxFields = new LinkedHashMap<>();
idxFields.put("a", true); // ascending
idxFields.put("b", true); // ascending

QueryEntity entity = new QueryEntity(Integer.class.getName(), "Val")
    .setTableName("T")
    .setKeyFieldName("id")
    .setFields(fields)
    .setIndexes(List.of(new QueryIndex(idxFields, 
QueryIndexType.SORTED).setName("T_A_B_IDX")));

client.createCache(new 
ClientCacheConfiguration().setName("t").setSqlSchema("PUBLIC").setQueryEntities(entity));

// The same entity through the server API, for comparison
QueryEntity same = new QueryEntity(entity).setTableName("S");
same.setIndexes(List.of(new QueryIndex(idxFields, 
QueryIndexType.SORTED).setName("S_A_B_IDX")));
server.createCache(new 
CacheConfiguration<>("s").setSqlSchema("PUBLIC").setQueryEntities(List.of(same)));

client.query(new SqlFieldsQuery(
    "SELECT INDEX_NAME, COLUMNS FROM SYS.INDEXES WHERE INDEX_NAME IN 
('T_A_B_IDX', 'S_A_B_IDX')"))
    .getAll().forEach(System.out::println);

System.out.println(client.cache("t").getConfiguration().getQueryEntities()[0].getIndexes().iterator().next().getFields());
{code}

Output:

{noformat}
[S_A_B_IDX, "A" ASC, "B" ASC, "_KEY" ASC]
[T_A_B_IDX, "A" DESC, "B" DESC, "_KEY" ASC]
{a=true, b=true}
{noformat}

h3. Impact

Results are correct, but queries that should read an ascending index in order 
sort instead. For a paged walk such as {{WHERE a = ? AND b > ? ORDER BY a, b 
LIMIT n}}, the plan uses {{T_A_B_IDX}} for the range but cannot use it for the 
order, so every page reads the rest of the range and sorts it. On a 
{{REPLICATED}} cache the plan shows this directly (no {{/* index sorted */}}). 
On a {{PARTITIONED}} cache the reduce step's merge prints {{/* index sorted 
*/}}, which hides it.

In our case, a filtered walk over 10,000 rows took 624 ms on the inverted index 
and 44 ms on a correctly ordered one.

An index's order cannot change once the cache exists, so caches already created 
this way keep their inverted indexes.

h3. Workaround

Pass the opposite flag from the thin client ({{false}} for ascending) and check 
the result in {{SYS.INDEXES}}.

h3. Suggested fix

Invert the flag in {{ClientUtils}} both when writing and when reading index 
fields, so the wire format matches the platform protocol:

* write: {{w.writeBoolean(!f.getValue())}}
* read: {{new SimpleEntry<>(reader.readString(), !reader.readBoolean())}}

Clients that already work around the bug would then get the inverted order 
again, so the fix needs a release note (and ideally a mention in the thin 
client's migration notes).



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to