-
Notifications
You must be signed in to change notification settings - Fork 132
/
CephFileStore.java
292 lines (246 loc) · 9.33 KB
/
CephFileStore.java
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
/*
* dCache - http://www.dcache.org/
*
* Copyright (C) 2016 - 2017 Deutsches Elektronen-Synchrotron
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.dcache.pool.repository.ceph;
import com.google.common.primitives.Longs;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import diskCacheV111.util.PnfsId;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.NoSuchFileException;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.util.Set;
import java.util.stream.Collectors;
import org.dcache.pool.movers.IoMode;
import org.dcache.pool.repository.FileStore;
import org.dcache.pool.repository.RepositoryChannel;
import org.dcache.rados4j.IoCtx;
import org.dcache.rados4j.Rados;
import org.dcache.rados4j.RadosClusterInfo;
import org.dcache.rados4j.RadosException;
import org.dcache.rados4j.Rbd;
import org.dcache.rados4j.RbdImage;
import org.dcache.rados4j.RbdImageInfo;
/**
* A CEPH based implementation of {@link FileStore}.
*/
public class CephFileStore implements FileStore {
protected static final Logger LOGGER = LoggerFactory.getLogger(CephFileStore.class);
/**
* RADOS objects extended attribute name to store creation time.
*/
private final static String CREATION_TIME_ATTR = "creation_time";
/**
* RADOS objects extended attribute name to store last access time.
*/
private final static String LAST_ACCESS_TIME_ATTR = "last_access_time";
/**
* RADOS objects extended attribute name to store last modification time.
*/
private final static String LAST_MODIFICATION_TIME_ATTR = "last_modification_time";
private final Rados rados;
private final IoCtx ctx;
private final Rbd rbd;
private final String poolName;
public CephFileStore(String poolName, String cluster, String config) throws RadosException {
rados = new Rados(cluster, config);
rados.connect();
ctx = rados.createIoContext(poolName);
rbd = ctx.createRbd();
this.poolName = poolName;
}
@Override
public URI get(PnfsId id) {
return toUri(toImageName(id));
}
@Override
public boolean contains(PnfsId id) {
try {
RbdImage image = rbd.openReadOnly(toImageName(id));
image.close();
return true;
} catch (RadosException e) {
return false;
}
}
@Override
public BasicFileAttributeView getFileAttributeView(PnfsId id) throws IOException {
String imageName = toImageName(id);
try(RbdImage image = rbd.openReadOnly(imageName)) {
final RbdImageInfo imageInfo = image.stat();
return new BasicFileAttributeView() {
@Override
public String name() {
return "basic";
}
@Override
public BasicFileAttributes readAttributes() throws IOException {
return new BasicFileAttributes() {
private FileTime getTimeFromXattr(String image, String attr) {
long time;
try {
byte[] b = new byte[Long.BYTES];
ctx.getXattr(toObjName(image), attr, b);
time = Longs.fromByteArray(b);
} catch (RadosException e) {
time = 0;
}
return FileTime.fromMillis(time);
}
@Override
public FileTime lastModifiedTime() {
return getTimeFromXattr(imageName, LAST_MODIFICATION_TIME_ATTR);
}
@Override
public FileTime lastAccessTime() {
return getTimeFromXattr(imageName, LAST_ACCESS_TIME_ATTR);
}
@Override
public FileTime creationTime() {
return getTimeFromXattr(imageName, CREATION_TIME_ATTR);
}
@Override
public boolean isRegularFile() {
return true;
}
@Override
public boolean isDirectory() {
return false;
}
@Override
public boolean isSymbolicLink() {
return false;
}
@Override
public boolean isOther() {
return false;
}
@Override
public long size() {
return imageInfo.obj_size.longValue();
}
@Override
public Object fileKey() {
return null;
}
};
}
private void setTimeToXattr(String image, String attr, FileTime time) throws RadosException {
ctx.setXattr(toObjName(image),
attr,
Longs.toByteArray(time.toMillis()));
}
@Override
public void setTimes(FileTime lastModifiedTime, FileTime lastAccessTime, FileTime createTime) throws IOException {
if (lastModifiedTime != null) {
setTimeToXattr(imageName, LAST_MODIFICATION_TIME_ATTR, lastModifiedTime);
}
if (lastAccessTime != null) {
setTimeToXattr(imageName, LAST_ACCESS_TIME_ATTR, lastAccessTime);
}
if (createTime != null) {
setTimeToXattr(imageName, CREATION_TIME_ATTR, createTime);
}
}
};
} catch (RadosException e) {
// try to map CEPH errors to dCache/java.io alternatives
// the errcodes are negative numbers :)
if ( Math.abs(e.getErrorCode()) == jnr.constants.platform.Errno.ENOENT.intValue()) {
throw new NoSuchFileException(imageName);
}
throw e;
}
}
@Override
public URI create(PnfsId id) throws IOException {
String imageName = toImageName(id);
rbd.create(imageName, 0);
ctx.setXattr(toObjName(imageName),
CREATION_TIME_ATTR,
Longs.toByteArray(System.currentTimeMillis()));
return toUri(imageName);
}
@Override
public void remove(PnfsId id) throws IOException {
rbd.remove(toImageName(id));
}
@Override
public RepositoryChannel openDataChannel(PnfsId id, IoMode ioMode) throws IOException {
return new CephRepositoryChannel(rbd, toImageName(id), ioMode.toOpenString());
}
@Override
public Set<PnfsId> index() throws IOException {
return rbd.list()
.stream()
.map(this::toPnfsId)
.collect(Collectors.toSet());
}
@Override
public long getFreeSpace() throws IOException {
RadosClusterInfo clusterInfo = rados.statCluster();
return clusterInfo.kb_avail.get() * 1024;
}
@Override
public long getTotalSpace() throws IOException {
RadosClusterInfo clusterInfo = rados.statCluster();
return clusterInfo.kb.get()*1024;
}
@Override
public boolean isOk() {
try {
rados.statPool(ctx);
} catch (RadosException e) {
LOGGER.error("Repository health check failed: {}", e.toString());
return false;
}
return true;
}
private String toImageName(PnfsId id) {
return id.toString();
}
private PnfsId toPnfsId(String s) {
return new PnfsId(s);
}
/**
* Returns object name corresponding to specified RBD image.
* @param image name.
*/
private String toObjName(String img) {
return img + ".rbd";
}
private URI toUri(String imageName) {
try {
return new URI("rbd", poolName, "/" + imageName, null, null);
} catch (URISyntaxException e) {
// we should never get here
throw new RuntimeException("Failed to build URI", e);
}
}
public void shutdown() throws RadosException {
try {
ctx.destroy();
} finally {
rados.shutdown();
}
}
}