-
Notifications
You must be signed in to change notification settings - Fork 56
/
manager.go
114 lines (104 loc) · 2.94 KB
/
manager.go
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
/*
* Copyright (c) 2008-2022, 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 nearcache
import (
"sync"
"sync/atomic"
"github.com/hazelcast/hazelcast-go-client/internal/client"
ilogger "github.com/hazelcast/hazelcast-go-client/internal/logger"
"github.com/hazelcast/hazelcast-go-client/internal/proto"
"github.com/hazelcast/hazelcast-go-client/internal/serialization"
"github.com/hazelcast/hazelcast-go-client/nearcache"
)
type Manager struct {
nearCaches map[string]*NearCache
nearCachesMu *sync.RWMutex
ss *serialization.Service
rt *ReparingTask
lg ilogger.LogAdaptor
doneCh chan struct{}
state int32
}
func NewManager(ic *client.Client, reconInterval, maxMiss int) *Manager {
doneCh := make(chan struct{})
cs := ic.ClusterService
is := ic.InvocationService
ss := ic.SerializationService
ps := ic.PartitionService
inf := ic.InvocationFactory
lg := ic.Logger
mf := NewInvalidationMetaDataFetcher(cs, is, inf, lg)
uuid := ic.ConnectionManager.ClientUUID()
rt := NewReparingTask(reconInterval, maxMiss, ss, ps, lg, mf, uuid, doneCh)
ncm := &Manager{
nearCaches: map[string]*NearCache{},
nearCachesMu: &sync.RWMutex{},
ss: ss,
rt: rt,
lg: lg,
doneCh: doneCh,
}
return ncm
}
func (m *Manager) Stop() {
if atomic.CompareAndSwapInt32(&m.state, 0, 1) {
close(m.doneCh)
m.nearCachesMu.Lock()
for _, nc := range m.nearCaches {
nc.Destroy()
}
m.nearCaches = map[string]*NearCache{}
m.nearCachesMu.Unlock()
}
}
func (m *Manager) GetOrCreateNearCache(name string, cfg nearcache.Config) *NearCache {
m.nearCachesMu.RLock()
nc, ok := m.nearCaches[name]
m.nearCachesMu.RUnlock()
if ok {
return nc
}
m.nearCachesMu.Lock()
nc, ok = m.nearCaches[name]
if !ok {
nc = NewNearCache(&cfg, m.ss, m.lg)
m.nearCaches[name] = nc
}
m.nearCachesMu.Unlock()
return nc
}
func (m *Manager) RepairingTask() *ReparingTask {
return m.rt
}
func (m *Manager) GetNearCacheStats() []proto.Pair {
m.nearCachesMu.RLock()
nameStats := make([]proto.Pair, 0, len(m.nearCaches))
for name, nc := range m.nearCaches {
nameStats = append(nameStats, proto.NewPair(name, nc.Stats()))
}
m.nearCachesMu.RUnlock()
return nameStats
}
func (m *Manager) DestroyNearCache(name string) {
m.nearCachesMu.Lock()
defer m.nearCachesMu.Unlock()
nc, ok := m.nearCaches[name]
if !ok {
return
}
nc.Destroy()
delete(m.nearCaches, name)
}