-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHddsVolume.java
393 lines (344 loc) · 12.4 KB
/
HddsVolume.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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.ozone.container.common.volume;
import static org.apache.hadoop.ozone.container.common.HDDSVolumeLayoutVersion.getLatestVersion;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicLong;
import org.apache.hadoop.fs.StorageType;
import org.apache.hadoop.hdds.annotation.InterfaceAudience;
import org.apache.hadoop.hdds.annotation.InterfaceStability;
import org.apache.hadoop.ozone.common.InconsistentStorageStateException;
import org.apache.hadoop.ozone.container.common.helpers.DatanodeVersionFile;
import org.apache.hadoop.ozone.container.common.utils.HddsVolumeUtil;
import org.apache.hadoop.util.Time;
import com.google.common.base.Preconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* HddsVolume represents volume in a datanode. {@link MutableVolumeSet}
* maintains a list of HddsVolumes, one for each volume in the Datanode.
* {@link VolumeInfo} in encompassed by this class.
* <p>
* The disk layout per volume is as follows:
* <p>../hdds/VERSION
* <p>{@literal ../hdds/<<clusterUuid>>/current/<<containerDir>>/<<containerID
* >>/metadata}
* <p>{@literal ../hdds/<<clusterUuid>>/current/<<containerDir>>/<<containerID
* >>/<<dataDir>>}
* <p>
* Each hdds volume has its own VERSION file. The hdds volume will have one
* clusterUuid directory for each SCM it is a part of (currently only one SCM is
* supported).
*
* During DN startup, if the VERSION file exists, we verify that the
* clusterID in the version file matches the clusterID from SCM.
*/
@InterfaceAudience.Private
@InterfaceStability.Unstable
@SuppressWarnings("finalclass")
public class HddsVolume extends StorageVolume {
private static final Logger LOG = LoggerFactory.getLogger(HddsVolume.class);
public static final String HDDS_VOLUME_DIR = "hdds";
private VolumeState state;
private final VolumeIOStats volumeIOStats;
private final VolumeInfoStats volumeInfoStats;
// VERSION file properties
private String storageID; // id of the file system
private String clusterID; // id of the cluster
private String datanodeUuid; // id of the DataNode
private long cTime; // creation time of the file system state
private int layoutVersion; // layout version of the storage data
private final AtomicLong committedBytes; // till Open containers become full
private final VolumeType type = VolumeType.DATA_VOLUME; // Mentions the type of volume
/**
* Builder for HddsVolume.
*/
public static class Builder extends StorageVolume.Builder<Builder> {
private String datanodeUuid;
private String clusterID;
public Builder(String volumeRootStr) {
super(volumeRootStr, HDDS_VOLUME_DIR);
}
@Override
public Builder getThis() {
return this;
}
public Builder datanodeUuid(String datanodeUUID) {
this.datanodeUuid = datanodeUUID;
return this;
}
public Builder clusterID(String cid) {
this.clusterID = cid;
return this;
}
public HddsVolume build() throws IOException {
return new HddsVolume(this);
}
}
private HddsVolume(Builder b) throws IOException {
super(b);
if (!b.getFailedVolume()) {
this.state = VolumeState.NOT_INITIALIZED;
this.clusterID = b.clusterID;
this.datanodeUuid = b.datanodeUuid;
this.volumeIOStats = new VolumeIOStats(b.getVolumeRootStr());
this.volumeInfoStats = new VolumeInfoStats(b.getVolumeRootStr(),this);
this.committedBytes = new AtomicLong(0);
LOG.info("Creating HddsVolume: {} of storage type : {} capacity : {}",
getStorageDir(), b.getStorageType(), getVolumeInfo().getCapacity());
initialize();
} else {
// Builder is called with failedVolume set, so create a failed volume
// HddsVolume Object.
volumeIOStats = null;
volumeInfoStats = null;
storageID = UUID.randomUUID().toString();
state = VolumeState.FAILED;
committedBytes = null;
}
}
/**
* Initializes the volume.
* Creates the Version file if not present,
* otherwise returns with IOException.
* @throws IOException
*/
private void initialize() throws IOException {
VolumeState intialVolumeState = analyzeVolumeState();
switch (intialVolumeState) {
case NON_EXISTENT:
// Root directory does not exist. Create it.
if (!getStorageDir().mkdirs()) {
throw new IOException("Cannot create directory " + getStorageDir());
}
setState(VolumeState.NOT_FORMATTED);
createVersionFile();
break;
case NOT_FORMATTED:
// Version File does not exist. Create it.
createVersionFile();
break;
case NOT_INITIALIZED:
// Version File exists. Verify its correctness and update property fields.
readVersionFile();
setState(VolumeState.NORMAL);
break;
case INCONSISTENT:
// Volume Root is in an inconsistent state. Skip loading this volume.
throw new IOException("Volume is in an " + VolumeState.INCONSISTENT +
" state. Skipped loading volume: " + getStorageDir().getPath());
default:
throw new IOException("Unrecognized initial state : " +
intialVolumeState + "of volume : " + getStorageDir());
}
}
private VolumeState analyzeVolumeState() {
if (!getStorageDir().exists()) {
// Volume Root does not exist.
return VolumeState.NON_EXISTENT;
}
if (!getStorageDir().isDirectory()) {
// Volume Root exists but is not a directory.
LOG.warn("Volume {} exists but is not a directory,"
+ " current volume state: {}.",
getStorageDir().getPath(), VolumeState.INCONSISTENT);
return VolumeState.INCONSISTENT;
}
File[] files = getStorageDir().listFiles();
if (files == null || files.length == 0) {
// Volume Root exists and is empty.
return VolumeState.NOT_FORMATTED;
}
if (!getVersionFile().exists()) {
// Volume Root is non empty but VERSION file does not exist.
LOG.warn("VERSION file does not exist in volume {},"
+ " current volume state: {}.",
getStorageDir().getPath(), VolumeState.INCONSISTENT);
return VolumeState.INCONSISTENT;
}
// Volume Root and VERSION file exist.
return VolumeState.NOT_INITIALIZED;
}
public void format(String cid) throws IOException {
Preconditions.checkNotNull(cid, "clusterID cannot be null while " +
"formatting Volume");
this.clusterID = cid;
initialize();
}
/**
* Create Version File and write property fields into it.
* @throws IOException
*/
private void createVersionFile() throws IOException {
this.storageID = HddsVolumeUtil.generateUuid();
this.cTime = Time.now();
this.layoutVersion = getLatestVersion().getVersion();
if (this.clusterID == null || datanodeUuid == null) {
// HddsDatanodeService does not have the cluster information yet. Wait
// for registration with SCM.
LOG.debug("ClusterID not available. Cannot format the volume {}",
getStorageDir().getPath());
setState(VolumeState.NOT_FORMATTED);
} else {
// Write the version file to disk.
writeVersionFile();
setState(VolumeState.NORMAL);
}
}
private void writeVersionFile() throws IOException {
Preconditions.checkNotNull(this.storageID,
"StorageID cannot be null in Version File");
Preconditions.checkNotNull(this.clusterID,
"ClusterID cannot be null in Version File");
Preconditions.checkNotNull(this.datanodeUuid,
"DatanodeUUID cannot be null in Version File");
Preconditions.checkArgument(this.cTime > 0,
"Creation Time should be positive");
Preconditions.checkArgument(this.layoutVersion ==
getLatestVersion().getVersion(),
"Version File should have the latest LayOutVersion");
File versionFile = getVersionFile();
LOG.debug("Writing Version file to disk, {}", versionFile);
DatanodeVersionFile dnVersionFile = new DatanodeVersionFile(this.storageID,
this.clusterID, this.datanodeUuid, this.cTime, this.layoutVersion);
dnVersionFile.createVersionFile(versionFile);
}
/**
* Read Version File and update property fields.
* Get common storage fields.
* Should be overloaded if additional fields need to be read.
*
* @throws IOException on error
*/
private void readVersionFile() throws IOException {
File versionFile = getVersionFile();
Properties props = DatanodeVersionFile.readFrom(versionFile);
if (props.isEmpty()) {
throw new InconsistentStorageStateException(
"Version file " + versionFile + " is missing");
}
LOG.debug("Reading Version file from disk, {}", versionFile);
this.storageID = HddsVolumeUtil.getStorageID(props, versionFile);
this.clusterID = HddsVolumeUtil.getClusterID(props, versionFile,
this.clusterID);
this.datanodeUuid = HddsVolumeUtil.getDatanodeUUID(props, versionFile,
this.datanodeUuid);
this.cTime = HddsVolumeUtil.getCreationTime(props, versionFile);
this.layoutVersion = HddsVolumeUtil.getLayOutVersion(props, versionFile);
}
private File getVersionFile() {
return HddsVolumeUtil.getVersionFile(super.getStorageDir());
}
public File getHddsRootDir() {
return super.getStorageDir();
}
@Override
public String getStorageID() {
return storageID;
}
public String getClusterID() {
return clusterID;
}
public String getDatanodeUuid() {
return datanodeUuid;
}
public long getCTime() {
return cTime;
}
public int getLayoutVersion() {
return layoutVersion;
}
public VolumeType getType() {
return type;
}
public VolumeState getStorageState() {
return state;
}
public void setState(VolumeState state) {
this.state = state;
}
public boolean isFailed() {
return (state == VolumeState.FAILED);
}
public VolumeIOStats getVolumeIOStats() {
return volumeIOStats;
}
public VolumeInfoStats getVolumeInfoStats() {
return volumeInfoStats;
}
@Override
public void failVolume() {
setState(VolumeState.FAILED);
super.failVolume();
if (volumeIOStats != null) {
volumeIOStats.unregister();
}
if (volumeInfoStats != null) {
volumeInfoStats.unregister();
}
}
@Override
public void shutdown() {
this.state = VolumeState.NON_EXISTENT;
super.shutdown();
if (volumeIOStats != null) {
volumeIOStats.unregister();
}
if (volumeInfoStats != null) {
volumeInfoStats.unregister();
}
}
/**
* VolumeState represents the different states a HddsVolume can be in.
* NORMAL => Volume can be used for storage
* FAILED => Volume has failed due and can no longer be used for
* storing containers.
* NON_EXISTENT => Volume Root dir does not exist
* INCONSISTENT => Volume Root dir is not empty but VERSION file is
* missing or Volume Root dir is not a directory
* NOT_FORMATTED => Volume Root exists but not formatted(no VERSION file)
* NOT_INITIALIZED => VERSION file exists but has not been verified for
* correctness.
*/
public enum VolumeState {
NORMAL,
FAILED,
NON_EXISTENT,
INCONSISTENT,
NOT_FORMATTED,
NOT_INITIALIZED
}
/**
* add "delta" bytes to committed space in the volume.
* @param delta bytes to add to committed space counter
* @return bytes of committed space
*/
public long incCommittedBytes(long delta) {
return committedBytes.addAndGet(delta);
}
/**
* return the committed space in the volume.
* @return bytes of committed space
*/
public long getCommittedBytes() {
return committedBytes.get();
}
}