/
QueryAdvancedTest.java
558 lines (472 loc) · 21.2 KB
/
QueryAdvancedTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/*
* Copyright (c) 2008-2020, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hazelcast.map.impl.query;
import com.hazelcast.config.Config;
import com.hazelcast.config.MapIndexConfig;
import com.hazelcast.config.MapStoreConfig;
import com.hazelcast.core.EntryAdapter;
import com.hazelcast.core.EntryEvent;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import com.hazelcast.core.MapStoreAdapter;
import com.hazelcast.nio.ObjectDataInput;
import com.hazelcast.nio.ObjectDataOutput;
import com.hazelcast.nio.serialization.DataSerializable;
import com.hazelcast.nio.serialization.Portable;
import com.hazelcast.nio.serialization.PortableFactory;
import com.hazelcast.query.Predicate;
import com.hazelcast.query.SampleTestObjects.Employee;
import com.hazelcast.query.SampleTestObjects.PortableEmployee;
import com.hazelcast.query.SampleTestObjects.ValueType;
import com.hazelcast.query.SqlPredicate;
import com.hazelcast.test.AssertTask;
import com.hazelcast.test.HazelcastParallelClassRunner;
import com.hazelcast.test.HazelcastTestSupport;
import com.hazelcast.test.TestHazelcastInstanceFactory;
import com.hazelcast.test.annotation.ParallelTest;
import com.hazelcast.test.annotation.QuickTest;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@RunWith(HazelcastParallelClassRunner.class)
@Category({QuickTest.class, ParallelTest.class})
public class QueryAdvancedTest extends HazelcastTestSupport {
@Rule
public ExpectedException expected = ExpectedException.none();
@Test
public void testQueryOperationAreNotSentToLiteMembers() {
TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
HazelcastInstance fullMember = nodeFactory.newHazelcastInstance();
HazelcastInstance liteMember = nodeFactory.newHazelcastInstance(new Config().setLiteMember(true));
assertClusterSizeEventually(2, fullMember);
IMap<Integer, Integer> map = fullMember.getMap(randomMapName());
DeserializationCountingPredicate predicate = new DeserializationCountingPredicate();
// initialize all partitions
for (int i = 0; i < 5000; i++) {
map.put(i, i);
}
map.values(predicate);
assertEquals(0, predicate.serializationCount());
}
public static class DeserializationCountingPredicate implements Predicate, DataSerializable {
private static final AtomicInteger counter = new AtomicInteger();
@Override
public boolean apply(Map.Entry mapEntry) {
return false;
}
@Override
public void writeData(ObjectDataOutput out) throws IOException {
}
@Override
public void readData(ObjectDataInput in) throws IOException {
counter.incrementAndGet();
}
int serializationCount() {
return counter.get();
}
}
@Test
@SuppressWarnings("deprecation")
public void testQueryWithTTL() {
Config config = getConfig();
String mapName = "default";
config.getMapConfig(mapName).setTimeToLiveSeconds(10);
HazelcastInstance instance = createHazelcastInstance(config);
IMap<String, Employee> map = instance.getMap(mapName);
map.addIndex("name", false);
map.addIndex("age", false);
map.addIndex("active", true);
int passiveEmployees = 5;
int activeEmployees = 5;
int allEmployees = passiveEmployees + activeEmployees;
final CountDownLatch latch = new CountDownLatch(allEmployees);
map.addEntryListener(new EntryAdapter() {
@Override
public void entryEvicted(EntryEvent event) {
latch.countDown();
}
}, false);
for (int i = 0; i < activeEmployees; i++) {
Employee employee = new Employee("activeEmployee" + i, 60, true, i);
map.put("activeEmployee" + i, employee);
}
for (int i = 0; i < passiveEmployees; i++) {
Employee employee = new Employee("passiveEmployee" + i, 60, false, i);
map.put("passiveEmployee" + i, employee);
}
// check the query result before eviction
Collection values = map.values(new SqlPredicate("active"));
assertEquals(String.format("Expected %s results but got %s. Number of evicted entries: %s.",
activeEmployees, values.size(), allEmployees - latch.getCount()),
activeEmployees, values.size());
// wait until eviction is completed
assertOpenEventually(latch);
// check the query result after eviction
values = map.values(new SqlPredicate("active"));
assertEquals(0, values.size());
}
@Test
public void testTwoNodesWithPartialIndexes() {
Config config = getConfig();
TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
HazelcastInstance instance1 = nodeFactory.newHazelcastInstance(config);
HazelcastInstance instance2 = nodeFactory.newHazelcastInstance(config);
IMap<String, Employee> map = instance1.getMap("employees");
map.addIndex("name", false);
map.addIndex("age", true);
map.addIndex("active", false);
for (int i = 0; i < 500; i++) {
Employee employee = new Employee(i, "name" + i % 100, "city" + (i % 100), i % 60, ((i & 1) == 1), (double) i);
map.put(String.valueOf(i), employee);
}
assertClusterSize(2, instance1, instance2);
map = instance2.getMap("employees");
map.addIndex("name", false);
map.addIndex("age", true);
map.addIndex("active", false);
Collection<Employee> entries = map.values(new SqlPredicate("name='name3' and city='city3' and age > 2"));
assertEquals(5, entries.size());
for (Employee employee : entries) {
assertEquals("name3", employee.getName());
assertEquals("city3", employee.getCity());
}
entries = map.values(new SqlPredicate("name LIKE '%name3' and city like '%city3' and age > 2"));
assertEquals(5, entries.size());
for (Employee employee : entries) {
assertEquals("name3", employee.getName());
assertEquals("city3", employee.getCity());
assertTrue(employee.getAge() > 2);
}
entries = map.values(new SqlPredicate("name LIKE '%name3%' and city like '%city30%'"));
assertEquals(5, entries.size());
for (Employee employee : entries) {
assertTrue(employee.getName().startsWith("name3"));
assertTrue(employee.getCity().startsWith("city3"));
}
}
@Test
public void testTwoNodesWithIndexes() {
Config config = getConfig();
TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
HazelcastInstance instance1 = nodeFactory.newHazelcastInstance(config);
HazelcastInstance instance2 = nodeFactory.newHazelcastInstance(config);
IMap<String, Employee> map = instance1.getMap("employees");
map.addIndex("name", false);
map.addIndex("city", false);
map.addIndex("age", true);
map.addIndex("active", false);
for (int i = 0; i < 5000; i++) {
Employee employee = new Employee(i, "name" + i % 100, "city" + (i % 100), i % 60, ((i & 1) == 1), (double) i);
map.put(String.valueOf(i), employee);
}
assertClusterSize(2, instance1, instance2);
map = instance2.getMap("employees");
map.addIndex("name", false);
map.addIndex("city", false);
map.addIndex("age", true);
map.addIndex("active", false);
Collection<Employee> entries = map.values(new SqlPredicate("name='name3' and city='city3' and age > 2"));
assertEquals(50, entries.size());
for (Employee employee : entries) {
assertEquals("name3", employee.getName());
assertEquals("city3", employee.getCity());
}
entries = map.values(new SqlPredicate("name LIKE '%name3' and city like '%city3' and age > 2"));
assertEquals(50, entries.size());
for (Employee employee : entries) {
assertEquals("name3", employee.getName());
assertEquals("city3", employee.getCity());
assertTrue(employee.getAge() > 2);
}
entries = map.values(new SqlPredicate("name LIKE '%name3%' and city like '%city30%'"));
assertEquals(50, entries.size());
for (Employee employee : entries) {
assertTrue(employee.getName().startsWith("name3"));
assertTrue(employee.getCity().startsWith("city3"));
}
}
@Test
public void testOneMemberWithoutIndex() {
HazelcastInstance instance = createHazelcastInstance(getConfig());
IMap<String, Employee> map = instance.getMap("employees");
QueryBasicTest.doFunctionalQueryTest(map);
}
@Test
public void testOneMemberWithIndex() {
HazelcastInstance instance = createHazelcastInstance(getConfig());
IMap<String, Employee> map = instance.getMap("employees");
map.addIndex("name", false);
map.addIndex("age", true);
map.addIndex("active", false);
QueryBasicTest.doFunctionalQueryTest(map);
}
@Test
public void testOneMemberSQLWithoutIndex() {
HazelcastInstance instance = createHazelcastInstance(getConfig());
IMap<String, Employee> map = instance.getMap("employees");
QueryBasicTest.doFunctionalSQLQueryTest(map);
Set<Map.Entry<String, Employee>> entries = map.entrySet(new SqlPredicate("active and age>23"));
assertEquals(27, entries.size());
}
@Test
public void testOneMemberSQLWithIndex() {
HazelcastInstance instance = createHazelcastInstance(getConfig());
IMap<String, Employee> map = instance.getMap("employees");
map.addIndex("name", false);
map.addIndex("age", true);
map.addIndex("active", false);
QueryBasicTest.doFunctionalSQLQueryTest(map);
}
@Test
public void testTwoMembers() {
Config config = getConfig();
TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
HazelcastInstance instance = nodeFactory.newHazelcastInstance(config);
nodeFactory.newHazelcastInstance(config);
IMap<String, Employee> map = instance.getMap("employees");
QueryBasicTest.doFunctionalQueryTest(map);
}
@Test
public void testTwoMembersWithIndexes() {
Config config = getConfig();
TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
HazelcastInstance instance = nodeFactory.newHazelcastInstance(config);
nodeFactory.newHazelcastInstance(config);
IMap<String, Employee> map = instance.getMap("employees");
map.addIndex("name", false);
map.addIndex("age", true);
map.addIndex("active", false);
QueryBasicTest.doFunctionalQueryTest(map);
}
@Test
public void testTwoMembersWithIndexesAndShutdown() {
Config config = getConfig();
TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
HazelcastInstance instance1 = nodeFactory.newHazelcastInstance(config);
HazelcastInstance instance2 = nodeFactory.newHazelcastInstance(config);
IMap<String, Employee> map = instance1.getMap("employees");
map.addIndex("name", false);
map.addIndex("age", true);
map.addIndex("active", false);
QueryBasicTest.doFunctionalQueryTest(map);
assertEquals(101, map.size());
instance2.getLifecycleService().shutdown();
assertEquals(101, map.size());
Set<Map.Entry<String, Employee>> entries = map.entrySet(new SqlPredicate("active and age=23"));
assertEquals(2, entries.size());
for (Map.Entry<String, Employee> entry : entries) {
Employee employee = entry.getValue();
assertEquals(employee.getAge(), 23);
assertTrue(employee.isActive());
}
}
@Test
public void testTwoMembersWithIndexesAndShutdown2() {
Config config = getConfig();
TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
HazelcastInstance instance1 = nodeFactory.newHazelcastInstance(config);
HazelcastInstance instance2 = nodeFactory.newHazelcastInstance(config);
IMap<String, Employee> map = instance1.getMap("employees");
map.addIndex("name", false);
map.addIndex("age", true);
map.addIndex("active", false);
QueryBasicTest.doFunctionalQueryTest(map);
assertEquals(101, map.size());
instance1.getLifecycleService().shutdown();
map = instance2.getMap("employees");
assertEquals(101, map.size());
Set<Map.Entry<String, Employee>> entries = map.entrySet(new SqlPredicate("active and age=23"));
assertEquals(2, entries.size());
for (Map.Entry<String, Employee> entry : entries) {
Employee employee = entry.getValue();
assertEquals(employee.getAge(), 23);
assertTrue(employee.isActive());
}
}
@Test
public void testTwoMembersWithIndexesAndShutdown3() {
Config config = getConfig();
TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
HazelcastInstance instance1 = nodeFactory.newHazelcastInstance(config);
IMap<String, Employee> map = instance1.getMap("employees");
map.addIndex("name", false);
map.addIndex("age", true);
map.addIndex("active", false);
QueryBasicTest.doFunctionalQueryTest(map);
assertEquals(101, map.size());
HazelcastInstance instance2 = nodeFactory.newHazelcastInstance(config);
assertEquals(101, map.size());
instance1.getLifecycleService().shutdown();
map = instance2.getMap("employees");
assertEquals(101, map.size());
Set<Map.Entry<String, Employee>> entries = map.entrySet(new SqlPredicate("active and age=23"));
assertEquals(2, entries.size());
for (Map.Entry<String, Employee> entry : entries) {
Employee employee = entry.getValue();
assertEquals(employee.getAge(), 23);
assertTrue(employee.isActive());
}
}
@Test
public void testSecondMemberAfterAddingIndexes() {
Config config = getConfig();
TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
HazelcastInstance instance = nodeFactory.newHazelcastInstance(config);
IMap<String, Employee> map = instance.getMap("employees");
map.addIndex("name", false);
map.addIndex("age", true);
map.addIndex("active", false);
nodeFactory.newHazelcastInstance(config);
QueryBasicTest.doFunctionalQueryTest(map);
}
@Test
public void testMapWithIndexAfterShutDown() {
Config config = getConfig();
String mapName = "default";
config.getMapConfig(mapName).addMapIndexConfig(new MapIndexConfig("typeName", false));
HazelcastInstance[] instances = createHazelcastInstanceFactory(3).newInstances(config);
final IMap<Integer, ValueType> map = instances[0].getMap(mapName);
final int sampleSize1 = 100;
final int sampleSize2 = 30;
int totalSize = sampleSize1 + sampleSize2;
for (int i = 0; i < sampleSize1; i++) {
map.put(i, new ValueType("type" + i));
}
for (int i = sampleSize1; i < totalSize; i++) {
map.put(i, new ValueType("typex"));
}
Collection typexValues = map.values(new SqlPredicate("typeName = typex"));
assertEquals(sampleSize2, typexValues.size());
instances[1].shutdown();
assertEquals(totalSize, map.size());
assertTrueEventually(new AssertTask() {
public void run() {
final Collection values = map.values(new SqlPredicate("typeName = typex"));
assertEquals(sampleSize2, values.size());
}
});
instances[2].shutdown();
assertEquals(totalSize, map.size());
assertTrueEventually(new AssertTask() {
public void run() {
final Collection values = map.values(new SqlPredicate("typeName = typex"));
assertEquals(sampleSize2, values.size());
}
});
}
// issue 1404 "to be fixed by issue 1404"
@Test
public void testQueryAfterInitialLoad() {
final int size = 100;
String name = "default";
Config config = getConfig();
MapStoreConfig mapStoreConfig = new MapStoreConfig();
mapStoreConfig.setEnabled(true);
mapStoreConfig.setImplementation(new MapStoreAdapter<Integer, Employee>() {
@Override
public Map<Integer, Employee> loadAll(Collection<Integer> keys) {
Map<Integer, Employee> map = new HashMap<Integer, Employee>();
for (Integer key : keys) {
Employee emp = new Employee();
emp.setActive(true);
map.put(key, emp);
}
return map;
}
@Override
public Set<Integer> loadAllKeys() {
Set<Integer> set = new HashSet<Integer>();
for (int i = 0; i < size; i++) {
set.add(i);
}
return set;
}
});
config.getMapConfig(name).setMapStoreConfig(mapStoreConfig);
HazelcastInstance instance = createHazelcastInstance(config);
final IMap map = instance.getMap(name);
assertTrueEventually(new AssertTask() {
@Override
public void run() throws Exception {
Collection values = map.values(new SqlPredicate("active = true"));
assertEquals(size, values.size());
}
});
}
@Test
// see: https://github.com/hazelcast/hazelcast/issues/3927
public void testUnknownPortableField_notCausesQueryException_withoutIndex() {
String mapName = randomMapName();
Config config = getConfig();
config.getSerializationConfig().addPortableFactory(666, new PortableFactory() {
public Portable create(int classId) {
return new PortableEmployee();
}
});
HazelcastInstance hazelcastInstance = createHazelcastInstance(config);
IMap<Integer, PortableEmployee> map = hazelcastInstance.getMap(mapName);
for (int i = 0; i < 5; i++) {
map.put(i, new PortableEmployee(i, "name_" + i));
}
Collection values = map.values(new SqlPredicate("notExist = name_0 OR a > 1"));
assertEquals(3, values.size());
}
@Test
// see: https://github.com/hazelcast/hazelcast/issues/3927
public void testUnknownPortableField_notCausesQueryException_withIndex() {
String mapName = "default";
Config config = getConfig();
config.getSerializationConfig().addPortableFactory(666, new PortableFactory() {
public Portable create(int classId) {
return new PortableEmployee();
}
});
config.getMapConfig(mapName)
.addMapIndexConfig(new MapIndexConfig("notExist", false))
.addMapIndexConfig(new MapIndexConfig("n", false));
HazelcastInstance hazelcastInstance = createHazelcastInstance(config);
IMap<Integer, PortableEmployee> map = hazelcastInstance.getMap(mapName);
for (int i = 0; i < 5; i++) {
map.put(i, new PortableEmployee(i, "name_" + i));
}
Collection values = map.values(new SqlPredicate("n = name_2 OR notExist = name_0"));
assertEquals(1, values.size());
}
@Test
public void testClassNotFoundErrorDelegatedToCallerOnQuery() {
Config config = getConfig();
HazelcastInstance hazelcastInstance = createHazelcastInstance(config);
IMap<Integer, Integer> map = hazelcastInstance.getMap("map");
map.put(1, 1);
//A remote predicate can throw Error in case of Usercodeployment and missing sub classes
//See the issue for actual problem https://github.com/hazelcast/hazelcast/issues/18052
//We are throwing error to see if the error is delegated to the caller
expected.expect(NoClassDefFoundError.class);
map.values(new ErrorThrowingPredicate());
}
}