-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontext.go
More file actions
107 lines (88 loc) · 2.76 KB
/
context.go
File metadata and controls
107 lines (88 loc) · 2.76 KB
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
package clusterkit
import (
"context"
"encoding/base64"
"fmt"
)
// SetCustomDataContext stores custom data with context support for cancellation
// The data is stored as raw bytes, allowing any serialization format (JSON, protobuf, msgpack, etc.)
// This operation goes through Raft consensus and will fail if this node is not the leader
// Maximum value size is 1MB to prevent excessive memory usage
func (ck *ClusterKit) SetCustomDataContext(ctx context.Context, key string, value []byte) error {
// Check context before expensive operations
select {
case <-ctx.Done():
return ctx.Err()
default:
}
if key == "" {
return fmt.Errorf("key cannot be empty")
}
if len(value) > 1024*1024 { // 1MB limit
return fmt.Errorf("value too large (max 1MB, got %d bytes)", len(value))
}
// Encode value as base64 for JSON serialization
valueEncoded := base64.StdEncoding.EncodeToString(value)
return ck.consensusManager.ProposeAction("set_custom_data", map[string]interface{}{
"key": key,
"value": valueEncoded,
})
}
// GetCustomDataContext retrieves custom data with context support
// This is a local read operation and does not require consensus
// Returns an error if the key is not found
func (ck *ClusterKit) GetCustomDataContext(ctx context.Context, key string) ([]byte, error) {
// Check context
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
ck.mu.RLock()
defer ck.mu.RUnlock()
if ck.cluster.CustomData == nil {
return nil, fmt.Errorf("key not found: %s", key)
}
value, exists := ck.cluster.CustomData[key]
if !exists {
return nil, fmt.Errorf("key not found: %s", key)
}
// Return a copy to prevent external modification
valueCopy := make([]byte, len(value))
copy(valueCopy, value)
return valueCopy, nil
}
// DeleteCustomDataContext removes custom data with context support
// This operation goes through Raft consensus and will fail if this node is not the leader
func (ck *ClusterKit) DeleteCustomDataContext(ctx context.Context, key string) error {
// Check context
select {
case <-ctx.Done():
return ctx.Err()
default:
}
if key == "" {
return fmt.Errorf("key cannot be empty")
}
return ck.consensusManager.ProposeAction("delete_custom_data", key)
}
// CreatePartitionsContext initializes partitions with context support
func (ck *ClusterKit) CreatePartitionsContext(ctx context.Context) error {
// Check context before expensive operation
select {
case <-ctx.Done():
return ctx.Err()
default:
}
return ck.CreatePartitions()
}
// RebalancePartitionsContext redistributes partitions with context support
func (ck *ClusterKit) RebalancePartitionsContext(ctx context.Context) error {
// Check context before expensive operation
select {
case <-ctx.Done():
return ctx.Err()
default:
}
return ck.RebalancePartitions()
}