Skip to content

Commit d439621

Browse files
committed
Implment Counter in local cache
1 parent 4aae206 commit d439621

File tree

9 files changed

+700
-11
lines changed

9 files changed

+700
-11
lines changed

pkg/console/context/context.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ import (
2121
ctx "context"
2222

2323
"github.com/apache/dubbo-admin/pkg/config/app"
24+
"github.com/apache/dubbo-admin/pkg/console/counter"
2425
"github.com/apache/dubbo-admin/pkg/core/manager"
2526
"github.com/apache/dubbo-admin/pkg/core/runtime"
2627
)
2728

2829
type Context interface {
2930
ResourceManager() manager.ResourceManager
31+
CounterManager() counter.CounterManager
3032

3133
Config() app.AdminConfig
3234

@@ -57,3 +59,15 @@ func (c *context) ResourceManager() manager.ResourceManager {
5759
rmc, _ := c.coreRt.GetComponent(runtime.ResourceManager)
5860
return rmc.(manager.ResourceManagerComponent).ResourceManager()
5961
}
62+
63+
func (c *context) CounterManager() counter.CounterManager {
64+
comp, err := c.coreRt.GetComponent(counter.ComponentType)
65+
if err != nil {
66+
return nil
67+
}
68+
managerComp, ok := comp.(counter.ManagerComponent)
69+
if !ok {
70+
return nil
71+
}
72+
return managerComp.CounterManager()
73+
}

pkg/console/counter/component.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package counter
19+
20+
import (
21+
"fmt"
22+
"math"
23+
24+
"github.com/apache/dubbo-admin/pkg/core/events"
25+
"github.com/apache/dubbo-admin/pkg/core/runtime"
26+
)
27+
28+
const ComponentType runtime.ComponentType = "counter manager"
29+
30+
func init() {
31+
runtime.RegisterComponent(&managerComponent{})
32+
}
33+
34+
type ManagerComponent interface {
35+
runtime.Component
36+
CounterManager() CounterManager
37+
}
38+
39+
var _ ManagerComponent = &managerComponent{}
40+
41+
type managerComponent struct {
42+
manager CounterManager
43+
}
44+
45+
func (c *managerComponent) Type() runtime.ComponentType {
46+
return ComponentType
47+
}
48+
49+
func (c *managerComponent) Order() int {
50+
return math.MaxInt - 1
51+
}
52+
53+
func (c *managerComponent) Init(runtime.BuilderContext) error {
54+
mgr := NewCounterManager()
55+
c.manager = mgr
56+
return nil
57+
}
58+
59+
func (c *managerComponent) Start(rt runtime.Runtime, _ <-chan struct{}) error {
60+
component, err := rt.GetComponent(runtime.EventBus)
61+
if err != nil {
62+
return err
63+
}
64+
bus, ok := component.(events.EventBus)
65+
if !ok {
66+
return fmt.Errorf("component %s does not implement events.EventBus", runtime.EventBus)
67+
}
68+
return c.manager.Bind(bus)
69+
}
70+
71+
func (c *managerComponent) CounterManager() CounterManager {
72+
return c.manager
73+
}

pkg/console/counter/counter.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package counter
19+
20+
import (
21+
"sync"
22+
"sync/atomic"
23+
)
24+
25+
type Counter struct {
26+
name string
27+
value atomic.Int64
28+
}
29+
30+
func NewCounter(name string) *Counter {
31+
return &Counter{name: name}
32+
}
33+
34+
func (c *Counter) Get() int64 {
35+
return c.value.Load()
36+
}
37+
38+
func (c *Counter) Increment() {
39+
c.value.Add(1)
40+
}
41+
42+
func (c *Counter) Decrement() {
43+
for {
44+
current := c.value.Load()
45+
if current == 0 {
46+
return
47+
}
48+
if c.value.CompareAndSwap(current, current-1) {
49+
return
50+
}
51+
}
52+
}
53+
54+
func (c *Counter) Reset() {
55+
c.value.Store(0)
56+
}
57+
58+
type DistributionCounter struct {
59+
name string
60+
data map[string]int64
61+
mu sync.RWMutex
62+
}
63+
64+
func NewDistributionCounter(name string) *DistributionCounter {
65+
return &DistributionCounter{
66+
name: name,
67+
data: make(map[string]int64),
68+
}
69+
}
70+
71+
func (c *DistributionCounter) Increment(key string) {
72+
c.mu.Lock()
73+
defer c.mu.Unlock()
74+
c.data[key]++
75+
}
76+
77+
func (c *DistributionCounter) Decrement(key string) {
78+
c.mu.Lock()
79+
defer c.mu.Unlock()
80+
if value, ok := c.data[key]; ok {
81+
value--
82+
if value <= 0 {
83+
delete(c.data, key)
84+
} else {
85+
c.data[key] = value
86+
}
87+
}
88+
}
89+
90+
func (c *DistributionCounter) GetAll() map[string]int64 {
91+
c.mu.RLock()
92+
defer c.mu.RUnlock()
93+
result := make(map[string]int64, len(c.data))
94+
for k, v := range c.data {
95+
result[k] = v
96+
}
97+
return result
98+
}
99+
100+
func (c *DistributionCounter) Reset() {
101+
c.mu.Lock()
102+
defer c.mu.Unlock()
103+
c.data = make(map[string]int64)
104+
}

0 commit comments

Comments
 (0)