-
Notifications
You must be signed in to change notification settings - Fork 588
HDDS-9400. Introduce DatanodeID to avoid passing UUID/String object in SCM #5417
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 78 commits
5b5f10c
5d6908e
ce386ef
1608faa
99c3467
82d452c
c76b039
f9a4aa7
081fd26
1a2af7f
df1690e
696408a
78297ea
8feb62e
93d7db0
3c28b19
1315a6a
2a43909
5e2bbdd
bdc0e8e
5a35e0d
57e3759
c4346c9
9773cc5
3327ef2
d55c151
2a46b2c
3e7357e
82b5d05
3b67c44
e495928
ee2e950
7400016
d8c771c
0044566
54270c0
4292fd4
e866507
75c6bb5
8380fad
21b21c1
51ffefd
0fa552f
a05539c
50869b9
f0bc4fa
c20e4eb
a94f111
bf86fe3
30543e6
f7deae1
abcf385
e7b38b6
6d819de
4dcec6e
adb1446
33f7c65
c5958fe
10d87cd
0bc2d20
9d39cd8
4fa1d76
0b0994b
d8b6278
85d64fd
c229d5c
5d26991
e0b98a4
92ccfd5
209a609
46c901d
7f69eb2
3a1672c
fc22b97
d5dab3e
5fa5353
7d84d44
4dd0c39
9536a98
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| /* | ||
| * 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.hdds.protocol; | ||
|
|
||
| import org.apache.hadoop.hdds.protocol.proto.HddsProtos; | ||
| import org.apache.hadoop.hdds.protocol.proto.HddsProtos.DatanodeIDProto; | ||
|
|
||
| import java.util.UUID; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.ConcurrentMap; | ||
|
|
||
| /** | ||
| * DatanodeID is the primary identifier of the Datanode. | ||
| * They are unique for every Datanode in the cluster. | ||
| * <p> | ||
| * This class is immutable and thread safe. | ||
| */ | ||
| public final class DatanodeID implements Comparable<DatanodeID> { | ||
|
|
||
| private static final ConcurrentMap<UUID, DatanodeID> CACHE = new ConcurrentHashMap<>(); | ||
|
|
||
| private final UUID uuid; | ||
| private final String uuidString; | ||
|
||
|
|
||
| private DatanodeID(final UUID uuid) { | ||
| this.uuid = uuid; | ||
| this.uuidString = uuid.toString(); | ||
| } | ||
|
|
||
| // Mainly used for JSON conversion | ||
| public String getID() { | ||
| return toString(); | ||
| } | ||
|
|
||
| @Override | ||
| public int compareTo(final DatanodeID that) { | ||
| return this.uuid.compareTo(that.uuid); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(final Object obj) { | ||
| return obj instanceof DatanodeID && | ||
| uuid.equals(((DatanodeID) obj).uuid); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return uuid.hashCode(); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return uuidString; | ||
| } | ||
|
|
||
| public DatanodeIDProto toProto() { | ||
| return DatanodeIDProto.newBuilder().setUuid(toProto(uuid)).build(); | ||
| } | ||
|
|
||
| public static DatanodeID fromProto(final DatanodeIDProto proto) { | ||
| return of(fromProto(proto.getUuid())); | ||
| } | ||
|
|
||
| public static DatanodeID fromUuidString(final String id) { | ||
| return of(UUID.fromString(id)); | ||
| } | ||
|
|
||
| public static DatanodeID of(final UUID id) { | ||
| return CACHE.computeIfAbsent(id, DatanodeID::new); | ||
| } | ||
|
|
||
| /** | ||
| * Returns a random DatanodeID. | ||
| */ | ||
| public static DatanodeID randomID() { | ||
| // We don't want to add Random ID to cache. | ||
| return new DatanodeID(UUID.randomUUID()); | ||
| } | ||
|
|
||
| private static UUID fromProto(final HddsProtos.UUID id) { | ||
| return new UUID(id.getMostSigBits(), id.getLeastSigBits()); | ||
| } | ||
|
|
||
| private static HddsProtos.UUID toProto(final UUID id) { | ||
| return HddsProtos.UUID.newBuilder() | ||
| .setMostSigBits(id.getMostSignificantBits()) | ||
| .setLeastSigBits(id.getLeastSignificantBits()) | ||
| .build(); | ||
| } | ||
|
|
||
| // TODO: Remove this in follow-up Jira. (HDDS-12015) | ||
| UUID getUuid() { | ||
| return uuid; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.