Skip to content

Commit 681d92e

Browse files
authored
Near Cache Test Updates (#850)
Updated NC tests.
1 parent 066f852 commit 681d92e

File tree

5 files changed

+171
-32
lines changed

5 files changed

+171
-32
lines changed

client_internals_test_utils.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// +build hazelcastinternal,hazelcastinternaltest
33

44
/*
5-
* Copyright (c) 2008-2021, Hazelcast, Inc. All Rights Reserved.
5+
* Copyright (c) 2008-2022, Hazelcast, Inc. All Rights Reserved.
66
*
77
* Licensed under the Apache License, Version 2.0 (the "License")
88
* you may not use this file except in compliance with the License.
@@ -54,11 +54,15 @@ func (ci *ClientInternal) SerializationService() *serialization.Service {
5454
return ci.client.ic.SerializationService
5555
}
5656

57+
func (ci *ClientInternal) NewNearCacheManager(reconInterval, maxMiss int) *inearcache.Manager {
58+
return inearcache.NewManager(ci.client.ic, reconInterval, maxMiss)
59+
}
60+
5761
// MakeNearCacheAdapterFromMap returns the nearcache of the given map.
5862
// It returns an interface{} instead of it.NearCacheAdapter in order not to introduce an import cycle.
5963
func MakeNearCacheAdapterFromMap(m *Map) interface{} {
6064
if m.hasNearCache {
61-
return NearCacheTestAdapter{m: m}
65+
return &NearCacheTestAdapter{m: m}
6266
}
6367
panic("hazelcast.MakeNearCacheAdapterFromMap: map has no near cache")
6468
}
@@ -67,6 +71,13 @@ type NearCacheTestAdapter struct {
6771
m *Map
6872
}
6973

74+
func (n *NearCacheTestAdapter) NearCache() *inearcache.NearCache {
75+
if n.m.hasNearCache {
76+
return n.m.ncm.nc
77+
}
78+
panic("hazelcast.NearCacheTestAdapter.NearCache: map has no near cache")
79+
}
80+
7081
func (n NearCacheTestAdapter) Size() int {
7182
return n.m.ncm.nc.Size()
7283
}

internal/it/adapters.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008-2021, Hazelcast, Inc. All Rights Reserved.
2+
* Copyright (c) 2008-2022, Hazelcast, Inc. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License")
55
* you may not use this file except in compliance with the License.
@@ -36,4 +36,5 @@ type NearCacheAdapter interface {
3636
GetRecord(key interface{}) (*inearcache.Record, bool)
3737
InvalidationRequests() int64
3838
ToNearCacheKey(key interface{}) interface{}
39+
NearCache() *inearcache.NearCache
3940
}

internal/it/util.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,16 @@ func (c TestCluster) DefaultConfig() hz.Config {
342342
return config
343343
}
344344

345+
func (c TestCluster) DefaultConfigWithNoSSL() hz.Config {
346+
config := hz.Config{}
347+
config.Cluster.Name = c.ClusterID
348+
config.Cluster.Network.SetAddresses(fmt.Sprintf("localhost:%d", c.Port))
349+
if TraceLoggingEnabled() {
350+
config.Logger.Level = logger.TraceLevel
351+
}
352+
return config
353+
}
354+
345355
func xmlConfig(clusterName string, port int) string {
346356
return fmt.Sprintf(`
347357
<hazelcast xmlns="http://www.hazelcast.com/schema/config"

internal/nearcache/repairing_task.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -481,10 +481,6 @@ func (mc *MetaDataContainer) CASStaleSequence(lastKnown, lastReceived int64) boo
481481
return atomic.CompareAndSwapInt64(&mc.staleSeq, lastKnown, lastReceived)
482482
}
483483

484-
func (mc *MetaDataContainer) SetMissedSequenceCount(count int64) {
485-
atomic.StoreInt64(&mc.missedSeqs, count)
486-
}
487-
488484
func (mc *MetaDataContainer) MissedSequenceCount() int64 {
489485
return atomic.LoadInt64(&mc.missedSeqs)
490486
}

nearcache/nearcache_it_test.go

Lines changed: 146 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"math/rand"
2626
"os"
2727
"strconv"
28+
"sync"
2829
"testing"
2930
"time"
3031

@@ -65,7 +66,7 @@ func TestSmokeNearCachePopulation(t *testing.T) {
6566
ctx := context.Background()
6667
ncc := nearcache.Config{Name: mapName}
6768
ncc.SetInvalidateOnChange(true)
68-
cfg := cls.DefaultConfig()
69+
cfg := cls.DefaultConfigWithNoSSL()
6970
cfg.AddNearCache(ncc)
7071
client := it.MustClient(hz.StartNewClientWithConfig(nil, cfg))
7172
defer client.Shutdown(ctx)
@@ -368,24 +369,36 @@ func TestNearCacheGet(t *testing.T) {
368369

369370
func TestNearCacheInvalidateOnChange(t *testing.T) {
370371
// port of: com.hazelcast.client.map.impl.nearcache.ClientMapNearCacheTest#testNearCacheInvalidateOnChange
371-
tcx := newNearCacheMapTestContextWithExpiration(t, nearcache.InMemoryFormatObject, true)
372-
tcx.Tester(func(tcx it.MapTestContext) {
373-
m := tcx.M
374-
t := tcx.T
375-
ctx := context.Background()
376-
const size = 118
377-
populateServerMapWithString(ctx, tcx, size)
378-
// populate Near Cache
379-
populateNearCacheWithString(tcx, size)
380-
oec := m.LocalMapStats().NearCacheStats.OwnedEntryCount
381-
require.Equal(t, int64(size), oec)
382-
// invalidate Near Cache from server side
383-
populateServerMapWithString(ctx, tcx, size)
384-
it.Eventually(t, func() bool {
385-
oec := m.LocalMapStats().NearCacheStats.OwnedEntryCount
386-
t.Logf("OEC: %d", oec)
387-
return oec == 0
388-
})
372+
tcx := it.MapTestContext{T: t}
373+
const port = 54001
374+
clusterName := t.Name()
375+
clsCfg := invalidationXMLConfig(clusterName, "non-existent", port)
376+
tcx.Cluster = it.StartNewClusterWithConfig(1, clsCfg, port)
377+
defer tcx.Cluster.Shutdown()
378+
ctx := context.Background()
379+
const size = 118
380+
tcx.MapName = it.NewUniqueObjectName("map")
381+
populateServerMapWithString(ctx, tcx, size)
382+
ncc := nearcache.Config{
383+
Name: tcx.MapName,
384+
}
385+
ncc.SetInvalidateOnChange(true)
386+
cfg := tcx.Cluster.DefaultConfigWithNoSSL()
387+
cfg.AddNearCache(ncc)
388+
tcx.Config = &cfg
389+
tcx.Client = it.MustClient(hz.StartNewClientWithConfig(ctx, *tcx.Config))
390+
defer tcx.Client.Shutdown(ctx)
391+
tcx.M = it.MustValue(tcx.Client.GetMap(ctx, tcx.MapName)).(*hz.Map)
392+
// populate Near Cache
393+
populateNearCacheWithString(tcx, size)
394+
oec := tcx.M.LocalMapStats().NearCacheStats.OwnedEntryCount
395+
require.Equal(t, int64(size), oec)
396+
// invalidate Near Cache from server side
397+
populateServerMapWithString(ctx, tcx, size)
398+
it.Eventually(t, func() bool {
399+
oec := tcx.M.LocalMapStats().NearCacheStats.OwnedEntryCount
400+
t.Logf("OEC: %d", oec)
401+
return oec == 0
389402
})
390403
}
391404

@@ -925,7 +938,7 @@ func TestMemberLoadAllInvalidatesClientNearCache(t *testing.T) {
925938
map.loadAll(true);
926939
`, tcx.MapName)
927940
}
928-
memberInvalidatesClientNearCache(t, f)
941+
memberInvalidatesClientNearCache(t, 51001, f)
929942
}
930943

931944
func TestMemberPutAllInvalidatesClientNearCache(t *testing.T) {
@@ -940,11 +953,12 @@ func TestMemberPutAllInvalidatesClientNearCache(t *testing.T) {
940953
map.putAll(items);
941954
`, size, tcx.MapName)
942955
}
943-
memberInvalidatesClientNearCache(t, f)
956+
memberInvalidatesClientNearCache(t, 51011, f)
944957
}
945958

946959
func TestMemberSetAllInvalidatesClientNearCache(t *testing.T) {
947960
// see: com.hazelcast.client.map.impl.nearcache.ClientMapNearCacheTest#testMemberSetAll_invalidates_clientNearCache
961+
it.SkipIf(t, "hz > 4, hz < 4.1")
948962
f := func(tcx it.MapTestContext, size int32) string {
949963
return fmt.Sprintf(`
950964
var items = new java.util.HashMap(%[1]d);
@@ -955,10 +969,117 @@ func TestMemberSetAllInvalidatesClientNearCache(t *testing.T) {
955969
map.setAll(items);
956970
`, size, tcx.MapName)
957971
}
958-
memberInvalidatesClientNearCache(t, f)
972+
memberInvalidatesClientNearCache(t, 51021, f)
959973
}
960974

961-
func memberInvalidatesClientNearCache(t *testing.T, makeScript func(tcx it.MapTestContext, size int32) string) {
975+
func TestForceRepairingTaskRun(t *testing.T) {
976+
// TODO: mark this test slow.
977+
// this is a test for covering async init of RepairingTask.
978+
clusterName := t.Name()
979+
mapName := it.NewUniqueObjectName("map")
980+
const port = 53001
981+
clsCfg := invalidationXMLConfig(clusterName, "non-existent", port)
982+
cls := it.StartNewClusterWithConfig(1, clsCfg, port)
983+
const mapSize = 1000
984+
// populate server side map
985+
for i := 0; i < mapSize; i++ {
986+
v := strconv.Itoa(i)
987+
it.MapSetOnServer(cls.ClusterID, mapName, v, v)
988+
}
989+
// add client with Near Cache
990+
ctx := context.Background()
991+
cfg := cls.DefaultConfigWithNoSSL()
992+
cfg.NearCacheInvalidation.SetReconciliationIntervalSeconds(30)
993+
cfg.NearCacheInvalidation.SetMaxToleratedMissCount(1)
994+
ncc := nearcache.Config{
995+
Name: mapName,
996+
}
997+
ncc.SetInvalidateOnChange(true)
998+
cfg.AddNearCache(ncc)
999+
client := it.MustClient(hz.StartNewClientWithConfig(nil, cfg))
1000+
defer client.Shutdown(ctx)
1001+
ic := hz.NewClientInternal(client)
1002+
mgr := ic.NewNearCacheManager(1, 1)
1003+
defer mgr.Stop()
1004+
m := it.MustValue(client.GetMap(ctx, mapName)).(*hz.Map)
1005+
nca := hz.MakeNearCacheAdapterFromMap(m).(it.NearCacheAdapter)
1006+
for i := int32(0); i < mapSize; i++ {
1007+
if _, err := m.Get(ctx, i); err != nil {
1008+
t.Fatal(err)
1009+
}
1010+
}
1011+
cls.Shutdown()
1012+
wg := &sync.WaitGroup{}
1013+
wg.Add(1)
1014+
go func() {
1015+
time.Sleep(2 * time.Second)
1016+
cls = it.StartNewClusterWithConfig(1, clsCfg, port)
1017+
t.Logf("starting the new cluster: %p", &cls)
1018+
wg.Done()
1019+
}()
1020+
defer func() {
1021+
t.Logf("stopping the new cluster: %p", &cls)
1022+
cls.Shutdown()
1023+
}()
1024+
_, err := mgr.RepairingTask().RegisterAndGetHandler(ctx, mapName, nca.NearCache())
1025+
if err != nil {
1026+
panic(err)
1027+
}
1028+
time.Sleep(2 * time.Second)
1029+
wg.Wait()
1030+
// populate server side map
1031+
for i := 0; i < mapSize; i++ {
1032+
v := strconv.Itoa(i)
1033+
it.MapSetOnServer(cls.ClusterID, mapName, v, v)
1034+
}
1035+
for i := int32(0); i < mapSize; i++ {
1036+
if _, err := m.Get(ctx, i); err != nil {
1037+
t.Fatal(err)
1038+
}
1039+
}
1040+
t.Logf("waiting for the repair task do its job")
1041+
time.Sleep(35 * time.Second)
1042+
if err := m.Destroy(ctx); err != nil {
1043+
t.Fatal(err)
1044+
}
1045+
}
1046+
1047+
func TestRepairingTaskRun(t *testing.T) {
1048+
// this is a test for covering sync init of RepairingTask.
1049+
clusterName := t.Name()
1050+
mapName := it.NewUniqueObjectName("map")
1051+
const port = 53011
1052+
clsCfg := invalidationXMLConfig(clusterName, "non-existent", port)
1053+
cls := it.StartNewClusterWithConfig(1, clsCfg, port)
1054+
defer cls.Shutdown()
1055+
const mapSize = 1000
1056+
// populate server side map
1057+
for i := 0; i < mapSize; i++ {
1058+
v := strconv.Itoa(i)
1059+
it.MapSetOnServer(cls.ClusterID, mapName, v, v)
1060+
}
1061+
// add client with Near Cache
1062+
ctx := context.Background()
1063+
cfg := cls.DefaultConfigWithNoSSL()
1064+
ncc := nearcache.Config{
1065+
Name: mapName,
1066+
}
1067+
cfg.AddNearCache(ncc)
1068+
client := it.MustClient(hz.StartNewClientWithConfig(nil, cfg))
1069+
defer client.Shutdown(ctx)
1070+
ic := hz.NewClientInternal(client)
1071+
mgr := ic.NewNearCacheManager(1, 1)
1072+
defer mgr.Stop()
1073+
m := it.MustValue(client.GetMap(ctx, mapName)).(*hz.Map)
1074+
for i := int32(0); i < mapSize; i++ {
1075+
if _, err := m.Get(ctx, i); err != nil {
1076+
t.Fatal(err)
1077+
}
1078+
}
1079+
time.Sleep(2 * time.Second)
1080+
}
1081+
1082+
func memberInvalidatesClientNearCache(t *testing.T, port int, makeScript func(tcx it.MapTestContext, size int32) string) {
9621083
// ported from: com.hazelcast.client.map.impl.nearcache.ClientMapNearCacheTest#testMemberLoadAll_invalidates_clientNearCache
9631084
tcx := it.MapTestContext{
9641085
T: t,
@@ -967,7 +1088,6 @@ func memberInvalidatesClientNearCache(t *testing.T, makeScript func(tcx it.MapTe
9671088
}
9681089
clusterName := t.Name()
9691090
tcx.MapName = it.NewUniqueObjectName("map")
970-
const port = 51001
9711091
clsCfg := invalidationXMLConfig(clusterName, tcx.MapName, port)
9721092
cls := it.StartNewClusterWithConfig(1, clsCfg, port)
9731093
defer cls.Shutdown()
@@ -978,10 +1098,11 @@ func memberInvalidatesClientNearCache(t *testing.T, makeScript func(tcx it.MapTe
9781098
InMemoryFormat: nearcache.InMemoryFormatBinary,
9791099
}
9801100
ncc.SetInvalidateOnChange(true)
981-
cfg := cls.DefaultConfig()
1101+
cfg := cls.DefaultConfigWithNoSSL()
9821102
cfg.AddNearCache(ncc)
9831103
tcx.Config = &cfg
9841104
client := it.MustClient(hz.StartNewClientWithConfig(nil, cfg))
1105+
defer client.Shutdown(ctx)
9851106
tcx.Client = client
9861107
m := it.MustValue(tcx.Client.GetMap(ctx, tcx.MapName)).(*hz.Map)
9871108
tcx.M = m

0 commit comments

Comments
 (0)