-
Notifications
You must be signed in to change notification settings - Fork 131
/
Pool.java
241 lines (205 loc) · 6.58 KB
/
Pool.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
package diskCacheV111.poolManager;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import diskCacheV111.poolManager.PoolSelectionUnit.SelectionLink;
import diskCacheV111.poolManager.PoolSelectionUnit.SelectionPool;
import diskCacheV111.poolManager.PoolSelectionUnit.SelectionPoolGroup;
import diskCacheV111.pools.PoolV2Mode;
import dmg.cells.nucleus.CellAddressCore;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
public class Pool extends PoolCore implements SelectionPool {
private static final long serialVersionUID = 8108406418388363116L;
final Map<String, PGroup> _pGroupList = new ConcurrentHashMap<>();
private boolean _enabled = true;
private volatile long _active;
private boolean _ping = true;
private long _serialId;
private boolean _rdOnly;
private ImmutableSet<String> _hsmInstances = ImmutableSet.of();
private ImmutableMap<String, String> _tags = ImmutableMap.of();
private PoolV2Mode _mode = new PoolV2Mode(PoolV2Mode.DISABLED);
private CellAddressCore _address;
private String _hostName;
public Pool(String name) {
super(name);
}
@Override
public Collection<SelectionLink> getLinksTargetingPool() {
return new ArrayList<>(_linkList.values());
}
@Override
public Collection<SelectionPoolGroup> getPoolGroupsMemberOf() {
return new ArrayList<>(_pGroupList.values());
}
@Override
public void setActive(boolean active) {
_active = active ? System.currentTimeMillis() : 0;
}
@Override
public long getActive() {
return _ping ? (System.currentTimeMillis() - _active) : 0L;
}
@Override
public boolean hasAnyHsmFrom(Collection<String> hsm) {
return _hsmInstances.stream().anyMatch(hsm::contains);
}
/**
* Returns true if pool heartbeat was received within the last 5 minutes.
*/
@Override
public boolean isActive() {
return getActive() < 5 * 60 * 1000;
}
@Override
public void setReadOnly(boolean rdOnly) {
_rdOnly = rdOnly;
}
@Override
public boolean isReadOnly() {
return _rdOnly;
}
/**
* Returns true if reading from the pool is allowed.
*/
@Override
public boolean canRead() {
return isEnabled() && _mode.getMode() != PoolV2Mode.DISABLED && !_mode.isDisabled(
PoolV2Mode.DISABLED_FETCH) && !_mode.isDisabled(PoolV2Mode.DISABLED_DEAD);
}
/**
* Returns true if writing to the pool is allowed. Since we cannot distinguish between a client
* write and a pool-to-pool write, both operations must be enabled on the pool.
*/
@Override
public boolean canWrite() {
return isEnabled() && !isReadOnly() && _mode.getMode() != PoolV2Mode.DISABLED
&& !_mode.isDisabled(PoolV2Mode.DISABLED_STORE) && !_mode.isDisabled(
PoolV2Mode.DISABLED_DEAD) && !_mode.isDisabled(PoolV2Mode.DISABLED_P2P_SERVER);
}
/**
* Returns true if the pool is allowed to read from tape.
*/
@Override
public boolean canReadFromTape() {
return isEnabled() && !isReadOnly() && _mode.getMode() != PoolV2Mode.DISABLED
&& !_mode.isDisabled(PoolV2Mode.DISABLED_STAGE) && !_mode.isDisabled(
PoolV2Mode.DISABLED_DEAD);
}
/**
* Returns true if the pool can deliver files for P2P operations.
*/
@Override
public boolean canReadForP2P() {
return isEnabled() && _mode.getMode() != PoolV2Mode.DISABLED && !_mode.isDisabled(
PoolV2Mode.DISABLED_P2P_SERVER) && !_mode.isDisabled(PoolV2Mode.DISABLED_DEAD);
}
/**
* Returns true if the pool can receive files for P2P operations.
*/
public boolean canWriteForP2P() {
return isEnabled() && !isReadOnly() && _mode.getMode() != PoolV2Mode.DISABLED
&& !_mode.isDisabled(PoolV2Mode.DISABLED_P2P_CLIENT) && !_mode.isDisabled(
PoolV2Mode.DISABLED_DEAD);
}
public void setEnabled(boolean enabled) {
_enabled = enabled;
}
@Override
public boolean isEnabled() {
return _enabled;
}
public void setPing(boolean ping) {
_ping = ping;
}
public boolean isPing() {
return _ping;
}
@Override
public String toString() {
return getName() + " (enabled=" + _enabled + ";active=" + (_active > 0 ? (getActive()
/ 1000) : "no") + ";rdOnly=" + isReadOnly() + ";links=" + _linkList.size()
+ ";pgroups=" + _pGroupList.size() + ";hsm=" + _hsmInstances.toString() + ";mode="
+ _mode + ")";
}
@Override
public boolean setSerialId(long serialId) {
if (serialId == _serialId) {
return false;
}
_serialId = serialId;
return true;
}
@Override
public long getSerialId() {
return _serialId;
}
@Override
public void setPoolMode(PoolV2Mode mode) {
_mode = mode;
}
@Override
public PoolV2Mode getPoolMode() {
return _mode;
}
@Override
public ImmutableSet<String> getHsmInstances() {
return _hsmInstances;
}
@Override
public void setHsmInstances(Set<String> hsmInstances) {
if (hsmInstances == null) {
_hsmInstances = ImmutableSet.of();
} else {
_hsmInstances = ImmutableSet.copyOf(hsmInstances);
}
}
@Override
public void setTags(Map<String, String> tags) {
if (tags == null) {
_tags = ImmutableMap.of();
} else {
_tags = ImmutableMap.copyOf(tags);
}
}
@Override
public Map<String, String> getTags() {
return _tags;
}
@Override
public CellAddressCore getAddress() {
return _address;
}
@Override
public void setAddress(CellAddressCore address) {
_address = address;
}
@Override
public void setCanonicalHostName(String hostName) {
_hostName = hostName;
}
@Override
public Optional<String> getCanonicalHostName() {
return Optional.ofNullable(_hostName);
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Pool group = (Pool) o;
return getName().equals(group.getName());
}
@Override
public int hashCode() {
return Objects.hash(getName());
}
}