24
24
import org .apache .commons .io .Charsets ;
25
25
import org .apache .commons .lang .StringUtils ;
26
26
import org .apache .flink .client .program .ClusterClient ;
27
- import org .apache .flink .client .program .MiniClusterClient ;
28
- import org .apache .flink .configuration .ConfigConstants ;
27
+ import org .apache .flink .client .program .rest .RestClusterClient ;
29
28
import org .apache .flink .configuration .Configuration ;
30
29
import org .apache .flink .configuration .GlobalConfiguration ;
31
30
import org .apache .flink .configuration .JobManagerOptions ;
32
31
import org .apache .flink .core .fs .FileSystem ;
33
32
import org .apache .flink .runtime .akka .AkkaUtils ;
34
- import org .apache .flink .runtime .minicluster .MiniCluster ;
35
- import org .apache .flink .runtime .minicluster .MiniClusterConfiguration ;
36
33
import org .apache .flink .runtime .util .LeaderConnectionInfo ;
37
34
import org .apache .flink .yarn .AbstractYarnClusterDescriptor ;
38
35
import org .apache .flink .yarn .YarnClusterDescriptor ;
57
54
*/
58
55
public class ClusterClientFactory {
59
56
57
+ private static final Logger LOG = LoggerFactory .getLogger (ClusterClientFactory .class );
58
+
59
+ private static final String HA_CLUSTER_ID = "high-availability.cluster-id" ;
60
+
61
+ private static final String HIGH_AVAILABILITY = "high-availability" ;
62
+
63
+ private static final String NODE = "NONE" ;
64
+
65
+ private static final String ZOOKEEPER = "zookeeper" ;
66
+
67
+ private static final String HADOOP_CONF = "fs.hdfs.hadoopconf" ;
68
+
60
69
public static ClusterClient createClusterClient (Options launcherOptions ) throws Exception {
61
70
String mode = launcherOptions .getMode ();
62
71
if (mode .equals (ClusterMode .standalone .name ())) {
@@ -70,10 +79,12 @@ public static ClusterClient createClusterClient(Options launcherOptions) throws
70
79
public static ClusterClient createStandaloneClient (Options launcherOptions ) throws Exception {
71
80
String flinkConfDir = launcherOptions .getFlinkconf ();
72
81
Configuration config = GlobalConfiguration .loadConfiguration (flinkConfDir );
73
- MiniClusterConfiguration .Builder configBuilder = new MiniClusterConfiguration .Builder ();
74
- configBuilder .setConfiguration (config );
75
- MiniCluster miniCluster = new MiniCluster (configBuilder .build ());
76
- MiniClusterClient clusterClient = new MiniClusterClient (config , miniCluster );
82
+
83
+ LOG .info ("------------config params-------------------------" );
84
+ config .toMap ().forEach ((key , value ) -> LOG .info ("{}: {}" , key , value ));
85
+ LOG .info ("-------------------------------------------" );
86
+
87
+ RestClusterClient clusterClient = new RestClusterClient <>(config , "clusterClient" );
77
88
LeaderConnectionInfo connectionInfo = clusterClient .getClusterConnectionInfo ();
78
89
InetSocketAddress address = AkkaUtils .getInetSocketAddressFromAkkaURL (connectionInfo .getAddress ());
79
90
config .setString (JobManagerOptions .ADDRESS , address .getAddress ().getHostName ());
@@ -89,6 +100,8 @@ public static ClusterClient createYarnSessionClient(Options launcherOptions) {
89
100
90
101
if (StringUtils .isNotBlank (yarnConfDir )) {
91
102
try {
103
+ boolean isHighAvailability ;
104
+
92
105
config .setString (ConfigConstants .PATH_HADOOP_CONFIG , yarnConfDir );
93
106
FileSystem .initialize (config );
94
107
@@ -101,6 +114,7 @@ public static ClusterClient createYarnSessionClient(Options launcherOptions) {
101
114
String yarnSessionConf = launcherOptions .getYarnSessionConf ();
102
115
yarnSessionConf = URLDecoder .decode (yarnSessionConf , Charsets .UTF_8 .toString ());
103
116
Properties yarnSessionConfProperties = PluginUtil .jsonStrToObject (yarnSessionConf , Properties .class );
117
+
104
118
Object yid = yarnSessionConfProperties .get ("yid" );
105
119
106
120
if (null != yid ) {
@@ -109,12 +123,22 @@ public static ClusterClient createYarnSessionClient(Options launcherOptions) {
109
123
applicationId = getYarnClusterApplicationId (yarnClient );
110
124
}
111
125
112
- System . out . println ( " applicationId=" + applicationId .toString ());
126
+ LOG . info ( "current applicationId = {}" , applicationId .toString ());
113
127
114
128
if (StringUtils .isEmpty (applicationId .toString ())) {
115
129
throw new RuntimeException ("No flink session found on yarn cluster." );
116
130
}
117
131
132
+ isHighAvailability = config .getString (HIGH_AVAILABILITY , NODE ).equals (ZOOKEEPER );
133
+
134
+ if (isHighAvailability && config .getString (HA_CLUSTER_ID , null ) == null ) {
135
+ config .setString (HA_CLUSTER_ID , applicationId .toString ());
136
+ }
137
+
138
+ LOG .info ("------------config params-------------------------" );
139
+ config .toMap ().forEach ((key , value ) -> LOG .info ("{}: {}" , key , value ));
140
+ LOG .info ("-------------------------------------------" );
141
+
118
142
AbstractYarnClusterDescriptor clusterDescriptor = new YarnClusterDescriptor (config , yarnConf , flinkConfDir , yarnClient , false );
119
143
ClusterClient clusterClient = clusterDescriptor .retrieve (applicationId );
120
144
clusterClient .setDetached (true );
@@ -158,15 +182,15 @@ private static ApplicationId getYarnClusterApplicationId(YarnClient yarnClient)
158
182
159
183
}
160
184
161
- if (StringUtils .isEmpty (applicationId .toString ())) {
185
+ if (applicationId == null || StringUtils .isEmpty (applicationId .toString ())) {
162
186
throw new RuntimeException ("No flink session found on yarn cluster." );
163
187
}
164
188
return applicationId ;
165
189
}
166
190
167
191
private static ApplicationId toApplicationId (String appIdStr ) {
168
192
Iterator <String > it = StringHelper ._split (appIdStr ).iterator ();
169
- if (!(it .next ()). equals ( "application" )) {
193
+ if (!"application" . equals (it .next ())) {
170
194
throw new IllegalArgumentException ("Invalid ApplicationId prefix: " + appIdStr + ". The valid ApplicationId should start with prefix " + "application" );
171
195
} else {
172
196
try {
0 commit comments