-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pool.php
200 lines (186 loc) · 5.88 KB
/
Pool.php
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
<?php
namespace Aternos\Rados\Cluster\Pool;
use Aternos\Rados\Cluster\Cluster;
use Aternos\Rados\Exception\PoolException;
use Aternos\Rados\Exception\RadosException;
use Aternos\Rados\Generated\Errno;
use Aternos\Rados\Util\Buffer\Buffer;
use FFI;
use InvalidArgumentException;
class Pool
{
/**
* Binding for rados_pool_lookup
* Lookup a pool id by name
*
* @param Cluster $cluster
* @param string $name
* @return int
* @throws RadosException
* @noinspection PhpUndefinedMethodInspection
*/
public static function lookupName(Cluster $cluster, string $name): int
{
return PoolException::handle($cluster->getFFI()->rados_pool_lookup($cluster->getCData(), $name));
}
/**
* Binding for rados_pool_reverse_lookup
* Lookup a pool name by id
*
* @param Cluster $cluster
* @param int $id
* @return string
* @throws RadosException
* @noinspection PhpUndefinedMethodInspection
*/
public static function lookupId(Cluster $cluster, int $id): string
{
$length = 256;
$ffi = $cluster->getFFI();
do {
$buffer = Buffer::create($ffi, $length);
$res = $ffi->rados_pool_reverse_lookup($cluster->getCData(), $id, $buffer->getCData(), $length);
$length = Buffer::grow($length);
} while (-$res === Errno::ERANGE->value);
$resultLength = PoolException::handle($res);
return $buffer->readString($resultLength);
}
/**
* @param Cluster $cluster
* @param string|null $name
* @param int|null $id
* @internal Use Cluster::createPool, Cluster::getPool, or Cluster::getPoolById instead
*/
public function __construct(protected Cluster $cluster, protected ?string $name, protected ?int $id)
{
if ($this->name === null && $this->id === null) {
throw new InvalidArgumentException("Either name or id must be set");
}
}
/**
* @return Cluster
*/
public function getCluster(): Cluster
{
return $this->cluster;
}
/**
* Binding for rados_pool_lookup
* Get the id of a pool
*
* @return int
* @throws RadosException
* @noinspection PhpUndefinedMethodInspection
*/
public function getId(): int
{
if ($this->id === null) {
$this->id = static::lookupName($this->getCluster(), $this->name);
}
return $this->id;
}
/**
* Binding for rados_pool_reverse_lookup
* Get the name of a pool
*
* @return string
* @throws RadosException
* @noinspection PhpUndefinedMethodInspection
*/
public function getName(): string
{
if ($this->name === null) {
$this->name = static::lookupId($this->getCluster(), $this->id);
}
return $this->name;
}
/**
* Binding for rados_pool_get_base_tier
* Returns the pool that is the base tier for this pool.
*
* The return value is the ID of the pool that should be used to read from/write to.
* If tiering is not set up for the pool, returns \c pool.
*
* @return int
* @throws RadosException
* @noinspection PhpUndefinedMethodInspection
*/
public function getBaseTier(): int
{
$result = $this->getCluster()->getFFI()->new("int64_t");
PoolException::handle($this->getCluster()->getFFI()->rados_pool_get_base_tier(
$this->getCluster()->getCData(),
$this->getId(), FFI::addr($result)
));
return $result->cdata;
}
/**
* Binding for rados_pool_delete
* Delete a pool and all data inside it
*
* The pool is removed from the cluster immediately,
* but the actual data is deleted in the background.
*
* @return $this
* @throws RadosException
* @noinspection PhpUndefinedMethodInspection
*/
public function delete(): static
{
PoolException::handle($this->getCluster()->getFFI()->rados_pool_delete(
$this->getCluster()->getCData(), $this->getName()
));
return $this;
}
/**
* Binding for rados_ioctx_create/rados_ioctx_create2
* Create an io context
*
* The io context allows you to perform operations within a particular pool.
*
* @return IOContext
* @throws RadosException
* @noinspection PhpUndefinedMethodInspection
*/
public function createIOContext(): IOContext
{
$context = $this->getCluster()->getFFI()->new("rados_ioctx_t");
if ($this->id !== null) {
PoolException::handle($this->getCluster()->getFFI()->rados_ioctx_create2(
$this->getCluster()->getCData(),
$this->id, FFI::addr($context)
));
} else {
PoolException::handle($this->getCluster()->getFFI()->rados_ioctx_create(
$this->getCluster()->getCData(),
$this->getName(), FFI::addr($context)
));
}
return new IOContext($this->getCluster(), $context, $this->getCluster()->getFFI());
}
/**
* Binding for rados_inconsistent_pg_list
* List inconsistent placement groups of the given pool
*
* @return string[]
* @throws RadosException
* @noinspection PhpUndefinedMethodInspection
*/
public function listInconsistentPGs(): array
{
$length = PoolException::handle($this->getCluster()->getFFI()->rados_inconsistent_pg_list(
$this->getCluster()->getCData(),
$this->getId(),
null,
0
));
$buffer = Buffer::create($this->getCluster()->getFFI(), $length);
PoolException::handle($this->getCluster()->getFFI()->rados_inconsistent_pg_list(
$this->getCluster()->getCData(),
$this->getId(),
$buffer->getCData(),
$length
));
return $buffer->readNullTerminatedStringList($length);
}
}