Skip to content

Commit 45e143c

Browse files
authored
Hz fix (#528)
Fixed issue with hazelcast query cache
1 parent 4406298 commit 45e143c

File tree

3 files changed

+108
-1
lines changed

3 files changed

+108
-1
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.devicehive.application;
2+
3+
/*
4+
* #%L
5+
* DeviceHive Dao RDBMS Implementation
6+
* %%
7+
* Copyright (C) 2016 DataArt
8+
* %%
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
* #L%
21+
*/
22+
23+
import com.hazelcast.hibernate.HazelcastLocalCacheRegionFactory;
24+
import com.hazelcast.hibernate.local.LocalRegionCache;
25+
import com.hazelcast.hibernate.region.HazelcastTimestampsRegion;
26+
import org.hibernate.cache.CacheException;
27+
import org.hibernate.cache.spi.TimestampsRegion;
28+
29+
import java.util.Properties;
30+
31+
// Fix for issues described in https://github.com/hazelcast/hazelcast/issues/13271 and https://github.com/hazelcast/hazelcast-hibernate5/issues/33
32+
// Monitor when either of those are fixed, after that we should remove this and DhTimestampsRegionaCache
33+
public class DhLocalCacheRegionFactory extends HazelcastLocalCacheRegionFactory {
34+
35+
@Override
36+
public TimestampsRegion buildTimestampsRegion(final String regionName, final Properties properties)
37+
throws CacheException {
38+
return new HazelcastTimestampsRegion<LocalRegionCache>(instance, regionName, properties,
39+
new DhTimestampsRegionCache(regionName, instance));
40+
}
41+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.devicehive.application;
2+
3+
/*
4+
* #%L
5+
* DeviceHive Dao RDBMS Implementation
6+
* %%
7+
* Copyright (C) 2016 DataArt
8+
* %%
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
* #L%
21+
*/
22+
23+
import com.hazelcast.core.HazelcastInstance;
24+
import com.hazelcast.hibernate.local.Timestamp;
25+
import com.hazelcast.hibernate.local.TimestampsRegionCache;
26+
import com.hazelcast.hibernate.serialization.Expirable;
27+
import com.hazelcast.hibernate.serialization.Value;
28+
29+
// Fix for issues described in https://github.com/hazelcast/hazelcast/issues/13271 and https://github.com/hazelcast/hazelcast-hibernate5/issues/33
30+
// Monitor when either of those are fixed, after that we should remove this and DhLocalCacheRegionFactory
31+
public class DhTimestampsRegionCache extends TimestampsRegionCache {
32+
33+
public DhTimestampsRegionCache(String name, HazelcastInstance hazelcastInstance) {
34+
super(name, hazelcastInstance);
35+
}
36+
37+
@Override
38+
protected void maybeInvalidate(final Object messageObject) {
39+
Timestamp ts = (Timestamp) messageObject;
40+
final Object key = ts.getKey();
41+
42+
if (ts.getTimestamp() > System.currentTimeMillis()) {
43+
ts = new Timestamp(ts.getKey(), System.currentTimeMillis());
44+
}
45+
46+
while (true) {
47+
final Expirable value = cache.get(key);
48+
final Long current = value != null ? (Long) value.getValue() : null;
49+
if (current != null) {
50+
if (ts.getTimestamp() > current) {
51+
if (cache.replace(key, value, new Value(value.getVersion(), nextTimestamp(), ts.getTimestamp()))) {
52+
return;
53+
}
54+
} else {
55+
return;
56+
}
57+
} else {
58+
if (cache.putIfAbsent(key, new Value(null, nextTimestamp(), ts.getTimestamp())) == null) {
59+
return;
60+
}
61+
}
62+
}
63+
}
64+
}

devicehive-rdbms-dao/src/main/resources/application-persistence.properties

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,7 @@ spring.jpa.properties.hibernate.cache.use_second_level_cache=true
3333
spring.jpa.properties.hibernate.cache.use_query_cache=true
3434
spring.jpa.properties.hibernate.generate_statistics=true
3535
spring.jpa.properties.hibernate.cache.use_structured_entries=true
36-
spring.jpa.properties.hibernate.cache.region.factory_class=com.hazelcast.hibernate.HazelcastLocalCacheRegionFactory
36+
# Replace back with com.hazelcast.hibernate.HazelcastLocalCacheRegionFactory when https://github.com/hazelcast/hazelcast/issues/13271
37+
# and https://github.com/hazelcast/hazelcast-hibernate5/issues/33 are fixed
38+
spring.jpa.properties.hibernate.cache.region.factory_class=com.devicehive.application.DhLocalCacheRegionFactory
3739
spring.jpa.properties.hibernate.cache.hazelcast.use_native_client=true

0 commit comments

Comments
 (0)