File: counter.md

package info (click to toggle)
yrmcds 1.1.9-1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye
  • size: 940 kB
  • sloc: cpp: 11,149; sh: 148; makefile: 117
file content (334 lines) | stat: -rw-r--r-- 11,956 bytes parent folder | download | duplicates (3)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#<cldoc:Counter support>

Counter support

Overview
--------

Counter extension provides distributed counters for resource management.  Values of counters represent how much a resource is consumed.  Multiple clients can acquire/release the resources simultaneously.

Each counter has a non-empty name.  The maximum name is up to 65535 bytes.  The number of resources available for a counter is represented as a 4 byte unsigned integer; this means a counter can manage up to 4294967295 resources.

- The counter extension is disabled by default.
- TCP port used for the counter protocol need to be different from that of the memcache protocol.
- The namespace of counter objects is different from that of the memcache objects.
- When a connection is closed, all resources acquired through the connection are released automatically.
- A counter is created dynamically at the first time the counter is acquired.
- Counters are not replicated.


Protocol
--------

The counter extension has its own binary protocol.

### General format of a packet

```
 Byte/     0       |       1       |       2       |       3       |
    /              |               |               |               |
   |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
   +---------------+---------------+---------------+---------------+
  0/ HEADER                                                        /
   /                                                               /
   /                                                               /
   /                                                               /
   +---------------+---------------+---------------+---------------+
 12/ COMMAND-SPECIFIC BODY (as needed)                             /
   +---------------+---------------+---------------+---------------+
```


### Request header

```
 Byte/     0       |       1       |       2       |       3       |
    /              |               |               |               |
   |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
   +---------------+---------------+---------------+---------------+
  0| Magic         | Opcode        | Flags         | Reserved      |
   +---------------+---------------+---------------+---------------+
  4| Body length                                                   |
   +---------------+---------------+---------------+---------------+
  8| Opaque                                                        |
   +---------------+---------------+---------------+---------------+
   Total 12 bytes
```

- `Magic` is `0x90`.
- `Opcode` is defined later.
- `Flags` is defined later.
- `Reserved` is always zero.
- `Body length` is a 4 byte unsigned integer in big-endian.
- `Opaque` is an arbitrary 4 byte data.


### Response header

```
 Byte/     0       |       1       |       2       |       3       |
    /              |               |               |               |
   |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
   +---------------+---------------+---------------+---------------+
  0| Magic         | Opcode        | Status        | Reserved      |
   +---------------+---------------+---------------+---------------+
  4| Body length                                                   |
   +---------------+---------------+---------------+---------------+
  8| Opaque                                                        |
   +---------------+---------------+---------------+---------------+
   Total 12 bytes
```

- `Magic` is `0x91`.
- `Opcode` and `Opaque` are copied from the corresponding request.
- `Status` is defined later.
- `Reserved` is always zero.
- `Body length` is a 4 byte unsigned integer in big-endian.


### Opcodes

| Code   | Operation
|--------|-----------
| `0x00` | `Noop`
| `0x01` | `Get`
| `0x02` | `Acquire`
| `0x03` | `Release`
| `0x10` | `Stats`
| `0x11` | `Dump`


### Flags

No flags are yet defined.


### Response status

| Code   | Status
|--------|-------------------
| `0x00` | No error
| `0x01` | Not found
| `0x04` | Invalid arguments
| `0x21` | Resource not available
| `0x22` | Not acquired
| `0x81` | Unknown command
| `0x82` | Out of memory


Packet specifications
---------------------

Unless otherwise noted, all integers are represented in big-endian.


### Error responses

Errors are signaled by the `Status` field of the response header.
Error messages are accompanied in the message body.


### Noop

`Noop` does nothing and returns a success response.

The request and response of `Noop` has no body.


### Acquire

`Acquire` acquires some resources associated to a counter.  In addition to the counter name, the command has two parameters; one is the number of resources to be acquired, and another is the number of total resources.

Note that, unlike semaphores, counters do not keep the resource capacity; the capacity will be specified by the acquire command.  This way, users can change the resource capacity dynamically anytime.

Request body:

```
 Byte/     0       |       1       |       2       |       3       |
    /              |               |               |               |
   |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
   +---------------+---------------+---------------+---------------+
  0| Resources                                                     |
   +---------------+---------------+---------------+---------------+
  4| Maximum                                                       |
   +---------------+---------------+---------------+---------------+
  8| Name length                   | (Name data) ...
   +---------------+---------------+
   Total 10 + (Name length) bytes
```

Successful response body:

