redis-allocator 0.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Invoker Bot
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,229 @@
1
+ Metadata-Version: 2.4
2
+ Name: redis-allocator
3
+ Version: 0.0.1
4
+ Summary: Redis-based resource allocation system.
5
+ Home-page: https://github.com/invoker-bot/RedisAllocator-python
6
+ Author: Invoker Bot
7
+ Author-email: invoker-bot@outlook.com
8
+ License: MIT
9
+ Classifier: Development Status :: 4 - Beta
10
+ Classifier: Intended Audience :: Developers
11
+ Classifier: Programming Language :: Python :: 3.9
12
+ Classifier: License :: OSI Approved :: MIT License
13
+ Classifier: Operating System :: OS Independent
14
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
15
+ Requires-Python: >=3.10
16
+ Description-Content-Type: text/markdown
17
+ License-File: LICENSE
18
+ Requires-Dist: redis>=5.0.0
19
+ Provides-Extra: test
20
+ Requires-Dist: pytest>=7.4.3; extra == "test"
21
+ Requires-Dist: pytest-cov>=4.1.0; extra == "test"
22
+ Requires-Dist: pytest-mock>=3.12.0; extra == "test"
23
+ Requires-Dist: fakeredis[lua]>=2.20.1; extra == "test"
24
+ Requires-Dist: flake8>=6.1.0; extra == "test"
25
+ Requires-Dist: freezegun>=1.4.0; extra == "test"
26
+ Dynamic: author
27
+ Dynamic: author-email
28
+ Dynamic: classifier
29
+ Dynamic: description
30
+ Dynamic: description-content-type
31
+ Dynamic: home-page
32
+ Dynamic: license
33
+ Dynamic: license-file
34
+ Dynamic: provides-extra
35
+ Dynamic: requires-dist
36
+ Dynamic: requires-python
37
+ Dynamic: summary
38
+
39
+ # RedisAllocator
40
+
41
+ ## Project Overview
42
+
43
+ RedisAllocator is an efficient Redis-based distributed memory allocation system. This system simulates traditional memory allocation mechanisms but implements them in a distributed environment, using Redis as the underlying storage and coordination tool.
44
+
45
+ > **Note**: Currently, RedisAllocator only supports single Redis instance deployments. For Redis cluster environments, we recommend using RedLock for distributed locking operations.
46
+
47
+ ### Core Features
48
+
49
+ - **Distributed Locking**: Provides robust distributed locking mechanisms to ensure data consistency in concurrent environments
50
+ - **Resource Allocation**: Implements a distributed resource allocation system with support for:
51
+ - Priority-based distribution
52
+ - Soft binding
53
+ - Garbage collection
54
+ - Health checking
55
+ - **Task Management**: Implements a distributed task queue system for efficient task processing across multiple workers
56
+ - **Object Allocation**: Supports allocation of resources with priority-based distribution and soft binding
57
+ - **Health Checking**: Monitors the health of distributed instances and automatically handles unhealthy resources
58
+ - **Garbage Collection**: Automatically identifies and reclaims unused resources, optimizing memory usage
59
+
60
+
61
+ ## Installation
62
+
63
+ ```bash
64
+ pip install redis-allocator
65
+ ```
66
+
67
+ ## Quick Start
68
+
69
+ ### Using RedisLock for Distributed Locking
70
+
71
+ ```python
72
+ from redis import Redis
73
+ from redis_allocator import RedisLock
74
+
75
+ # Initialize Redis client
76
+ redis = Redis(host='localhost', port=6379)
77
+
78
+ # Create a RedisLock instance
79
+ lock = RedisLock(redis, "myapp", "resource-lock")
80
+
81
+ # Acquire a lock
82
+ if lock.lock("resource-123", timeout=60):
83
+ try:
84
+ # Perform operations with the locked resource
85
+ print("Resource locked successfully")
86
+ finally:
87
+ # Release the lock when done
88
+ lock.unlock("resource-123")
89
+ ```
90
+
91
+ ### Using RedisAllocator for Resource Management
92
+
93
+ ```python
94
+ from redis import Redis
95
+ from redis_allocator import RedisAllocator
96
+
97
+ # Initialize Redis client
98
+ redis = Redis(host='localhost', port=6379)
99
+
100
+ # Create a RedisAllocator instance
101
+ allocator = RedisAllocator(
102
+ redis,
103
+ prefix='myapp',
104
+ suffix='allocator',
105
+ shared=False # Whether resources can be shared
106
+ )
107
+
108
+ # Add resources to the pool
109
+ allocator.extend(['resource-1', 'resource-2', 'resource-3'])
110
+
111
+ # Allocate a resource key (returns only the key)
112
+ key = allocator.malloc_key(timeout=120)
113
+ if key:
114
+ try:
115
+ # Use the allocated resource
116
+ print(f"Allocated resource: {key}")
117
+ finally:
118
+ # Free the resource when done
119
+ allocator.free_keys(key)
120
+
121
+ # Allocate a resource with object (returns a RedisAllocatorObject)
122
+ allocated_obj = allocator.malloc(timeout=120)
123
+ if allocated_obj:
124
+ try:
125
+ # The key is available as a property
126
+ print(f"Allocated resource: {allocated_obj.key}")
127
+
128
+ # Update the resource's lock timeout
129
+ allocated_obj.update(timeout=60)
130
+ finally:
131
+ # Free the resource when done
132
+ allocator.free(allocated_obj)
133
+
134
+ # Using soft binding (associates a name with a resource)
135
+ allocator.update_soft_bind("worker-1", "resource-1")
136
+ # Later...
137
+ allocator.unbind_soft_bind("worker-1")
138
+
139
+ # Garbage collection (reclaims unused resources)
140
+ allocator.gc(count=10) # Check 10 items for cleanup
141
+ ```
142
+
143
+ ### Using RedisTaskQueue for Distributed Task Processing
144
+
145
+ ```python
146
+ from redis import Redis
147
+ from redis_allocator import RedisTaskQueue, TaskExecutePolicy
148
+ import json
149
+
150
+ # Initialize Redis client
151
+ redis = Redis(host='localhost', port=6379)
152
+
153
+ # Process tasks in a worker
154
+ def process_task(task):
155
+ # Process the task (task is a RedisTask object)
156
+ # You can access task.id, task.name, task.params
157
+ # You can update progress with task.update(current, total)
158
+ return json.dumps({"result": "processed"})
159
+
160
+
161
+ # Create a task queue
162
+ task_queue = RedisTaskQueue(redis, "myapp", task_fn=process_task)
163
+
164
+ # Submit a task with query method
165
+ result = task_queue.query(
166
+ id="task-123",
167
+ name="example-task",
168
+ params={"input": "data"},
169
+ timeout=300, # Optional timeout in seconds
170
+ policy=TaskExecutePolicy.Auto, # Execution policy
171
+ once=False # Whether to delete the result after getting it
172
+ )
173
+
174
+ # Start listening for tasks
175
+ task_queue.listen(
176
+ names=["example-task"], # List of task names to listen for
177
+ workers=128, # Number of worker threads
178
+ event=None # Optional event to signal when to stop listening
179
+ )
180
+ ```
181
+
182
+ ## Modules
183
+
184
+ RedisAllocator consists of several modules, each providing specific functionality:
185
+
186
+ - **lock.py**: Provides `RedisLock` and `RedisLockPool` for distributed locking mechanisms
187
+ - **task_queue.py**: Implements `RedisTaskQueue` for distributed task processing
188
+ - **allocator.py**: Contains `RedisAllocator` and `RedisThreadHealthChecker` for resource allocation
189
+
190
+
191
+ ## Roadmap
192
+
193
+ ### Phase 1 (Completed)
194
+ - [x] Distributed lock mechanism implementation
195
+ - [x] Task queue processing system
196
+ - [x] Resource allocation and management
197
+ - [x] Basic health checking and monitoring
198
+ - [x] Object allocation with serialization
199
+ - [x] Unit tests for core components
200
+
201
+ ### Phase 2 (In Progress)
202
+ - [ ] Advanced sharding implementation
203
+ - [ ] Performance optimization and benchmarking
204
+ - [ ] Documentation improvement
205
+ - [ ] Enhanced error handling and recovery
206
+
207
+ ### Phase 3 (Planned)
208
+ - [ ] Advanced garbage collection strategies
209
+ - [ ] Redis cluster support
210
+ - [ ] Fault recovery mechanisms
211
+ - [ ] Automated resource scaling
212
+
213
+ ### Phase 4 (Future)
214
+ - [ ] API stability and backward compatibility
215
+ - [ ] Performance monitoring and tuning tools
216
+ - [ ] Advanced features (transaction support, data compression, etc.)
217
+ - [ ] Production environment validation and case studies
218
+
219
+ ## Contributing
220
+
221
+ Contributions and suggestions are welcome! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for more information.
222
+
223
+ ## License
224
+
225
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
226
+
227
+ ## Contact
228
+
229
+ For questions or suggestions, please contact us through GitHub Issues.
@@ -0,0 +1,191 @@
1
+ # RedisAllocator
2
+
3
+ ## Project Overview
4
+
5
+ RedisAllocator is an efficient Redis-based distributed memory allocation system. This system simulates traditional memory allocation mechanisms but implements them in a distributed environment, using Redis as the underlying storage and coordination tool.
6
+
7
+ > **Note**: Currently, RedisAllocator only supports single Redis instance deployments. For Redis cluster environments, we recommend using RedLock for distributed locking operations.
8
+
9
+ ### Core Features
10
+
11
+ - **Distributed Locking**: Provides robust distributed locking mechanisms to ensure data consistency in concurrent environments
12
+ - **Resource Allocation**: Implements a distributed resource allocation system with support for:
13
+ - Priority-based distribution
14
+ - Soft binding
15
+ - Garbage collection
16
+ - Health checking
17
+ - **Task Management**: Implements a distributed task queue system for efficient task processing across multiple workers
18
+ - **Object Allocation**: Supports allocation of resources with priority-based distribution and soft binding
19
+ - **Health Checking**: Monitors the health of distributed instances and automatically handles unhealthy resources
20
+ - **Garbage Collection**: Automatically identifies and reclaims unused resources, optimizing memory usage
21
+
22
+
23
+ ## Installation
24
+
25
+ ```bash
26
+ pip install redis-allocator
27
+ ```
28
+
29
+ ## Quick Start
30
+
31
+ ### Using RedisLock for Distributed Locking
32
+
33
+ ```python
34
+ from redis import Redis
35
+ from redis_allocator import RedisLock
36
+
37
+ # Initialize Redis client
38
+ redis = Redis(host='localhost', port=6379)
39
+
40
+ # Create a RedisLock instance
41
+ lock = RedisLock(redis, "myapp", "resource-lock")
42
+
43
+ # Acquire a lock
44
+ if lock.lock("resource-123", timeout=60):
45
+ try:
46
+ # Perform operations with the locked resource
47
+ print("Resource locked successfully")
48
+ finally:
49
+ # Release the lock when done
50
+ lock.unlock("resource-123")
51
+ ```
52
+
53
+ ### Using RedisAllocator for Resource Management
54
+
55
+ ```python
56
+ from redis import Redis
57
+ from redis_allocator import RedisAllocator
58
+
59
+ # Initialize Redis client
60
+ redis = Redis(host='localhost', port=6379)
61
+
62
+ # Create a RedisAllocator instance
63
+ allocator = RedisAllocator(
64
+ redis,
65
+ prefix='myapp',
66
+ suffix='allocator',
67
+ shared=False # Whether resources can be shared
68
+ )
69
+
70
+ # Add resources to the pool
71
+ allocator.extend(['resource-1', 'resource-2', 'resource-3'])
72
+
73
+ # Allocate a resource key (returns only the key)
74
+ key = allocator.malloc_key(timeout=120)
75
+ if key:
76
+ try:
77
+ # Use the allocated resource
78
+ print(f"Allocated resource: {key}")
79
+ finally:
80
+ # Free the resource when done
81
+ allocator.free_keys(key)
82
+
83
+ # Allocate a resource with object (returns a RedisAllocatorObject)
84
+ allocated_obj = allocator.malloc(timeout=120)
85
+ if allocated_obj:
86
+ try:
87
+ # The key is available as a property
88
+ print(f"Allocated resource: {allocated_obj.key}")
89
+
90
+ # Update the resource's lock timeout
91
+ allocated_obj.update(timeout=60)
92
+ finally:
93
+ # Free the resource when done
94
+ allocator.free(allocated_obj)
95
+
96
+ # Using soft binding (associates a name with a resource)
97
+ allocator.update_soft_bind("worker-1", "resource-1")
98
+ # Later...
99
+ allocator.unbind_soft_bind("worker-1")
100
+
101
+ # Garbage collection (reclaims unused resources)
102
+ allocator.gc(count=10) # Check 10 items for cleanup
103
+ ```
104
+
105
+ ### Using RedisTaskQueue for Distributed Task Processing
106
+
107
+ ```python
108
+ from redis import Redis
109
+ from redis_allocator import RedisTaskQueue, TaskExecutePolicy
110
+ import json
111
+
112
+ # Initialize Redis client
113
+ redis = Redis(host='localhost', port=6379)
114
+
115
+ # Process tasks in a worker
116
+ def process_task(task):
117
+ # Process the task (task is a RedisTask object)
118
+ # You can access task.id, task.name, task.params
119
+ # You can update progress with task.update(current, total)
120
+ return json.dumps({"result": "processed"})
121
+
122
+
123
+ # Create a task queue
124
+ task_queue = RedisTaskQueue(redis, "myapp", task_fn=process_task)
125
+
126
+ # Submit a task with query method
127
+ result = task_queue.query(
128
+ id="task-123",
129
+ name="example-task",
130
+ params={"input": "data"},
131
+ timeout=300, # Optional timeout in seconds
132
+ policy=TaskExecutePolicy.Auto, # Execution policy
133
+ once=False # Whether to delete the result after getting it
134
+ )
135
+
136
+ # Start listening for tasks
137
+ task_queue.listen(
138
+ names=["example-task"], # List of task names to listen for
139
+ workers=128, # Number of worker threads
140
+ event=None # Optional event to signal when to stop listening
141
+ )
142
+ ```
143
+
144
+ ## Modules
145
+
146
+ RedisAllocator consists of several modules, each providing specific functionality:
147
+
148
+ - **lock.py**: Provides `RedisLock` and `RedisLockPool` for distributed locking mechanisms
149
+ - **task_queue.py**: Implements `RedisTaskQueue` for distributed task processing
150
+ - **allocator.py**: Contains `RedisAllocator` and `RedisThreadHealthChecker` for resource allocation
151
+
152
+
153
+ ## Roadmap
154
+
155
+ ### Phase 1 (Completed)
156
+ - [x] Distributed lock mechanism implementation
157
+ - [x] Task queue processing system
158
+ - [x] Resource allocation and management
159
+ - [x] Basic health checking and monitoring
160
+ - [x] Object allocation with serialization
161
+ - [x] Unit tests for core components
162
+
163
+ ### Phase 2 (In Progress)
164
+ - [ ] Advanced sharding implementation
165
+ - [ ] Performance optimization and benchmarking
166
+ - [ ] Documentation improvement
167
+ - [ ] Enhanced error handling and recovery
168
+
169
+ ### Phase 3 (Planned)
170
+ - [ ] Advanced garbage collection strategies
171
+ - [ ] Redis cluster support
172
+ - [ ] Fault recovery mechanisms
173
+ - [ ] Automated resource scaling
174
+
175
+ ### Phase 4 (Future)
176
+ - [ ] API stability and backward compatibility
177
+ - [ ] Performance monitoring and tuning tools
178
+ - [ ] Advanced features (transaction support, data compression, etc.)
179
+ - [ ] Production environment validation and case studies
180
+
181
+ ## Contributing
182
+
183
+ Contributions and suggestions are welcome! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for more information.
184
+
185
+ ## License
186
+
187
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
188
+
189
+ ## Contact
190
+
191
+ For questions or suggestions, please contact us through GitHub Issues.
@@ -0,0 +1,11 @@
1
+ [build-system]
2
+ requires = ['setuptools>=42']
3
+ build-backend = 'setuptools.build_meta'
4
+
5
+ [tool.commitizen]
6
+ name = "cz_conventional_commits"
7
+ version = "0.1.0"
8
+ tag_format = "v$version"
9
+
10
+ [tool.commitizen.version_files]
11
+ "setup.py" = { search = "__version__ = '", replace = "__version__ = '" }
@@ -0,0 +1,28 @@
1
+ """RedisAllocator package for distributed memory allocation using Redis.
2
+
3
+ This package provides efficient, Redis-based distributed memory allocation
4
+ services that simulate traditional memory allocation mechanisms in a
5
+ distributed environment.
6
+ """
7
+
8
+ from redis_allocator.lock import (RedisLock, RedisLockPool, LockStatus,
9
+ BaseLock, BaseLockPool, ThreadLock, ThreadLockPool)
10
+ from redis_allocator.task_queue import TaskExecutePolicy, RedisTask, RedisTaskQueue
11
+ from redis_allocator.allocator import RedisAllocator
12
+
13
+
14
+ __version__ = '0.0.1'
15
+
16
+ __all__ = [
17
+ 'RedisLock',
18
+ 'RedisLockPool',
19
+ 'LockStatus',
20
+ 'BaseLock',
21
+ 'BaseLockPool',
22
+ 'ThreadLock',
23
+ 'ThreadLockPool',
24
+ 'TaskExecutePolicy',
25
+ 'RedisTask',
26
+ 'RedisTaskQueue',
27
+ 'RedisAllocator',
28
+ ]