-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
MasterWorkerInfo.java
349 lines (314 loc) · 11 KB
/
MasterWorkerInfo.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
/*
* Licensed to the University of California, Berkeley 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 tachyon.master.block.meta;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.Preconditions;
import com.google.common.collect.Sets;
import tachyon.Constants;
import tachyon.StorageTierAssoc;
import tachyon.WorkerStorageTierAssoc;
import tachyon.thrift.WorkerInfo;
import tachyon.thrift.WorkerNetAddress;
import tachyon.util.CommonUtils;
/**
* Metadata for a Tachyon worker.
*/
public final class MasterWorkerInfo {
private static final Logger LOG = LoggerFactory.getLogger(Constants.LOGGER_TYPE);
/** Worker's address */
private final WorkerNetAddress mWorkerAddress;
/** The id of the worker */
private final long mId;
/** Start time of the worker in ms */
private final long mStartTimeMs;
/** Capacity of worker in bytes */
private long mCapacityBytes;
/** Worker's used bytes */
private long mUsedBytes;
/** Worker's last updated time in ms */
private long mLastUpdatedTimeMs;
/** If true, the worker is considered registered. */
private boolean mIsRegistered;
/** Worker-specific mapping between storage tier alias and storage tier ordinal. */
private StorageTierAssoc mStorageTierAssoc;
/** Mapping from storage tier alias to total bytes */
private Map<String, Long> mTotalBytesOnTiers;
/** Mapping from storage tier alias to used bytes */
private Map<String, Long> mUsedBytesOnTiers;
/** ids of blocks the worker contains */
private Set<Long> mBlocks;
/** ids of blocks the worker should remove */
private Set<Long> mToRemoveBlocks;
/**
* Creates a new instance of {@link MasterWorkerInfo}.
*
* @param id the worker id to use
* @param address the worker address to use
*/
public MasterWorkerInfo(long id, WorkerNetAddress address) {
mWorkerAddress = Preconditions.checkNotNull(address);
mId = id;
mStartTimeMs = System.currentTimeMillis();
mLastUpdatedTimeMs = System.currentTimeMillis();
mIsRegistered = false;
mStorageTierAssoc = null;
mTotalBytesOnTiers = new HashMap<String, Long>();
mUsedBytesOnTiers = new HashMap<String, Long>();
mBlocks = new HashSet<Long>();
mToRemoveBlocks = new HashSet<Long>();
}
/**
* Marks the worker as registered, while updating all of its metadata.
*
* @param globalStorageTierAssoc global mapping between storage aliases and ordinal position
* @param storageTierAliases list of storage tier alises in order of their position in the
* hierarchy
* @param totalBytesOnTiers mapping from storage tier alias to total bytes
* @param usedBytesOnTiers mapping from storage tier alias to used byes
* @param blocks set of block ids on this worker
* @return A Set of blocks removed (or lost) from this worker
*/
public synchronized Set<Long> register(final StorageTierAssoc globalStorageTierAssoc,
final List<String> storageTierAliases, final Map<String, Long> totalBytesOnTiers,
final Map<String, Long> usedBytesOnTiers, final Set<Long> blocks) {
// If the storage aliases do not have strictly increasing ordinal value based on the total
// ordering, throw an error
for (int i = 0; i < storageTierAliases.size() - 1; i ++) {
if (globalStorageTierAssoc.getOrdinal(storageTierAliases.get(i)) >= globalStorageTierAssoc
.getOrdinal(storageTierAliases.get(i + 1))) {
throw new IllegalArgumentException(
"Worker cannot place storage tier " + storageTierAliases.get(i) + " above "
+ storageTierAliases.get(i + 1) + " in the hierarchy");
}
}
mStorageTierAssoc = new WorkerStorageTierAssoc(storageTierAliases);
// validate the number of tiers
if (mStorageTierAssoc.size() != totalBytesOnTiers.size()
|| mStorageTierAssoc.size() != usedBytesOnTiers.size()) {
throw new IllegalArgumentException(
"totalBytesOnTiers and usedBytesOnTiers should have the same number of tiers as "
+ "storageTierAliases, but storageTierAliases has " + mStorageTierAssoc.size()
+ " tiers, while totalBytesOnTiers has " + totalBytesOnTiers.size()
+ " tiers and usedBytesOnTiers has " + usedBytesOnTiers.size() + " tiers");
}
// defensive copy
mTotalBytesOnTiers = new HashMap<String, Long>(totalBytesOnTiers);
mUsedBytesOnTiers = new HashMap<String, Long>(usedBytesOnTiers);
mCapacityBytes = 0;
for (long bytes : mTotalBytesOnTiers.values()) {
mCapacityBytes += bytes;
}
mUsedBytes = 0;
for (long bytes : mUsedBytesOnTiers.values()) {
mUsedBytes += bytes;
}
Set<Long> removedBlocks;
if (mIsRegistered) {
// This is a re-register of an existing worker. Assume the new block ownership data is more
// up-to-date and update the existing block information.
LOG.info("re-registering an existing workerId: {}", mId);
// Compute the difference between the existing block data, and the new data.
removedBlocks = Sets.difference(mBlocks, blocks);
} else {
removedBlocks = Collections.emptySet();
}
// Set the new block information.
mBlocks = new HashSet<Long>(blocks);
mIsRegistered = true;
return removedBlocks;
}
/**
* Adds a block to the worker.
*
* @param blockId the id of the block to be added
*/
public synchronized void addBlock(long blockId) {
mBlocks.add(blockId);
}
/**
* Removes a block from the worker.
*
* @param blockId the id of the block to be removed
*/
public synchronized void removeBlock(long blockId) {
mBlocks.remove(blockId);
mToRemoveBlocks.remove(blockId);
}
/**
* @return generated {@link WorkerInfo} for this worker
*/
public synchronized WorkerInfo generateClientWorkerInfo() {
WorkerInfo ret = new WorkerInfo();
ret.id = mId;
ret.address = mWorkerAddress;
ret.lastContactSec =
(int) ((CommonUtils.getCurrentMs() - mLastUpdatedTimeMs) / Constants.SECOND_MS);
ret.state = "In Service";
ret.capacityBytes = mCapacityBytes;
ret.usedBytes = mUsedBytes;
ret.startTimeMs = mStartTimeMs;
return ret;
}
/**
* @return the worker's address
*/
public synchronized WorkerNetAddress getAddress() {
return mWorkerAddress;
}
/**
* @return the available space of the worker in bytes
*/
public synchronized long getAvailableBytes() {
return mCapacityBytes - mUsedBytes;
}
/**
* @return ids of all blocks the worker contains
*/
public synchronized Set<Long> getBlocks() {
return new HashSet<Long>(mBlocks);
}
/**
* @return the capacity of the worker in bytes
*/
public synchronized long getCapacityBytes() {
return mCapacityBytes;
}
/**
* @return the id of the worker
*/
public synchronized long getId() {
return mId;
}
/**
* @return the last updated time of the worker in ms
*/
public synchronized long getLastUpdatedTimeMs() {
return mLastUpdatedTimeMs;
}
/**
* @return ids of blocks the worker should remove
*/
public synchronized List<Long> getToRemoveBlocks() {
return new ArrayList<Long>(mToRemoveBlocks);
}
/**
* @return used space of the worker in bytes
*/
public synchronized long getUsedBytes() {
return mUsedBytes;
}
/**
* @return the storage tier mapping for the worker
*/
public synchronized StorageTierAssoc getStorageTierAssoc() {
return mStorageTierAssoc;
}
/**
* @return the total bytes on each storage tier
*/
public synchronized Map<String, Long> getTotalBytesOnTiers() {
return mTotalBytesOnTiers;
}
/**
* @return the used bytes on each storage tier
*/
public synchronized Map<String, Long> getUsedBytesOnTiers() {
return mUsedBytesOnTiers;
}
/**
* @return the start time in milliseconds
*/
public synchronized long getStartTime() {
return mStartTimeMs;
}
/**
* @return the free bytes on each storage tier
*/
public synchronized Map<String, Long> getFreeBytesOnTiers() {
Map<String, Long> freeCapacityBytes = new HashMap<String, Long>();
for (Map.Entry<String, Long> entry : mTotalBytesOnTiers.entrySet()) {
freeCapacityBytes.put(entry.getKey(),
entry.getValue() - mUsedBytesOnTiers.get(entry.getKey()));
}
return freeCapacityBytes;
}
@Override
public synchronized String toString() {
StringBuilder sb = new StringBuilder("MasterWorkerInfo(");
sb.append(" ID: ").append(mId);
sb.append(", mWorkerAddress: ").append(mWorkerAddress);
sb.append(", TOTAL_BYTES: ").append(mCapacityBytes);
sb.append(", mUsedBytes: ").append(mUsedBytes);
sb.append(", mAvailableBytes: ").append(mCapacityBytes - mUsedBytes);
sb.append(", mLastUpdatedTimeMs: ").append(mLastUpdatedTimeMs);
sb.append(", mBlocks: [ ");
for (long blockId : mBlocks) {
sb.append(blockId).append(", ");
}
sb.append("] )");
return sb.toString();
}
/**
* Updates the last updated time of the worker in ms.
*/
public synchronized void updateLastUpdatedTimeMs() {
mLastUpdatedTimeMs = System.currentTimeMillis();
}
/**
* Adds or removes a block from the to-be-removed blocks set of the worker.
*
* @param add true if to add, to remove otherwise
* @param blockId the id of the block to be added or removed
*/
public synchronized void updateToRemovedBlock(boolean add, long blockId) {
if (add) {
if (mBlocks.contains(blockId)) {
mToRemoveBlocks.add(blockId);
}
} else {
mToRemoveBlocks.remove(blockId);
}
}
/**
* Sets the used space of the worker in bytes.
*
* @param usedBytesOnTiers used bytes on each storage tier
*/
public synchronized void updateUsedBytes(Map<String, Long> usedBytesOnTiers) {
mUsedBytes = 0;
mUsedBytesOnTiers = usedBytesOnTiers;
for (long t : mUsedBytesOnTiers.values()) {
mUsedBytes += t;
}
}
/**
* Sets the used space of the worker in bytes.
*
* @param tierAlias alias of storage tier
* @param usedBytesOnTier used bytes on certain storage tier
*/
public synchronized void updateUsedBytes(String tierAlias, long usedBytesOnTier) {
mUsedBytes += usedBytesOnTier - mUsedBytesOnTiers.get(tierAlias);
mUsedBytesOnTiers.put(tierAlias, usedBytesOnTier);
}
}