```
 Byte/     0       |       1       |       2       |       3       |
    /              |               |               |               |
   |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
   +---------------+---------------+---------------+---------------+
  0| Resources                                                     |
   +---------------+---------------+---------------+---------------+
   Total 4 bytes
```

- `Resources` is a 4-byte unsigned integer number.
  This request will acquire this count of resources.
  If this is zero, `Invalid arguments` is returned.
- `Maximum` is a 4-byte unsigned integer number.
  This parameter represents the total number of resources.
  `Maximum` must be equal to or greater than `Resources`, otherwise
  `Invalid arguments` is returned.
- `Name length` is a 2-byte unsigned integer number.
  If this is zero, `Invalid arguments` is returned.

If the named counter does not exist, `Acquire` creates a new counter, sets its consumption to `Resources`, and returns the success.

Otherwise, if `Consumption + Resources <= Maximum` where `Consumption` is the current consumption of the resource, then this request will augment `Consumption` by `Resources`, and return the success.  Else, this returns `Resource not available` error.


### Release

`Release` returns the acquired resources back.

Request body:

```
 Byte/     0       |       1       |       2       |       3       |
    /              |               |               |               |
   |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
   +---------------+---------------+---------------+---------------+
  0| Resources                                                     |
   +---------------+---------------+---------------+---------------+
  4| Name length                   |  (Name data) ...
   +---------------+---------------+
   Total 6 + (Name length) bytes
```

A successful response has no body.

- `Resources` is the count of resources to return.
  It can be zero.
- `Name length` is a 2-byte unsigned integer number.
  If this is zero, `Invalid arguments` is returned.

If the named counter does not exist, `Release` returns `Not found` error.
If `Resources` is greater than the current number of acquired resources, `Release` returns `Not acquired` error.

Otherwise, the operation succeeds and the value of the counter is increased by `Resources`.


### Get

`Get` obtains the current consumption of a counter.

Request body:

```
 Byte/     0       |       1       |       2       |       3       |
    /              |               |               |               |
   |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
   +---------------+---------------+---------------+---------------+
  0| Name length                   | (Name data) ...
   +---------------+---------------+
   Total 2 + (Name length) bytes
```

Success response body:

```
 Byte/     0       |       1       |       2       |       3       |
    /              |               |               |               |
   |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
   +---------------+---------------+---------------+---------------+
  0| Consumption                                                   |
   +---------------+---------------+---------------+---------------+
   Total 4 bytes
```

- `Name length` is a 2-byte unsigned integer number.
  If this is zero, `Invalid arguments` is returned.

If the named counter does not exist, `Get` returns `Not found` error.


### Stats

`Stats` obtains statistics information about counters.

Request body: No body.

Successful response body:

The body consists of a series of name-value pairs.

```
 Byte/     0       |       1       |       2       |       3       |
    /              |               |               |               |
   |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
   +---------------+---------------+---------------+---------------+
  0| Name length 1                 | Value length 1                |
   +---------------+---------------+---------------+---------------+
  4| Name1 ... Value1 ...
   +---------------+---------------+---------------+---------------+
  N| Name length 2                 | Value length 2                |
   +---------------+---------------+---------------+---------------+
N+4| Name2 ... Value2 ...
   ...
   Total (Body length) bytes.
```

- `Name` is a name of a statistics item.
- `Value` is an ASCII text information.


### Dump

`Dump` dumps all counters.

Request body: No body.

Successful responses:

`Dump` command returns multiple responses for one request.

Each response contains the name of a counter and the current consumption of the resource.
The response also contains the maximum consumption of resources in the interval given in the configuration file.

The end of the series of responses are indicated by the response whose body length is zero.

```
 Byte/     0       |       1       |       2       |       3       |
    /              |               |               |               |
   |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
   +---------------+---------------+---------------+---------------+
  0| Current consumption                                           |
   +---------------+---------------+---------------+---------------+
  4| Maximum consumption                                           |
   +---------------+---------------+---------------+---------------+
  8| Name length                   | (Name data) ...
   +---------------+---------------+
   Total 10 + (Name length) bytes
```


Configurations
--------------

Following parameters can be specified in the configuration file:

```ini
# If true, the counter extension is enabled. (default: false)
counter.enable = false

# TCP port used for the counter protocol. (default: 11215)
counter.port = 11215

# The maximum number of connections in the counter protocol.
# 0 means unlimited. (default: 0)
counter.max_connections = 0

# The size of the counter hash table. (default: 1000000)
counter.buckets = 1000000

# The interval of measuring the maximum number of resource consumption
# in seconds. (default: 86400)
counter.stats_interval = 86400
```