1919package org .apache .tez .client .registry ;
2020
2121import java .util .Objects ;
22+ import java .util .Optional ;
2223
2324import org .apache .hadoop .classification .InterfaceAudience ;
2425import org .apache .hadoop .registry .client .types .ServiceRecord ;
2526import org .apache .hadoop .yarn .api .records .ApplicationId ;
27+ import org .apache .tez .client .registry .zookeeper .ZkConfig ;
2628
2729
2830/**
3739@ InterfaceAudience .Public
3840public class AMRecord {
3941 private static final String APP_ID_RECORD_KEY = "appId" ;
40- private static final String HOST_RECORD_KEY = "host" ;
42+ private static final String HOST_NAME_RECORD_KEY = "hostName" ;
43+ private static final String HOST_IP_RECORD_KEY = "hostIp" ;
4144 private static final String PORT_RECORD_KEY = "port" ;
42- private static final String OPAQUE_ID_KEY = "id" ;
45+ private static final String EXTERNAL_ID_KEY = "externalId" ;
46+ private static final String COMPUTE_GROUP_NAME_KEY = "computeName" ;
4347
4448 private final ApplicationId appId ;
45- private final String host ;
49+ private final String hostName ;
50+ private final String hostIp ;
4651 private final int port ;
47- private final String id ;
52+ private final String externalId ;
53+ private final String computeName ;
54+
55+ private ServiceRecord serviceRecord ;
4856
4957 /**
5058 * Creates a new {@code AMRecord} with the given application ID, host, port, and identifier.
@@ -54,17 +62,23 @@ public class AMRecord {
5462 * Although this constructor may not be used directly within Tez internals,
5563 * it is part of the public API for Tez clients that handle unmanaged sessions.
5664 *
57- * @param appId the {@link ApplicationId} of the Tez application
58- * @param host the hostname where the Application Master is running
59- * @param port the port number on which the Application Master is listening
60- * @param id an opaque identifier for the record; if {@code null}, defaults to an empty string
65+ * @param appId the {@link ApplicationId} of the Tez application
66+ * @param hostName the hostname where the Application Master is running
67+ * @param hostIp the IP address of the Application Master host
68+ * @param port the RPC port number on which the Application Master is listening
69+ * @param externalId an optional external identifier for the record; if {@code null}, defaults to an empty string
70+ * @param computeName the compute group or cluster name; if {@code null},
71+ * defaults to {@link ZkConfig#DEFAULT_COMPUTE_GROUP_NAME}
6172 */
62- public AMRecord (ApplicationId appId , String host , int port , String id ) {
73+ public AMRecord (ApplicationId appId , String hostName , String hostIp , int port , String externalId ,
74+ String computeName ) {
6375 this .appId = appId ;
64- this .host = host ;
76+ this .hostName = hostName ;
77+ this .hostIp = hostIp ;
6578 this .port = port ;
66- //If id is not provided, convert to empty string
67- this .id = (id == null ) ? "" : id ;
79+ //externalId is optional, if not provided, convert to empty string
80+ this .externalId = Optional .ofNullable (externalId ).orElse ("" );
81+ this .computeName = Optional .ofNullable (computeName ).orElse (ZkConfig .DEFAULT_COMPUTE_GROUP_NAME );
6882 }
6983
7084 /**
@@ -78,10 +92,15 @@ public AMRecord(ApplicationId appId, String host, int port, String id) {
7892 * @param other the {@code AMRecord} instance to copy
7993 */
8094 public AMRecord (AMRecord other ) {
81- this .appId = other .getApplicationId ();
82- this .host = other .getHost ();
83- this .port = other .getPort ();
84- this .id = other .getId ();
95+ this .appId = other .appId ;
96+ this .hostName = other .hostName ;
97+ this .hostIp = other .hostIp ;
98+ this .port = other .port ;
99+ this .externalId = other .externalId ;
100+ this .computeName = other .computeName ;
101+ // all fields are final immutable, we can copy the serviceRecord,
102+ // if it's initialized there already, as it won't change
103+ this .serviceRecord = other .serviceRecord ;
85104 }
86105
87106 /**
@@ -97,25 +116,35 @@ public AMRecord(AMRecord other) {
97116 */
98117 public AMRecord (ServiceRecord serviceRecord ) {
99118 this .appId = ApplicationId .fromString (serviceRecord .get (APP_ID_RECORD_KEY ));
100- this .host = serviceRecord .get (HOST_RECORD_KEY );
119+ this .hostName = serviceRecord .get (HOST_NAME_RECORD_KEY );
120+ this .hostIp = serviceRecord .get (HOST_IP_RECORD_KEY );
101121 this .port = Integer .parseInt (serviceRecord .get (PORT_RECORD_KEY ));
102- this .id = serviceRecord .get (OPAQUE_ID_KEY );
122+ this .externalId = serviceRecord .get (EXTERNAL_ID_KEY );
123+ this .computeName = serviceRecord .get (COMPUTE_GROUP_NAME_KEY );
103124 }
104125
105126 public ApplicationId getApplicationId () {
106127 return appId ;
107128 }
108129
109- public String getHost () {
110- return host ;
130+ public String getHostName () {
131+ return hostName ;
132+ }
133+
134+ public String getHostIp () {
135+ return hostIp ;
111136 }
112137
113138 public int getPort () {
114139 return port ;
115140 }
116141
117- public String getId () {
118- return id ;
142+ public String getExternalId () {
143+ return externalId ;
144+ }
145+
146+ public String getComputeName () {
147+ return computeName ;
119148 }
120149
121150 @ Override
@@ -125,9 +154,11 @@ public boolean equals(Object other) {
125154 }
126155 if (other instanceof AMRecord otherRecord ) {
127156 return appId .equals (otherRecord .appId )
128- && host .equals (otherRecord .host )
157+ && hostName .equals (otherRecord .hostName )
158+ && hostIp .equals (otherRecord .hostIp )
129159 && port == otherRecord .port
130- && id .equals (otherRecord .id );
160+ && externalId .equals (otherRecord .externalId )
161+ && computeName .equals (otherRecord .computeName );
131162 } else {
132163 return false ;
133164 }
@@ -148,16 +179,27 @@ public boolean equals(Object other) {
148179 * @return a {@link ServiceRecord} populated with the values of this {@code AMRecord}
149180 */
150181 public ServiceRecord toServiceRecord () {
151- ServiceRecord serviceRecord = new ServiceRecord ();
182+ if (serviceRecord != null ) {
183+ return serviceRecord ;
184+ }
185+ serviceRecord = new ServiceRecord ();
152186 serviceRecord .set (APP_ID_RECORD_KEY , appId );
153- serviceRecord .set (HOST_RECORD_KEY , host );
187+ serviceRecord .set (HOST_NAME_RECORD_KEY , hostName );
188+ serviceRecord .set (HOST_IP_RECORD_KEY , hostIp );
154189 serviceRecord .set (PORT_RECORD_KEY , port );
155- serviceRecord .set (OPAQUE_ID_KEY , id );
190+ serviceRecord .set (EXTERNAL_ID_KEY , externalId );
191+ serviceRecord .set (COMPUTE_GROUP_NAME_KEY , computeName );
192+
156193 return serviceRecord ;
157194 }
158195
196+ @ Override
197+ public String toString () {
198+ return toServiceRecord ().attributes ().toString ();
199+ }
200+
159201 @ Override
160202 public int hashCode () {
161- return Objects .hash (appId , host , port , id );
203+ return Objects .hash (appId , hostName , hostIp , externalId , computeName , port );
162204 }
163205}
0 commit comments